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