• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_SCHEDULER_H
17 #define OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H
18 
19 #include "ability_info.h"
20 #include "ability_running_record.h"
21 #include "ams_mgr_stub.h"
22 #include "app_debug_listener_interface.h"
23 #include "app_mgr_constants.h"
24 #include "app_mgr_service_inner.h"
25 #include "app_record_id.h"
26 #include "app_running_record.h"
27 #include "app_scheduler_proxy.h"
28 #include "appexecfwk_errors.h"
29 #include "application_info.h"
30 #include "if_system_ability_manager.h"
31 #include "nocopyable.h"
32 #include "system_ability.h"
33 #include "task_handler_wrap.h"
34 
35 namespace OHOS {
36 namespace AppExecFwk {
37 class AmsMgrScheduler : public AmsMgrStub {
38 public:
39     AmsMgrScheduler(
40         const std::shared_ptr<AppMgrServiceInner> &MgrServiceInner_,
41         const std::shared_ptr<AAFwk::TaskHandlerWrap> &Handler_);
42     virtual ~AmsMgrScheduler() override;
43 
44     /**
45      * LoadAbility, call LoadAbility() through proxy project, load the ability that needed to be started.
46      *
47      * @param token, the unique identification to start the ability.
48      * @param preToken, the unique identification to call the ability.
49      * @param abilityInfo, the ability information.
50      * @param appInfo, the app information.
51      * @param want, the starting information.
52      */
53     virtual void LoadAbility(const std::shared_ptr<AbilityInfo> &abilityInfo,
54         const std::shared_ptr<ApplicationInfo> &appInfo,
55         const std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AbilityRuntime::LoadParam> loadParam) override;
56 
57     /**
58      * TerminateAbility, call TerminateAbility() through the proxy object, terminate the token ability.
59      *
60      * @param token, token, he unique identification to terminate the ability.
61      * @param clearMissionFlag, indicates whether terminate the ability when clearMission.
62      * @return
63      */
64     virtual void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag) override;
65 
66     /**
67      * UpdateAbilityState, call UpdateAbilityState() through the proxy object, update the ability status.
68      *
69      * @param token, the unique identification to update the ability.
70      * @param state, ability status that needs to be updated.
71      * @return
72      */
73     virtual void UpdateAbilityState(const sptr<IRemoteObject> &token, const AbilityState state) override;
74 
75     /**
76      * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status.
77      *
78      * @param token, the unique identification to update the extension.
79      * @param state, extension status that needs to be updated.
80      * @return
81      */
82     virtual void UpdateExtensionState(const sptr<IRemoteObject> &token, const ExtensionState state) override;
83 
84     /**
85      * RegisterAppStateCallback, call RegisterAppStateCallback() through the proxy object, register the callback.
86      *
87      * @param callback, Ams register the callback.
88      * @return
89      */
90     virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback) override;
91 
92     /**
93      * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object,
94      * kill the process by ability token.
95      *
96      * @param token, the unique identification to the ability.
97      * @return
98      */
99     virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token) override;
100 
101     /**
102      * KillProcessesByUserId, call KillProcessesByUserId() through proxy object,
103      * kill the processes by userId.
104      * Send appSpawn uninstall debug hap message.
105      *
106      * @param userId, the user id.
107      * @param isNeedSendAppSpawnMsg, true send appSpawn message otherwise not send.
108      * @return
109      */
110     virtual void KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnMsg = false,
111         sptr<AAFwk::IUserCallback> callback = nullptr) override;
112 
113     /**
114      * KillProcessesByPids, only in process call is allowed,
115      * kill the processes by pid list given.
116      *
117      * @param pids, the pid list of processes are going to be killed.
118      * @param reason, the reason to kill the processes.
119      */
120     virtual void KillProcessesByPids(const std::vector<int32_t> &pids,
121         const std::string &reason = "KillProcessesByPids") override;
122 
123     /**
124      * Set child and parent relationship
125      * @param token child process
126      * @param callerToken parent process
127      */
128     virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken) override;
129 
130     /**
131      * KillProcessWithAccount, call KillProcessWithAccount() through proxy object, kill the
132      * process.
133      *
134      * @param bundleName, bundle name in Application record.
135      * @param accountId, account ID.
136      * @return ERR_OK, return back success, others fail.
137      */
138     virtual int32_t KillProcessWithAccount(const std::string &bundleName, const int accountId,
139         const bool clearPageStack = false, int32_t appIndex = 0) override;
140 
141     /**
142      * KillProcessesInBatch, kill processes in batch, call KillProcessesInBatch() through proxy object;
143      * the killed bundle won't be started by the watcher.
144      *
145      * @param pids, the pid list of processes are going to be killed.
146      * @return ERR_OK, return back success, others fail.
147      */
148     virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids) override;
149 
150     /**
151      * UpdateApplicationInfoInstalled, call UpdateApplicationInfoInstalled() through proxy object,
152      * update the application info after new module installed.
153      *
154      * @param bundleName, bundle name in Application record.
155      * @param  uid, uid.
156      * @return ERR_OK, return back success, others fail.
157      */
158     virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid,
159         const std::string &moduleName) override;
160 
161     /**
162      * KillApplication, call KillApplication() through proxy object, kill the application.
163      *
164      * @param  bundleName, bundle name in Application record.
165      * @return ERR_OK, return back success, others fail.
166      */
167     virtual int32_t KillApplication(const std::string &bundleName, bool clearPageStack = false,
168         int32_t appIndex = 0) override;
169 
170     /**
171      * ForceKillApplication, force kill the application.
172      *
173      * @param  bundleName, bundle name in Application record.
174      * @param  userId, userId.
175      * @param  appIndex, appIndex.
176      * @return ERR_OK, return back success, others fail.
177      */
178     virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1,
179         const int appIndex = 0) override;
180 
181     /**
182      * KillProcessesByAccessTokenId.
183      *
184      * @param  accessTokenId, accessTokenId.
185      * @return ERR_OK, return back success, others fail.
186      */
187     virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId) override;
188 
189     /**
190      * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
191      *
192      * @param  bundleName, bundle name in Application record.
193      * @param  uid, uid.
194      * @param  reason, caller function name.
195      * @return ERR_OK, return back success, others fail.
196      */
197     virtual int KillApplicationByUid(const std::string &bundleName, const int uid,
198         const std::string& reason = "KillApplicationByUid") override;
199 
200     /**
201      * KillApplicationSelf, this allows app to terminate itself.
202      *
203      * @param clearPageStack, the flag indicates if ClearPageStack lifecycle should be scheduled.
204      * @return ERR_OK for success call, others for failure.
205      */
206     virtual int KillApplicationSelf(const bool clearPageStack = false,
207         const std::string& reason = "KillApplicationSelf") override;
208 
209     /**
210      * Get application info by process id.
211      *
212      * @param pid Process id.
213      * @param application Application information got.
214      * @param debug Whether IsDebugApp.
215      * @return Returns ERR_OK on success, others on failure.
216      */
217     int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug) override;
218 
219     /**
220      * Record process exit reason to appRunningRecord
221      * @param pid pid
222      * @param reason reason enum
223      * @param exitMsg exitMsg
224      * @return Returns ERR_OK on success, others on failure.
225      */
226     virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg) override;
227 
228     /**
229      * AbilityAttachTimeOut, called by ability manager service when an ability is loaded timeout.
230      *
231      * @param token indicates the token of the ability which is timeout.
232      */
233     virtual void AbilityAttachTimeOut(const sptr<IRemoteObject> &token) override;
234 
235     /**
236      * PrepareTerminate, called before terminating one ability by ability manager service to notify application.
237      *
238      * @param token indicates the token of the ability to be terminated.
239      * @param clearMissionFlag indicates whether it is caused by cleaning mission.
240      */
241     virtual void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false) override;
242 
243     /**
244      * GetRunningProcessInfoByToken, get process info for one ability.
245      *
246      * @param token indicates the token of the ability requested.
247      * @param info output of the information.
248      */
249     virtual void GetRunningProcessInfoByToken(
250         const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info) override;
251 
252     /**
253      * Set AbilityForegroundingFlag of an app-record to true.
254      *
255      * @param pid, pid.
256      *
257      */
258     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) override;
259 
260     /**
261      * Start specified ability.
262      *
263      * @param want Want contains information of the ability to start.
264      * @param abilityInfo Ability information.
265      * @param requestId request id to callback
266      */
267     virtual void StartSpecifiedAbility(
268         const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, int32_t requestId = 0) override;
269 
270     /**
271      * Register response of start specified ability.
272      *
273      * @param response Response of start specified ability.
274      */
275     virtual void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response) override;
276 
277     virtual void PrepareTerminateApp(const pid_t pid, const std::string &moduleName) override;
278 
279     /**
280      * Start specified process.
281      *
282      * @param want Want contains information wish to start.
283      * @param abilityInfo Ability information.
284      * @param requestId for callback
285      */
286     virtual void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
287         int32_t requestId = 0) override;
288 
289     /**
290      * SetCurrentUserId, set the userid to app mgr by ability mgr when switching to another user.
291      *
292      * @param userId the new user.
293      */
294     virtual void SetCurrentUserId(const int32_t userId) override;
295 
296     virtual void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) override;
297 
298     /**
299      * GetBundleNameByPid, get bundleName and uid of a process.
300      *
301      * @param pid input param indicates the pid of the process.
302      * @param bundleName output for bundleName.
303      * @param uid output for uid.
304      * @return ERR_OK for success call, others for failure.
305      */
306     virtual int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) override;
307 
308     /**
309      * @brief Register app debug listener.
310      * @param listener App debug listener.
311      * @return ERR_OK, return back success, others fail.
312      */
313     int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
314 
315     /**
316      * @brief Unregister app debug listener.
317      * @param listener App debug listener.
318      * @return ERR_OK, return back success, others fail.
319      */
320     int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
321 
322     /**
323      * @brief Attach app debug.
324      * @param bundleName The application bundle name.
325      * @return Returns ERR_OK on success, others on failure.
326      */
327     int32_t AttachAppDebug(const std::string &bundleName, bool isDebugFromLocal) override;
328 
329     /**
330      * @brief Detach app debug.
331      * @param bundleName The application bundle name.
332      * @return Returns ERR_OK on success, others on failure.
333      */
334     int32_t DetachAppDebug(const std::string &bundleName) override;
335 
336     /**
337      * @brief Set app waiting debug mode.
338      * @param bundleName The application bundle name.
339      * @param isPersist The persist flag.
340      * @return Returns ERR_OK on success, others on failure.
341      */
342     int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist) override;
343 
344     /**
345      * @brief Cancel app waiting debug mode.
346      * @return Returns ERR_OK on success, others on failure.
347      */
348     int32_t CancelAppWaitingDebug() override;
349 
350     /**
351      * @brief Get waiting debug mode application.
352      * @param debugInfoList The debug info list, including bundle name and persist flag.
353      * @return Returns ERR_OK on success, others on failure.
354      */
355     int32_t GetWaitingDebugApp(std::vector<std::string> &debugInfoList) override;
356 
357     /**
358      * @brief Determine whether it is a waiting debug application based on the bundle name.
359      * @return Returns true if it is a waiting debug application, otherwise it returns false.
360      */
361     bool IsWaitingDebugApp(const std::string &bundleName) override;
362 
363     /**
364      * @brief Clear non persist waiting debug flag.
365      */
366     void ClearNonPersistWaitingDebugFlag() override;
367 
368     /**
369      * @brief Registering ability debug mode response.
370      * @param response Response for ability debug object.
371      * @return Returns ERR_OK on success, others on failure.
372      */
373     int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response) override;
374 
375     /**
376      * @brief Determine whether it is an attachment debug application based on the bundle name.
377      * @param bundleName The application bundle name.
378      * @return Returns true if it is an attach debug application, otherwise it returns false.
379      */
380     bool IsAttachDebug(const std::string &bundleName) override;
381 
382     /**
383      * @brief Set resident process enable status.
384      * @param bundleName The application bundle name.
385      * @param enable The current updated enable status.
386      * @param uid indicates user, 0 for all users
387      */
388     void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid) override;
389 
390     /**
391      * @brief Set non-resident keep-alive process status.
392      * @param bundleName The application bundle name.
393      * @param enable The current updated enable status.
394      * @param uid indicates user, 0 for all users
395      */
396     void SetKeepAliveDkv(const std::string &bundleName, bool enable, int32_t uid) override;
397 
398     /**
399      * To clear the process by ability token.
400      *
401      * @param token the unique identification to the ability.
402      */
403     virtual void ClearProcessByToken(sptr<IRemoteObject> token) override;
404 
405     /**
406      * whether memory size is sufficient.
407      * @return Returns true is sufficient memory size, others return false.
408      */
409     virtual bool IsMemorySizeSufficent() override;
410 
411     /**
412      * Notifies that one ability is attached to status bar.
413      *
414      * @param token the token of the abilityRecord that is attached to status bar.
415      */
416     void AttachedToStatusBar(const sptr<IRemoteObject> &token) override;
417 
418     virtual void BlockProcessCacheByPids(const std::vector<int32_t> &pids) override;
419 
420     /**
421      * whether killed for upgrade web.
422      *
423      * @param bundleName the bundle name is killed for upgrade web.
424      * @return Returns true is killed for upgrade web, others return false.
425      */
426     virtual bool IsKilledForUpgradeWeb(const std::string &bundleName) override;
427 
428     /**
429      * Request to clean uiability from user.
430      *
431      * @param token the token of ability.
432      * @return Returns true if clean success, others return false.
433      */
434     virtual bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token) override;
435 
436     /**
437      * whether the abilities of process specified by pid type only UIAbility.
438      * @return Returns true is only UIAbility, otherwise return false
439      */
440     virtual bool IsProcessContainsOnlyUIAbility(const pid_t pid) override;
441 
442     /**
443      * Whether a process is attached, refer to AttachApplication
444      */
445     virtual bool IsProcessAttached(sptr<IRemoteObject> token) override;
446 
447     virtual bool IsCallerKilling(const std::string& callerKey) override;
448 
449 private:
450     /**
451      * @brief Judge whether the application service is ready.
452      *
453      * @return Returns true means service is ready, otherwise service is not ready.
454      */
455     bool IsReady() const;
456 
457 private:
458     std::shared_ptr<AppMgrServiceInner> amsMgrServiceInner_;
459     std::shared_ptr<AAFwk::TaskHandlerWrap> amsHandler_;
460     sptr<ISystemAbilityManager> systemAbilityMgr_;
461 
462     DISALLOW_COPY_AND_MOVE(AmsMgrScheduler);
463 };
464 }  // namespace AppExecFwk
465 }  // namespace OHOS
466 #endif  // OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H
467