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