1 /*
2  * Copyright (c) 2022-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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_FREE_INSTALL_BUNDLE_CONNECT_ABILITY_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_FREE_INSTALL_BUNDLE_CONNECT_ABILITY_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 
23 #include "bms_ecological_rule_mgr_service_client.h"
24 #include "free_install_params.h"
25 #include "inner_bundle_info.h"
26 #include "install_result.h"
27 #include "iremote_broker.h"
28 #include "serial_queue.h"
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 using namespace OHOS::AAFwk;
34 using ErmsCallerInfo = OHOS::AppExecFwk::BmsCallerInfo;
35 using BmsExperienceRule = OHOS::AppExecFwk::BmsExperienceRule;
36 class ServiceCenterConnection;
37 class BundleConnectAbilityMgr : public std::enable_shared_from_this<BundleConnectAbilityMgr> {
38 public:
39     BundleConnectAbilityMgr();
40     ~BundleConnectAbilityMgr();
41 
42     /**
43      * @brief Query the AbilityInfo by the given Want.
44      * @param want Indicates the information of the ability.
45      * @param flags Indicates the information contained in the AbilityInfo object to be returned.
46      * @param userId Indicates the user ID.
47      * @param abilityInfo Indicates the obtained AbilityInfo object.
48      * @param callBack Indicates the callback object for ability manager service.
49      * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise.
50      */
51     bool QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId,
52         AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack);
53     /**
54      * @brief Silent install by the given Want.
55      * @param want Indicates the information of the want.
56      * @param userId Indicates the user ID.
57      * @param callBack Indicates the callback to be invoked for return the operation result.
58      * @return Returns true if silent install successfully; returns false otherwise.
59      */
60     bool SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack);
61     /**
62      * @brief Upgrade atomic service status
63      * @param want Query the AbilityInfo by the given Want.
64      * @param userId Indicates the user ID.
65      */
66     void UpgradeAtomicService(const Want &want, int32_t userId);
67     /**
68      * @brief Executed when a service callback is retrieved
69      * @param installResult The json string of InstallResult
70      */
71     void OnServiceCenterCall(std::string installResult);
72     /**
73      * @brief Executed when a service callback is delayed heartbeat
74      * @param installResult The json string of InstallResult
75      */
76     void OnDelayedHeartbeat(std::string installResult);
77 
78     /**
79      * @brief SendCallback to ability manager service for death recipient
80      */
81     void DeathRecipientSendCallback();
82 
83     /**
84      * @brief Connecte service center
85      * @param callerToken Caller form extension token.
86      * @return Returns true if successfully connected to service center; returns false otherwise.
87      */
88     bool ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken);
89 
90     /**
91      * @brief send preload request to service center.
92      * @param preloadItems the modules need to be preloaded.
93      */
94     bool ProcessPreload(const Want &want);
95 
96     /**
97      * @brief Disconnect service center
98      */
99     void DisconnectAbility();
100 
101     bool SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply);
102 
103     /**
104      * @brief Executed when a service callback
105      * @param installResultStr The json string of InstallResult
106      */
107     void OnServiceCenterReceived(std::string installResultStr);
108 private:
109     /**
110      * @brief Notify the service center center to start the installation free process.
111      * @param targetAbilityInfo Indicates the information which will be send to service center.
112      * @param want Indicates the information of the need start ability.
113      * @param freeInstallParams The value of ability manager service callback map.
114      * @param userId Designation User ID.
115      * @return Returns true if create async task successfully called; returns false otherwise.
116      */
117     bool SilentInstall(TargetAbilityInfo &targetAbilityInfo, const Want &want,
118         const FreeInstallParams &freeInstallParams, int32_t userId);
119 
120     /**
121      * @brief Notify the service center to check for updates.
122      * @param targetAbilityInfo Indicates the information which will be send to service center.
123      * @param want Indicates the information of the need start ability.
124      * @param freeInstallParams The value of ability manager service callback map.
125      * @param userId Designation User ID.
126      * @return Returns true if create async task successfully called; returns false otherwise.
127      */
128     bool UpgradeCheck(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
129         const FreeInstallParams &freeInstallParams, int32_t userId);
130 
131     /**
132      * @brief Notify the service center to install new ability.
133      * @param targetAbilityInfo Indicates the information which will be send to service center.
134      * @param want Indicates the information of the need start ability.
135      * @param freeInstallParams The value of ability manager service callback map.
136      * @param userId Designation User ID.
137      * @return Returns true if create async task successfully called; returns false otherwise.
138      */
139     bool UpgradeInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
140         const FreeInstallParams &freeInstallParams, int32_t userId);
141 
142     /**
143      * @brief Obtains the Calling Info object
144      * @param userId Indicates the user ID.
145      * @param callingUid Indicates the user's callingUid.
146      * @param bundleNames Indicates the obtained bundle names.
147      * @param callingAppids Indicates the ids of teh calling app.
148      * @return Returns true if get callingInfo successfully; returns false otherwise.
149      */
150     void GetCallingInfo(int32_t userId, int32_t callingUid, std::vector<std::string> &bundleNames,
151         std::vector<std::string> &callingAppIds);
152 
153     /**
154      * @brief Obtains the target ability Info object which will be send to service center.
155      * @param want Indicates the information of the ability.
156      * @param userId Indicates the user ID.
157      * @param innerBundleInfo Indicates the innerBundleInfo of the bundle which will be using.
158      * @param targetAbilityInfo Indicates the targetAbilityInfo of the bundle which will be returned.
159      */
160     void GetTargetAbilityInfo(const Want &want, int32_t userId, const InnerBundleInfo &innerBundleInfo,
161         sptr<TargetAbilityInfo> &targetAbilityInfo);
162 
163     /**
164      * @brief Check is need update module.
165      * @param innerBundleInfo Indicates the innerBundleInfo of the bundle which will be using.
166      * @param want Indicates the information of the ability.
167      * @param userId Indicates the user ID.
168      * @param callBack Indicates the callback object for ability manager service.
169      * @return Returns true if module need update.
170      */
171     bool CheckIsModuleNeedUpdate(
172         InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack);
173 
174     /**
175      * @brief Send atomic service status callback to ability manager service
176      * @param resultCode The result code to ability manager service call back
177      * @param want Indicates the information of the need start ability.
178      * @param userId Designation User ID.
179      * @param transactId The key of ability manager service callback map
180      */
181     void SendCallBack(int32_t resultCode, const Want &want, int32_t userId, const std::string &transactId);
182 
183     /**
184      * @brief Send atomic service status callback to ability manager service
185      * @param transactId The key of ability manager service callback map
186      * @param freeInstallParams The value of ability manager service callback map
187      */
188     void SendCallBack(const std::string &transactId, const FreeInstallParams &freeInstallParams);
189 
190     /**
191      * @brief Determine whether there are reusable connection
192      * @param flag Indicates service function
193      * @param targetAbilityInfo Indicates the information of the ability.
194      * @param want Indicates the information of the need start ability.
195      * @param userId Designation User ID.
196      * @param freeInstallParams The value of ability manager service callback map.
197      * @return Returns true if successfully Send request with RemoteObject
198      */
199     bool SendRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want,
200         int32_t userId, const FreeInstallParams &freeInstallParams);
201 
202     /**
203      * @brief Send request with RemoteObject,this is a asynchronous function.
204      * @param flag Indicates service function
205      * @param targetAbilityInfo Indicates the information of the ability.
206      * @param want Indicates the information of the need start ability.
207      * @param userId Designation User ID.
208      * @param freeInstallParams The value of ability manager service callback map.
209      */
210     void SendRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want, int32_t userId,
211         const FreeInstallParams &freeInstallParams);
212 
213     /**
214      * @brief Get the ability manager service Call Back with transactId
215      * @param transactId The key of ability manager service callback.
216      * @return Return the Indicates callback to be invoked for return ability manager service the operation result.
217      */
218     sptr<IRemoteObject> GetAbilityManagerServiceCallBack(std::string transactId);
219 
220     /**
221      * @brief Listening service center processing timeout
222      * @param transactId The key of ability manager service callback.
223      */
224     void OutTimeMonitor(std::string transactId);
225 
GetTransactId()226     int GetTransactId() const
227     {
228         transactId_++;
229         return transactId_.load();
230     }
231 
232     /**
233      * @brief Send callback to ability manager service
234      * @param resultCode The result code to ability manager service call back
235      * @param want Indicates the information of the ability.
236      * @param userId Indicates the user ID.
237      * @param callBack Indicates the callback object for ability manager service.
238      */
239     void CallAbilityManager(int32_t resultCode, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack);
240 
241     /**
242      * @brief Judge whether the ability information has been queried
243      * @param want Indicates the information of the ability.
244      * @param flags Indicates the information contained in the AbilityInfo object to be returned.
245      * @param userId Indicates the user ID.
246      * @param abilityInfo Indicates the obtained AbilityInfo object.
247      * @param callBack Indicates the callback object for ability manager service.
248      * @return Returns true if the ability information has been queried; returns false otherwise.
249      */
250     bool IsObtainAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo,
251         const sptr<IRemoteObject> &callBack, InnerBundleInfo &innerBundleInfo);
252 
253     bool GetAbilityMgrProxy();
254     void WaitFromConnecting(std::unique_lock<std::mutex> &lock);
255     void WaitFromConnected(std::unique_lock<std::mutex> &lock);
256     void DisconnectDelay();
257 
258     void PreloadRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo);
259     bool ProcessPreloadCheck(const TargetAbilityInfo &targetAbilityInfo);
260     void ProcessPreloadRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo);
261     void GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId);
262 
263     int32_t GetPreloadFlag();
264     bool GetPreloadList(const std::string &bundleName, const std::string &moduleName,
265         int32_t userId, sptr<TargetAbilityInfo> &targetAbilityInfo);
266     void LoadDownloadService() const;
267 
268     bool CheckEcologicalRule(const Want &want, ErmsCallerInfo &callerInfo, BmsExperienceRule &rule);
269     bool CheckIsOnDemandLoad(const TargetAbilityInfo &targetAbilityInfo) const;
270     bool GetModuleName(const InnerBundleInfo &innerBundleInfo, const Want &want, std::string &moduleName) const;
271     bool CheckIsModuleNeedUpdateWrap(InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId,
272         const sptr<IRemoteObject> &callBack);
273     bool CheckSubPackageName(const InnerBundleInfo &innerBundleInfo, const Want &want);
274 
275     mutable std::atomic<int> transactId_ = 0;
276     std::condition_variable cv_;
277     std::mutex mapMutex_;
278     std::mutex mutex_;
279     sptr<ServiceCenterConnection> serviceCenterConnection_;
280     std::map<std::string, FreeInstallParams> freeInstallParamsMap_;
281     sptr<IRemoteObject> serviceCenterRemoteObject_;
282     int32_t connectState_ = ServiceCenterConnectState::DISCONNECTED;
283     std::shared_ptr<SerialQueue> serialQueue_;
284 };
285 }  // namespace AppExecFwk
286 }  // namespace OHOS
287 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_FREE_INSTALL_BUNDLE_CONNECT_ABILITY_H
288