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_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
35namespace OHOS {
36namespace AppExecFwk {
37class AmsMgrScheduler : public AmsMgrStub {
38public:
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     *
105     * @param userId, the user id.
106     * @return
107     */
108    virtual void KillProcessesByUserId(int32_t userId) override;
109
110    /**
111     * KillProcessesByPids, only in process call is allowed,
112     * kill the processes by pid list given.
113     *
114     * @param pids, the pid list of processes are going to be killed.
115     */
116    virtual void KillProcessesByPids(std::vector<int32_t> &pids) override;
117
118    /**
119     * Set child and parent relationship
120     * @param token child process
121     * @param callerToken parent process
122     */
123    virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken) override;
124
125    /**
126     * KillProcessWithAccount, call KillProcessWithAccount() through proxy object, kill the
127     * process.
128     *
129     * @param bundleName, bundle name in Application record.
130     * @param accountId, account ID.
131     * @return ERR_OK, return back success, others fail.
132     */
133    virtual int32_t KillProcessWithAccount(
134        const std::string &bundleName, const int accountId, const bool clearPageStack = false) override;
135
136    /**
137     * UpdateApplicationInfoInstalled, call UpdateApplicationInfoInstalled() through proxy object,
138     * update the application info after new module installed.
139     *
140     * @param bundleName, bundle name in Application record.
141     * @param  uid, uid.
142     * @return ERR_OK, return back success, others fail.
143     */
144    virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid) override;
145
146    /**
147     * KillApplication, call KillApplication() through proxy object, kill the application.
148     *
149     * @param  bundleName, bundle name in Application record.
150     * @return ERR_OK, return back success, others fail.
151     */
152    virtual int32_t KillApplication(const std::string &bundleName,  const bool clearPageStack = false) override;
153
154    /**
155     * ForceKillApplication, force kill the application.
156     *
157     * @param  bundleName, bundle name in Application record.
158     * @param  userId, userId.
159     * @param  appIndex, appIndex.
160     * @return ERR_OK, return back success, others fail.
161     */
162    virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1,
163        const int appIndex = 0) override;
164
165    /**
166     * KillProcessesByAccessTokenId.
167     *
168     * @param  accessTokenId, accessTokenId.
169     * @return ERR_OK, return back success, others fail.
170     */
171    virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId) override;
172
173    /**
174     * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
175     *
176     * @param  bundleName, bundle name in Application record.
177     * @param  uid, uid.
178     * @param  reason, caller function name.
179     * @return ERR_OK, return back success, others fail.
180     */
181    virtual int KillApplicationByUid(const std::string &bundleName, const int uid,
182        const std::string& reason = "KillApplicationByUid") override;
183
184    /**
185     * KillApplicationSelf, this allows app to terminate itself.
186     *
187     * @param clearPageStack, the flag indicates if ClearPageStack lifecycle should be scheduled.
188     * @return ERR_OK for success call, others for failure.
189     */
190    virtual int KillApplicationSelf(const bool clearPageStack = false,
191        const std::string& reason = "KillApplicationSelf") override;
192
193    /**
194     * Get application info by process id.
195     *
196     * @param pid Process id.
197     * @param application Application information got.
198     * @param debug Whether IsDebugApp.
199     * @return Returns ERR_OK on success, others on failure.
200     */
201    int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug) override;
202
203    /**
204     * Record process exit reason to appRunningRecord
205     * @param pid pid
206     * @param reason reason enum
207     * @param exitMsg exitMsg
208     * @return Returns ERR_OK on success, others on failure.
209     */
210    virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg) override;
211
212    /**
213     * AbilityAttachTimeOut, called by ability manager service when an ability is loaded timeout.
214     *
215     * @param token indicates the token of the ability which is timeout.
216     */
217    virtual void AbilityAttachTimeOut(const sptr<IRemoteObject> &token) override;
218
219    /**
220     * PrepareTerminate, called before terminating one ability by ability manager service to notify application.
221     *
222     * @param token indicates the token of the ability to be terminated.
223     * @param clearMissionFlag indicates whether it is caused by cleaning mission.
224     */
225    virtual void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false) override;
226
227    /**
228     * GetRunningProcessInfoByToken, get process info for one ability.
229     *
230     * @param token indicates the token of the ability requested.
231     * @param info output of the information.
232     */
233    virtual void GetRunningProcessInfoByToken(
234        const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info) override;
235
236    /**
237     * Set AbilityForegroundingFlag of an app-record to true.
238     *
239     * @param pid, pid.
240     *
241     */
242    void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) override;
243
244    /**
245     * Start specified ability.
246     *
247     * @param want Want contains information of the ability to start.
248     * @param abilityInfo Ability information.
249     * @param requestId request id to callback
250     */
251    virtual void StartSpecifiedAbility(
252        const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, int32_t requestId = 0) override;
253
254    /**
255     * Register response of start specified ability.
256     *
257     * @param response Response of start specified ability.
258     */
259    virtual void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response) override;
260
261    /**
262     * Start specified process.
263     *
264     * @param want Want contains information wish to start.
265     * @param abilityInfo Ability information.
266     * @param requestId for callback
267     */
268    virtual void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
269        int32_t requestId = 0) override;
270
271    /**
272     * SetCurrentUserId, set the userid to app mgr by ability mgr when switching to another user.
273     *
274     * @param userId the new user.
275     */
276    virtual void SetCurrentUserId(const int32_t userId) override;
277
278    virtual void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) override;
279
280    /**
281     * GetBundleNameByPid, get bundleName and uid of a process.
282     *
283     * @param pid input param indicates the pid of the process.
284     * @param bundleName output for bundleName.
285     * @param uid output for uid.
286     * @return ERR_OK for success call, others for failure.
287     */
288    virtual int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) override;
289
290    /**
291     * @brief Register app debug listener.
292     * @param listener App debug listener.
293     * @return ERR_OK, return back success, others fail.
294     */
295    int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
296
297    /**
298     * @brief Unregister app debug listener.
299     * @param listener App debug listener.
300     * @return ERR_OK, return back success, others fail.
301     */
302    int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
303
304    /**
305     * @brief Attach app debug.
306     * @param bundleName The application bundle name.
307     * @return Returns ERR_OK on success, others on failure.
308     */
309    int32_t AttachAppDebug(const std::string &bundleName) override;
310
311    /**
312     * @brief Detach app debug.
313     * @param bundleName The application bundle name.
314     * @return Returns ERR_OK on success, others on failure.
315     */
316    int32_t DetachAppDebug(const std::string &bundleName) override;
317
318    /**
319     * @brief Set app waiting debug mode.
320     * @param bundleName The application bundle name.
321     * @param isPersist The persist flag.
322     * @return Returns ERR_OK on success, others on failure.
323     */
324    int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist) override;
325
326    /**
327     * @brief Cancel app waiting debug mode.
328     * @return Returns ERR_OK on success, others on failure.
329     */
330    int32_t CancelAppWaitingDebug() override;
331
332    /**
333     * @brief Get waiting debug mode application.
334     * @param debugInfoList The debug info list, including bundle name and persist flag.
335     * @return Returns ERR_OK on success, others on failure.
336     */
337    int32_t GetWaitingDebugApp(std::vector<std::string> &debugInfoList) override;
338
339    /**
340     * @brief Determine whether it is a waiting debug application based on the bundle name.
341     * @return Returns true if it is a waiting debug application, otherwise it returns false.
342     */
343    bool IsWaitingDebugApp(const std::string &bundleName) override;
344
345    /**
346     * @brief Clear non persist waiting debug flag.
347     */
348    void ClearNonPersistWaitingDebugFlag() override;
349
350    /**
351     * @brief Registering ability debug mode response.
352     * @param response Response for ability debug object.
353     * @return Returns ERR_OK on success, others on failure.
354     */
355    int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response) override;
356
357    /**
358     * @brief Determine whether it is an attachment debug application based on the bundle name.
359     * @param bundleName The application bundle name.
360     * @return Returns true if it is an attach debug application, otherwise it returns false.
361     */
362    bool IsAttachDebug(const std::string &bundleName) override;
363
364    /**
365     * @brief Set resident process enable status.
366     * @param bundleName The application bundle name.
367     * @param enable The current updated enable status.
368     * @param uid indicates user, 0 for all users
369     */
370    void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid) override;
371
372    /**
373     * To clear the process by ability token.
374     *
375     * @param token the unique identification to the ability.
376     */
377    virtual void ClearProcessByToken(sptr<IRemoteObject> token) override;
378
379    /**
380     * whether memory size is sufficient.
381     * @return Returns true is sufficient memory size, others return false.
382     */
383    virtual bool IsMemorySizeSufficent() override;
384
385    /**
386     * Notifies that one ability is attached to status bar.
387     *
388     * @param token the token of the abilityRecord that is attached to status bar.
389     */
390    void AttachedToStatusBar(const sptr<IRemoteObject> &token) override;
391
392    virtual void BlockProcessCacheByPids(const std::vector<int32_t> &pids) override;
393
394    /**
395     * Request to clean uiability from user.
396     *
397     * @param token the token of ability.
398     * @return Returns true if clean success, others return false.
399     */
400    virtual bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token) override;
401
402    /**
403     * whether killed for upgrade web.
404     *
405     * @param bundleName the bundle name is killed for upgrade web.
406     * @return Returns true is killed for upgrade web, others return false.
407     */
408    virtual bool IsKilledForUpgradeWeb(const std::string &bundleName) override;
409
410    /**
411     * whether the abilities of process specified by pid type only UIAbility.
412     * @return Returns true is only UIAbility, otherwise return false
413     */
414    virtual bool IsProcessContainsOnlyUIAbility(const pid_t pid) override;
415
416    /**
417     * Whether a process is attached, refer to AttachApplication
418     */
419    virtual bool IsProcessAttached(sptr<IRemoteObject> token) override;
420
421    virtual bool IsAppKilling(sptr<IRemoteObject> token) override;
422
423    virtual void SetAppExceptionCallback(sptr<IRemoteObject> callback) override;
424
425private:
426    /**
427     * @brief Judge whether the application service is ready.
428     *
429     * @return Returns true means service is ready, otherwise service is not ready.
430     */
431    bool IsReady() const;
432
433private:
434    std::shared_ptr<AppMgrServiceInner> amsMgrServiceInner_;
435    std::shared_ptr<AAFwk::TaskHandlerWrap> amsHandler_;
436    sptr<ISystemAbilityManager> systemAbilityMgr_;
437
438    DISALLOW_COPY_AND_MOVE(AmsMgrScheduler);
439};
440}  // namespace AppExecFwk
441}  // namespace OHOS
442#endif  // OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H
443