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