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     void StartResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos);
70     void StartResidentProcessWithMainElement(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
71     void OnAppStateChanged(const AppInfo &info);
72     int32_t PutResidentAbility(const std::string &bundleName, const std::string &abilityName, int32_t userId);
73     bool IsResidentAbility(const std::string &bundleName, const std::string &abilityName, int32_t userId);
74     void RemoveResidentAbility(int32_t residentId);
75     bool GetResidentBundleInfosForUser(std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
76 private:
77     bool CheckMainElement(const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::string &processName,
78         std::string &mainElement, std::set<uint32_t> &needEraseIndexSet, size_t bundleInfoIndex, int32_t userId = 0);
79     void UpdateResidentProcessesStatus(const std::string &bundleName, bool localEnable, bool updateEnable);
80     void NotifyDisableResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos, int32_t userId);
81     void UpdateMainElement(const std::string &bundleName, const std::string &moduleName,
82         const std::string &mainElement, bool updateEnable, int32_t userId);
83 
84     std::mutex residentAbilityInfoMutex_;
85     std::list<ResidentAbilityInfo> residentAbilityInfos_;
86     int32_t residentId_ = 0;
87 };
88 }  // namespace AAFwk
89 }  // namespace OHOS
90 #endif  // OHOS_ABILITY_RUNTIME_RESIDENT_PROCESS_MANAGER_H
91