• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
74      * @brief SendCallback to ability manager service for death recipient
75      */
76     void DeathRecipientSendCallback();
77 
78     /**
79      * @brief Connecte service center
80      * @param callerToken Caller form extension token.
81      * @return Returns true if successfully connected to service center; returns false otherwise.
82      */
83     bool ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken);
84 
85     /**
86      * @brief send preload request to service center.
87      * @param preloadItems the modules need to be preloaded.
88      */
89     bool ProcessPreload(const Want &want);
90 
91     /**
92      * @brief Disconnect service center
93      */
94     void DisconnectAbility();
95 
96     bool SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply);
97 private:
98     /**
99      * @brief Notify the service center center to start the installation free process.
100      * @param targetAbilityInfo Indicates the information which will be send to service center.
101      * @param want Indicates the information of the need start ability.
102      * @param freeInstallParams The value of ability manager service callback map.
103      * @param userId Designation User ID.
104      * @return Returns true if create async task successfully called; returns false otherwise.
105      */
106     bool SilentInstall(TargetAbilityInfo &targetAbilityInfo, const Want &want,
107         const FreeInstallParams &freeInstallParams, int32_t userId);
108 
109     /**
110      * @brief Notify the service center to check for updates.
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 UpgradeCheck(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
118         const FreeInstallParams &freeInstallParams, int32_t userId);
119 
120     /**
121      * @brief Notify the service center to install new ability.
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 UpgradeInstall(const TargetAbilityInfo &targetAbilityInfo, const Want &want,
129         const FreeInstallParams &freeInstallParams, int32_t userId);
130 
131     /**
132      * @brief Obtains the Calling Info object
133      * @param userId Indicates the user ID.
134      * @param callingUid Indicates the user's callingUid.
135      * @param bundleNames Indicates the obtained bundle names.
136      * @param callingAppids Indicates the ids of teh calling app.
137      * @return Returns true if get callingInfo successfully; returns false otherwise.
138      */
139     void GetCallingInfo(int32_t userId, int32_t callingUid, std::vector<std::string> &bundleNames,
140         std::vector<std::string> &callingAppIds);
141 
142     /**
143      * @brief Obtains the target ability Info object which will be send to service center.
144      * @param want Indicates the information of the ability.
145      * @param userId Indicates the user ID.
146      * @param innerBundleInfo Indicates the innerBundleInfo of the bundle which will be using.
147      * @param targetAbilityInfo Indicates the targetAbilityInfo of the bundle which will be returned.
148      */
149     void GetTargetAbilityInfo(const Want &want, int32_t userId, const InnerBundleInfo &innerBundleInfo,
150         sptr<TargetAbilityInfo> &targetAbilityInfo);
151 
152     /**
153      * @brief Check is need update module.
154      * @param innerBundleInfo Indicates the innerBundleInfo of the bundle which will be using.
155      * @param want Indicates the information of the ability.
156      * @param userId Indicates the user ID.
157      * @param callBack Indicates the callback object for ability manager service.
158      * @return Returns true if module need update.
159      */
160     bool CheckIsModuleNeedUpdate(
161         InnerBundleInfo &innerBundleInfo, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack);
162 
163     /**
164      * @brief Send atomic service status callback to ability manager service
165      * @param resultCode The result code to ability manager service call back
166      * @param want Indicates the information of the need start ability.
167      * @param userId Designation User ID.
168      * @param transactId The key of ability manager service callback map
169      */
170     void SendCallBack(int32_t resultCode, const Want &want, int32_t userId, const std::string &transactId);
171 
172     /**
173      * @brief Send atomic service status callback to ability manager service
174      * @param transactId The key of ability manager service callback map
175      * @param freeInstallParams The value of ability manager service callback map
176      */
177     void SendCallBack(const std::string &transactId, const FreeInstallParams &freeInstallParams);
178 
179     /**
180      * @brief Determine whether there are reusable connection
181      * @param flag Indicates service function
182      * @param targetAbilityInfo Indicates the information of the ability.
183      * @param want Indicates the information of the need start ability.
184      * @param userId Designation User ID.
185      * @param freeInstallParams The value of ability manager service callback map.
186      * @return Returns true if successfully Send request with RemoteObject
187      */
188     bool SendRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want,
189         int32_t userId, const FreeInstallParams &freeInstallParams);
190 
191     /**
192      * @brief Send request with RemoteObject,this is a asynchronous function.
193      * @param flag Indicates service function
194      * @param targetAbilityInfo Indicates the information of the ability.
195      * @param want Indicates the information of the need start ability.
196      * @param userId Designation User ID.
197      * @param freeInstallParams The value of ability manager service callback map.
198      */
199     void SendRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo, const Want &want, int32_t userId,
200         const FreeInstallParams &freeInstallParams);
201 
202     /**
203      * @brief Get the ability manager service Call Back with transactId
204      * @param transactId The key of ability manager service callback.
205      * @return Return the Indicates callback to be invoked for return ability manager service the operation result.
206      */
207     sptr<IRemoteObject> GetAbilityManagerServiceCallBack(std::string transactId);
208 
209     /**
210      * @brief Listening service center processing timeout
211      * @param transactId The key of ability manager service callback.
212      */
213     void OutTimeMonitor(std::string transactId);
214 
GetTransactId()215     int GetTransactId() const
216     {
217         transactId_++;
218         return transactId_.load();
219     }
220 
221     /**
222      * @brief Send callback to ability manager service
223      * @param resultCode The result code to ability manager service call back
224      * @param want Indicates the information of the ability.
225      * @param userId Indicates the user ID.
226      * @param callBack Indicates the callback object for ability manager service.
227      */
228     void CallAbilityManager(int32_t resultCode, const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack);
229 
230     /**
231      * @brief Judge whether the ability information has been queried
232      * @param want Indicates the information of the ability.
233      * @param flags Indicates the information contained in the AbilityInfo object to be returned.
234      * @param userId Indicates the user ID.
235      * @param abilityInfo Indicates the obtained AbilityInfo object.
236      * @param callBack Indicates the callback object for ability manager service.
237      * @return Returns true if the ability information has been queried; returns false otherwise.
238      */
239     bool IsObtainAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo,
240         const sptr<IRemoteObject> &callBack, InnerBundleInfo &innerBundleInfo);
241 
242     bool GetAbilityMgrProxy();
243     void WaitFromConnecting(std::unique_lock<std::mutex> &lock);
244     void WaitFromConnected(std::unique_lock<std::mutex> &lock);
245     void DisconnectDelay();
246 
247     void PreloadRequest(int32_t flag, const TargetAbilityInfo &targetAbilityInfo);
248     bool ProcessPreloadCheck(const TargetAbilityInfo &targetAbilityInfo);
249     void ProcessPreloadRequestToServiceCenter(int32_t flag, const TargetAbilityInfo &targetAbilityInfo);
250     void GetEcologicalCallerInfo(const Want &want, ErmsCallerInfo &callerInfo, int32_t userId);
251 
252     int32_t GetPreloadFlag();
253     bool GetPreloadList(const std::string &bundleName, const std::string &moduleName,
254         int32_t userId, sptr<TargetAbilityInfo> &targetAbilityInfo);
255     void LoadDownloadService() const;
256 
257     bool CheckEcologicalRule(const Want &want, ErmsCallerInfo &callerInfo, BmsExperienceRule &rule);
258     bool CheckIsOnDemandLoad(const TargetAbilityInfo &targetAbilityInfo) const;
259     bool GetModuleName(const InnerBundleInfo &innerBundleInfo, const Want &want, std::string &moduleName) const;
260 
261     mutable std::atomic<int> transactId_ = 0;
262     std::condition_variable cv_;
263     std::mutex mapMutex_;
264     std::mutex mutex_;
265     sptr<ServiceCenterConnection> serviceCenterConnection_;
266     std::map<std::string, FreeInstallParams> freeInstallParamsMap_;
267     sptr<IRemoteObject> serviceCenterRemoteObject_;
268     int32_t connectState_ = ServiceCenterConnectState::DISCONNECTED;
269     std::shared_ptr<SerialQueue> serialQueue_;
270 };
271 }  // namespace AppExecFwk
272 }  // namespace OHOS
273 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_FREE_INSTALL_BUNDLE_CONNECT_ABILITY_H
274