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