• 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 
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      * GetSystemProcessInfo, Get process info by said.
198      *
199      * @param systemAbilityId, Need the said of sa which wants to get process info.
200      * @param systemProcessInfo, Issue a parameter and return it as a result.
201      * @return ERR_OK indicates that the get successfully.
202      */
203     int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo) override;
204 
205     /**
206      * GetRunningSystemProcess, Get all processes currently running.
207      *
208      * @param systemProcessInfos, Issue a parameter and return it as a result.
209      * @return ERR_OK indicates that the get successfully.
210      */
211     int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) override;
212 
213     /**
214      * SubscribeSystemProcess, Subscribe the status of process.
215      *
216      * @param listener, callback.
217      * @return ERR_OK indicates that the Subscribe successfully.
218      */
219     int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override;
220 
221     /**
222      * SendStrategy, Send strategy to SA.
223      *
224      * @param type, type is a certain device status type.
225      * @param systemAbilityIds, Need the vector of said which wants to send strategy.
226      * @param level, level is level of a certain device status type.
227      * @param action, action is scheduling strategy.
228      * @return ERR_OK indicates that the Subscribe successfully.
229      */
230     int32_t SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds,
231         int32_t level, std::string& action) override;
232 
233     /**
234      * UnSubscribeSystemProcess, UnSubscribe the status of process.
235      *
236      * @param listener, callback.
237      * @return ERR_OK indicates that the UnSubscribe successfully.
238      */
239     int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) override;
240 
241     /**
242      * GetExtensionSaIds, Return list of saId that match extension.
243      *
244      * @param extension, extension, match with profile extension.
245      * @param saIds, list of saId that match extension
246      * @return ERR_OK indicates that the list of saId that match extension success.
247      */
248     int32_t GetExtensionSaIds(const std::string& extension, std::vector<int32_t> &saIds) override;
249 
250     /**
251      * GetExtensionRunningSaList, Return started list of hanlde that match extension.
252      *
253      * @param extension, extension, match with profile extension.
254      * @param saList, started list of remote obj that match extension
255      * @return ERR_OK indicates that the list of hanlde that match extension success.
256      */
257     int32_t GetExtensionRunningSaList(const std::string& extension, std::vector<sptr<IRemoteObject>>& saList) override;
258     int32_t GetRunningSaExtensionInfoList(const std::string& extension,
259         std::vector<SaExtensionInfo>& infoList) override;
260     int32_t GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList,
261         const std::string& eventName = "") override;
262     int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) override;
263     int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
264         std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) override;
265     int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
266         const std::vector<SystemAbilityOnDemandEvent>& sabilityOnDemandEvents) override;
267     sptr<IRemoteObject> Recompute(int32_t systemAbilityId, int32_t code) override;
268     int32_t GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds) override;
269 private:
270     sptr<IRemoteObject> GetSystemAbilityWrapper(int32_t systemAbilityId, const std::string& deviceId = "");
271     sptr<IRemoteObject> CheckSystemAbilityWrapper(int32_t code, MessageParcel& data);
272     sptr<IRemoteObject> CheckSystemAbilityWrapper(int32_t code, MessageParcel& data, int32_t& errCode);
273     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId, int32_t& errCode);
274     sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist, int32_t& errCode);
275     int32_t MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const;
276     int32_t AddSystemAbilityWrapper(int32_t code, MessageParcel& data);
277     int32_t RemoveSystemAbilityWrapper(int32_t code, MessageParcel& data);
278     int32_t ReadSystemProcessFromParcel(MessageParcel& reply, std::list<SystemProcessInfo>& systemProcessInfos);
279     int32_t ReadProcessInfoFromParcel(MessageParcel& reply, SystemProcessInfo& systemProcessInfo);
280     sptr<IRemoteObject> CheckSystemAbilityTransaction(int32_t systemAbilityId);
281     bool IsOnDemandSystemAbility(int32_t systemAbilityId);
282     int32_t ListExtensionSendReq(const std::string& extension,
283         SamgrInterfaceCode cmd, MessageParcel& reply, MessageOption& option);
284 private:
285     static inline BrokerDelegator<SystemAbilityManagerProxy> delegator_;
286     std::set<int32_t> onDemandSystemAbilityIdsSet_;
287     std::mutex onDemandSaLock_;
288 };
289 
290 class SystemAbilityProxyCallback : public SystemAbilityLoadCallbackStub {
291 public:
292     void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
293         const sptr<IRemoteObject> &remoteObject) override;
294     void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
295     std::mutex callbackLock_;
296     std::condition_variable cv_;
297     sptr<IRemoteObject> loadproxy_;
298 };
299 } // namespace OHOS
300 
301 #endif // !defined(INTERFACES_INNERKITS_SAMGR_INCLUDE_SYSTEM_ABILITY_MANAGER_PROXY_H)
302