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