1 /* 2 * Copyright (c) 2022 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 21 #include <iremote_object.h> 22 #include <iremote_stub.h> 23 24 #include "ability_info.h" 25 #include "want.h" 26 27 namespace OHOS { 28 namespace AAFwk { 29 class AbilityManagerService; 30 31 struct FreeInstallInfo { 32 Want want; 33 int32_t userId = -1; 34 int32_t requestCode = -1; 35 int64_t startInstallTime = 0; 36 std::shared_ptr<std::promise<int32_t>> promise; 37 bool isInstalled = false; 38 sptr<IRemoteObject> callerToken = nullptr; 39 sptr<IRemoteObject> dmsCallback = nullptr; 40 }; 41 42 /** 43 * @class FreeInstallManager 44 * FreeInstallManager. 45 */ 46 class FreeInstallManager : public std::enable_shared_from_this<FreeInstallManager> { 47 public: 48 explicit FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server); 49 virtual ~FreeInstallManager() = default; 50 51 /** 52 * OnInstallFinished, StartFreeInstall is complete. 53 * 54 * @param resultCode, ERR_OK on success, others on failure. 55 * @param want, installed ability. 56 * @param userId, user`s id. 57 */ 58 void OnInstallFinished(int resultCode, const Want &want, int32_t userId, int64_t startInstallTime); 59 60 /** 61 * OnRemoteInstallFinished, DMS has finished. 62 * 63 * @param resultCode, ERR_OK on success, others on failure. 64 * @param want, installed ability. 65 * @param userId, user`s id. 66 */ 67 void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId, 68 int64_t startInstallTime); 69 70 /** 71 * Start to free install. 72 * 73 * @param want, the want of the ability to free install. 74 * @param userId, designation User ID. 75 * @param requestCode, ability request code. 76 * @param callerToken, caller ability token. 77 * @return Returns ERR_OK on success, others on failure. 78 */ 79 int StartFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken); 80 81 /** 82 * Start to remote free install. 83 * 84 * @param want, the want of the ability to free install. 85 * @param requestCode, ability request code. 86 * @param validUserId, designation User ID. 87 * @param callerToken, caller ability token. 88 * @return Returns ERR_OK on success, others on failure. 89 */ 90 int StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId, 91 const sptr<IRemoteObject> &callerToken); 92 93 /** 94 * Start to free install from another devices. 95 * The request is send from DMS. 96 * 97 * @param want, the want of the ability to free install. 98 * @param callback, used to notify caller the result of free install. 99 * @param userId, designation User ID. 100 * @param requestCode, ability request code. 101 * @return Returns ERR_OK on success, others on failure. 102 */ 103 int FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback, 104 int32_t userId, int requestCode); 105 106 /** 107 * Connect if the request is free install. 108 * @param want, the want of the ability to free install. 109 * @param userId, designation User ID. 110 * @param callerToken, caller ability token. 111 * @param localDeviceId, the device id of local. 112 * @return Returns ERR_OK on success, others on failure. 113 */ 114 int ConnectFreeInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callerToken, 115 const std::string& localDeviceId); 116 117 private: 118 std::weak_ptr<AbilityManagerService> server_; 119 std::vector<FreeInstallInfo> freeInstallList_; 120 std::vector<FreeInstallInfo> dmsFreeInstallCbs_; 121 std::map<std::string, std::time_t> timeStampMap_; 122 std::mutex distributedFreeInstallLock_; 123 std::mutex freeInstallListLock_; 124 /** 125 * Start remote free install. 126 * 127 * @param want, the want of the ability to remote free install. 128 * @param userId, designation User ID. 129 * @param requestCode, ability request code. 130 * @param callerToken, caller ability token. 131 * @return Returns ERR_OK on success, others on failure. 132 */ 133 int RemoteFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken); 134 135 int NotifyDmsCallback(const Want &want, int resultCode); 136 bool IsTopAbility(const sptr<IRemoteObject> &callerToken); 137 void NotifyFreeInstallResult(const Want &want, int resultCode, int64_t startInstallTime); 138 FreeInstallInfo BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode, 139 const sptr<IRemoteObject> &callerToken); 140 std::time_t GetTimeStamp(); 141 }; 142 } // namespace AAFwk 143 } // namespace OHOS 144 #endif // OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H 145