1 /* 2 * Copyright (c) 2021-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 SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H_ 17 #define SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H_ 18 19 #include <map> 20 #include <mutex> 21 #include <set> 22 #include <shared_mutex> 23 #include <string> 24 #include <utility> 25 26 #include "event_handler.h" 27 #include "rpc_callback_imp.h" 28 #include "dbinder_service.h" 29 #include "dbinder_service_stub.h" 30 #include "sa_profiles.h" 31 #include "system_ability_definition.h" 32 #include "system_ability_manager_stub.h" 33 34 namespace OHOS { 35 struct SAInfo { 36 sptr<IRemoteObject> remoteObj; 37 bool isDistributed = false; 38 std::u16string capability; 39 std::string permission; 40 }; 41 42 enum { 43 UUID = 0, 44 NODE_ID, 45 UNKNOWN, 46 }; 47 48 class SystemAbilityManager : public SystemAbilityManagerStub { 49 public: 50 virtual ~SystemAbilityManager(); 51 static sptr<SystemAbilityManager> GetInstance(); 52 53 int32_t RemoveSystemAbility(const sptr<IRemoteObject>& ability); 54 std::vector<std::u16string> ListSystemAbilities(uint32_t dumpFlags) override; 55 56 void SetDeviceName(const std::u16string &name); 57 58 const std::u16string& GetDeviceName() const; 59 60 const sptr<DBinderService> GetDBinder() const; 61 62 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override; 63 64 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override; 65 66 int32_t RemoveSystemAbility(int32_t systemAbilityId) override; 67 68 int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override; 69 int32_t UnSubscribeSystemAbility(int32_t systemAbilityId, 70 const sptr<ISystemAbilityStatusChange>& listener) override; 71 void UnSubscribeSystemAbility(const sptr<IRemoteObject>& remoteObject); 72 73 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 74 75 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 76 77 int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override; 78 79 sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override; 80 81 void NotifyRemoteSaDied(const std::u16string& name); 82 void NotifyRemoteDeviceOffline(const std::string& deviceId); 83 int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability, 84 const SAExtraProp& extraProp) override; 85 std::string TransformDeviceId(const std::string& deviceId, int32_t type, bool isPrivate); 86 std::string GetLocalNodeId(); 87 void Init(); 88 89 int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override; 90 int32_t RemoveSystemProcess(const sptr<IRemoteObject>& procObject); 91 92 int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override; 93 void OnAbilityCallbackDied(const sptr<IRemoteObject>& remoteObject); 94 sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId); 95 private: 96 enum class AbilityState { 97 INIT, 98 STARTING, 99 STARTED, 100 }; 101 struct AbilityItem { 102 AbilityState state = AbilityState::INIT; 103 std::list<std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>> callbackList; 104 }; 105 106 SystemAbilityManager(); 107 void DoInsertSaData(const std::u16string& name, const sptr<IRemoteObject>& ability, const SAExtraProp& extraProp); 108 bool IsNameInValid(const std::u16string& name); 109 int32_t StartOnDemandAbility(int32_t systemAbilityId); 110 void ParseRemoteSaName(const std::u16string& name, std::string& deviceId, std::u16string& saName); 111 bool IsLocalDeviceId(const std::string& deviceId); 112 bool CheckDistributedPermission(); 113 int32_t AddSystemAbility(const std::u16string& name, const sptr<IRemoteObject>& ability, 114 const SAExtraProp& extraProp); 115 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, int32_t code); 116 int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 117 118 sptr<IRemoteObject> GetSystemProcess(const std::u16string& procName); 119 120 void InitSaProfile(); 121 bool GetSaProfile(int32_t saId, SaProfile& saProfile); 122 void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code, 123 const sptr<ISystemAbilityStatusChange>& listener); 124 void UnSubscribeSystemAbilityLocked(std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>& listenerList, 125 const sptr<IRemoteObject>& listener); 126 127 void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 128 void SendSystemAbilityRemovedMsg(int32_t systemAbilityId); 129 130 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject); 131 void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject, 132 const sptr<ISystemAbilityLoadCallback>& callback); 133 void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback); 134 int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId); 135 void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId); 136 void StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem); 137 int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId); 138 void RemoveStartingAbilityCallback(AbilityItem& abilityItem, const sptr<IRemoteObject>& remoteObject); 139 void RemoveStartingAbilityCallbackLocked(std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>& itemPair); 140 void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, 141 const sptr<ISystemAbilityLoadCallback>& callback); 142 void RemoveCheckLoadedMsg(int32_t systemAbilityId); 143 144 std::u16string deviceName_; 145 static sptr<SystemAbilityManager> instance; 146 static std::mutex instanceLock; 147 sptr<IRemoteObject::DeathRecipient> abilityDeath_; 148 sptr<IRemoteObject::DeathRecipient> systemProcessDeath_; 149 sptr<IRemoteObject::DeathRecipient> abilityStatusDeath_; 150 sptr<IRemoteObject::DeathRecipient> abilityCallbackDeath_; 151 sptr<DBinderService> dBinderService_; 152 std::shared_ptr<RpcSystemAbilityCallback> rpcCallbackImp_; 153 bool isDbinderStart_ = false; 154 155 // must hold abilityMapLock_ never access other locks 156 std::shared_mutex abilityMapLock_; 157 std::map<int32_t, SAInfo> abilityMap_; 158 159 // maybe hold listenerMapLock_ and then access onDemandLock_ 160 std::recursive_mutex listenerMapLock_; 161 std::map<int32_t, std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>> listenerMap_; 162 std::map<int32_t, int32_t> subscribeCountMap_; 163 164 std::recursive_mutex onDemandLock_; 165 std::map<int32_t, std::u16string> onDemandAbilityMap_; 166 std::map<int32_t, AbilityItem> startingAbilityMap_; 167 std::map<std::u16string, sptr<IRemoteObject>> systemProcessMap_; 168 std::map<std::u16string, int64_t> startingProcessMap_; 169 std::map<int32_t, int32_t> callbackCountMap_; 170 171 std::shared_ptr<AppExecFwk::EventHandler> workHandler_; 172 173 std::map<int32_t, SaProfile> saProfileMap_; 174 std::mutex saProfileMapLock_; 175 }; 176 } // namespace OHOS 177 178 #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H_) 179