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 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 #include "cpp/mutex.h" 23 24 #include "iremote_object.h" 25 #include "refbase.h" 26 27 #include "app_running_record.h" 28 #include "ability_info.h" 29 #include "application_info.h" 30 #include "app_state_data.h" 31 #include "record_query_result.h" 32 #include "running_process_info.h" 33 #include "bundle_info.h" 34 #include "app_malloc_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 69 /** 70 * CheckAppRunningRecordIsExistByBundleName, Check whether the process of the application exists. 71 * 72 * @param bundleName, the bundle name. 73 * 74 * @return, Return true if exist. 75 */ 76 bool CheckAppRunningRecordIsExistByBundleName(const std::string &bundleName); 77 78 /** 79 * GetAppRunningRecordByPid, Get process record by application pid. 80 * 81 * @param pid, the application pid. 82 * 83 * @return process record. 84 */ 85 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid); 86 87 /** 88 * GetAppRunningRecordByAbilityToken, Get process record by ability token. 89 * 90 * @param abilityToken, the ability token. 91 * 92 * @return process record. 93 */ 94 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken); 95 96 /** 97 * OnRemoteDied, Equipment death notification. 98 * 99 * @param remote, Death client. 100 * @return 101 */ 102 std::shared_ptr<AppRunningRecord> OnRemoteDied(const wptr<IRemoteObject> &remote); 103 104 /** 105 * GetAppRunningRecordMap, Get application record list. 106 * 107 * @return the application record list. 108 */ 109 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> GetAppRunningRecordMap(); 110 111 /** 112 * RemoveAppRunningRecordById, Remove application information through application id. 113 * 114 * @param recordId, the application id. 115 * @return 116 */ 117 void RemoveAppRunningRecordById(const int32_t recordId); 118 119 /** 120 * ClearAppRunningRecordMap, Clear application record list. 121 * 122 * @return 123 */ 124 void ClearAppRunningRecordMap(); 125 126 /** 127 * Get the pid of a non-resident process. 128 * 129 * @return Return true if found, otherwise return false. 130 */ 131 bool ProcessExitByBundleName(const std::string &bundleName, std::list<pid_t> &pids); 132 /** 133 * Get Foreground Applications. 134 * 135 * @return Foreground Applications. 136 */ 137 void GetForegroundApplications(std::vector<AppStateData> &list); 138 139 /* 140 * ANotify application update system environment changes. 141 * 142 * @param config System environment change parameters. 143 * @return Returns ERR_OK on success, others on failure. 144 */ 145 int32_t UpdateConfiguration(const Configuration &config); 146 147 /* 148 * Notify application background of current memory level. 149 * 150 * @param level current memory level. 151 * @return Returns ERR_OK on success, others on failure. 152 */ 153 int32_t NotifyMemoryLevel(int32_t level); 154 155 /* 156 * Get the application's memory allocation info. 157 * 158 * @param pid, pid input. 159 * @param mallocInfo, dynamic storage information output. 160 * 161 * @return Returns ERR_OK on success, others on failure. 162 */ 163 int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo); 164 165 /** 166 * Set AbilityForegroundingFlag of an app-record to true. 167 * 168 * @param pid, pid. 169 * 170 */ 171 void SetAbilityForegroundingFlagToAppRecord(const pid_t pid); 172 173 void HandleTerminateTimeOut(int64_t eventId); 174 void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token); 175 std::shared_ptr<AppRunningRecord> GetAppRunningRecord(const int64_t eventId); 176 void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag, 177 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner); 178 179 /** 180 * 181 * @brief update the application info after new module installed. 182 * 183 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 184 * 185 */ 186 int32_t ProcessUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo); 187 188 bool ProcessExitByBundleNameAndUid(const std::string &bundleName, const int uid, std::list<pid_t> &pids); 189 bool GetPidsByUserId(int32_t userId, std::list<pid_t> &pids); 190 191 void PrepareTerminate(const sptr<IRemoteObject> &token); 192 193 std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &abilityToken); 194 195 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 196 void GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info); 197 198 void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr); 199 void HandleAddAbilityStageTimeOut(const int64_t eventId); 200 void HandleStartSpecifiedAbilityTimeOut(const int64_t eventId); 201 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByRenderPid(const pid_t pid); 202 std::shared_ptr<RenderRecord> OnRemoteRenderDied(const wptr<IRemoteObject> &remote); 203 bool ProcessExitByPid(pid_t pid); 204 bool GetAppRunningStateByBundleName(const std::string &bundleName); 205 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 206 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 207 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 208 bool IsApplicationFirstForeground(const AppRunningRecord &foregroundingRecord); 209 bool IsApplicationBackground(const std::string &bundleName); 210 bool IsApplicationFirstFocused(const AppRunningRecord &foregroundingRecord); 211 bool IsApplicationUnfocused(const std::string &bundleName); 212 private: 213 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId); 214 void AssignRunningProcessInfoByAppRecord( 215 std::shared_ptr<AppRunningRecord> appRecord, AppExecFwk::RunningProcessInfo &info) const; 216 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPidInner(const pid_t pid); 217 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByTokenInner(const sptr<IRemoteObject> &abilityToken); 218 219 private: 220 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> appRunningRecordMap_; 221 std::map<const std::string, int> processRestartRecord_; 222 ffrt::mutex lock_; 223 }; 224 } // namespace AppExecFwk 225 } // namespace OHOS 226 227 #endif // OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H 228