• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 "dbinder_service.h"
28 #include "dbinder_service_stub.h"
29 #include "device_status_collect_manager.h"
30 #include "rpc_callback_imp.h"
31 #include "thread_pool.h"
32 #include "timer.h"
33 #include "sa_profiles.h"
34 #include "system_ability_manager_stub.h"
35 #include "schedule/system_ability_state_scheduler.h"
36 
37 namespace OHOS {
38 struct SAInfo {
39     sptr<IRemoteObject> remoteObj;
40     bool isDistributed = false;
41     std::u16string capability;
42     std::string permission;
43 };
44 
45 enum {
46     UUID = 0,
47     NODE_ID,
48     UNKNOWN,
49 };
50 
51 class SystemAbilityManager : public SystemAbilityManagerStub {
52 public:
53     virtual ~SystemAbilityManager();
54     static sptr<SystemAbilityManager> GetInstance();
55 
56     int32_t RemoveSystemAbility(const sptr<IRemoteObject>& ability);
57     std::vector<std::u16string> ListSystemAbilities(uint32_t dumpFlags) override;
58 
59     void SetDeviceName(const std::u16string &name);
60 
61     const std::u16string& GetDeviceName() const;
62 
63     const sptr<DBinderService> GetDBinder() const;
64 
65     sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override;
66 
67     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override;
68 
69     int32_t RemoveSystemAbility(int32_t systemAbilityId) override;
70 
71     int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override;
72     int32_t UnSubscribeSystemAbility(int32_t systemAbilityId,
73         const sptr<ISystemAbilityStatusChange>& listener) override;
74     void UnSubscribeSystemAbility(const sptr<IRemoteObject>& remoteObject);
75 
76     sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
77 
78     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
79 
80     int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override;
81 
82     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override;
83     bool DoLoadOnDemandAbility(int32_t systemAbilityId, bool& isExist);
84 
85     void NotifyRemoteSaDied(const std::u16string& name);
86     void NotifyRemoteDeviceOffline(const std::string& deviceId);
87     int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
88         const SAExtraProp& extraProp) override;
89     std::string TransformDeviceId(const std::string& deviceId, int32_t type, bool isPrivate);
90     std::string GetLocalNodeId();
91     void Init();
92     void WatchDogInit();
93 
94     int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override;
95     int32_t RemoveSystemProcess(const sptr<IRemoteObject>& procObject);
96     int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) override;
97     int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override;
98     int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override;
99     int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) override;
100     int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override;
101     int32_t DoLoadSystemAbility(int32_t systemAbilityId, const std::u16string& procName,
102         const sptr<ISystemAbilityLoadCallback>& callback, int32_t callingPid, const OnDemandEvent& event);
103     int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
104         const sptr<ISystemAbilityLoadCallback>& callback) override;
105     int32_t UnloadSystemAbility(int32_t systemAbilityId) override;
106     int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName, const OnDemandEvent& event);
107     int32_t CancelUnloadSystemAbility(int32_t systemAbilityId) override;
108     int32_t DoUnloadSystemAbility(int32_t systemAbilityId, const std::u16string& procName);
109     bool IdleSystemAbility(int32_t systemAbilityId, const std::u16string& procName,
110         const nlohmann::json& idleReason, int32_t& delayTime);
111     bool ActiveSystemAbility(int32_t systemAbilityId, const std::u16string& procName,
112         const nlohmann::json& activeReason);
113     void OnAbilityCallbackDied(const sptr<IRemoteObject>& remoteObject);
114     void OnRemoteCallbackDied(const sptr<IRemoteObject>& remoteObject);
115     sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId);
116     bool LoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId,
117         const sptr<ISystemAbilityLoadCallback>& callback);
118     int32_t DoLoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId,
119         const std::u16string& procName, const sptr<ISystemAbilityLoadCallback>& callback, const OnDemandEvent& event);
120     void NotifyRpcLoadCompleted(const std::string& srcDeviceId, int32_t systemAbilityId,
121         const sptr<IRemoteObject>& remoteObject);
122     void StartDfxTimer();
123     void DoLoadForPerf();
124     void ProcessOnDemandEvent(const OnDemandEvent& event, const std::list<SaControlInfo>& saControlList);
125     int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
126         std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override;
127     int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
128         const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override;
129 private:
130     enum class AbilityState {
131         INIT,
132         STARTING,
133         STARTED,
134     };
135 
136     using CallbackList = std::list<std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>>;
137 
138     struct AbilityItem {
139         AbilityState state = AbilityState::INIT;
140         std::map<std::string, CallbackList> callbackMap; // key : networkid
141         OnDemandEvent event;
142     };
143 
144     SystemAbilityManager();
145     std::string EventToJson(const OnDemandEvent& event);
146     void DoInsertSaData(const std::u16string& name, const sptr<IRemoteObject>& ability, const SAExtraProp& extraProp);
147     bool IsNameInValid(const std::u16string& name);
148     int32_t StartOnDemandAbility(int32_t systemAbilityId, bool& isExist);
149     void ParseRemoteSaName(const std::u16string& name, std::string& deviceId, std::u16string& saName);
150     bool IsLocalDeviceId(const std::string& deviceId);
151     bool CheckDistributedPermission();
152     int32_t AddSystemAbility(const std::u16string& name, const sptr<IRemoteObject>& ability,
153         const SAExtraProp& extraProp);
154     int32_t FindSystemAbilityNotify(int32_t systemAbilityId, int32_t code);
155     int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code);
156 
157     sptr<IRemoteObject> GetSystemProcess(const std::u16string& procName);
158 
159     void InitSaProfile();
160     bool GetSaProfile(int32_t saId, SaProfile& saProfile);
161     void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code,
162         const sptr<ISystemAbilityStatusChange>& listener);
163     void UnSubscribeSystemAbilityLocked(std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>& listenerList,
164         const sptr<IRemoteObject>& listener);
165 
166     void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
167     void SendSystemAbilityRemovedMsg(int32_t systemAbilityId);
168 
169     void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
170     void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject,
171         const sptr<ISystemAbilityLoadCallback>& callback);
172     void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback);
173     int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event);
174     void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId);
175     int32_t StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem);
176     int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event);
177     bool StopOnDemandAbility(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event);
178     bool StopOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, const OnDemandEvent& event);
179     void RemoveStartingAbilityCallback(CallbackList& callbackList, const sptr<IRemoteObject>& remoteObject);
180     void RemoveStartingAbilityCallbackForDevice(AbilityItem& abilityItem, const sptr<IRemoteObject>& remoteObject);
181     void RemoveStartingAbilityCallbackLocked(std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>& itemPair);
182     void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, const std::string& srcDeviceId,
183         const sptr<ISystemAbilityLoadCallback>& callback);
184     void RemoveCheckLoadedMsg(int32_t systemAbilityId);
185     void SendLoadedSystemAbilityMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject,
186         const sptr<ISystemAbilityLoadCallback>& callback);
187     void DoLoadRemoteSystemAbility(int32_t systemAbilityId, int32_t callingPid,
188         int32_t callingUid, const std::string& deviceId, const sptr<ISystemAbilityLoadCallback>& callback);
189     sptr<DBinderServiceStub> DoMakeRemoteBinder(int32_t systemAbilityId, int32_t callingPid, int32_t callingUid,
190         const std::string& deviceId);
191     void RemoveRemoteCallbackLocked(std::list<sptr<ISystemAbilityLoadCallback>>& callbacks,
192         const sptr<IRemoteObject>& remoteObject);
193     void CleanCallbackForLoadFailed(int32_t systemAbilityId, const std::u16string& name,
194         const std::string& srcDeviceId, const sptr<ISystemAbilityLoadCallback>& callback);
195     int32_t CheckStartEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl,
196         sptr<ISystemAbilityLoadCallback> callback);
197     int32_t CheckStopEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl);
198     bool IsSameEvent(const OnDemandEvent& event, std::list<OnDemandEvent>& enableOnceList);
199     void UpdateSaFreMap(int32_t uid, int32_t saId);
200     uint64_t GenerateFreKey(int32_t uid, int32_t saId) const;
201     void ReportGetSAPeriodically();
202     void OndemandLoad();
203     void OndemandLoadForPerf();
204     std::list<int32_t> GetAllOndemandSa();
205     std::string EventToStr(const OnDemandEvent& event);
206     bool CheckCallerProcess(SaProfile& saProfile);
207     bool CheckAllowUpdate(OnDemandPolicyType type, SaProfile& saProfile);
208     void ConvertToOnDemandEvent(const SystemAbilityOnDemandEvent& from, OnDemandEvent& to);
209     void ConvertToSystemAbilityOnDemandEvent(const OnDemandEvent& from, SystemAbilityOnDemandEvent& to);
210 #ifdef SUPPORT_DEVICE_MANAGER
211     void DeviceIdToNetworkId(std::string& networkId);
212 #endif
213 
214     std::u16string deviceName_;
215     static sptr<SystemAbilityManager> instance;
216     static std::mutex instanceLock;
217     sptr<IRemoteObject::DeathRecipient> abilityDeath_;
218     sptr<IRemoteObject::DeathRecipient> systemProcessDeath_;
219     sptr<IRemoteObject::DeathRecipient> abilityStatusDeath_;
220     sptr<IRemoteObject::DeathRecipient> abilityCallbackDeath_;
221     sptr<IRemoteObject::DeathRecipient> remoteCallbackDeath_;
222     sptr<DBinderService> dBinderService_;
223     sptr<DeviceStatusCollectManager> collectManager_;
224     std::shared_ptr<RpcSystemAbilityCallback> rpcCallbackImp_;
225 
226     // must hold abilityMapLock_ never access other locks
227     std::shared_mutex abilityMapLock_;
228     std::map<int32_t, SAInfo> abilityMap_;
229 
230     // maybe hold listenerMapLock_ and then access onDemandLock_
231     std::recursive_mutex listenerMapLock_;
232     std::map<int32_t, std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>> listenerMap_;
233     std::map<int32_t, int32_t> subscribeCountMap_;
234 
235     std::recursive_mutex onDemandLock_;
236     std::map<int32_t, std::u16string> onDemandAbilityMap_;
237     std::map<int32_t, AbilityItem> startingAbilityMap_;
238     std::map<std::u16string, sptr<IRemoteObject>> systemProcessMap_;
239     std::map<std::u16string, int64_t> startingProcessMap_;
240     std::map<int32_t, int32_t> callbackCountMap_;
241     std::mutex startEnableOnceLock_;
242     std::map<int32_t, std::list<OnDemandEvent>> startEnableOnceMap_;
243     std::mutex stopEnableOnceLock_;
244     std::map<int32_t, std::list<OnDemandEvent>> stopEnableOnceMap_;
245 
246     std::shared_ptr<AppExecFwk::EventHandler> workHandler_;
247 
248     std::map<int32_t, SaProfile> saProfileMap_;
249     std::mutex saProfileMapLock_;
250     std::mutex loadRemoteLock_;
251     std::map<std::string, std::list<sptr<ISystemAbilityLoadCallback>>> remoteCallbacks_; // key : said_deviceId
252 
253     std::mutex saFrequencyLock_;
254     std::map<uint64_t, int32_t> saFrequencyMap_; // {pid_said, count}
255 
256     std::unique_ptr<Utils::Timer> reportEventTimer_;
257     std::shared_ptr<SystemAbilityStateScheduler> abilityStateScheduler_;
258 };
259 } // namespace OHOS
260 
261 #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H)
262