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 INTERFACES_INNERKITS_SAMGR_INCLUDE_IF_SYSTEM_ABILITY_MANAGER_H
17 #define INTERFACES_INNERKITS_SAMGR_INCLUDE_IF_SYSTEM_ABILITY_MANAGER_H
18 
19 #include <string>
20 #include <list>
21 
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iremote_proxy.h"
25 #include "isystem_ability_load_callback.h"
26 #include "isystem_ability_status_change.h"
27 #include "isystem_process_status_change.h"
28 #include "samgr_ipc_interface_code.h"
29 #include "system_ability_on_demand_event.h"
30 
31 namespace OHOS {
32 class ISystemAbilityManager : public IRemoteBroker {
33 public:
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     virtual std::vector<std::u16string> ListSystemAbilities(unsigned int dumpFlags = DUMP_FLAG_PRIORITY_ALL) = 0;
41 
42     enum {
43         SHEEFT_CRITICAL = 0,
44         SHEEFT_HIGH,
45         SHEEFT_NORMAL,
46         SHEEFT_DEFAULT,
47         SHEEFT_PROTO,
48     };
49 
50     static const unsigned int DUMP_FLAG_PRIORITY_CRITICAL = 1 << SHEEFT_CRITICAL;
51     static const unsigned int DUMP_FLAG_PRIORITY_HIGH = 1 << SHEEFT_HIGH;
52     static const unsigned int DUMP_FLAG_PRIORITY_NORMAL = 1 << SHEEFT_NORMAL;
53 
54     static const unsigned int DUMP_FLAG_PRIORITY_DEFAULT = 1 << SHEEFT_DEFAULT;
55     static const unsigned int DUMP_FLAG_PRIORITY_ALL = DUMP_FLAG_PRIORITY_CRITICAL |
56         DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;
57     static const unsigned int DUMP_FLAG_PROTO = 1 << SHEEFT_PROTO;
58 
59     /**
60      * GetSystemAbility, Retrieve an existing ability, retrying and blocking for a few seconds if it doesn't exist.
61      *
62      * @param systemAbilityId, Need to obtain the said of sa.
63      * @return nullptr indicates acquisition failure.
64      */
65     virtual sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId) = 0;
66 
67     /**
68      * CheckSystemAbility, Retrieve an existing ability, no-blocking.
69      *
70      * @param systemAbilityId, Need to obtain the said of sa.
71      * @return nullptr indicates acquisition failure.
72      */
73     virtual sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId) = 0;
74 
75     /**
76      * RemoveSystemAbility, Remove an ability.
77      *
78      * @param systemAbilityId, Need to remove the said of sa.
79      * @return ERR_OK indicates remove success.
80      */
81     virtual int32_t RemoveSystemAbility(int32_t systemAbilityId) = 0;
82 
83     /**
84      * SubscribeSystemAbility, Subscribe a system ability status.
85      *
86      * @param systemAbilityId, Need to subscribe the said of sa.
87      * @param listener, Need to implement OnAddSystemAbility, OnRemoveSystemAbility.
88      * @return ERR_OK indicates SubscribeSystemAbility success.
89      */
90     virtual int32_t SubscribeSystemAbility(int32_t systemAbilityId,
91         const sptr<ISystemAbilityStatusChange>& listener) = 0;
92 
93     /**
94      * UnSubscribeSystemAbility, UnSubscribe a system ability status.
95      *
96      * @param systemAbilityId, Need to UnSubscribe the said of sa.
97      * @param listener, Need to implement OnAddSystemAbility, OnRemoveSystemAbility.
98      * @return ERR_OK indicates SubscribeSystemAbility success.
99      */
100     virtual int32_t UnSubscribeSystemAbility(int32_t systemAbilityId,
101         const sptr<ISystemAbilityStatusChange>& listener) = 0;
102 
103     /**
104      * GetSystemAbility, Retrieve an existing ability, blocking for a few seconds if it doesn't exist.
105      *
106      * @param systemAbilityId, Need to get the said of sa.
107      * @param deviceId, If the device id is empty, it indicates that it is a local get.
108      * @return nullptr indicates acquisition failure.
109      */
110     virtual sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId, const std::string& deviceId) = 0;
111 
112     /**
113      * CheckSystemAbility, Retrieve an existing ability, no-blocking.
114      *
115      * @param systemAbilityId, Need to get the said of sa.
116      * @param deviceId, If the device id is empty, it indicates that it is a local get.
117      * @return nullptr indicates acquisition failure.
118      */
119     virtual sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) = 0;
120 
121     /**
122      * AddOnDemandSystemAbilityInfo, Add ondemand ability info.
123      *
124      * @param systemAbilityId, Need to add info the said of sa.
125      * @param localAbilityManagerName, Process Name.
126      * @return ERR_OK indicates AddOnDemandSystemAbilityInfo success.
127      */
128     virtual int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId,
129         const std::u16string& localAbilityManagerName) = 0;
130 
131     /**
132      * CheckSystemAbility, Retrieve an ability, no-blocking.
133      *
134      * @param systemAbilityId, Need to check the said of sa.
135      * @param isExist, Issue parameters, and a result of true indicates success.
136      * @return nullptr indicates acquisition failure.
137      */
138     virtual sptr<IRemoteObject> CheckSystemAbility(int32_t systemAbilityId, bool& isExist) = 0;
139 
140     struct SAExtraProp {
141         SAExtraProp() = default;
SAExtraPropSAExtraProp142         SAExtraProp(bool isDistributed, unsigned int dumpFlags, const std::u16string& capability,
143             const std::u16string& permission)
144         {
145             this->isDistributed = isDistributed;
146             this->dumpFlags = dumpFlags;
147             this->capability = capability;
148             this->permission = permission;
149         }
150 
151         bool isDistributed = false;
152         unsigned int dumpFlags = DUMP_FLAG_PRIORITY_DEFAULT;
153         std::u16string capability;
154         std::u16string permission;
155     };
156 
157     /**
158      * AddSystemAbility, add an ability to samgr.
159      *
160      * @param systemAbilityId, Need to add the said of sa.
161      * @param ability, SA to be added.
162      * @param extraProp, Additional parameters for sa, such as whether it is distributed.
163      * @return ERR_OK indicates successful add.
164      */
165     virtual int32_t AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
166         const SAExtraProp& extraProp = SAExtraProp(false, DUMP_FLAG_PRIORITY_DEFAULT, u"", u"")) = 0;
167 
168     /**
169      * AddSystemProcess, add an process.
170      *
171      * @param procName, Need to add the procName of process.
172      * @param procObject, Remoteobject of procName.
173      * @return ERR_OK indicates successful add.
174      */
175     virtual int32_t AddSystemProcess(const std::u16string& procName, const sptr<IRemoteObject>& procObject) = 0;
176 
177     /**
178      * LoadSystemAbility, Load sa.
179      *
180      * @param systemAbilityId, Need to load the said of sa.
181      * @param timeout, limited time to load sa.
182      * @return remote object means that the load was successful.
183      */
184     virtual sptr<IRemoteObject> LoadSystemAbility(int32_t systemAbilityId, int32_t timeout) = 0;
185 
186     /**
187      * LoadSystemAbility, Load sa.
188      *
189      * @param systemAbilityId, Need to load the said of sa.
190      * @param callback, OnLoadSystemAbilityFail and OnLoadSystemAbilitySuccess need be rewritten.
191      * @return ERR_OK It does not mean that the load was successful.
192      */
193     virtual int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr<ISystemAbilityLoadCallback>& callback) = 0;
194 
195     /**
196      * LoadSystemAbility, Load sa.
197      *
198      * @param systemAbilityId, Need to load the said of sa.
199      * @param deviceId, if deviceId is empty, it indicates local load.
200      * @param callback, OnLoadSystemAbilityFail and OnLoadSystemAbilitySuccess need be rewritten.
201      * @return ERR_OK It does not mean that the load was successful.
202      */
203     virtual int32_t LoadSystemAbility(int32_t systemAbilityId, const std::string& deviceId,
204         const sptr<ISystemAbilityLoadCallback>& callback) = 0;
205 
206     /**
207      * UnloadSystemAbility, UnLoad sa.
208      *
209      * @param systemAbilityId, Need to UnLoad the said of sa.
210      * @return ERR_OK It does not mean that the unload was successful, but sa entered an idle state.
211      */
212     virtual int32_t UnloadSystemAbility(int32_t systemAbilityId) = 0;
213 
214     /**
215      * CancelUnloadSystemAbility, CancelUnload sa.
216      *
217      * @param systemAbilityId, Need to CancelUnload the said of sa.
218      * @return ERR_OK indicates that the uninstall was canceled successfully.
219      */
220     virtual int32_t CancelUnloadSystemAbility(int32_t systemAbilityId) = 0;
221 
222     /**
223      * UnloadAllIdleSystemAbility, unload all idle sa.
224      * only support for memmgrservice
225      *
226      * @return ERR_OK It means unload all idle sa success.
227      */
228     virtual int32_t UnloadAllIdleSystemAbility() = 0;
229 
230     /**
231      * GetSystemProcessInfo, Get process info by said.
232      *
233      * @param systemAbilityId, Need the said of sa which wants to get process info.
234      * @param systemProcessInfo, Issue a parameter and return it as a result.
235      * @return ERR_OK indicates that the get successfully.
236      */
237     virtual int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo) = 0;
238 
239     /**
240      * GetRunningSystemProcess, Get all processes currently running.
241      *
242      * @param systemProcessInfos, Issue a parameter and return it as a result.
243      * @return ERR_OK indicates that the get successfully.
244      */
245     virtual int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos) = 0;
246 
247     /**
248      * SubscribeSystemProcess, Subscribe the status of process.
249      *
250      * @param listener, callback
251      * @return ERR_OK indicates that the Subscribe successfully.
252      */
253     virtual int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) = 0;
254 
255     /**
256      * SendStrategy, Send strategy to SA.
257      *
258      * @param type, type is a certain device status type.
259      * @param systemAbilityIds, Need the vector of said which wants to send strategy.
260      * @param level, level is level of a certain device status type.
261      * @param action, action is scheduling strategy.
262      * @return ERR_OK indicates that the Subscribe successfully.
263      */
264     virtual int32_t SendStrategy(int32_t type, std::vector<int32_t>& systemAbilityIds,
265         int32_t level, std::string& action) = 0;
266 
267     /**
268      * UnSubscribeSystemProcess, UnSubscribe the status of process.
269      *
270      * @param listener, callback
271      * @return ERR_OK indicates that the UnSubscribe successfully.
272      */
273     virtual int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener) = 0;
274 
275     /**
276      * GetExtensionSaIds, Return list of saId that match extension.
277      *
278      * @param extension, extension, match with profile extension.
279      * @param saIds, list of saId that match extension
280      * @return ERR_OK indicates that the list of saId that match extension success.
281      */
282     virtual int32_t GetExtensionSaIds(const std::string& extension, std::vector<int32_t> &saIds) = 0;
283 
284     /**
285      * GetExtensionRunningSaList, Return started list of hanlde that match extension.
286      *
287      * @param extension, extension, match with profile extension.
288      * @param saList, started list of remote obj that match extension
289      * @return ERR_OK indicates that the list of hanlde that match extension success.
290      */
291     virtual int32_t GetExtensionRunningSaList(const std::string& extension,
292         std::vector<sptr<IRemoteObject>>& saList) = 0;
293 
294     /**
295      * GetRunningSaExtensionInfoList, Return list of started said and process hanlde that match extension.
296      *
297      * @param extension, extension, match with profile extension.
298      * @param infoList, list of started said and sa process remote obj that match extension
299      * @return ERR_OK indicates that the list of hanlde that match extension success.
300      */
301     struct SaExtensionInfo {
302         int32_t saId = -1;
303         sptr<IRemoteObject> processObj = nullptr;
304     };
305     virtual int32_t GetRunningSaExtensionInfoList(const std::string& extension,
306         std::vector<SaExtensionInfo>& infoList) = 0;
307 
308     virtual int32_t GetCommonEventExtraDataIdlist(int32_t saId, std::vector<int64_t>& extraDataIdList,
309         const std::string& eventName = "") = 0;
310 
311     virtual int32_t GetOnDemandReasonExtraData(int64_t extraDataId, MessageParcel& extraDataParcel) = 0;
312     virtual int32_t GetOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
313         std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) = 0;
314     virtual int32_t UpdateOnDemandPolicy(int32_t systemAbilityId, OnDemandPolicyType type,
315         const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents) = 0;
316     virtual int32_t GetOnDemandSystemAbilityIds(std::vector<int32_t>& systemAbilityIds) = 0;
317 public:
318     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISystemAbilityManager");
319 protected:
320     static constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000000;
321     static constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff;
CheckInputSysAbilityId(int32_t sysAbilityId)322     bool CheckInputSysAbilityId(int32_t sysAbilityId) const
323     {
324         if (sysAbilityId >= FIRST_SYS_ABILITY_ID && sysAbilityId <= LAST_SYS_ABILITY_ID) {
325             return true;
326         }
327         return false;
328     }
329     static inline const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
330 };
331 } // namespace OHOS
332 
333 #endif // !defined(INTERFACES_INNERKITS_SAMGR_INCLUDE_IF_SYSTEM_ABILITY_MANAGER_H )
334