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