• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dbinder_service.h"
28 #include "dbinder_service_stub.h"
29 #include "rpc_callback_imp.h"
30 #include "thread_pool.h"
31 #include "timer.h"
32 #include "sa_profiles.h"
33 #include "system_ability_definition.h"
34 #include "system_ability_manager_stub.h"
35 
36 namespace OHOS {
37 struct SAInfo {
38     sptr<IRemoteObject> remoteObj;
39     bool isDistributed = false;
40     std::u16string capability;
41     std::string permission;
42 };
43 
44 enum {
45     UUID = 0,
46     NODE_ID,
47     UNKNOWN,
48 };
49 
50 class SystemAbilityManager : public SystemAbilityManagerStub {
51 public:
52     virtual ~SystemAbilityManager();
53     static sptr<SystemAbilityManager> GetInstance();
54 
55     int32_t RemoveSystemAbility(const sptr<IRemoteObject>& ability);
56     std::vector<std::u16string> ListSystemAbilities(uint32_t dumpFlags) override;
57 
58     void SetDeviceName(const std::u16string &name);
59 
60     const std::u16string& GetDeviceName() const;
61 
62     const sptr<DBinderService> GetDBinder() const;
63 
64     sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) override;
65 
66     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) override;
67 
68     int32_t RemoveSystemAbility(int32_t systemAbilityId) override;
69 
70     int32_t SubscribeSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityStatusChange>& listener) override;
71     int32_t UnSubscribeSystemAbility(int32_t systemAbilityId,
72         const sptr<ISystemAbilityStatusChange>& listener) override;
73     void UnSubscribeSystemAbility(const sptr<IRemoteObject>& remoteObject);
74 
75     sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
76 
77     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
78 
79     int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override;
80 
81     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override;
82 
83     void NotifyRemoteSaDied(const std::u16string& name);
84     void NotifyRemoteDeviceOffline(const std::string& deviceId);
85     int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
86         const SAExtraProp& extraProp) override;
87     std::string TransformDeviceId(const std::string& deviceId, int32_t type, bool isPrivate);
88     std::string GetLocalNodeId();
89     void Init();
90 
91     int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) override;
92     int32_t RemoveSystemProcess(const sptr<IRemoteObject>& procObject);
93 
94     int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) override;
95     int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
96         const sptr<ISystemAbilityLoadCallback>& callback) override;
97     void OnAbilityCallbackDied(const sptr<IRemoteObject>& remoteObject);
98     void OnRemoteCallbackDied(const sptr<IRemoteObject>& remoteObject);
99     sptr<IRemoteObject> GetSystemAbilityFromRemote(int32_t systemAbilityId);
100     bool LoadSystemAbilityFromRpc(const std::string& srcDeviceId, int32_t systemAbilityId,
101         const sptr<ISystemAbilityLoadCallback>& callback);
102     void NotifyRpcLoadCompleted(const std::string& srcDeviceId, int32_t systemAbilityId,
103         const sptr<IRemoteObject>& remoteObject);
104     void StartDfxTimer();
105     void DoLoadForPerf();
106 private:
107     enum class AbilityState {
108         INIT,
109         STARTING,
110         STARTED,
111     };
112 
113     using CallbackList = std::list<std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>>;
114 
115     struct AbilityItem {
116         AbilityState state = AbilityState::INIT;
117         std::map<std::string, CallbackList> callbackMap; // key : networkid
118     };
119 
120     SystemAbilityManager();
121     void DoInsertSaData(const std::u16string& name, const sptr<IRemoteObject>& ability, const SAExtraProp& extraProp);
122     bool IsNameInValid(const std::u16string& name);
123     int32_t StartOnDemandAbility(int32_t systemAbilityId);
124     void ParseRemoteSaName(const std::u16string& name, std::string& deviceId, std::u16string& saName);
125     bool IsLocalDeviceId(const std::string& deviceId);
126     bool CheckDistributedPermission();
127     int32_t AddSystemAbility(const std::u16string& name, const sptr<IRemoteObject>& ability,
128         const SAExtraProp& extraProp);
129     int32_t FindSystemAbilityNotify(int32_t systemAbilityId, int32_t code);
130     int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code);
131 
132     sptr<IRemoteObject> GetSystemProcess(const std::u16string& procName);
133 
134     void InitSaProfile();
135     bool GetSaProfile(int32_t saId, SaProfile& saProfile);
136     void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code,
137         const sptr<ISystemAbilityStatusChange>& listener);
138     void UnSubscribeSystemAbilityLocked(std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>& listenerList,
139         const sptr<IRemoteObject>& listener);
140 
141     void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
142     void SendSystemAbilityRemovedMsg(int32_t systemAbilityId);
143 
144     void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
145     void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject,
146         const sptr<ISystemAbilityLoadCallback>& callback);
147     void NotifySystemAbilityLoadFail(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback);
148     int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId);
149     void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId);
150     void StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem);
151     int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId);
152     void RemoveStartingAbilityCallback(CallbackList& callbackList, const sptr<IRemoteObject>& remoteObject);
153     void RemoveStartingAbilityCallbackForDevice(AbilityItem& abilityItem, const sptr<IRemoteObject>& remoteObject);
154     void RemoveStartingAbilityCallbackLocked(std::pair<sptr<ISystemAbilityLoadCallback>, int32_t>& itemPair);
155     void SendCheckLoadedMsg(int32_t systemAbilityId, const std::u16string& name, const std::string& srcDeviceId,
156         const sptr<ISystemAbilityLoadCallback>& callback);
157     void RemoveCheckLoadedMsg(int32_t systemAbilityId);
158     void SendLoadedSystemAblityMsg(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject,
159         const sptr<ISystemAbilityLoadCallback>& callback);
160     void DoLoadRemoteSystemAbility(int32_t systemAbilityId, int32_t callingPid,
161         int32_t callingUid, const std::string& deviceId, const sptr<ISystemAbilityLoadCallback>& callback);
162     sptr<DBinderServiceStub> DoMakeRemoteBinder(int32_t systemAbilityId, int32_t callingPid, int32_t callingUid,
163         const std::string& deviceId);
164     void RemoveRemoteCallbackLocked(std::list<sptr<ISystemAbilityLoadCallback>>& callbacks,
165         const sptr<IRemoteObject>& remoteObject);
166     void CleanCallbackForLoadFailed(int32_t systemAbilityId, const std::u16string& name,
167         const std::string& srcDeviceId, const sptr<ISystemAbilityLoadCallback>& callback);
168 
169     void UpdateSaFreMap(int32_t pid, int32_t saId);
170     uint64_t GenerateFreKey(int32_t pid, int32_t saId) const;
171     void ReportGetSAPeriodically();
172     void OndemandLoad();
173     void OndemandLoadForPerf();
174     std::list<int32_t> GetAllOndemandSa();
175 
176     std::u16string deviceName_;
177     static sptr<SystemAbilityManager> instance;
178     static std::mutex instanceLock;
179     sptr<IRemoteObject::DeathRecipient> abilityDeath_;
180     sptr<IRemoteObject::DeathRecipient> systemProcessDeath_;
181     sptr<IRemoteObject::DeathRecipient> abilityStatusDeath_;
182     sptr<IRemoteObject::DeathRecipient> abilityCallbackDeath_;
183     sptr<IRemoteObject::DeathRecipient> remoteCallbackDeath_;
184     sptr<DBinderService> dBinderService_;
185     std::shared_ptr<RpcSystemAbilityCallback> rpcCallbackImp_;
186 
187     // must hold abilityMapLock_ never access other locks
188     std::shared_mutex abilityMapLock_;
189     std::map<int32_t, SAInfo> abilityMap_;
190 
191     // maybe hold listenerMapLock_ and then access onDemandLock_
192     std::recursive_mutex listenerMapLock_;
193     std::map<int32_t, std::list<std::pair<sptr<ISystemAbilityStatusChange>, int32_t>>> listenerMap_;
194     std::map<int32_t, int32_t> subscribeCountMap_;
195 
196     std::recursive_mutex onDemandLock_;
197     std::map<int32_t, std::u16string> onDemandAbilityMap_;
198     std::map<int32_t, AbilityItem> startingAbilityMap_;
199     std::map<std::u16string, sptr<IRemoteObject>> systemProcessMap_;
200     std::map<std::u16string, int64_t> startingProcessMap_;
201     std::map<int32_t, int32_t> callbackCountMap_;
202 
203     std::shared_ptr<AppExecFwk::EventHandler> workHandler_;
204 
205     std::map<int32_t, SaProfile> saProfileMap_;
206     std::mutex saProfileMapLock_;
207 
208     std::mutex loadRemoteLock_;
209     std::map<std::string, std::list<sptr<ISystemAbilityLoadCallback>>> remoteCallbacks_; // key : said_deviceId
210 
211     ThreadPool loadPool_;
212 
213     std::mutex saFrequencyLock_;
214     std::map<uint64_t, int32_t> saFrequencyMap_; // {pid_said, count}
215 
216     std::unique_ptr<Utils::Timer> reportEventTimer_;
217 };
218 } // namespace OHOS
219 
220 #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_MANAGER_H_)
221