• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
17 #define OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
18 
19 #include <list>
20 #include <string>
21 #include <vector>
22 
23 #include "ability_info.h"
24 #include "ability_running_record.h"
25 #include "ams_mgr_scheduler.h"
26 #include "app_malloc_info.h"
27 #include "app_mgr_constants.h"
28 #include "app_mgr_service_event_handler.h"
29 #include "app_mgr_service_inner.h"
30 #include "app_mgr_stub.h"
31 #include "app_record_id.h"
32 #include "app_running_record.h"
33 #include "app_running_status_listener_interface.h"
34 #include "app_scheduler_proxy.h"
35 #include "appexecfwk_errors.h"
36 #include "application_info.h"
37 #include "if_system_ability_manager.h"
38 #include "nocopyable.h"
39 #include "system_ability.h"
40 #include "task_handler_wrap.h"
41 
42 namespace OHOS {
43 namespace AppExecFwk {
44 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
45 
46 struct AppMgrServiceState {
47     ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START;
48     SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT;
49 };
50 
51 class AMSEventHandler;
52 
53 class AppMgrService : public SystemAbility, public AppMgrStub {
54 public:
55     DECLEAR_SYSTEM_ABILITY(AppMgrService);
56 
57     AppMgrService();
58     explicit AppMgrService(const int32_t serviceId, bool runOnCreate = false);
59     virtual ~AppMgrService() override;
60 
61     // the function about application
62     // attach the application to ams, then ams can control it.
63     /**
64      * AttachApplication, call AttachApplication() through proxy object,
65      * get all the information needed to start the Application (data related to the Application ).
66      *
67      * @param app, information needed to start the Application.
68      * @return
69      */
70     virtual void AttachApplication(const sptr<IRemoteObject> &app) override;
71 
72     // notify the ams update the state of an app, when it entered foreground.
73 
74     /**
75      * ApplicationForegrounded, call ApplicationForegrounded() through proxy object,
76      * set the application to Foreground State.
77      *
78      * @param recordId, a unique record that identifies this Application from others.
79      * @return
80      */
81     virtual void ApplicationForegrounded(const int32_t recordId) override;
82 
83     /**
84      * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object,
85      * set the application to Backgrounded State.
86      *
87      * @param recordId, a unique record that identifies this Application from others.
88      * @return
89      */
90     virtual void ApplicationBackgrounded(const int32_t recordId) override;
91 
92     /**
93      * ApplicationTerminated, call ApplicationTerminated() through proxy object,
94      * terminate the application.
95      *
96      * @param recordId, a unique record that identifies this Application from others.
97      * @return
98      */
99     virtual void ApplicationTerminated(const int32_t recordId) override;
100 
101     /**
102      * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record.
103      *
104      * @param token, a unique record that identifies AbilityCleaned from others.
105      * @return
106      */
107     virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override;
108 
109     /**
110      * ClearUpApplicationData, call ClearUpApplicationData() through proxy project,
111      * clear the application data.
112      *
113      * @param bundleName, bundle name in Application record.
114      * @return Returns ERR_OK on success, others on failure.
115      */
116     virtual int32_t ClearUpApplicationData(const std::string &bundleName,
117         const int32_t userId = -1) override;
118 
119     /**
120      * ClearUpApplicationDataBySelf, call ClearUpApplicationDataBySelf() through proxy project,
121      * clear the application data by self.
122      *
123      * @param userId, user ID.
124      * @return Returns ERR_OK on success, others on failure.
125      */
126     virtual int32_t ClearUpApplicationDataBySelf(int32_t userId = -1) override;
127 
128     /**
129      * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project.
130      * Obtains information about application processes that are running on the device.
131      *
132      * @param info, app name in Application record.
133      * @return Returns ERR_OK on success, others on failure.
134      */
135     virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override;
136 
137     /**
138      * GetAllRenderProcesses, call GetAllRenderProcesses() through proxy project.
139      * Obtains information about render processes that are running on the device.
140      *
141      * @param info, render process info.
142      * @return ERR_OK, return back success, others fail.
143      */
144     virtual int32_t GetAllRenderProcesses(std::vector<RenderProcessInfo> &info) override;
145 
146     /**
147      * JudgeSandboxByPid, call JudgeSandboxByPid() through proxy project.
148      * Obtains information about application processes that are running on the device.
149      *
150      * @param pid, the pid of current app running record.
151      * @param isSandbox, current app is or not a sandbox.
152      * @return Returns ERR_OK on success, others on failure.
153      */
154     virtual int32_t JudgeSandboxByPid(pid_t pid, bool &isSandbox) override;
155 
156     /**
157      * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project.
158      * Obtains information about application processes that are running on the device.
159      *
160      * @param info, app name in Application record.
161      * @param userId, userId.
162      *
163      * @return Returns ERR_OK on success, others on failure.
164      */
165     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override;
166 
167     /**
168      * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project.
169      * Obtains information about current application process which is running on the device.
170      *
171      * @param info, app name in Application record.
172      *
173      * @return Returns ERR_OK on success, others on failure.
174      */
175     virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override;
176 
177     /**
178      * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project.
179      * Notify applications background the current memory level.
180      *
181      * @param level, current memory level.
182      * @return Returns ERR_OK on success, others on failure.
183      */
184     virtual int32_t NotifyMemoryLevel(int32_t level) override;
185 
186     /**
187      * DumpHeapMemory, call DumpHeapMemory() through proxy project.
188      * Get the application's memory allocation info.
189      *
190      * @param pid, pid input.
191      * @param mallocInfo, dynamic storage information output.
192      * @return Returns ERR_OK on success, others on failure.
193      */
194     virtual int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo) override;
195 
196     // the function about system
197     /**
198      * CheckPermission, call CheckPermission() through proxy object, check the permission.
199      *
200      * @param recordId, a unique record that identifies this Application from others.
201      * @param permission, check the permissions.
202      * @return Returns ERR_OK on success, others on failure.
203      */
204     virtual int32_t CheckPermission(const int32_t recordId, const std::string &permission) override;
205 
206     // the function about service running info
207     /**
208      * QueryServiceState, Query application service status.
209      *
210      * @return the application service status.
211      */
212     AppMgrServiceState QueryServiceState();
213 
214     /**
215      * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance.
216      *
217      * @return sptr<IAmsMgr>, return to AMS interface instance.
218      */
219     virtual sptr<IAmsMgr> GetAmsMgr() override;
220 
221     /**
222      * Notify that the ability stage has been updated
223      * @param recordId, the app record.
224      */
225     virtual void AddAbilityStageDone(const int32_t recordId) override;
226 
227     /**
228      * Start all resident process
229      */
230     virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
231 
232     /**
233      * Start user test process.
234      * @param want, want object.
235      * @param observer, test observer remote object.
236      * @param bundleInfo, bundle info.
237      * @param userId the user id.
238      * @return Returns ERR_OK on success, others on failure.
239      */
240     virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
241         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override;
242 
243     /**
244      * @brief Finish user test.
245      * @param msg user test message.
246      * @param resultCode user test result Code.
247      * @param bundleName user test bundleName.
248      *
249      * @return Returns ERR_OK on success, others on failure.
250      */
251     virtual int FinishUserTest(
252         const std::string &msg, const int64_t &resultCode, const std::string &bundleName) override;
253 
254     /**
255      * @brief Application hidumper.
256      * @param fd Indicates the fd.
257      * @param args Indicates the params.
258      * @return Returns the dump result.
259      */
260     int Dump(int fd, const std::vector<std::u16string>& args) override;
261 
262     virtual void ScheduleAcceptWantDone(
263         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
264 
265     virtual void ScheduleNewProcessRequestDone(
266         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
267 
268     virtual int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override;
269 
270     virtual int PreStartNWebSpawnProcess() override;
271 
272     virtual int StartRenderProcess(const std::string &renderParam,
273                                    int32_t ipcFd, int32_t sharedFd,
274                                    int32_t crashFd, pid_t &renderPid) override;
275 
276     virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override;
277 
278     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override;
279 
280     virtual int32_t GetConfiguration(Configuration& config) override;
281 
282     virtual int32_t UpdateConfiguration(const Configuration &config) override;
283 
284     virtual int32_t RegisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
285 
286     virtual int32_t UnregisterConfigurationObserver(const sptr<IConfigurationObserver> &observer) override;
287 
288     #ifdef ABILITY_COMMAND_FOR_TEST
289     /**
290      * Block app service.
291      *
292      * @return Returns ERR_OK on success, others on failure.
293      */
294     virtual int BlockAppService() override;
295     #endif
296 
297     bool GetAppRunningStateByBundleName(const std::string &bundleName) override;
298 
299     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
300 
301     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
302 
303     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback) override;
304 
305 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
306     int32_t SetContinuousTaskProcess(int32_t pid, bool isContinuousTask) override;
307 #endif
308 
309     /**
310      * @brief Check whether the shared bundle is running.
311      *
312      * @param bundleName Shared bundle name.
313      * @param versionCode Shared bundle version code.
314      * @return Returns the shared bundle running result. The result is true if running, false otherwise.
315      */
316     virtual bool IsSharedBundleRunning(const std::string &bundleName, uint32_t versionCode) override;
317 
318     virtual int32_t StartNativeProcessForDebugger(const AAFwk::Want &want) override;
319 
320     /**
321      * Get bundleName by pid.
322      *
323      * @param pid process id.
324      * @param bundleName Output parameters, return bundleName.
325      * @param uid Output parameters, return userId.
326      * @return Returns ERR_OK on success, others on failure.
327      */
328     virtual int32_t GetBundleNameByPid(const int32_t pid, std::string &bundleName, int32_t &uid) override;
329 
330     /**
331      * Notify Fault Data
332      *
333      * @param faultData the fault data.
334      * @return Returns ERR_OK on success, others on failure.
335      */
336     int32_t NotifyAppFault(const FaultData &faultData) override;
337 
338     /**
339      * Notify Fault Data By SA
340      *
341      * @param faultData the fault data notified by SA.
342      * @return Returns ERR_OK on success, others on failure.
343      */
344     int32_t NotifyAppFaultBySA(const AppFaultDataBySA &faultData) override;
345 
346     /**
347      * get memorySize by pid.
348      *
349      * @param pid process id.
350      * @param memorySize Output parameters, return memorySize in KB.
351      * @return Returns ERR_OK on success, others on failure.
352      */
353     virtual int32_t GetProcessMemoryByPid(const int32_t pid, int32_t &memorySize) override;
354 
355     /**
356      * get application processes information list by bundleName.
357      *
358      * @param bundleName Bundle name.
359      * @param userId user Id in Application record.
360      * @param info Output parameters, return running process info list.
361      * @return Returns ERR_OK on success, others on failure.
362      */
363     virtual int32_t GetRunningProcessInformation(
364         const std::string &bundleName, int32_t userId, std::vector<RunningProcessInfo> &info) override;
365 
366     /**
367      * on add systemAbility.
368      *
369      * @return
370      */
371     virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
372 
373     /**
374      * on remove systemAbility.
375      *
376      * @return
377      */
378     virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
379 
380     /**
381      * @brief Notify NativeEngine GC of status change.
382      *
383      * @param state GC state
384      * @param pid pid
385      *
386      * @return Is the status change completed.
387      */
388     virtual int32_t ChangeAppGcState(pid_t pid, int32_t state) override;
389 
390     /**
391      * Register appRunning status listener.
392      *
393      * @param listener Running status listener.
394      * @return Returns ERR_OK on success, others on failure.
395      */
396     int32_t RegisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
397 
398     /**
399      * Unregister appRunning status listener.
400      *
401      * @param listener Running status listener.
402      * @return Returns ERR_OK on success, others on failure.
403      */
404     int32_t UnregisterAppRunningStatusListener(const sptr<IRemoteObject> &listener) override;
405 
406     /**
407      * Register application foreground state observer.
408      * @param observer Is App Foreground Statue Observer
409      * @return Returns ERR_OK on success, others on failure.
410      */
411     int32_t RegisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
412 
413     /**
414      * Unregister application foreground state observer.
415      * @param observer Is App Foreground Statue Observer
416      * @return Returns ERR_OK on success, others on failure.
417      */
418     int32_t UnregisterAppForegroundStateObserver(const sptr<IAppForegroundStateObserver> &observer) override;
419 
420     /**
421      * Start child process, called by ChildProcessManager.
422      *
423      * @param srcEntry Child process source file entrance path to be started.
424      * @param childPid Created child process pid.
425      * @return Returns ERR_OK on success, others on failure.
426      */
427     int32_t StartChildProcess(const std::string &srcEntry, pid_t &childPid) override;
428 
429     /**
430      * Get child process record for self.
431      *
432      * @return child process info.
433      */
434     int32_t GetChildProcessInfoForSelf(ChildProcessInfo &info) override;
435 
436     /**
437      * Attach child process scheduler to app manager service.
438      *
439      * @param childScheduler scheduler of child process.
440      */
441     void AttachChildProcess(const sptr<IRemoteObject> &childScheduler) override;
442 
443     /**
444      * Exit child process, called by itself.
445      */
446     void ExitChildProcessSafely() override;
447 
448 private:
449     /**
450      * Init, Initialize application services.
451      *
452      * @return ERR_OK, return back success, others fail.
453      */
454     ErrCode Init();
455 
456     // the function that overrode from SystemAbility
457     /**
458      * OnStart, Start application service.
459      *
460      * @return
461      */
462     virtual void OnStart() override;
463 
464     /**
465      * OnStop, Stop application service.
466      *
467      * @return
468      */
469     virtual void OnStop() override;
470 
471     /**
472      * @brief Judge whether the application service is ready.
473      *
474      * @return Returns true means service is ready, otherwise service is not ready.
475      */
476     bool IsReady() const;
477 
478     /**
479      * SetInnerService, Setting application service Inner instance.
480      *
481      * @return
482      */
483     void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService);
484 
485     /**
486      * Register application or process state observer.
487      * @param observer, ability token.
488      * @return Returns ERR_OK on success, others on failure.
489      */
490     virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer,
491         const std::vector<std::string> &bundleNameList = {}) override;
492 
493     /**
494      * Unregister application or process state observer.
495      * @param observer, ability token.
496      * @return Returns ERR_OK on success, others on failure.
497      */
498     virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
499 
500     /**
501      * Register application or process state observer.
502      * @param observer, Is ability foreground state observer
503      * @return Returns ERR_OK on success, others on failure.
504      */
505     int32_t RegisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
506 
507     /**
508      * Unregister application or process state observer.
509      * @param observer, Is ability foreground state observer
510      * @return Returns ERR_OK on success, others on failure.
511      */
512     int32_t UnregisterAbilityForegroundStateObserver(const sptr<IAbilityForegroundStateObserver> &observer) override;
513 
514     /**
515      * Get foreground applications.
516      * @param list, foreground apps.
517      * @return Returns ERR_OK on success, others on failure.
518      */
519     virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override;
520 
521     void Dump(const std::vector<std::u16string>& args, std::string& result) const;
522     void ShowHelp(std::string& result) const;
523 
524     bool JudgeAppSelfCalled(int32_t recordId);
525 
526     /**
527      * @brief Notify AbilityManagerService the page show.
528      * @param token Ability identify.
529      * @param pageStateData The data of ability's page state.
530      * @return Returns ERR_OK on success, others on failure.
531      */
532     virtual int32_t NotifyPageShow(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
533 
534     /**
535      * @brief Notify AbilityManagerService the page hide.
536      * @param token Ability identify.
537      * @param pageStateData The data of ability's page state.
538      * @return Returns ERR_OK on success, others on failure.
539      */
540     virtual int32_t NotifyPageHide(const sptr<IRemoteObject> &token, const PageStateData &pageStateData) override;
541 
542     /**
543      * Check whether the bundle is running.
544      *
545      * @param bundleName Indicates the bundle name of the bundle.
546      * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise.
547      * @return Return ERR_OK if success, others fail.
548      */
549     int32_t IsApplicationRunning(const std::string &bundleName, bool &isRunning) override;
550 
551     /**
552      * Whether the current application process is the last surviving process.
553      *
554      * @return Returns true is final application process, others return false.
555      */
556     bool IsFinalAppProcess() override;
557 
558 private:
559     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
560     AppMgrServiceState appMgrServiceState_;
561     std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler_;
562     std::shared_ptr<AMSEventHandler> eventHandler_;
563     sptr<ISystemAbilityManager> systemAbilityMgr_;
564     sptr<IAmsMgr> amsMgrScheduler_;
565 
566     DISALLOW_COPY_AND_MOVE(AppMgrService);
567 };
568 }  // namespace AppExecFwk
569 }  // namespace OHOS
570 #endif  // OHOS_ABILITY_RUNTIME_APP_MGR_SERVICE_H
571