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 
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      * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization.
94      *
95      * @param token, the unique identification to start the ability.
96      * @param preToken, the unique identification to call the ability.
97      * @param visibility, the visibility information about windows info.
98      * @param perceptibility, the Perceptibility information about windows info.
99      * @param connectionState, the service ability connection state.
100      * @return
101      */
102     virtual void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
103         const int32_t visibility, const int32_t perceptibility, const int32_t connectionState) override;
104 
105     /**
106      * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object,
107      * kill the process by ability token.
108      *
109      * @param token, the unique identification to the ability.
110      * @return
111      */
112     virtual void KillProcessByAbilityToken(const sptr<IRemoteObject> &token) override;
113 
114     /**
115      * KillProcessesByUserId, call KillProcessesByUserId() through proxy object,
116      * kill the processes by userId.
117      *
118      * @param userId, the user id.
119      * @return
120      */
121     virtual void KillProcessesByUserId(int32_t userId) override;
122 
123     virtual void KillProcessesByPids(std::vector<int32_t> &pids) override;
124 
125     virtual void AttachPidToParent(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callerToken) override;
126 
127     /**
128      * KillProcessWithAccount, call KillProcessWithAccount() through proxy object, kill the
129      * process.
130      *
131      * @param bundleName, bundle name in Application record.
132      * @param accountId, account ID.
133      * @return ERR_OK, return back success, others fail.
134      */
135     virtual int32_t KillProcessWithAccount(
136         const std::string &bundleName, const int accountId, const bool clearPageStack = true) override;
137 
138     /**
139      * KillProcessesInBatch, kill processes in batch, call KillProcessesInBatch() through proxy object;
140      * the killed bundle won't be started by the watcher.
141      *
142      * @param pids, the pid list of processes are going to be killed.
143      * @return ERR_OK, return back success, others fail.
144      */
145     virtual int32_t KillProcessesInBatch(const std::vector<int32_t> &pids) override;
146 
147     /**
148      * UpdateApplicationInfoInstalled, call UpdateApplicationInfoInstalled() through proxy object,
149      * update the application info after new module installed.
150      *
151      * @param bundleName, bundle name in Application record.
152      * @param  uid, uid.
153      * @return ERR_OK, return back success, others fail.
154      */
155     virtual int32_t UpdateApplicationInfoInstalled(const std::string &bundleName, const int uid) override;
156 
157     /**
158      * KillApplication, call KillApplication() through proxy object, kill the application.
159      *
160      * @param  bundleName, bundle name in Application record.
161      * @return ERR_OK, return back success, others fail.
162      */
163     virtual int32_t KillApplication(const std::string &bundleName,  const bool clearPageStack = true) override;
164 
165     /**
166      * ForceKillApplication, force kill the application.
167      *
168      * @param  bundleName, bundle name in Application record.
169      * @param  userId, userId.
170      * @param  appIndex, appIndex.
171      * @return ERR_OK, return back success, others fail.
172      */
173     virtual int32_t ForceKillApplication(const std::string &bundleName, const int userId = -1,
174         const int appIndex = 0) override;
175 
176     /**
177      * KillProcessesByAccessTokenId.
178      *
179      * @param  accessTokenId, accessTokenId.
180      * @return ERR_OK, return back success, others fail.
181      */
182     virtual int32_t KillProcessesByAccessTokenId(const uint32_t accessTokenId) override;
183 
184     /**
185      * KillApplicationByUid, call KillApplicationByUid() through proxy object, kill the application.
186      *
187      * @param  bundleName, bundle name in Application record.
188      * @param  uid, uid.
189      * @param  reason, caller function name.
190      * @return ERR_OK, return back success, others fail.
191      */
192     virtual int KillApplicationByUid(const std::string &bundleName, const int uid,
193         const std::string& reason = "KillApplicationByUid") override;
194 
195     virtual int KillApplicationSelf(const bool clearPageStack = true,
196         const std::string& reason = "KillApplicationSelf") override;
197 
198     int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug) override;
199 
200     virtual int32_t NotifyAppMgrRecordExitReason(int32_t pid, int32_t reason, const std::string &exitMsg) override;
201 
202     virtual void AbilityAttachTimeOut(const sptr<IRemoteObject> &token) override;
203 
204     virtual void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false) override;
205 
206     virtual void GetRunningProcessInfoByToken(
207         const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info) override;
208 
209     /**
210      * Set AbilityForegroundingFlag of an app-record to true.
211      *
212      * @param pid, pid.
213      *
214      */
215     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) override;
216 
217     virtual void StartSpecifiedAbility(
218         const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo, int32_t requestId = 0) override;
219 
220     virtual void RegisterStartSpecifiedAbilityResponse(const sptr<IStartSpecifiedAbilityResponse> &response) override;
221 
222     virtual void StartSpecifiedProcess(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo,
223         int32_t requestId = 0) override;
224 
225     virtual void SetCurrentUserId(const int32_t userId) override;
226 
227     virtual void SetEnableStartProcessFlagByUserId(int32_t userId, bool enableStartProcess) override;
228 
229     virtual int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid) override;
230 
231     /**
232      * @brief Register app debug listener.
233      * @param listener App debug listener.
234      * @return ERR_OK, return back success, others fail.
235      */
236     int32_t RegisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
237 
238     /**
239      * @brief Unregister app debug listener.
240      * @param listener App debug listener.
241      * @return ERR_OK, return back success, others fail.
242      */
243     int32_t UnregisterAppDebugListener(const sptr<IAppDebugListener> &listener) override;
244 
245     /**
246      * @brief Attach app debug.
247      * @param bundleName The application bundle name.
248      * @return Returns ERR_OK on success, others on failure.
249      */
250     int32_t AttachAppDebug(const std::string &bundleName) override;
251 
252     /**
253      * @brief Detach app debug.
254      * @param bundleName The application bundle name.
255      * @return Returns ERR_OK on success, others on failure.
256      */
257     int32_t DetachAppDebug(const std::string &bundleName) override;
258 
259     /**
260      * @brief Set app waiting debug mode.
261      * @param bundleName The application bundle name.
262      * @param isPersist The persist flag.
263      * @return Returns ERR_OK on success, others on failure.
264      */
265     int32_t SetAppWaitingDebug(const std::string &bundleName, bool isPersist) override;
266 
267     /**
268      * @brief Cancel app waiting debug mode.
269      * @return Returns ERR_OK on success, others on failure.
270      */
271     int32_t CancelAppWaitingDebug() override;
272 
273     /**
274      * @brief Get waiting debug mode application.
275      * @param debugInfoList The debug info list, including bundle name and persist flag.
276      * @return Returns ERR_OK on success, others on failure.
277      */
278     int32_t GetWaitingDebugApp(std::vector<std::string> &debugInfoList) override;
279 
280     /**
281      * @brief Determine whether it is a waiting debug application based on the bundle name.
282      * @return Returns true if it is a waiting debug application, otherwise it returns false.
283      */
284     bool IsWaitingDebugApp(const std::string &bundleName) override;
285 
286     /**
287      * @brief Clear non persist waiting debug flag.
288      */
289     void ClearNonPersistWaitingDebugFlag() override;
290 
291     /**
292      * @brief Registering ability debug mode response.
293      * @param response Response for ability debug object.
294      * @return Returns ERR_OK on success, others on failure.
295      */
296     int32_t RegisterAbilityDebugResponse(const sptr<IAbilityDebugResponse> &response) override;
297 
298     /**
299      * @brief Determine whether it is an attachment debug application based on the bundle name.
300      * @param bundleName The application bundle name.
301      * @return Returns true if it is an attach debug application, otherwise it returns false.
302      */
303     bool IsAttachDebug(const std::string &bundleName) override;
304 
305     /**
306      * @brief Set resident process enable status.
307      * @param bundleName The application bundle name.
308      * @param enable The current updated enable status.
309      */
310     void SetKeepAliveEnableState(const std::string &bundleName, bool enable, int32_t uid) override;
311 
312     /**
313      * To clear the process by ability token.
314      *
315      * @param token the unique identification to the ability.
316      */
317     virtual void ClearProcessByToken(sptr<IRemoteObject> token) override;
318 
319     /**
320      * whether memory size is sufficent.
321      * @return Returns true is sufficent memory size, others return false.
322      */
323     virtual bool IsMemorySizeSufficent() override;
324 
325     /**
326      * Notifies that one ability is attached to status bar.
327      *
328      * @param token the token of the abilityRecord that is attached to status bar.
329      */
330     void AttachedToStatusBar(const sptr<IRemoteObject> &token) override;
331 
332     virtual void BlockProcessCacheByPids(const std::vector<int32_t> &pids) override;
333 
334     /**
335      * whether killed for upgrade web.
336      *
337      * @param bundleName the bundle name is killed for upgrade web.
338      * @return Returns true is killed for upgrade web, others return false.
339      */
340     virtual bool IsKilledForUpgradeWeb(const std::string &bundleName) override;
341 
342     /**
343      * Request to clean uiability from user.
344      *
345      * @param token the token of ability.
346      * @return Returns true if clean success, others return false.
347      */
348     virtual bool CleanAbilityByUserRequest(const sptr<IRemoteObject> &token) override;
349 
350     /**
351      * whether the abilities of process specified by pid type only UIAbility.
352      * @return Returns true is only UIAbility, otherwise return false
353      */
354     virtual bool IsProcessContainsOnlyUIAbility(const pid_t pid) override;
355 
356     virtual bool IsProcessAttached(sptr<IRemoteObject> token) override;
357 
358 private:
359     /**
360      * @brief Judge whether the application service is ready.
361      *
362      * @return Returns true means service is ready, otherwise service is not ready.
363      */
364     bool IsReady() const;
365 
366 private:
367     std::shared_ptr<AppMgrServiceInner> amsMgrServiceInner_;
368     std::shared_ptr<AAFwk::TaskHandlerWrap> amsHandler_;
369     sptr<ISystemAbilityManager> systemAbilityMgr_;
370 
371     DISALLOW_COPY_AND_MOVE(AmsMgrScheduler);
372 };
373 }  // namespace AppExecFwk
374 }  // namespace OHOS
375 #endif  // OHOS_ABILITY_RUNTIME_AMS_MGR_SCHEDULER_H
376