1 /*
2  * Copyright (c) 2021-2024 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 OHOS_ABILITY_RUNTIME_AMS_MGR_INTERFACE_H
17 #define OHOS_ABILITY_RUNTIME_AMS_MGR_INTERFACE_H
18 
19 #include "ability_debug_response_interface.h"
20 #include "ability_info.h"
21 #include "app_debug_listener_interface.h"
22 #include "app_record_id.h"
23 #include "application_info.h"
24 #include "configuration.h"
25 #include "iapp_state_callback.h"
26 #include "iremote_broker.h"
27 #include "iremote_object.h"
28 #include "istart_specified_ability_response.h"
29 #include "running_process_info.h"
30 
31 namespace OHOS {
32 namespace AbilityRuntime {
33 struct LoadParam;
34 }
35 namespace AppExecFwk {
36 class IAmsMgr : public IRemoteBroker {
37 public:
38     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.IAmsMgr");
39 
40     /**
41      * LoadAbility, call LoadAbility() through proxy project, load the ability that needed to be started.
42      *
43      * @param token, the unique identification to start the ability.
44      * @param preToken, the unique identification to call the ability.
45      * @param abilityInfo, the ability information.
46      * @param appInfo, the app information.
47      * @return
48      */
LoadAbility(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<ApplicationInfo> & appInfo,const std::shared_ptr<AAFwk::Want> & want,std::shared_ptr<AbilityRuntime::LoadParam> loadParam)49     virtual void LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo,
50         const std::shared_ptr<ApplicationInfo> &appInfo,
51         const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam) {};
52 
53     /**
54      * TerminateAbility, call TerminateAbility() through the proxy object, terminate the token ability.
55      *
56      * @param token, token, he unique identification to terminate the ability.
57      * @param clearMissionFlag, indicates whether terminate the ability when clearMission.
58      * @return
59      */
60     virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag) = 0;
61 
62     /**
63      * UpdateAbilityState, call UpdateAbilityState() through the proxy object, update the ability status.
64      *
65      * @param token, the unique identification to update the ability.
66      * @param state, ability status that needs to be updated.
67      * @return
68      */
69     virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state) = 0;
70 
71     /**
72      * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status.
73      *
74      * @param token, the unique identification to update the extension.
75      * @param state, extension status that needs to be updated.
76      * @return
77      */
78     virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state) = 0;
79 
80     /**
81      * RegisterAppStateCallback, call RegisterAppStateCallback() through the proxy object, register the callback.
82      *
83      * @param callback, Ams register the callback.
84      * @return
85      */
86     virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback) = 0;
87 
88     /**
89      * AbilityBehaviorAnalysis,call AbilityBehaviorAnalysis() through the proxy object,
90      * ability behavior analysis assistant process optimization.
91      *
92      * @param token, the unique identification to start the ability.
93      * @param preToken, the unique identification to call the ability.
94      * @param visibility, the visibility information about windows info.
95      * @param perceptibility, the Perceptibility information about windows info.
96      * @param connectionState, the service ability connection state.
97      * @return
98      */
99     virtual void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
100         const int32_t visibility, const int32_t perceptibility, const int32_t connectionState) = 0;
101 
102     /**
103      * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object,
104      * kill the process by ability token.
105      *
106      * @param token, the unique identification to the ability.
107      * @return
108      */
109     virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token) = 0;
110 
111     /**
112      * KillProcessesByUserId, call KillProcessesByUserId() through proxy object,
113      * kill the processes by userId.
114      *
115      * @param userId, the user id.
116      * @return
117      */
118     virtual void KillProcessesByUserId(int32_t userId) = 0;
119 
KillProcessesByPids(std::vector<int32_t> & pids)120     virtual void KillProcessesByPids(std::vector<int32_t> &pids) {}
121 
AttachPidToParent(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callerToken)122     virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken) {}
123 
124     /**
125      * KillProcessWithAccount, call KillProcessWithAccount() through proxy object,
126      * kill the process.
127      *
128      * @param bundleName, bundle name in Application record.
129      * @param accountId, account ID.
130      * @return ERR_OK, return back success, others fail.
131      */
132     virtual int KillProcessWithAccount(
133         const std::string &bundleName, const int accountId, const bool clearPageStack = true) = 0;
134 
135     /**
136      * KillProcessesInBatch, kill processes in batch, call KillProcessesInBatch() through proxy object;
137      * the killed bundle won't be started by the watcher.
138      *
139      * @param pids, the pid list of processes are going to be killed.
140      * @return ERR_OK, return back success, others fail.
141      */
142     virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids) = 0;
143 
144     /**
145      * UpdateApplicationInfoInstalled, call UpdateApplicationInfoInstalled() through proxy object,
146      * update the application info after new module installed.
147      *
148      * @param bundleName, bundle name in Application record.
149      * @param  uid, uid.
150      * @return ERR_OK, return back success, others fail.
151      */
152     virtual int UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid) = 0;
153 
154     /**
155      * KillApplication, call KillApplication() through proxy object, kill the application.
156      *
157      * @param  bundleName, bundle name in Application record.
158      * @return ERR_OK, return back success, others fail.
159      */
160     virtual int KillApplication(const std::string &bundleName, const bool clearPageStack = true) = 0;
161 
162     /**
163      * ForceKillApplication, call ForceKillApplication() through proxy object, force kill the application.
164      *
165      * @param  bundleName, bundle name in Application record.
166      * @param  userId, userId.
167      * @param  appIndex, appIndex.
168      * @return ERR_OK, return back success, others fail.
169      */
170     virtual int ForceKillApplication(const std::string &bundleName, const int userId = -1, const int appIndex = 0) = 0;
171 
172     /**
173      * KillProcessesByAccessTokenId, call KillProcessesByAccessTokenId() through proxy object,
174      * force kill the application.
175      *
176      * @param  accessTokenId, accessTokenId.
177      * @return ERR_OK, return back success, others fail.
178      */
179     virtual int KillProcessesByAccessTokenId(const uint32_t accessTokenId) = 0;
180 
181     /**
182      * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
183      *
184      * @param  bundleName, bundle name in Application record.
185      * @param  userId, userId.
186      * @param  reason, caller function name.
187      * @return ERR_OK, return back success, others fail.
188      */
189     virtual int KillApplicationByUid(const std::string &bundleName, const int uid,
190         const std::string& reason = "KillApplicationByUid") = 0;
191 
192     /**
193      * Kill the application self.
194      *
195      * @return Returns ERR_OK on success, others on failure.
196      */
197     virtual int KillApplicationSelf(const bool clearPageStack = true, const std::string& reason = "KillApplicationSelf")
198     {
199         return ERR_OK;
200     }
201 
202     virtual void AbilityAttachTimeOut(const sptr<IRemoteObject> &token) = 0;
203 
204     virtual void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false) = 0;
205 
206     virtual void GetRunningProcessInfoByToken(
207         const sptr<IRemoteObject> &token, OHOS::AppExecFwk::RunningProcessInfo &info) = 0;
208 
209     virtual void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) = 0;
210 
211     virtual void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
212         int32_t requestId = 0) = 0;
213 
214     virtual void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response) = 0;
215 
216     virtual void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
217         int32_t requestId = 0) = 0;
218 
219     virtual int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug) = 0;
220 
221     /**
222      * Record process exit reason to appRunningRecord
223      * @param pid pid
224      * @param reason reason enum
225      * @param exitMsg exitMsg
226      * @return Returns ERR_OK on success, others on failure.
227      */
228     virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg) = 0;
229 
230     /**
231      * Set the current userId of appMgr.
232      *
233      * @param userId the user id.
234      *
235      * @return
236      */
237     virtual void SetCurrentUserId(const int32_t userId) = 0;
238 
239     /**
240      * Set enable start process flag by userId
241      * @param userId the user id.
242      * @param enableStartProcess enable start process.
243      * @return
244      */
SetEnableStartProcessFlagByUserId(int32_t userId,bool enableStartProcess)245     virtual void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) {}
246 
247     /**
248      * Get bundleName by pid.
249      *
250      * @param pid process id.
251      * @param bundleName Output parameters, return bundleName.
252      * @param uid Output parameters, return userId.
253      * @return Returns ERR_OK on success, others on failure.
254      */
255     virtual int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) = 0;
256 
257     /**
258      * @brief Register app debug listener.
259      * @param listener App debug listener.
260      * @return Returns ERR_OK on success, others on failure.
261      */
262     virtual int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) = 0;
263 
264     /**
265      * @brief Unregister app debug listener.
266      * @param listener App debug listener.
267      * @return Returns ERR_OK on success, others on failure.
268      */
269     virtual int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) = 0;
270 
271     /**
272      * @brief Attach app debug.
273      * @param bundleName The application bundle name.
274      * @return Returns ERR_OK on success, others on failure.
275      */
276     virtual int32_t AttachAppDebug(const std::string &bundleName) = 0;
277 
278     /**
279      * @brief Detach app debug.
280      * @param bundleName The application bundle name.
281      * @return Returns ERR_OK on success, others on failure.
282      */
283     virtual int32_t DetachAppDebug(const std::string &bundleName) = 0;
284 
285     /**
286      * @brief Set app waiting debug mode.
287      * @param bundleName The application bundle name.
288      * @param isPersist The persist flag.
289      * @return Returns ERR_OK on success, others on failure.
290      */
291     virtual int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist) = 0;
292 
293     /**
294      * @brief Cancel app waiting debug mode.
295      * @return Returns ERR_OK on success, others on failure.
296      */
297     virtual int32_t CancelAppWaitingDebug() = 0;
298 
299     /**
300      * @brief Get waiting debug mode application.
301      * @param debugInfoList The debug info list, including bundle name and persist flag.
302      * @return Returns ERR_OK on success, others on failure.
303      */
304     virtual int32_t GetWaitingDebugApp(std::vector<std::string> &debugInfoList) = 0;
305 
306     /**
307      * @brief Determine whether it is a waiting debug application based on the bundle name.
308      * @return Returns true if it is a waiting debug application, otherwise it returns false.
309      */
310     virtual bool IsWaitingDebugApp(const std::string &bundleName) = 0;
311 
312     /**
313      * @brief Clear non persist waiting debug flag.
314      */
315     virtual void ClearNonPersistWaitingDebugFlag() = 0;
316 
317     /**
318      * @brief Registering ability debug mode response.
319      * @param response Response for ability debug object.
320      * @return Returns ERR_OK on success, others on failure.
321      */
322     virtual int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response) = 0;
323 
324     /**
325      * @brief Determine whether it is an attachment debug application based on the bundle name.
326      * @param bundleName The application bundle name.
327      * @return Returns true if it is an attach debug application, otherwise it returns false.
328      */
329     virtual bool IsAttachDebug(const std::string &bundleName) = 0;
330 
331     /**
332      * @brief Set resident process enable status.
333      * @param bundleName The application bundle name.
334      * @param enable The current updated enable status.
335      */
SetKeepAliveEnableState(const std::string & bundleName,bool enable,int32_t uid)336     virtual void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid) {};
337 
338     /**
339      * To clear the process by ability token.
340      *
341      * @param token the unique identification to the ability.
342      */
ClearProcessByToken(sptr<IRemoteObject> token)343     virtual void ClearProcessByToken(sptr<IRemoteObject> token) {}
344 
345     /**
346      * whether memory size is sufficent.
347      * @return Returns true is sufficent memory size, others return false.
348      */
349     virtual bool IsMemorySizeSufficent() = 0;
350 
351     /**
352      * Notifies that one ability is attached to status bar.
353      *
354      * @param token the token of the abilityRecord that is attached to status bar.
355      */
AttachedToStatusBar(const sptr<IRemoteObject> & token)356     virtual void AttachedToStatusBar(const sptr<IRemoteObject> &token) {}
357 
358     /**
359      * Temporarily block the process cache feature.
360      *
361      * @param pids the pids of the processes that should be blocked.
362      */
BlockProcessCacheByPids(const std::vector<int32_t> & pids)363     virtual void BlockProcessCacheByPids(const std::vector<int32_t> &pids) {}
364 
365     /**
366      * whether killed for upgrade web.
367      *
368      * @param bundleName the bundle name is killed for upgrade web.
369      * @return Returns true is killed for upgrade web, others return false.
370      */
IsKilledForUpgradeWeb(const std::string & bundleName)371     virtual bool IsKilledForUpgradeWeb(const std::string &bundleName)
372     {
373         return true;
374     }
375 
376     /**
377      * Request to clean uiability from user.
378      *
379      * @param token the token of ability.
380      * @return Returns true if clean success, others return false.
381      */
CleanAbilityByUserRequest(const sptr<IRemoteObject> & token)382     virtual bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token)
383     {
384         return false;
385     }
386 
387     /**
388      * whether the abilities of process specified by pid type only UIAbility.
389      * @return Returns true is only UIAbility, otherwise return false
390      */
IsProcessContainsOnlyUIAbility(const pid_t pid)391     virtual bool IsProcessContainsOnlyUIAbility(const pid_t pid)
392     {
393         return false;
394     }
395 
IsProcessAttached(sptr<IRemoteObject> token)396     virtual bool IsProcessAttached(sptr<IRemoteObject> token)
397     {
398         return false;
399     }
400 
401     enum class Message {
402         LOAD_ABILITY = 0,
403         TERMINATE_ABILITY,
404         UPDATE_ABILITY_STATE,
405         UPDATE_EXTENSION_STATE,
406         REGISTER_APP_STATE_CALLBACK,
407         ABILITY_BEHAVIOR_ANALYSIS,
408         KILL_PEOCESS_BY_ABILITY_TOKEN,
409         KILL_PROCESSES_BY_USERID,
410         KILL_PROCESS_WITH_ACCOUNT,
411         KILL_APPLICATION,
412         ABILITY_ATTACH_TIMEOUT,
413         PREPARE_TERMINATE_ABILITY,
414         KILL_APPLICATION_BYUID,
415         GET_RUNNING_PROCESS_INFO_BY_TOKEN,
416         START_SPECIFIED_ABILITY,
417         REGISTER_START_SPECIFIED_ABILITY_RESPONSE,
418         UPDATE_CONFIGURATION,
419         GET_CONFIGURATION,
420         GET_APPLICATION_INFO_BY_PROCESS_ID,
421         KILL_APPLICATION_SELF,
422         UPDATE_APPLICATION_INFO_INSTALLED,
423         SET_CURRENT_USER_ID,
424         Get_BUNDLE_NAME_BY_PID,
425         SET_ABILITY_FOREGROUNDING_FLAG,
426         REGISTER_APP_DEBUG_LISTENER,
427         UNREGISTER_APP_DEBUG_LISTENER,
428         ATTACH_APP_DEBUG,
429         DETACH_APP_DEBUG,
430         SET_APP_WAITING_DEBUG,
431         CANCEL_APP_WAITING_DEBUG,
432         GET_WAITING_DEBUG_APP,
433         IS_WAITING_DEBUG_APP,
434         CLEAR_NON_PERSIST_WAITING_DEBUG_FLAG,
435         REGISTER_ABILITY_DEBUG_RESPONSE,
436         IS_ATTACH_DEBUG,
437         START_SPECIFIED_PROCESS,
438         CLEAR_PROCESS_BY_TOKEN,
439         REGISTER_ABILITY_MS_DELEGATE,
440         KILL_PROCESSES_BY_PIDS,
441         ATTACH_PID_TO_PARENT,
442         IS_MEMORY_SIZE_SUFFICIENT,
443         NOTIFY_APP_MGR_RECORD_EXIT_REASON,
444         SET_KEEP_ALIVE_ENABLE_STATE,
445         ATTACHED_TO_STATUS_BAR,
446         BLOCK_PROCESS_CACHE_BY_PIDS,
447         IS_KILLED_FOR_UPGRADE_WEB,
448         IS_PROCESS_CONTAINS_ONLY_UI_EXTENSION,
449         FORCE_KILL_APPLICATION,
450         CLEAN_UIABILITY_BY_USER_REQUEST,
451         FORCE_KILL_APPLICATION_BY_ACCESS_TOKEN_ID = 49,
452         IS_PROCESS_ATTACHED,
453         ENABLE_START_PROCESS_FLAG_BY_USER_ID,
454         KILL_PROCESSES_IN_BATCH,
455     };
456 };
457 }  // namespace AppExecFwk
458 }  // namespace OHOS
459 
460 #endif  // OHOS_ABILITY_RUNTIME_AMS_MGR_INTERFACE_H
461