• 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_RUNNING_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <regex>
22 
23 #include "ability_info.h"
24 #include "app_debug_listener_interface.h"
25 #include "app_malloc_info.h"
26 #include "app_running_record.h"
27 #include "app_state_data.h"
28 #include "application_info.h"
29 #include "bundle_info.h"
30 #include "cpp/mutex.h"
31 #include "iremote_object.h"
32 #include "record_query_result.h"
33 #include "refbase.h"
34 #include "running_process_info.h"
35 
36 namespace OHOS {
37 namespace AppExecFwk {
38 class AppRunningManager {
39 public:
40     AppRunningManager();
41     virtual ~AppRunningManager();
42     /**
43      * CreateAppRunningRecord, Get or create application record information.
44      *
45      * @param token, the unique identification to start the ability.
46      * @param abilityInfo, ability information.
47      * @param appInfo, app information.
48      * @param processName, app process name.
49      * @param uid, app uid in Application record.
50      * @param result, If error occurs, error code is in |result|.
51      *
52      * @return AppRunningRecord pointer if success get or create.
53      */
54     std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(
55         const std::shared_ptr<ApplicationInfo> &appInfo, const std::string &processName, const BundleInfo &bundleInfo);
56 
57     /**
58      * CheckAppRunningRecordIsExist, Get process record by application name and process Name.
59      *
60      * @param appName, the application name.
61      * @param processName, the process name.
62      * @param uid, the process uid.
63      *
64      * @return process record.
65      */
66     std::shared_ptr<AppRunningRecord> CheckAppRunningRecordIsExist(const std::string &appName,
67         const std::string &processName, const int uid, const BundleInfo &bundleInfo,
68         const std::string &specifiedProcessFlag = "");
69 
70     /**
71      * CheckAppRunningRecordIsExistByBundleName, Check whether the process of the application exists.
72      *
73      * @param bundleName, the bundle name.
74      *
75      * @return, Return true if exist.
76      */
77     bool CheckAppRunningRecordIsExistByBundleName(const std::string &bundleName);
78 
79     /**
80      * GetAppRunningRecordByPid, Get process record by application pid.
81      *
82      * @param pid, the application pid.
83      *
84      * @return process record.
85      */
86     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid);
87 
88     /**
89      * GetAppRunningRecordByAbilityToken, Get process record by ability token.
90      *
91      * @param abilityToken, the ability token.
92      *
93      * @return process record.
94      */
95     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken);
96 
97     /**
98      * OnRemoteDied, Equipment death notification.
99      *
100      * @param remote, Death client.
101      * @return
102      */
103     std::shared_ptr<AppRunningRecord> OnRemoteDied(const wptr<IRemoteObject> &remote);
104 
105     /**
106      * GetAppRunningRecordMap, Get application record list.
107      *
108      * @return the application record list.
109      */
110     std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> GetAppRunningRecordMap();
111 
112     /**
113      * RemoveAppRunningRecordById, Remove application information through application id.
114      *
115      * @param recordId, the application id.
116      * @return
117      */
118     void RemoveAppRunningRecordById(const int32_t recordId);
119 
120     /**
121      * ClearAppRunningRecordMap, Clear application record list.
122      *
123      * @return
124      */
125     void ClearAppRunningRecordMap();
126 
127     /**
128      * Get the pid of a non-resident process.
129      *
130      * @return Return true if found, otherwise return false.
131      */
132     bool ProcessExitByBundleName(const std::string &bundleName, std::list<pid_t> &pids);
133     /**
134      * Get Foreground Applications.
135      *
136      * @return Foreground Applications.
137      */
138     void GetForegroundApplications(std::vector<AppStateData> &list);
139 
140     /*
141     *  ANotify application update system environment changes.
142     *
143     * @param config System environment change parameters.
144     * @return Returns ERR_OK on success, others on failure.
145     */
146     int32_t UpdateConfiguration(const Configuration &config);
147 
148     /*
149     *  Notify application background of current memory level.
150     *
151     * @param level current memory level.
152     * @return Returns ERR_OK on success, others on failure.
153     */
154     int32_t NotifyMemoryLevel(int32_t level);
155 
156     /*
157     * Get the application's memory allocation info.
158     *
159     * @param pid, pid input.
160     * @param mallocInfo, dynamic storage information output.
161     *
162     * @return Returns ERR_OK on success, others on failure.
163     */
164     int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo);
165 
166     /**
167      * Set AbilityForegroundingFlag of an app-record to true.
168      *
169      * @param pid, pid.
170      *
171      */
172     void SetAbilityForegroundingFlagToAppRecord(const pid_t pid);
173 
174     void HandleTerminateTimeOut(int64_t eventId);
175     void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token);
176     std::shared_ptr<AppRunningRecord> GetAppRunningRecord(const int64_t eventId);
177     void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag,
178         std::shared_ptr<AppMgrServiceInner> appMgrServiceInner);
179 
180     /**
181      *
182      * @brief update the application info after new module installed.
183      *
184      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
185      *
186      */
187     int32_t ProcessUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo);
188 
189     bool ProcessExitByBundleNameAndUid(const std::string &bundleName, const int uid, std::list<pid_t> &pids);
190     bool GetPidsByUserId(int32_t userId, std::list<pid_t> &pids);
191 
192     void PrepareTerminate(const sptr<IRemoteObject> &token);
193 
194     std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &abilityToken);
195 
196     void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info);
197     void GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info);
198 
199     void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr);
200     void HandleAddAbilityStageTimeOut(const int64_t eventId);
201     void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId);
202     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByRenderPid(const pid_t pid);
203     std::shared_ptr<RenderRecord> OnRemoteRenderDied(const wptr<IRemoteObject> &remote);
204     bool ProcessExitByPid(pid_t pid);
205     bool GetAppRunningStateByBundleName(const std::string &bundleName);
206     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
207     int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
208     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback);
209     bool IsApplicationFirstForeground(const AppRunningRecord &foregroundingRecord);
210     bool IsApplicationBackground(const std::string &bundleName);
211     bool IsApplicationFirstFocused(const AppRunningRecord &foregroundingRecord);
212     bool IsApplicationUnfocused(const std::string &bundleName);
213     void OnWindowVisibilityChanged(const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos);
214 
215     /**
216      * @brief Set attach app debug mode.
217      * @param bundleName The application bundle name.
218      * @param isAttachDebug Determine if it is in attach debug mode.
219      */
220     void SetAttachAppDebug(const std::string &bundleName, const bool &isAttachDebug);
221 
222     /**
223      * @brief Obtain app debug infos through bundleName.
224      * @param bundleName The application bundle name.
225      * @param isDetachDebug Determine if it is a Detach.
226      * @return Specify the stored app informations based on bundle name output.
227      */
228     std::vector<AppDebugInfo> GetAppDebugInfosByBundleName(const std::string &bundleName, const bool &isDetachDebug);
229 
230     /**
231      * @brief Obtain ability tokens through bundleName.
232      * @param bundleName The application bundle name.
233      * @param abilityTokens Specify the stored ability token based on bundle name output.
234      */
235     void GetAbilityTokensByBundleName(const std::string &bundleName, std::vector<sptr<IRemoteObject>> &abilityTokens);
236 
237     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByChildProcessPid(const pid_t pid);
238     std::shared_ptr<ChildProcessRecord> OnChildProcessRemoteDied(const wptr<IRemoteObject> &remote);
239 
240     /**
241      * @brief Obtain number of app through bundlename.
242      * @param bundleName The application bundle name.
243      * @return Returns the number of queries.
244      */
245     int32_t GetAllAppRunningRecordCountByBundleName(const std::string &bundleName);
246 
247 private:
248     std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId);
249     void AssignRunningProcessInfoByAppRecord(
250         std::shared_ptr<AppRunningRecord> appRecord, AppExecFwk::RunningProcessInfo &info) const;
251     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPidInner(const pid_t pid);
252     std::shared_ptr<AppRunningRecord> GetAppRunningRecordByTokenInner(const sptr<IRemoteObject> &abilityToken);
253     bool isCollaboratorReserveType(const std::shared_ptr<AppRunningRecord> &appRecord);
254 
255 private:
256     std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> appRunningRecordMap_;
257     std::map<const std::string, int> processRestartRecord_;
258     ffrt::mutex lock_;
259 };
260 }  // namespace AppExecFwk
261 }  // namespace OHOS
262 
263 #endif  // OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H
264