1 /* 2 * Copyright (c) 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_KEEP_ALIVE_PROCESS_MANAGER_H 17 #define OHOS_ABILITY_RUNTIME_KEEP_ALIVE_PROCESS_MANAGER_H 18 19 #include <functional> 20 21 #include "ability_manager_service.h" 22 #include "app_scheduler.h" 23 #include "bundle_info.h" 24 #include "ffrt.h" 25 #include "singleton.h" 26 27 namespace OHOS { 28 namespace AAFwk { 29 using AbilityKeepAliveService = AbilityRuntime::AbilityKeepAliveService; 30 31 struct KeepAliveAbilityInfo { 32 int32_t userId = 0; 33 int32_t appCloneIndex = 0; 34 int32_t uid = 0; 35 std::string bundleName; 36 std::string moduleName; 37 std::string abilityName; 38 }; 39 40 class CheckStatusBarTask { 41 public: 42 CheckStatusBarTask() = delete; 43 CheckStatusBarTask(int32_t uid,std::function<void (void)> && task)44 CheckStatusBarTask(int32_t uid, std::function<void(void)>&& task) 45 : uid_(uid), task_(task) {}; 46 ~CheckStatusBarTask()47 ~CheckStatusBarTask() {}; 48 49 void Cancel(); 50 51 void Run(); 52 GetUid()53 inline int32_t GetUid() 54 { 55 return uid_; 56 } 57 58 private: 59 int32_t uid_; 60 ffrt::mutex cancelMutex_; 61 std::function<void(void)> task_; 62 }; 63 64 /** 65 * @class KeepAliveProcessManager 66 * KeepAliveProcessManager 67 */ 68 class KeepAliveProcessManager { 69 public: 70 /** 71 * Get the instance of KeepAliveProcessManager. 72 * 73 * @return Returns the instance of KeepAliveProcessManager. 74 */ 75 static KeepAliveProcessManager &GetInstance(); 76 77 /** 78 * Set the enable flag for keep-alive processes. 79 * 80 * @param bundleName, The bundle name of the keep-alive process. 81 * @param userId, The user ID of the bundle. 82 * @param updateEnable, Set value. 83 * @return Returns ERR_OK on success, others on failure. 84 */ 85 int32_t SetApplicationKeepAlive(const std::string &bundleName, int32_t userId, bool updateEnable, 86 bool isByEDM, bool isInner); 87 88 /** 89 * @brief Query keep-alive applications. 90 * @param appType App type. 91 * @param userId User id. 92 * @param infoList Output parameters, return keep-alive info list. 93 * @return Returns ERR_OK on success, others on failure. 94 */ 95 int32_t QueryKeepAliveApplications(int32_t appType, int32_t userId, std::vector<KeepAliveInfo> &infoList, 96 bool isByEDM); 97 98 /** 99 * If bundle has right main element, start the main element 100 * 101 * @param bundleInfos bundles of keep-alive processes. 102 * @param userId, The user ID of the bundle. 103 */ 104 void StartKeepAliveProcessWithMainElement(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId); 105 106 /** 107 * Once one process created, query keep-alive status from db and update then 108 * 109 * @param appInfo The App info. 110 */ 111 void OnAppStateChanged(const AppInfo &info); 112 113 /** 114 * Check if it is a keep-alive bundle under the specified user. 115 * 116 * @param bundleName, The bundle name of the keep-alive process. 117 * @param userId, The user ID of the bundle. 118 */ 119 bool IsKeepAliveBundle(const std::string &bundleName, int32_t userId); 120 121 /** 122 * query keep-alive bundles for user 123 * 124 * @param bundleInfos bundles of keep-alive processes. 125 * @param userId, The user ID of the bundle. 126 */ 127 bool GetKeepAliveBundleInfosForUser(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId); 128 129 int32_t StartKeepAliveMainAbility(const KeepAliveAbilityInfo &info); 130 131 void RemoveCheckStatusBarTask(int32_t uid, bool shouldCancel); 132 133 private: 134 KeepAliveProcessManager(); 135 ~KeepAliveProcessManager(); 136 137 int32_t CheckPermission(); 138 int32_t CheckPermissionForEDM(); 139 void StartKeepAliveProcessWithMainElementPerBundle(const AppExecFwk::BundleInfo &bundleInfo, 140 int32_t userId); 141 void AfterStartKeepAliveApp(const std::string &bundleName, uint32_t accessTokenId, int32_t uid, int32_t userId, 142 bool isMultiInstance); 143 bool IsRunningAppInStatusBar(const AppExecFwk::BundleInfo &bundleInfo); 144 145 ffrt::mutex checkStatusBarTasksMutex_; 146 std::vector<std::shared_ptr<CheckStatusBarTask>> checkStatusBarTasks_; 147 148 DISALLOW_COPY_AND_MOVE(KeepAliveProcessManager); 149 }; 150 } // namespace AAFwk 151 } // namespace OHOS 152 #endif // OHOS_ABILITY_RUNTIME_KEEP_ALIVE_PROCESS_MANAGER_H 153