• 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_SCHEDULER_H
17 #define OHOS_ABILITY_RUNTIME_APP_SCHEDULER_H
18 
19 #include <memory>
20 #include <unordered_set>
21 
22 #include "ability_info.h"
23 #include "appmgr/app_mgr_client.h"
24 #include "appmgr/app_state_callback_host.h"
25 #include "appmgr/start_specified_ability_response_stub.h"
26 #include "application_info.h"
27 #include "bundle_info.h"
28 #include "fault_data.h"
29 #include "iremote_object.h"
30 #include "refbase.h"
31 #include "singleton.h"
32 #include "system_memory_attr.h"
33 #include "running_process_info.h"
34 #include "want.h"
35 
36 namespace OHOS {
37 namespace AppExecFwk {
38 class Configuration;
39 }
40 namespace AAFwk {
41 /**
42  * @enum AppAbilityState
43  * AppAbilityState defines the life cycle state of app ability.
44  */
45 enum class AppAbilityState {
46     ABILITY_STATE_UNDEFINED = 0,
47     ABILITY_STATE_FOREGROUND,
48     ABILITY_STATE_BACKGROUND,
49     ABILITY_STATE_END,
50 };
51 
52 enum class AppState {
53     BEGIN = 0,
54     READY,
55     FOREGROUND,
56     FOCUS,
57     BACKGROUND,
58     TERMINATED,
59     END,
60     SUSPENDED,
61 };
62 
63 struct AppData {
64     std::string appName;
65     int32_t uid;
66 };
67 
68 struct AppInfo {
69     std::vector<AppData> appData;
70     std::string processName;
71     AppState state;
72 };
73 /**
74  * @class AppStateCallback
75  * AppStateCallback.
76  */
77 class AppStateCallback {
78 public:
AppStateCallback()79     AppStateCallback()
80     {}
~AppStateCallback()81     virtual ~AppStateCallback()
82     {}
83 
84     virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) = 0;
85 
86     virtual void OnAppStateChanged(const AppInfo &info) = 0;
87 };
88 
89 class StartSpecifiedAbilityResponse : public AppExecFwk::StartSpecifiedAbilityResponseStub {
90 public:
91     StartSpecifiedAbilityResponse() = default;
92     virtual ~StartSpecifiedAbilityResponse() = default;
93 
94     virtual void OnAcceptWantResponse(const AAFwk::Want &want, const std::string &flag) override;
95     virtual void OnTimeoutResponse(const AAFwk::Want &want) override;
96 };
97 
98 /**
99  * @class AppScheduler
100  * AppScheduler , access app manager service.
101  */
102 class AppScheduler : virtual RefBase, public AppExecFwk::AppStateCallbackHost {
103     DECLARE_DELAYED_SINGLETON(AppScheduler)
104 public:
105     /**
106      * init app scheduler.
107      * @param callback, app state call back.
108      * @return true on success ,false on failure.
109      */
110     bool Init(const std::weak_ptr<AppStateCallback> &callback);
111 
112     /**
113      * load ability with token, ability info and application info.
114      *
115      * @param token, the token of ability.
116      * @param preToken, the token of ability's caller.
117      * @param abilityInfo, ability info.
118      * @param applicationInfo, application info.
119      * @param want ability want
120      * @return true on success ,false on failure.
121      */
122     int LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
123         const AppExecFwk::AbilityInfo &abilityInfo, const AppExecFwk::ApplicationInfo &applicationInfo,
124         const Want &want);
125 
126     /**
127      * terminate ability with token.
128      *
129      * @param token, the token of ability.
130      * @param clearMissionFlag, indicates whether terminate the ability when clearMission.
131      * @return true on success ,false on failure.
132      */
133     int TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag);
134 
135     /**
136      * move ability to foreground.
137      *
138      * @param token, the token of ability.
139      */
140     void MoveToForeground(const sptr<IRemoteObject> &token);
141 
142     /**
143      * move ability to background.
144      *
145      * @param token, the token of ability.
146      */
147     void MoveToBackground(const sptr<IRemoteObject> &token);
148 
149     /**
150      * Update ability state.
151      *
152      * @param token, the token of ability.
153      * @param state, ability state.
154      */
155     void UpdateAbilityState(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state);
156 
157     /**
158      * UpdateExtensionState, call UpdateExtensionState() through the proxy object, update the extension status.
159      *
160      * @param token, the unique identification to update the extension.
161      * @param state, extension status that needs to be updated.
162      * @return
163      */
164     void UpdateExtensionState(const sptr<IRemoteObject> &token, const AppExecFwk::ExtensionState state);
165 
166     /**
167      * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization.
168      *
169      * @param token, the unique identification to start the ability.
170      * @param preToken, the unique identification to call the ability.
171      * @param visibility, the visibility information about windows info.
172      * @param perceptibility, the Perceptibility information about windows info.
173      * @param connectionState, the service ability connection state.
174      * @return Returns RESULT_OK on success, others on failure.
175      */
176     void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
177         const int32_t visibility, const int32_t perceptibility, const int32_t connectionState);
178 
179     /**
180      * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object,
181      * kill the process by ability token.
182      *
183      * @param token, the unique identification to the ability.
184      */
185     void KillProcessByAbilityToken(const sptr<IRemoteObject> &token);
186 
187     /**
188      * KillProcessesByUserId, call KillProcessesByUserId() through proxy object,
189      * kill the process by user id.
190      *
191      * @param userId, the user id.
192      */
193     void KillProcessesByUserId(int32_t userId);
194 
195     /**
196      * convert ability state to app ability state.
197      *
198      * @param state, the state of ability.
199      */
200     AppAbilityState ConvertToAppAbilityState(const int32_t state);
201 
202     /**
203      * get ability state.
204      *
205      * @return state, the state of app ability.
206      */
207     AppAbilityState GetAbilityState() const;
208 
209     /**
210      * kill the application
211      *
212      * @param bundleName.
213      */
214     int KillApplication(const std::string &bundleName);
215 
216     /**
217      * kill the application by uid
218      *
219      * @param bundleName name of bundle.
220      * @param uid uid of bundle.
221      * @return 0 if success.
222      */
223     int KillApplicationByUid(const std::string &bundleName, int32_t uid);
224 
225      /**
226      * update the application info after new module installed.
227      *
228      * @param bundleName, bundle name in Application record.
229      * @param  uid, uid.
230      * @return 0 if success.
231      */
232     int UpdateApplicationInfoInstalled(const std::string &bundleName, const int32_t uid);
233 
234     /**
235      * clear the application data
236      *
237      * @param bundleName.
238      */
239     int ClearUpApplicationData(const std::string &bundleName);
240 
241     void AttachTimeOut(const sptr<IRemoteObject> &token);
242 
243     void PrepareTerminate(const sptr<IRemoteObject> &token);
244 
245     void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info);
246 
247     void GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info) const;
248 
249     /**
250      * Set AbilityForegroundingFlag of an app-record to true.
251      *
252      * @param pid, pid.
253      *
254      */
255     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid) const;
256 
257     /**
258      * Start a resident process
259      */
260     void StartupResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos);
261 
262     void StartSpecifiedAbility(const AAFwk::Want &want, const AppExecFwk::AbilityInfo &abilityInfo);
263     int GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info);
264 
265     /**
266      * Start a user test
267      */
268     int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer, const AppExecFwk::BundleInfo &bundleInfo,
269         int32_t userId);
270 
271     /**
272      * @brief Finish user test.
273      * @param msg user test message.
274      * @param resultCode user test result Code.
275      * @param bundleName user test bundleName.
276      *
277      * @return Returns ERR_OK on success, others on failure.
278      */
279     int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName);
280 
281     int GetProcessRunningInfosByUserId(std::vector<AppExecFwk::RunningProcessInfo> &info, int32_t userId);
282     std::string ConvertAppState(const AppState &state);
283 
284     /**
285      *  ANotify application update system environment changes.
286      *
287      * @param config System environment change parameters.
288      * @return Returns ERR_OK on success, others on failure.
289      */
290     int UpdateConfiguration(const AppExecFwk::Configuration &config);
291 
292     int GetConfiguration(AppExecFwk::Configuration &config);
293 
294     /**
295      *  Get the token of ability records by process ID.
296      *
297      * @param pid The process id.
298      * @param tokens The token of ability records.
299      * @return Returns ERR_OK on success, others on failure.
300      */
301     int GetAbilityRecordsByProcessID(const int pid, std::vector<sptr<IRemoteObject>> &tokens);
302 
303     /**
304      *  Get the application info by process ID.
305      *
306      * @param pid The process id.
307      * @param application The application info.
308      * @param debug The app is or not debug.
309      * @return Returns ERR_OK on success, others on failure.
310      */
311     int GetApplicationInfoByProcessID(const int pid, AppExecFwk::ApplicationInfo &application, bool &debug);
312 
313     /**
314      * Set the current userId of appMgr, only used by abilityMgr.
315      *
316      * @param userId the user id.
317      *
318      * @return
319      */
320     void SetCurrentUserId(int32_t userId);
321 
322     #ifdef ABILITY_COMMAND_FOR_TEST
323     /**
324      * Block app service.
325      *
326      * @return Returns ERR_OK on success, others on failure.
327      */
328     int BlockAppService();
329     #endif
330 
331     /**
332      * Get bundleName by pid.
333      *
334      * @param pid process id.
335      * @param bundleName Output parameters, return bundleName.
336      * @param uid Output parameters, return userId.
337      * @return Returns ERR_OK on success, others on failure.
338      */
339     int32_t GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid);
340 
341     /**
342      * Notify Fault Data
343      *
344      * @param faultData the fault data.
345      * @return Returns ERR_OK on success, others on failure.
346      */
347     int32_t NotifyFault(const AppExecFwk::FaultData &faultData);
348 
349 protected:
350     /**
351      * OnAbilityRequestDone, app manager service call this interface after ability request done.
352      *
353      * @param token,ability's token.
354      * @param state,the state of ability lift cycle.
355      */
356     virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state) override;
357 
358     /**
359      * Application state changed callback.
360      *
361      * @param appProcessData Process data
362      */
363     virtual void OnAppStateChanged(const AppExecFwk::AppProcessData &appData) override;
364 
365 private:
366     std::mutex lock_;
367     bool isInit_  {false};
368     std::weak_ptr<AppStateCallback> callback_;
369     std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_;
370     AppAbilityState appAbilityState_ = AppAbilityState::ABILITY_STATE_UNDEFINED;
371     sptr<StartSpecifiedAbilityResponse> startSpecifiedAbilityResponse_;
372 };
373 }  // namespace AAFwk
374 }  // namespace OHOS
375 #endif  // OHOS_ABILITY_RUNTIME_APP_SCHEDULER_H
376