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 #ifdef SUPPORT_ERMS 24 #include "ecological_rule_mgr_service_client.h" 25 #else 26 #include "erms_mgr_interface.h" 27 #include "erms_mgr_param.h" 28 #endif 29 #include "free_install_params.h" 30 #include "inner_bundle_info.h" 31 #include "install_result.h" 32 #include "iremote_broker.h" 33 #include "serial_queue.h" 34 #include "want.h" 35 36 namespace OHOS { 37 namespace AppExecFwk { 38 using namespace OHOS::AAFwk; 39 #ifdef SUPPORT_ERMS 40 using namespace OHOS::EcologicalRuleMgrService; 41 using ErmsCallerInfo = OHOS::EcologicalRuleMgrService::CallerInfo; 42 using ExperienceRule = OHOS::EcologicalRuleMgrService::ExperienceRule; 43 #else 44 using ErmsCallerInfo = OHOS::AppExecFwk::ErmsParams::CallerInfo; 45 using ExperienceRule = OHOS::AppExecFwk::ErmsParams::ExperienceRule; 46 #endif 47 class ServiceCenterConnection; 48 class BundleConnectAbilityMgr : public std::enable_shared_from_this<BundleConnectAbilityMgr> { 49 public: 50 BundleConnectAbilityMgr(); 51 ~BundleConnectAbilityMgr(); 52 53 /** 54 * @brief Query the AbilityInfo by the given Want. 55 * @param want Indicates the information of the ability. 56 * @param flags Indicates the information contained in the AbilityInfo object to be returned. 57 * @param userId Indicates the user ID. 58 * @param abilityInfo Indicates the obtained AbilityInfo object. 59 * @param callBack Indicates the callback object for ability manager service. 60 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 61 */ 62 bool QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, 63 AbilityInfo &abilityInfo, const sptr<IRemoteObject> &callBack); 64 /** 65 * @brief Silent install by the given Want. 66 * @param want Indicates the information of the want. 67 * @param userId Indicates the user ID. 68 * @param callBack Indicates the callback to be invoked for return the operation result. 69 * @return Returns true if silent install successfully; returns false otherwise. 70 */ 71 bool SilentInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callBack); 72 /** 73 * @brief Upgrade atomic service status 74 * @param want Query the AbilityInfo by the given Want. 75 * @param userId Indicates the user ID. 76 */ 77 void UpgradeAtomicService(const Want &want, int32_t userId); 78 /** 79 * @brief Executed when a service callback is retrieved 80 * @param installResult The json string of InstallResult 81 */ 82 void OnServiceCenterCall(std::string installResult); 83 84 /** 85 * @brief SendCallback to ability manager service for death recipient 86 */ 87 void DeathRecipientSendCallback(); 88 89 /** 90 * @brief Connecte service center 91 * @param callerToken Caller form extension token. 92 * @return Returns true if successfully connected to service center; returns false otherwise. 93 */ 94 bool ConnectAbility(const Want &want, const sptr<IRemoteObject> &callerToken); 95 96 /** 97 * @brief send preload request to service center. 98 * @param preloadItems the modules need to be preloaded. 99 */ 100 bool ProcessPreload(const Want &want); 101 102 /** 103 * @brief Disconnect service center 104 */ 105 void DisconnectAbility(); 106 107 bool SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply); 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 bool CheckDependencies(const std::string &moduleName, const InnerBundleInfo &innerBundleInfo); 267 268 #ifndef SUPPORT_ERMS 269 sptr<AppExecFwk::IEcologicalRuleManager> CheckEcologicalRuleMgr(); 270 #endif 271 272 bool CheckEcologicalRule(const Want &want, ErmsCallerInfo &callerInfo, ExperienceRule &rule); 273 bool CheckIsOnDemandLoad(const TargetAbilityInfo &targetAbilityInfo) const; 274 bool GetModuleName(const InnerBundleInfo &innerBundleInfo, const Want &want, std::string &moduleName) const; 275 276 mutable std::atomic<int> transactId_ = 0; 277 std::condition_variable cv_; 278 std::mutex mapMutex_; 279 std::mutex mutex_; 280 sptr<ServiceCenterConnection> serviceCenterConnection_; 281 std::map<std::string, FreeInstallParams> freeInstallParamsMap_; 282 sptr<IRemoteObject> serviceCenterRemoteObject_; 283 int32_t connectState_ = ServiceCenterConnectState::DISCONNECTED; 284 #ifndef SUPPORT_ERMS 285 sptr<IEcologicalRuleManager> iErMgr_ = nullptr; 286 // should remove when AG SA online 287 int32_t ECOLOGICAL_RULE_SA_ID = 9999; 288 #endif 289 std::shared_ptr<SerialQueue> serialQueue_; 290 }; 291 } // namespace AppExecFwk 292 } // namespace OHOS 293 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_FREE_INSTALL_BUNDLE_CONNECT_ABILITY_H 294