1 /* 2 * Copyright (c) 2021-2024 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 <set> 23 24 #include "ability_info.h" 25 #include "app_debug_listener_interface.h" 26 #include "app_jsheap_mem_info.h" 27 #include "app_malloc_info.h" 28 #include "app_mem_info.h" 29 #include "app_running_record.h" 30 #include "app_state_data.h" 31 #include "application_info.h" 32 #include "bundle_info.h" 33 #include "configuration.h" 34 #include "iremote_object.h" 35 #include "kill_process_config.h" 36 #include "record_query_result.h" 37 #include "refbase.h" 38 #include "running_process_info.h" 39 #include "simple_process_info.h" 40 41 namespace OHOS { 42 namespace Rosen { 43 class WindowVisibilityInfo; 44 } 45 namespace AppExecFwk { 46 47 class AppRunningManager : public std::enable_shared_from_this<AppRunningManager> { 48 public: 49 AppRunningManager(); 50 virtual ~AppRunningManager(); 51 /** 52 * CreateAppRunningRecord, Get or create application record information. 53 * 54 * @param token, the unique identification to start the ability. 55 * @param abilityInfo, ability information. 56 * @param appInfo, app information. 57 * @param processName, app process name. 58 * @param uid, app uid in Application record. 59 * @param result, If error occurs, error code is in |result|. 60 * 61 * @return AppRunningRecord pointer if success get or create. 62 */ 63 std::shared_ptr<AppRunningRecord> CreateAppRunningRecord(const std::shared_ptr<ApplicationInfo> &appInfo, 64 const std::string &processName, const BundleInfo &bundleInfo, const std::string &instanceKey, 65 const std::string &customProcessFlag = ""); 66 67 /** 68 * CheckAppRunningRecordIsExist, Get process record by application name and process Name. 69 * 70 * @param appName, the application name. 71 * @param processName, the process name. 72 * @param uid, the process uid. 73 * 74 * @return process record. 75 */ 76 std::shared_ptr<AppRunningRecord> CheckAppRunningRecordIsExist(const std::string &appName, 77 const std::string &processName, const int uid, const BundleInfo &bundleInfo, 78 const std::string &specifiedProcessFlag = "", bool *isProCache = nullptr, const std::string &instanceKey = "", 79 const std::string &customProcessFlag = ""); 80 81 #ifdef APP_NO_RESPONSE_DIALOG 82 /** 83 * CheckAppRunningRecordIsExist, Check whether the process of the app exists by bundle name and process Name. 84 * 85 * @param bundleName, Indicates the bundle name of the bundle.. 86 * @param abilityName, ability name. 87 * 88 * @return true if exist. 89 */ 90 bool CheckAppRunningRecordIsExist(const std::string &bundleName, const std::string &abilityName); 91 #endif 92 93 /** 94 * Check whether the process of the application exists. 95 * 96 * @param accessTokenId, the accessTokenId. 97 * @return, Return true if exist. 98 */ 99 bool IsAppExist(uint32_t accessTokenId); 100 101 /** 102 * CheckAppRunningRecordIsExistByUid, check app exist when concurrent. 103 * 104 * @param uid, the process uid. 105 * @return, Return true if exist. 106 */ 107 bool CheckAppRunningRecordIsExistByUid(int32_t uid); 108 109 /** 110 * Check whether the process of the application exists. 111 * 112 * @param bundleName Indicates the bundle name of the bundle. 113 * @param appCloneIndex the app index of the bundle. 114 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 115 * @return, Return ERR_OK if success, others fail. 116 */ 117 int32_t CheckAppCloneRunningRecordIsExistByBundleName(const std::string &bundleName, 118 int32_t appCloneIndex, bool &isRunning); 119 120 /** 121 * Check whether the process of the application under the specified user exists. 122 * 123 * @param bundleName Indicates the bundle name of the bundle. 124 * @param userId the userId of the bundle. 125 * @param isRunning Obtain the running status of the application, the result is true if running, false otherwise. 126 * @return, Return ERR_OK if success, others fail. 127 */ 128 int32_t IsAppRunningByBundleNameAndUserId(const std::string &bundleName, 129 int32_t userId, bool &isRunning); 130 131 /** 132 * GetAppRunningRecordByPid, Get process record by application pid. 133 * 134 * @param pid, the application pid. 135 * 136 * @return process record. 137 */ 138 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByPid(const pid_t pid); 139 140 /** 141 * GetAppRunningRecordByAbilityToken, Get process record by ability token. 142 * 143 * @param abilityToken, the ability token. 144 * 145 * @return process record. 146 */ 147 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByAbilityToken(const sptr<IRemoteObject> &abilityToken); 148 149 /** 150 * OnRemoteDied, Equipment death notification. 151 * 152 * @param remote, Death client. 153 * @param appMgrServiceInner, Application manager service inner instance. 154 * @return 155 */ 156 std::shared_ptr<AppRunningRecord> OnRemoteDied(const wptr<IRemoteObject> &remote, 157 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner); 158 159 /** 160 * GetAppRunningRecordMap, Get application record list. 161 * 162 * @return the application record list. 163 */ 164 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> GetAppRunningRecordMap(); 165 166 /** 167 * RemoveAppRunningRecordById, Remove application information through application id. 168 * 169 * @param recordId, the application id. 170 * @return 171 */ 172 void RemoveAppRunningRecordById(const int32_t recordId); 173 174 /** 175 * ClearAppRunningRecordMap, Clear application record list. 176 * 177 * @return 178 */ 179 void ClearAppRunningRecordMap(); 180 181 /** 182 * Get the pid of a non-resident process. 183 * 184 * @return Return true if found, otherwise return false. 185 */ 186 bool ProcessExitByBundleName( 187 const std::string &bundleName, std::list<pid_t> &pids, const bool clearPageStack = false); 188 /** 189 * Get Foreground Applications. 190 * 191 * @return Foreground Applications. 192 */ 193 void GetForegroundApplications(std::vector<AppStateData> &list); 194 195 /* 196 * ANotify application update system environment changes. 197 * 198 * @param config System environment change parameters. 199 * @return Returns ERR_OK on success, others on failure. 200 */ 201 int32_t UpdateConfiguration(const Configuration &config, const int32_t userId = -1); 202 203 /** 204 * Update config by sa. 205 * 206 * @param config Application enviroment change parameters. 207 * @param name Application bundle name. 208 * @return Returns ERR_OK on success, others on failure. 209 */ 210 int32_t UpdateConfigurationByBundleName(const Configuration &config, const std::string &name, int32_t appIndex); 211 212 /* 213 * Notify application background of current memory level. 214 * 215 * @param level current memory level. 216 * @return Returns ERR_OK on success, others on failure. 217 */ 218 int32_t NotifyMemoryLevel(int32_t level); 219 220 /** 221 * Notify applications the current memory level. 222 * 223 * @param procLevelMap , <pid_t, MemoryLevel>. 224 * @return Returns ERR_OK on success, others on failure. 225 */ 226 int32_t NotifyProcMemoryLevel(const std::map<pid_t, MemoryLevel> &procLevelMap); 227 228 /* 229 * Get the application's memory allocation info. 230 * 231 * @param pid, pid input. 232 * @param mallocInfo, dynamic storage information output. 233 * 234 * @return Returns ERR_OK on success, others on failure. 235 */ 236 int32_t DumpHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo); 237 238 /** 239 * DumpJsHeapMemory, call DumpJsHeapMemory() through proxy project. 240 * triggerGC and dump the application's jsheap memory info. 241 * 242 * @param info, pid, tid, needGc, needSnapshot 243 * @return Returns ERR_OK on success, others on failure. 244 */ 245 int32_t DumpJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info); 246 247 /** 248 * Set AbilityForegroundingFlag of an app-record to true. 249 * 250 * @param pid, pid. 251 * 252 */ 253 void SetAbilityForegroundingFlagToAppRecord(const pid_t pid); 254 255 void HandleTerminateTimeOut(int64_t eventId); 256 void HandleAbilityAttachTimeOut(const sptr<IRemoteObject> &token, std::shared_ptr<AppMgrServiceInner> serviceInner); 257 void TerminateAbility(const sptr<IRemoteObject> &token, bool clearMissionFlag, 258 std::shared_ptr<AppMgrServiceInner> appMgrServiceInner); 259 260 /** 261 * 262 * @brief update the application info after new module installed. 263 * 264 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 265 * 266 */ 267 int32_t ProcessUpdateApplicationInfoInstalled(const ApplicationInfo &appInfo, const std::string &moduleName); 268 269 bool ProcessExitByBundleNameAndUid( 270 const std::string &bundleName, const int uid, std::list<pid_t> &pids, const KillProcessConfig &config = {}); 271 bool ProcessExitByBundleNameAndAppIndex(const std::string &bundleName, int32_t appIndex, std::list<pid_t> &pids, 272 bool clearPageStack); 273 bool ProcessExitByTokenIdAndInstance(uint32_t accessTokenId, const std::string &instanceKey, std::list<pid_t> &pids, 274 bool clearPageStack); 275 bool GetPidsByUserId(int32_t userId, std::list<pid_t> &pids); 276 bool GetProcessInfosByUserId(int32_t userId, std::list<SimpleProcessInfo> &processInfos); 277 278 void PrepareTerminate(const sptr<IRemoteObject> &token, bool clearMissionFlag = false); 279 280 std::shared_ptr<AppRunningRecord> GetTerminatingAppRunningRecord(const sptr<IRemoteObject> &abilityToken); 281 282 void GetRunningProcessInfoByToken(const sptr<IRemoteObject> &token, AppExecFwk::RunningProcessInfo &info); 283 int32_t GetRunningProcessInfoByPid(const pid_t pid, OHOS::AppExecFwk::RunningProcessInfo &info); 284 int32_t GetRunningProcessInfoByChildProcessPid(const pid_t childPid, OHOS::AppExecFwk::RunningProcessInfo &info); 285 286 void ClipStringContent(const std::regex &re, const std::string &source, std::string &afterCutStr); 287 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByRenderPid(const pid_t pid); 288 std::shared_ptr<RenderRecord> OnRemoteRenderDied(const wptr<IRemoteObject> &remote); 289 bool GetAppRunningStateByBundleName(const std::string &bundleName); 290 int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 291 int32_t NotifyHotReloadPage(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 292 int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback); 293 bool IsApplicationFirstForeground(const AppRunningRecord &foregroundingRecord); 294 bool IsApplicationBackground(const AppRunningRecord &backgroundingRecord); 295 bool IsApplicationFirstFocused(const AppRunningRecord &foregroundingRecord); 296 bool IsApplicationUnfocused(const std::string &bundleName); 297 #ifdef SUPPORT_SCREEN 298 void OnWindowVisibilityChanged(const std::vector<sptr<OHOS::Rosen::WindowVisibilityInfo>> &windowVisibilityInfos); 299 #endif //SUPPORT_SCREEN 300 /** 301 * @brief Set attach app debug mode. 302 * @param bundleName The application bundle name. 303 * @param isAttachDebug Determine if it is in attach debug mode. 304 */ 305 void SetAttachAppDebug(const std::string &bundleName, const bool &isAttachDebug, bool isDebugFromLocal); 306 307 /** 308 * @brief Obtain app debug infos through bundleName. 309 * @param bundleName The application bundle name. 310 * @param isDetachDebug Determine if it is a Detach. 311 * @return Specify the stored app informations based on bundle name output. 312 */ 313 std::vector<AppDebugInfo> GetAppDebugInfosByBundleName(const std::string &bundleName, const bool &isDetachDebug); 314 315 /** 316 * @brief Obtain ability tokens through bundleName. 317 * @param bundleName The application bundle name. 318 * @param abilityTokens Specify the stored ability token based on bundle name output. 319 */ 320 void GetAbilityTokensByBundleName(const std::string &bundleName, std::vector<sptr<IRemoteObject>> &abilityTokens); 321 322 #ifdef SUPPORT_CHILD_PROCESS 323 std::shared_ptr<AppRunningRecord> GetAppRunningRecordByChildProcessPid(const pid_t pid); 324 std::shared_ptr<ChildProcessRecord> OnChildProcessRemoteDied(const wptr<IRemoteObject> &remote); 325 bool IsChildProcessReachLimit(uint32_t accessTokenId); 326 #endif //SUPPORT_CHILD_PROCESS 327 328 /** 329 * @brief Obtain number of app through bundlename. 330 * @param bundleName The application bundle name. 331 * @return Returns the number of queries. 332 */ 333 int32_t GetAllAppRunningRecordCountByBundleName(const std::string &bundleName); 334 335 int32_t SignRestartAppFlag(int32_t uid, const std::string &instanceKey); 336 337 int32_t GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRunningUniqueId); 338 339 int32_t GetAllUIExtensionRootHostPid(pid_t pid, std::vector<pid_t> &hostPids); 340 341 int32_t GetAllUIExtensionProviderPid(pid_t hostPid, std::vector<pid_t> &providerPids); 342 343 int32_t AddUIExtensionLauncherItem(int32_t uiExtensionAbilityId, pid_t hostPid, pid_t providerPid); 344 int32_t RemoveUIExtensionLauncherItem(pid_t pid); 345 int32_t RemoveUIExtensionLauncherItemById(int32_t uiExtensionAbilityId); 346 347 int DumpIpcAllStart(std::string& result); 348 349 int DumpIpcAllStop(std::string& result); 350 351 int DumpIpcAllStat(std::string& result); 352 353 int DumpIpcStart(const int32_t pid, std::string& result); 354 355 int DumpIpcStop(const int32_t pid, std::string& result); 356 357 int DumpIpcStat(const int32_t pid, std::string& result); 358 359 int DumpFfrt(const std::vector<int32_t>& pids, std::string& result); 360 361 bool IsAppProcessesAllCached(const std::string &bundleName, int32_t uid, 362 const std::set<std::shared_ptr<AppRunningRecord>> &cachedSet); 363 364 int32_t UpdateConfigurationDelayed(const std::shared_ptr<AppRunningRecord> &appRecord); 365 366 bool GetPidsByBundleNameUserIdAndAppIndex(const std::string &bundleName, 367 const int userId, const int appIndex, std::list<pid_t> &pids); 368 369 bool HandleUserRequestClean(const sptr<IRemoteObject> &abilityToken, pid_t &pid, int32_t &uid); 370 371 int32_t CheckIsKiaProcess(pid_t pid, bool &isKia); 372 373 bool CheckAppRunningRecordIsLast(const std::shared_ptr<AppRunningRecord> &appRecord); 374 375 void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey); 376 std::shared_ptr<AppRunningRecord> QueryAppRecordPlus(int32_t pid, int32_t uid); 377 private: 378 std::shared_ptr<AbilityRunningRecord> GetAbilityRunningRecord(const int64_t eventId); 379 int32_t AssignRunningProcessInfoByAppRecord( 380 std::shared_ptr<AppRunningRecord> appRecord, AppExecFwk::RunningProcessInfo &info) const; 381 bool isCollaboratorReserveType(const std::shared_ptr<AppRunningRecord> &appRecord); 382 void NotifyAppPreCache(const std::shared_ptr<AppRunningRecord>& appRecord, 383 const std::shared_ptr<AppMgrServiceInner>& appMgrServiceInner); 384 void AddRecordToDeadList(std::shared_ptr<AppRunningRecord> appRecord); 385 void RemoveTimeoutDeadAppRecord(); 386 387 private: 388 std::mutex runningRecordMapMutex_; 389 std::map<const int32_t, const std::shared_ptr<AppRunningRecord>> appRunningRecordMap_; 390 std::list<std::pair<int64_t, std::shared_ptr<AppRunningRecord>>> deadAppRecordList_; // dead time and record 391 392 std::mutex uiExtensionMapLock_; 393 std::map<int32_t, std::pair<pid_t, pid_t>> uiExtensionLauncherMap_; 394 395 std::mutex updateConfigurationDelayedLock_; 396 std::map<const int32_t, bool> updateConfigurationDelayedMap_; 397 }; 398 } // namespace AppExecFwk 399 } // namespace OHOS 400 401 #endif // OHOS_ABILITY_RUNTIME_APP_RUNNING_MANAGER_H 402