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 OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H 18 19 #include <future> 20 #include "cpp/mutex.h" 21 22 #include <iremote_object.h> 23 #include <iremote_stub.h> 24 #include <memory> 25 26 #include "ability_info.h" 27 #include "free_install_observer_manager.h" 28 #include "start_options.h" 29 #include "want.h" 30 31 namespace OHOS { 32 namespace AAFwk { 33 class AbilityManagerService; 34 35 struct FreeInstallInfo { 36 bool isInstalled = false; 37 bool isPreStartMissionCalled = false; 38 bool isStartUIAbilityBySCBCalled = false; 39 bool isFreeInstallFinished = false; 40 bool isOpenAtomicServiceShortUrl = false; 41 uint32_t specifyTokenId = 0; 42 int32_t userId = -1; 43 int32_t requestCode = -1; 44 int resultCode = 0; 45 std::shared_ptr<std::promise<int32_t>> promise; 46 std::string identity; 47 sptr<IRemoteObject> callerToken = nullptr; 48 sptr<IRemoteObject> dmsCallback = nullptr; 49 std::shared_ptr<Want> originalWant = nullptr; 50 std::shared_ptr<StartOptions> startOptions = nullptr; 51 Want want; 52 }; 53 54 struct FreeInstallParams { 55 bool isAsync = false; 56 bool isOpenAtomicServiceShortUrl = false; 57 uint32_t specifyTokenId = 0; 58 std::shared_ptr<Want> originalWant = nullptr; 59 std::shared_ptr<StartOptions> startOptions = nullptr; 60 }; 61 62 /** 63 * @class FreeInstallManager 64 * FreeInstallManager. 65 */ 66 class FreeInstallManager : public std::enable_shared_from_this<FreeInstallManager> { 67 public: 68 explicit FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server); 69 virtual ~FreeInstallManager() = default; 70 71 /** 72 * OnInstallFinished, StartFreeInstall is complete. 73 * 74 * @param resultCode, ERR_OK on success, others on failure. 75 * @param want, installed ability. 76 * @param userId, user`s id. 77 */ 78 void OnInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId, bool isAsync = false); 79 80 /** 81 * OnRemoteInstallFinished, DMS has finished. 82 * 83 * @param resultCode, ERR_OK on success, others on failure. 84 * @param want, installed ability. 85 * @param userId, user`s id. 86 */ 87 void OnRemoteInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId); 88 89 /** 90 * Start to free install. 91 * 92 * @param want, the want of the ability to free install. 93 * @param userId, designation User ID. 94 * @param requestCode, ability request code. 95 * @param callerToken, caller ability token. 96 * @param isAsync, the request is async. 97 * @param isOpenAtomicServiceShortUrl, the flag of open atomic service short url. 98 * @return Returns ERR_OK on success, others on failure. 99 */ 100 int StartFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken, 101 std::shared_ptr<FreeInstallParams> param = nullptr); 102 103 /** 104 * Start to remote free install. 105 * 106 * @param want, the want of the ability to free install. 107 * @param requestCode, ability request code. 108 * @param validUserId, designation User ID. 109 * @param callerToken, caller ability token. 110 * @return Returns ERR_OK on success, others on failure. 111 */ 112 int StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId, 113 const sptr<IRemoteObject> &callerToken); 114 115 /** 116 * Start to free install from another devices. 117 * The request is send from DMS. 118 * 119 * @param want, the want of the ability to free install. 120 * @param callback, used to notify caller the result of free install. 121 * @param userId, designation User ID. 122 * @param requestCode, ability request code. 123 * @return Returns ERR_OK on success, others on failure. 124 */ 125 int FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback, 126 int32_t userId, int requestCode); 127 128 /** 129 * Connect if the request is free install. 130 * @param want, the want of the ability to free install. 131 * @param userId, designation User ID. 132 * @param callerToken, caller ability token. 133 * @param localDeviceId, the device id of local. 134 * @return Returns ERR_OK on success, others on failure. 135 */ 136 int ConnectFreeInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callerToken, 137 const std::string& localDeviceId); 138 139 /** 140 * Add an observer from application into freeInstallObserverManager. 141 * @param observer, the observer of the ability to free install. 142 * @return Returns ERR_OK on success, others on failure. 143 */ 144 int AddFreeInstallObserver(const sptr<IRemoteObject> &callerToken, 145 const sptr<AbilityRuntime::IFreeInstallObserver> &observer); 146 147 /** 148 * Get free install task info. 149 * 150 * @param bundleName, the bundle name of the task. 151 * @param abilityName, the ability name of the task. 152 * @param startTime, the start time of the task. 153 * @param taskInfo, the found task info 154 * @return Returns true on success, false on failure. 155 */ 156 bool GetFreeInstallTaskInfo(const std::string& bundleName, const std::string& abilityName, 157 const std::string& startTime, FreeInstallInfo& taskInfo); 158 159 /** 160 * Get free install task info. 161 * 162 * @param sessionId, the sessionId of the task. 163 * @param taskInfo, the found task info 164 * @return Returns true on success, false on failure. 165 */ 166 bool GetFreeInstallTaskInfo(const std::string& sessionId, FreeInstallInfo& taskInfo); 167 168 /** 169 * Set the isStartUIAbilityBySCBCalled flag of the given free install task. 170 * 171 * @param bundleName, the bundle name of the task. 172 * @param abilityName, the abilitu name of the task. 173 * @param startTime, the start time of the task. 174 * @param scbCallStatus, the status of whether StartUIAbilityBySCB is called. 175 */ 176 void SetSCBCallStatus(const std::string& bundleName, const std::string& abilityName, 177 const std::string& startTime, bool scbCallStatus); 178 179 /** 180 * Set the isPreStartMissionCalled flag of the given free install task. 181 * 182 * @param bundleName, the bundle name of the task. 183 * @param abilityName, the abilitu name of the task. 184 * @param startTime, the start time of the task. 185 * @param preStartMissionCallStatus, the status of whether PreStartMission is called. 186 */ 187 void SetPreStartMissionCallStatus(const std::string& bundleName, const std::string& abilityName, 188 const std::string& startTime, bool preStartMissionCallStatus); 189 190 /** 191 * Set the sessionId of the given free install task. 192 * 193 * @param bundleName, the bundle name of the task. 194 * @param abilityName, the abilitu name of the task. 195 * @param startTime, the start time of the task. 196 * @param sessionId, the sessionId of the free install task. 197 */ 198 void SetFreeInstallTaskSessionId(const std::string& bundleName, const std::string& abilityName, 199 const std::string& startTime, const std::string& sessionId); 200 201 private: 202 std::weak_ptr<AbilityManagerService> server_; 203 std::vector<FreeInstallInfo> freeInstallList_; 204 std::vector<FreeInstallInfo> dmsFreeInstallCbs_; 205 std::map<std::string, std::time_t> timeStampMap_; 206 ffrt::mutex distributedFreeInstallLock_; 207 ffrt::mutex freeInstallListLock_; 208 ffrt::mutex freeInstallObserverLock_; 209 210 int SetAppRunningState(Want &want); 211 212 /** 213 * Start remote free install. 214 * 215 * @param want, the want of the ability to remote free install. 216 * @param userId, designation User ID. 217 * @param requestCode, ability request code. 218 * @param callerToken, caller ability token. 219 * @return Returns ERR_OK on success, others on failure. 220 */ 221 int RemoteFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken); 222 223 int NotifyDmsCallback(const Want &want, int resultCode); 224 bool IsTopAbility(const sptr<IRemoteObject> &callerToken); 225 void NotifyFreeInstallResult(int32_t recordId, const Want &want, int resultCode, bool isAsync = false); 226 FreeInstallInfo BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode, 227 const sptr<IRemoteObject> &callerToken, std::shared_ptr<FreeInstallParams> param = nullptr); 228 std::time_t GetTimeStamp(); 229 230 void RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName, 231 const std::string &startTime); 232 233 void PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId); 234 235 void RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime); 236 237 void StartAbilityByFreeInstall(FreeInstallInfo &info, std::string &bundleName, std::string &abilityName, 238 std::string &startTime); 239 void StartAbilityByPreInstall(int32_t recordId, FreeInstallInfo &info, std::string &bundleName, 240 std::string &abilityName, std::string &startTime); 241 int32_t UpdateElementName(Want &want, int32_t userId) const; 242 void HandleFreeInstallResult(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); 243 void HandleOnFreeInstallSuccess(int32_t recordId, FreeInstallInfo &freeInstallInfo, bool isAsync); 244 void HandleOnFreeInstallFail(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync); 245 void StartAbilityByConvertedWant(FreeInstallInfo &info, const std::string &startTime); 246 void StartAbilityByOriginalWant(FreeInstallInfo &info, const std::string &startTime); 247 bool VerifyStartFreeInstallPermission(const sptr<IRemoteObject> &callerToken); 248 int32_t GetRecordIdByToken(const sptr<IRemoteObject> &callerToken); 249 void NotifyInsightIntentFreeInstallResult(const Want &want, int resultCode); 250 void NotifyInsightIntentExecuteDone(const Want &want, int resultCode); 251 }; 252 } // namespace AAFwk 253 } // namespace OHOS 254 #endif // OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H 255