• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_H
18 
19 #include <list>
20 #include <string>
21 #include <vector>
22 
23 #include "if_system_ability_manager.h"
24 #include "nocopyable.h"
25 #include "system_ability.h"
26 
27 #include "ability_info.h"
28 #include "ability_running_record.h"
29 #include "appexecfwk_errors.h"
30 #include "application_info.h"
31 #include "app_mgr_constants.h"
32 #include "app_mgr_stub.h"
33 #include "app_mgr_service_event_handler.h"
34 #include "app_mgr_service_inner.h"
35 #include "app_record_id.h"
36 #include "app_running_record.h"
37 #include "app_scheduler_proxy.h"
38 #include "ams_mgr_scheduler.h"
39 #include "ams_mgr_scheduler.h"
40 
41 namespace OHOS {
42 namespace AppExecFwk {
43 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
44 
45 struct AppMgrServiceState {
46     ServiceRunningState serviceRunningState = ServiceRunningState::STATE_NOT_START;
47     SpawnConnectionState connectionState = SpawnConnectionState::STATE_NOT_CONNECT;
48 };
49 
50 class AMSEventHandler;
51 
52 class AppMgrService : public SystemAbility, public AppMgrStub {
53 public:
54     DECLEAR_SYSTEM_ABILITY(AppMgrService);
55 
56     AppMgrService();
57     AppMgrService(const int32_t serviceId, bool runOnCreate = false);
58     virtual ~AppMgrService() override;
59 
60     // the function about application
61     // attach the application to ams, then ams can control it.
62     /**
63      * AttachApplication, call AttachApplication() through proxy object,
64      * get all the information needed to start the Application (data related to the Application ).
65      *
66      * @param app, information needed to start the Application.
67      * @return
68      */
69     virtual void AttachApplication(const sptr<IRemoteObject> &app) override;
70 
71     // notify the ams update the state of an app, when it entered foreground.
72 
73     /**
74      * ApplicationForegrounded, call ApplicationForegrounded() through proxy object,
75      * set the application to Foreground State.
76      *
77      * @param recordId, a unique record that identifies this Application from others.
78      * @return
79      */
80     virtual void ApplicationForegrounded(const int32_t recordId) override;
81 
82     /**
83      * ApplicationBackgrounded, call ApplicationBackgrounded() through proxy object,
84      * set the application to Backgrounded State.
85      *
86      * @param recordId, a unique record that identifies this Application from others.
87      * @return
88      */
89     virtual void ApplicationBackgrounded(const int32_t recordId) override;
90 
91     /**
92      * ApplicationTerminated, call ApplicationTerminated() through proxy object,
93      * terminate the application.
94      *
95      * @param recordId, a unique record that identifies this Application from others.
96      * @return
97      */
98     virtual void ApplicationTerminated(const int32_t recordId) override;
99 
100     /**
101      * AbilityCleaned,call through AbilityCleaned() proxy project, clean Ability record.
102      *
103      * @param token, a unique record that identifies AbilityCleaned from others.
104      * @return
105      */
106     virtual void AbilityCleaned(const sptr<IRemoteObject> &token) override;
107 
108     /**
109      * ClearUpApplicationData, call ClearUpApplicationData() through proxy project,
110      * clear the application data.
111      *
112      * @param bundleName, bundle name in Application record.
113      * @return ERR_OK, return back success, others fail.
114      */
115     virtual int32_t ClearUpApplicationData(const std::string &bundleName) override;
116 
117     /**
118      * GetAllRunningProcesses, call GetAllRunningProcesses() through proxy project.
119      * Obtains information about application processes that are running on the device.
120      *
121      * @param info, app name in Application record.
122      * @return ERR_OK ,return back success,others fail.
123      */
124     virtual int32_t GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override;
125 
126     /**
127      * GetProcessRunningInfosByUserId, call GetProcessRunningInfosByUserId() through proxy project.
128      * Obtains information about application processes that are running on the device.
129      *
130      * @param info, app name in Application record.
131      * @param userId, userId.
132      *
133      * @return ERR_OK ,return back success,others fail.
134      */
135     virtual int32_t GetProcessRunningInfosByUserId(std::vector<RunningProcessInfo> &info, int32_t userId) override;
136 
137     // the function about system
138     /**
139      * CheckPermission, call CheckPermission() through proxy object, check the permission.
140      *
141      * @param recordId, a unique record that identifies this Application from others.
142      * @param permission, check the permissions.
143      * @return ERR_OK, return back success, others fail.
144      */
145     virtual int32_t CheckPermission(const int32_t recordId, const std::string &permission) override;
146 
147     // the function about service running info
148     /**
149      * QueryServiceState, Query application service status.
150      *
151      * @return the application service status.
152      */
153     AppMgrServiceState QueryServiceState();
154 
155     /**
156      * GetAmsMgr, call GetAmsMgr() through proxy object, get AMS interface instance.
157      *
158      * @return sptr<IAmsMgr>, return to AMS interface instance.
159      */
160     virtual sptr<IAmsMgr> GetAmsMgr() override;
161 
162     /**
163      * Get system memory information.
164      * @param SystemMemoryAttr, memory information.
165      */
166     virtual void GetSystemMemoryAttr(SystemMemoryAttr &memoryInfo, std::string &strConfig) override;
167 
168     /**
169      * Notify that the ability stage has been updated
170      * @param recordId, the app record.
171      */
172     virtual void AddAbilityStageDone(const int32_t recordId) override;
173 
174     /**
175      * Start all resident process
176      */
177     virtual void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
178 
179     /**
180      * Start user test process.
181      * @param want, want object.
182      * @param observer, test observer remote object.
183      * @param bundleInfo, bundle info.
184      * @param userId the user id.
185      * @return Returns ERR_OK on success, others on failure.
186      */
187     virtual int StartUserTestProcess(const AAFwk::Want &want, const sptr<IRemoteObject> &observer,
188         const AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override;
189 
190     /**
191      * @brief Finish user test.
192      * @param msg user test message.
193      * @param resultCode user test result Code.
194      * @param bundleName user test bundleName.
195      *
196      * @return Returns ERR_OK on success, others on failure.
197      */
198     virtual int FinishUserTest(const std::string &msg, const int &resultCode, const std::string &bundleName) override;
199 
200     virtual void ScheduleAcceptWantDone(
201         const int32_t recordId, const AAFwk::Want &want, const std::string &flag) override;
202 
203     /**
204      *  Get the token of ability records by process ID.
205      *
206      * @param pid The process id.
207      * @param tokens The token of ability records.
208      * @return Returns true on success, others on failure.
209      */
210     int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens) override;
211 
212     virtual int StartRenderProcess(const std::string &renderParam, int32_t ipcFd,
213         int32_t sharedFd, pid_t &renderPid) override;
214 
215     virtual void AttachRenderProcess(const sptr<IRemoteObject> &shceduler) override;
216 
217     virtual int GetRenderProcessTerminationStatus(pid_t renderPid, int &status) override;
218 
219     /**
220      * Post a task to the not response process.
221      *
222      * @param pid, the not response process id.
223      */
224     virtual void PostANRTaskByProcessID(const pid_t pid) override;
225 
226 private:
227     /**
228      * Init, Initialize application services.
229      *
230      * @return ERR_OK, return back success, others fail.
231      */
232     ErrCode Init();
233 
234     // the function that overrode from SystemAbility
235     /**
236      * OnStart, Start application service.
237      *
238      * @return
239      */
240     virtual void OnStart() override;
241 
242     /**
243      * OnStop, Stop application service.
244      *
245      * @return
246      */
247     virtual void OnStop() override;
248 
249     /**
250      * @brief Judge whether the application service is ready.
251      *
252      * @return Returns true means service is ready, otherwise service is not ready.
253      */
254     bool IsReady() const;
255 
256     /**
257      * AddAppDeathRecipient, Add monitoring death application record.
258      *
259      * @param pid, the application pid.
260      * @param appDeathRecipient, Application death recipient list.
261      *
262      * @return
263      */
264     void AddAppDeathRecipient(const pid_t pid) const;
265 
266     /**
267      * SetInnerService, Setting application service Inner instance.
268      *
269      * @return
270      */
271     void SetInnerService(const std::shared_ptr<AppMgrServiceInner> &innerService);
272 
273     /**
274      * Register application or process state observer.
275      * @param observer, ability token.
276      * @return Returns ERR_OK on success, others on failure.
277      */
278     virtual int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
279 
280     /**
281      * Unregister application or process state observer.
282      * @param observer, ability token.
283      * @return Returns ERR_OK on success, others on failure.
284      */
285     virtual int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer) override;
286 
287     /**
288      * Get foreground applications.
289      * @param list, foreground apps.
290      * @return Returns ERR_OK on success, others on failure.
291      */
292     virtual int32_t GetForegroundApplications(std::vector<AppStateData> &list) override;
293 
294     bool JudgeSelfCalledByRecordId(int32_t recordId);
295 
296 private:
297     std::shared_ptr<AppMgrServiceInner> appMgrServiceInner_;
298     AppMgrServiceState appMgrServiceState_;
299     std::shared_ptr<EventRunner> runner_;
300     std::shared_ptr<AMSEventHandler> handler_;
301     sptr<ISystemAbilityManager> systemAbilityMgr_;
302     sptr<IAmsMgr> amsMgrScheduler_;
303 
304     DISALLOW_COPY_AND_MOVE(AppMgrService);
305 };
306 }  // namespace AppExecFwk
307 }  // namespace OHOS
308 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_SERVICE_H
309