• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_RUNNING_MANAGER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_RUNNING_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <regex>
22 
23 #include "iremote_object.h"
24 #include "refbase.h"
25 
26 #include "app_running_record.h"
27 #include "ability_info.h"
28 #include "application_info.h"
29 #include "app_state_data.h"
30 #include "record_query_result.h"
31 #include "running_process_info.h"
32 #include "bundle_info.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 class AppRunningManager {
37 public:
38     AppRunningManager();
39     virtual ~AppRunningManager();
40     /**
41      * CreateAppRunningRecord, Get or create application record information.
42      *
43      * @param token, the unique identification to start the ability.
44      * @param abilityInfo, ability information.
45      * @param appInfo, app information.
46      * @param processName, app process name.
47      * @param uid, app uid in Application record.
48      * @param result, If error occurs, error code is in |result|.
49      *
50      * @return AppRunningRecord pointer if success get or create.
51      */
52     std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(
53         const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName, const BundleInfo &bundleInfo);
54 
55     /**
56      * CheckAppRunningRecordIsExist, Get process record by application name and process Name.
57      *
58      * @param appName, the application name.
59      * @param processName, the process name.
60      * @param uid, the process uid.
61      *
62      * @return process record.
63      */
64     std::shared_ptr<AppRunningRecord> CheckAppRunningRecordIsExist(const std::string &appName,
65         const std::string &processName, const int uid, const BundleInfo &bundleInfo);
66 
67     /**
68      * GetAppRunningRecordByPid, Get process record by application pid.
69      *
70      * @param pid, the application pid.
71      *
72      * @return process record.
73      */
74     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid);
75 
76     /**
77      * GetAppRunningRecordByAbilityToken, Get process record by ability token.
78      *
79      * @param abilityToken, the ability token.
80      *
81      * @return process record.
82      */
83     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken);
84 
85     /**
86      * OnRemoteDied, Equipment death notification.
87      *
88      * @param remote, Death client.
89      * @return
90      */
91     std::shared_ptr<AppRunningRecord> OnRemoteDied(const wptr<IRemoteObject> &remote);
92 
93     /**
94      * GetAppRunningRecordMap, Get application record list.
95      *
96      * @return the application record list.
97      */
98     const std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> &GetAppRunningRecordMap();
99 
100     /**
101      * RemoveAppRunningRecordById, Remove application information through application id.
102      *
103      * @param recordId, the application id.
104      * @return
105      */
106     void RemoveAppRunningRecordById(const int32_t recordId);
107 
108     /**
109      * ClearAppRunningRecordMap, Clear application record list.
110      *
111      * @return
112      */
113     void ClearAppRunningRecordMap();
114 
115     /**
116      * Get the pid of a non-resident process.
117      *
118      * @return Return true if found, otherwise return false.
119      */
120     bool ProcessExitByBundleName(const std::string &bundleName, std::list<pid_t> &pids);
121     /**
122      * Get Foreground Applications.
123      *
124      * @return Foreground Applications.
125      */
126     void GetForegroundApplications(std::vector<AppStateData> &list);
127 
128     /*
129     *  ANotify application update system environment changes.
130     *
131     * @param config System environment change parameters.
132     * @return
133     */
134     void UpdateConfiguration(const Configuration &config);
135     void HandleTerminateTimeOut(int64_t eventId);
136     void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token);
137     std::shared_ptr<AppRunningRecord> GetAppRunningRecord(const int64_t eventId);
138     void TerminateAbility(const sptr<IRemoteObject> &token);
139     bool ProcessExitByBundleNameAndUid(const std::string &bundleName, const int uid, std::list<pid_t> &pids);
140     bool GetPidsByUserId(int32_t userId, std::list<pid_t> &pids);
141 
142     void PrepareTerminate(const sptr<IRemoteObject> &token);
143 
144     std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &abilityToken);
145 
146     void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info);
147 
148     void ClipStringContent(const std::regex &re, const std::string &sorce, std::string &afferCutStr);
149     void HandleAddAbilityStageTimeOut(const int64_t eventId);
150     void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId);
151     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByRenderPid(const pid_t pid);
152     void OnRemoteRenderDied(const wptr<IRemoteObject> &remote);
153 private:
154     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId);
155 
156 private:
157     std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> appRunningRecordMap_;
158     std::map<const std::string, int> processRestartRecord_;
159     std::recursive_mutex lock_;
160 };
161 }  // namespace AppExecFwk
162 }  // namespace OHOS
163 
164 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_MGR_RUNNING_MANAGER_H