1 /*
2  * Copyright (c) 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_RESIDENT_PROCESS_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_RESIDENT_PROCESS_MANAGER_H
18 
19 #include <list>
20 #include <mutex>
21 
22 #include "app_scheduler.h"
23 #include "bundle_info.h"
24 #include "singleton.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 struct ResidentAbilityInfo {
29     std::string bundleName;
30     std::string abilityName;
31     int32_t userId = 0;
32     int32_t residentId = -1;
33 };
34 
35 class ResidentAbilityInfoGuard {
36 public:
37     ResidentAbilityInfoGuard() = default;
38     ~ResidentAbilityInfoGuard();
39     ResidentAbilityInfoGuard(ResidentAbilityInfoGuard &) = delete;
40     void operator=(ResidentAbilityInfoGuard &) = delete;
41     ResidentAbilityInfoGuard(const std::string &bundleName, const std::string &abilityName, int32_t userId);
42     void SetResidentAbilityInfo(const std::string &bundleName, const std::string &abilityName, int32_t userId);
43 private:
44     int32_t residentId_ = -1;
45 };
46 /**
47  * @class ResidentProcessManager
48  * ResidentProcessManager
49  */
50 class ResidentProcessManager : public std::enable_shared_from_this<ResidentProcessManager> {
51     DECLARE_DELAYED_SINGLETON(ResidentProcessManager)
52 public:
53 
54     /**
55      * Handle tasks such as initializing databases.
56      *
57     */
58     void Init();
59 
60     /**
61      * Set the enable flag for resident processes.
62      *
63      * @param bundleName, The bundle name of the resident process.
64      * @param callerName, The name of the caller, usually the system application.
65      * @param updateEnable, Set value, if true, start the resident process, If false, stop the resident process
66      * @return Returns ERR_OK on success, others on failure.
67      */
68     int32_t SetResidentProcessEnabled(const std::string &bundleName, const std::string &callerName, bool updateEnable);
69 
70     /**
71      * start empty resident processes.
72      *
73      * @param bundleInfos bundles of resident processes.
74      */
75     void StartResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos);
76     /**
77      * If bundle has right main element, start the main element
78      */
79     void StartResidentProcessWithMainElement(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
80     /**
81      * Once one process created, query keepalive status from db and update then
82      */
83     void OnAppStateChanged(const AppInfo &info);
84     /**
85      * Before starting a resident element, store it.
86      */
87     int32_t PutResidentAbility(const std::string &bundleName, const std::string &abilityName, int32_t userId);
88     bool IsResidentAbility(const std::string &bundleName, const std::string &abilityName, int32_t userId);
89     /**
90      * After a resident element being started, remove it
91      */
92     void RemoveResidentAbility(int32_t residentId);
93     /**
94      * query resident bundles for user
95      */
96     bool GetResidentBundleInfosForUser(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
97     void StartFailedResidentAbilities();
98 private:
99     void UpdateResidentProcessesStatus(const std::string &bundleName, bool localEnable, bool updateEnable);
100     void AddFailedResidentAbility(const std::string &bundleName, const std::string &abilityName, int32_t userId);
101     void NotifyDisableResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
102 
103     std::mutex residentAbilityInfoMutex_;
104     std::list<ResidentAbilityInfo> residentAbilityInfos_;
105     int32_t residentId_ = 0;
106 
107     std::mutex failedResidentAbilityInfoMutex_;
108     std::list<ResidentAbilityInfo> failedResidentAbilityInfos_;
109     std::atomic_bool unlockedAfterBoot_ = false;
110 };
111 }  // namespace AAFwk
112 }  // namespace OHOS
113 #endif  // OHOS_ABILITY_RUNTIME_RESIDENT_PROCESS_MANAGER_H
114