1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef APP_DOMAIN_VERIFY_MGR_CLIENT_H
17 #define APP_DOMAIN_VERIFY_MGR_CLIENT_H
18 
19 #include <memory>
20 #include "singleton.h"
21 
22 #include "app_domain_verify_mgr_service_proxy.h"
23 #include "errors.h"
24 #include "i_app_domain_verify_mgr_service.h"
25 #include "skill_uri.h"
26 #include "domain_verify_status.h"
27 
28 namespace OHOS {
29 namespace AppDomainVerify {
30 
31 class AppDomainVerifyMgrClient : public DelayedSingleton<AppDomainVerifyMgrClient> {
32     DECLARE_DELAYED_SINGLETON(AppDomainVerifyMgrClient);
33 
34 public:
35     DISALLOW_COPY_AND_MOVE(AppDomainVerifyMgrClient);
36     /**
37      * VerifyDomain
38      * @descrition verify applink in skillUris.
39      * @param appIdentifier appIdentifier.
40      * @param bundleName bundleName.
41      * @param fingerprint fingerprint.
42      * @param skillUris skillUris.
43      */
44     void VerifyDomain(const std::string& appIdentifier, const std::string& bundleName, const std::string& fingerprint,
45         const std::vector<SkillUri>& skillUris);
46 
47     /**
48      * ClearDomainVerifyStatus
49      * @descrition delete domain name verification result.
50      * @param appIdentifier appIdentifier.
51      * @param bundleName bundleName.
52      * @return bool clear result.
53      */
54     bool ClearDomainVerifyStatus(const std::string& appIdentifier, const std::string& bundleName);
55 
56     /**
57      * FilterAbilities
58      * @descrition Perform a further filtering on the ability list based on the domain verification.
59      * @param want want to start ability.
60      * @param originAbilityInfos AbilityInfo vector input.
61      * @param filtedAbilityInfos AbilityInfo vector output.
62      * @return bool filterAbilities success or not.
63      */
64     bool FilterAbilities(const OHOS::AAFwk::Want& want,
65         const std::vector<OHOS::AppExecFwk::AbilityInfo>& originAbilityInfos,
66         std::vector<OHOS::AppExecFwk::AbilityInfo>& filtedAbilityInfos);
67 
68     /**
69      * QueryDomainVerifyStatus
70      * @descrition query domain verify status.
71      * @param bundleName bundleName.
72      * @param domainVerificationState domainVerificationState.
73      * @return bool query success or not.
74      */
75     bool QueryDomainVerifyStatus(const std::string& bundleName, DomainVerifyStatus& domainVerificationState);
76 
77     /**
78      * QueryAllDomainVerifyStatus
79      * @descrition QueryAllDomainVerifyStatus.
80      * @param bundleVerifyStatusInfo bundleVerifyStatusInfo.
81      * @return bool query success or not.
82      */
83     bool QueryAllDomainVerifyStatus(BundleVerifyStatusInfo& bundleVerifyStatusInfo);
84 
85     /**
86      * SaveDomainVerifyStatus
87      * @descrition SaveDomainVerifyStatus.
88      * @param bundleName bundleName.
89      * @param verifyResultInfo verifyResultInfo.
90      * @return bool query success or not.
91      */
92     bool SaveDomainVerifyStatus(const std::string& bundleName, const VerifyResultInfo& verifyResultInfo);
93 
94     /**
95      * IsAtomicServiceUrl
96      * @descrition check input url is atomic service or not.
97      * @param url input url to check.
98      * @return bool is atomic service or not.
99      */
100     bool IsAtomicServiceUrl(const std::string& url);
101 
102     /**
103      * UpdateWhiteListUrl
104      * @descrition update whitelist urls.
105      * @param urls input whitelist urls.
106      */
107     void UpdateWhiteListUrls(const std::vector<std::string>& urls);
108     /**
109      * ConvertToExplicitWant
110      * @descrition convert implicit want to explicit want.
111      * @param implicitWant implicit want to convert.
112      * @param callback callback when convert finish.
113      */
114     void ConvertToExplicitWant(OHOS::AAFwk::Want& implicitWant, sptr<IConvertCallback>& callback);
115 
116     /**
117      * QueryAssociatedDomains
118      * @descrition query domains associated to bundle name.
119      * @param bundleName bundleName as key.
120      * @param domains domains as result.
121      * @return int result of query.
122      */
123     int QueryAssociatedDomains(const std::string& bundleName, std::vector<std::string>& domains);
124     /**
125      * QueryAssociatedBundleNames
126      * @descrition query bundleNames associated to domain.
127      * @param bundleName domain as key.
128      * @param bundleNames domains as result.
129      * @return int result of query.
130      */
131     int QueryAssociatedBundleNames(const std::string& domain, std::vector<std::string>& bundleNames);
132 
133     /**
134      * OnRemoteSaDied
135      * @descrition
136      * @param object systemAbility proxy object
137      * @return void.
138      */
139     void OnRemoteSaDied(const wptr<IRemoteObject>& object);
140 
141 private:
142     bool IsServiceAvailable();
143     void ConnectService();
144     bool IsValidUrl(OHOS::Uri& uri);
145     bool IsValidPath(const std::string& url);
146 
147 private:
148     class StaticDestoryMonitor {
149     public:
StaticDestoryMonitor()150         StaticDestoryMonitor() : destoryed_(false)
151         {
152         }
~StaticDestoryMonitor()153         ~StaticDestoryMonitor()
154         {
155             destoryed_ = true;
156         }
157 
IsDestoryed()158         bool IsDestoryed() const
159         {
160             return destoryed_;
161         }
162 
163     private:
164         bool destoryed_;
165     };
166 
167 private:
168     static std::mutex proxyLock_;
169     static sptr<IAppDomainVerifyMgrService> appDomainVerifyMgrServiceProxy_;
170     static StaticDestoryMonitor staticDestoryMonitor_;
171     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
172 };
173 
174 class AppDomainVerifyMgrSaDeathRecipient : public IRemoteObject::DeathRecipient {
175 public:
176     explicit AppDomainVerifyMgrSaDeathRecipient();
177     virtual ~AppDomainVerifyMgrSaDeathRecipient();
178     void OnRemoteDied(const wptr<IRemoteObject>& object) override;
179 
180 private:
181     DISALLOW_COPY_AND_MOVE(AppDomainVerifyMgrSaDeathRecipient);
182 };
183 }  // namespace AppDomainVerify
184 }  // namespace OHOS
185 
186 #endif