1 /* 2 * Copyright (c) 2021-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 17 #ifndef INTERFACES_INNERKITS_SAMGR_INCLUDE_SYSTEM_ABILITY_MANAGER_PROXY_H 18 #define INTERFACES_INNERKITS_SAMGR_INCLUDE_SYSTEM_ABILITY_MANAGER_PROXY_H 19 20 #include <string> 21 #include <set> 22 #include "dynamic_cache.h" 23 #include "if_system_ability_manager.h" 24 #include "system_ability_on_demand_event.h" 25 #include "system_ability_load_callback_stub.h" 26 27 namespace OHOS { 28 class SystemAbilityManagerProxy : 29 public DynamicCache, public IRemoteProxy<ISystemAbilityManager> { 30 public: SystemAbilityManagerProxy(const sptr<IRemoteObject> & impl)31 explicit SystemAbilityManagerProxy(const sptr<IRemoteObject>& impl) 32 : IRemoteProxy<ISystemAbilityManager>(impl) {} 33 ~SystemAbilityManagerProxy() = default; 34 /** 35 * ListSystemAbilities, Return list of all existing abilities. 36 * 37 * @param dumpFlags, dump all 38 * @return Returns the sa where the current samgr exists. 39 */ 40 std::vector<std::u16string> ListSystemAbilities(unsigned int dumpFlags) override; 41 42 /** 43 * GetSystemAbility, Retrieve an existing ability, retrying and blocking for a few seconds if it doesn't exist. 44 * 45 * @param systemAbilityId, Need to obtain the said of sa. 46 * @return nullptr indicates acquisition failure. 47 */ 48 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override; 49 50 /** 51 * CheckSystemAbility, Retrieve an existing ability, no-blocking. 52 * 53 * @param systemAbilityId, Need to obtain the said of sa. 54 * @return nullptr indicates acquisition failure. 55 */ 56 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override; 57 58 /** 59 * RemoveSystemAbility, Remove an ability. 60 * 61 * @param systemAbilityId, Need to remove the said of sa. 62 * @return ERR_OK indicates remove success. 63 */ 64 int32_t RemoveSystemAbility(int32_t systemAbilityId) override; 65 66 /** 67 * SubscribeSystemAbility, Subscribe a system ability status, and inherit from ISystemAbilityStatusChange class. 68 * 69 * @param systemAbilityId, Need to subscribe the said of sa. 70 * @param listener, Need to implement OnAddSystemAbility, OnRemoveSystemAbility. 71 * @return ERR_OK indicates SubscribeSystemAbility success. 72 */ 73 int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override; 74 75 /** 76 * UnSubscribeSystemAbility, UnSubscribe a system ability status, and inherit from ISystemAbilityStatusChange class. 77 * 78 * @param systemAbilityId, Need to UnSubscribe the said of sa. 79 * @param listener, Need to implement OnAddSystemAbility, OnRemoveSystemAbility. 80 * @return ERR_OK indicates SubscribeSystemAbility success. 81 */ 82 int32_t UnSubscribeSystemAbility(int32_t systemAbilityId, 83 const sptr<ISystemAbilityStatusChange> &listener) override; 84 85 /** 86 * GetSystemAbility, Retrieve an existing ability, blocking for a few seconds if it doesn't exist. 87 * 88 * @param systemAbilityId, Need to get the said of sa. 89 * @param deviceId, If the device id is empty, it indicates that it is a local get. 90 * @return nullptr indicates acquisition failure. 91 */ 92 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 93 94 /** 95 * CheckSystemAbility, Retrieve an existing ability, no-blocking. 96 * 97 * @param systemAbilityId, Need to get the said of sa. 98 * @param deviceId, If the device id is empty, it indicates that it is a local get. 99 * @return nullptr indicates acquisition failure. 100 */ 101 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 102 103 /** 104 * AddOnDemandSystemAbilityInfo, Add ondemand ability info. 105 * 106 * @param systemAbilityId, Need to add info the said of sa. 107 * @param localAbilityManagerName, Process Name. 108 * @return ERR_OK indicates AddOnDemandSystemAbilityInfo success. 109 */ 110 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, 111 const std::u16string& localAbilityManagerName) override; 112 113 /** 114 * CheckSystemAbility, Retrieve an ability, no-blocking. 115 * 116 * @param systemAbilityId, Need to check the said of sa. 117 * @param isExist, Issue parameters, and a result of true indicates success. 118 * @return nullptr indicates acquisition failure. 119 */ 120 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override; 121 122 /** 123 * AddSystemAbility, add an ability to samgr 124 * 125 * @param systemAbilityId, Need to add the said of sa. 126 * @param ability, SA to be added. 127 * @param extraProp, Additional parameters for sa, such as whether it is distributed. 128 * @return ERR_OK indicates successful add. 129 */ 130 int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability, 131 const SAExtraProp& extraProp) override; 132 133 /** 134 * AddSystemProcess, add an process. 135 * 136 * @param procName, Need to add the procName of process. 137 * @param procObject, Remoteobject of procName. 138 * @return ERR_OK indicates successful add. 139 */ 140 int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override; 141 142 /** 143 * LoadSystemAbility, Load sa. 144 * 145 * @param systemAbilityId, Need to load the said of sa. 146 * @param timeout, OnLoadSystemAbilityFail and OnLoadSystemAbilitySuccess need be rewritten. 147 * @return return is not nullptr means that the load was successful. 148 */ 149 sptr<IRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout) override; 150 151 /** 152 * LoadSystemAbility, Load sa. 153 * 154 * @param systemAbilityId, Need to load the said of sa. 155 * @param callback, OnLoadSystemAbilityFail and OnLoadSystemAbilitySuccess need be rewritten. 156 * @return ERR_OK It does not mean that the load was successful, but a callback function is. 157 required to confirm whether it was successful. 158 */ 159 int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override; 160 161 /** 162 * LoadSystemAbility, Load sa. 163 * 164 * @param systemAbilityId, Need to load the said of sa. 165 * @param deviceId, if deviceId is empty, it indicates local load. 166 * @param callback, OnLoadSystemAbilityFail and OnLoadSystemAbilitySuccess need be rewritten. 167 * @return ERR_OK It does not mean that the load was successful 168 */ 169 int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId, 170 const sptr<ISystemAbilityLoadCallback>& callback) override; 171 172 /** 173 * UnloadSystemAbility, UnLoad sa. 174 * 175 * @param systemAbilityId, Need to UnLoad the said of sa. 176 * @return ERR_OK It does not mean that the unload was successful. 177 */ 178 int32_t UnloadSystemAbility(int32_t systemAbilityId) override; 179 180 /** 181 * CancelUnloadSystemAbility, CancelUnload sa. 182 * 183 * @param systemAbilityId, Need to CancelUnload the said of sa. 184 * @return ERR_OK indicates that the uninstall was canceled successfully. 185 */ 186 int32_t CancelUnloadSystemAbility(int32_t systemAbilityId) override; 187 188 /** 189 * UnloadAllIdleSystemAbility, unload all idle sa. 190 * only support for memmgrservice 191 * 192 * @return ERR_OK It means unload all idle sa success. 193 */ 194 int32_t UnloadAllIdleSystemAbility() override; 195 196 /** 197 * UnloadProcess, unload process by process name list. 198 * only support for memmgrservice 199 * 200 * @return ERR_OK It means unload all process in list. 201 */ 202 virtual int32_t UnloadProcess(const std::vector<std::u16string>& processList) override; 203 204 /** 205 * GetSystemProcessInfo, Get process info by said. 206 * only support for memmgrservice 207 * 208 * @param processList, Issue a parameter and return it as a result. 209 * @return ERR_OK indicates that the get successfully. 210 */ 211 virtual int32_t GetLruIdleSystemAbilityProc(std::vector<IdleProcessInfo>& processInfos) override; 212 213 /** 214 * GetSystemProcessInfo, Get process info by said. 215 * 216 * @param systemAbilityId, Need the said of sa which wants to get process info. 217 * @param systemProcessInfo, Issue a parameter and return it as a result. 218 * @return ERR_OK indicates that the get successfully. 219 */ 220 int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo) override; 221 222 /** 223 * GetRunningSystemProcess, Get all processes currently running. 224 * 225 * @param systemProcessInfos, Issue a parameter and return it as a result. 226 * @return ERR_OK indicates that the get successfully. 227 */ 228 int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) override; 229 230 /** 231 * SubscribeSystemProcess, Subscribe the status of process. 232 * 233 * @param listener, callback. 234 * @return ERR_OK indicates that the Subscribe successfully. 235 */ 236 int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 237 238 /** 239 * SendStrategy, Send strategy to SA. 240 * 241 * @param type, type is a certain device status type. 242 * @param systemAbilityIds, Need the vector of said which wants to send strategy. 243 * @param level, level is level of a certain device status type. 244 * @param action, action is scheduling strategy. 245 * @return ERR_OK indicates that the Subscribe successfully. 246 */ 247 int32_t SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds, 248 int32_t level, std::string& action) override; 249 250 /** 251 * UnSubscribeSystemProcess, UnSubscribe the status of process. 252 * 253 * @param listener, callback. 254 * @return ERR_OK indicates that the UnSubscribe successfully. 255 */ 256 int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override; 257 258 /** 259 * GetExtensionSaIds, Return list of saId that match extension. 260 * 261 * @param extension, extension, match with profile extension. 262 * @param saIds, list of saId that match extension 263 * @return ERR_OK indicates that the list of saId that match extension success. 264 */ 265 int32_t GetExtensionSaIds(const std::string& extension, std::vector<int32_t> &saIds) override; 266 267 /** 268 * GetExtensionRunningSaList, Return started list of hanlde that match extension. 269 * 270 * @param extension, extension, match with profile extension. 271 * @param saList, started list of remote obj that match extension 272 * @return ERR_OK indicates that the list of hanlde that match extension success. 273 */ 274 int32_t GetExtensionRunningSaList(const std::string& extension, std::vector<sptr<IRemoteObject>>& saList) override; 275 276 /** 277 * GetLocalAbilityManagerProxy, Return local ability manager proxy. 278 * 279 * @param systemAbilityId, need to obtain the said of sa. 280 * @return nullptr indicates acquistion failure. 281 */ 282 sptr<IRemoteObject> GetLocalAbilityManagerProxy(int32_t systemAbilityId) override; 283 284 int32_t GetRunningSaExtensionInfoList(const std::string& extension, 285 std::vector<SaExtensionInfo>& infoList) override; 286 int32_t GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList, 287 const std::string& eventName = "") override; 288 int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) override; 289 int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 290 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override; 291 int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type, 292 const std::vector<SystemAbilityOnDemandEvent>& sabilityOnDemandEvents) override; 293 sptr<IRemoteObject> Recompute(int32_t systemAbilityId, int32_t code) override; 294 int32_t GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds) override; 295 private: 296 sptr<IRemoteObject> GetSystemAbilityWrapper(int32_t systemAbilityId, const std::string& deviceId = ""); 297 sptr<IRemoteObject> CheckSystemAbilityWrapper(int32_t code, MessageParcel& data); 298 sptr<IRemoteObject> CheckSystemAbilityWrapper(int32_t code, MessageParcel& data, int32_t& errCode); 299 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId, int32_t& errCode); 300 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist, int32_t& errCode); 301 int32_t MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const; 302 int32_t AddSystemAbilityWrapper(int32_t code, MessageParcel& data); 303 int32_t RemoveSystemAbilityWrapper(int32_t code, MessageParcel& data); 304 int32_t ReadSystemProcessFromParcel(MessageParcel& reply, std::list<SystemProcessInfo>& systemProcessInfos); 305 int32_t ReadProcessInfoFromParcel(MessageParcel& reply, SystemProcessInfo& systemProcessInfo); 306 int32_t ReadIdleProcessInfoFromParcel(MessageParcel& reply, std::vector<IdleProcessInfo>& procInfos); 307 sptr<IRemoteObject> CheckSystemAbilityTransaction(int32_t systemAbilityId); 308 bool IsOnDemandSystemAbility(int32_t systemAbilityId); 309 int32_t ListExtensionSendReq(const std::string& extension, 310 SamgrInterfaceCode cmd, MessageParcel& reply, MessageOption& option); 311 private: 312 static inline BrokerDelegator<SystemAbilityManagerProxy> delegator_; 313 std::set<int32_t> onDemandSystemAbilityIdsSet_; 314 std::mutex onDemandSaLock_; 315 }; 316 317 class SystemAbilityProxyCallback : public SystemAbilityLoadCallbackStub { 318 public: 319 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, 320 const sptr<IRemoteObject> &remoteObject) override; 321 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 322 std::mutex callbackLock_; 323 std::condition_variable cv_; 324 sptr<IRemoteObject> loadproxy_; 325 }; 326 } // namespace OHOS 327 328 #endif // !defined(INTERFACES_INNERKITS_SAMGR_INCLUDE_SYSTEM_ABILITY_MANAGER_PROXY_H) 329