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 #include "exit_resident_process_manager.h"
16
17 #include <mutex>
18
19 #include "ability_manager_errors.h"
20 #include "hilog_tag_wrapper.h"
21 #include "in_process_call_wrapper.h"
22 #include "remote_client_manager.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 constexpr int32_t U0_USER_ID = 0;
28 constexpr int32_t BASE_USER_RANGE = 200000;
29 }
~ExitResidentProcessManager()30 ExitResidentProcessManager::~ExitResidentProcessManager() {}
31
ExitResidentProcessManager()32 ExitResidentProcessManager::ExitResidentProcessManager() {}
33
GetInstance()34 ExitResidentProcessManager &ExitResidentProcessManager::GetInstance()
35 {
36 static ExitResidentProcessManager instance;
37 return instance;
38 }
39
IsMemorySizeSufficient() const40 bool ExitResidentProcessManager::IsMemorySizeSufficient() const
41 {
42 std::lock_guard<ffrt::mutex> lock(mutexLock_);
43 return currentMemorySizeState_ == MemoryState::MEMORY_RECOVERY;
44 }
45
IsNoRequireBigMemory() const46 bool ExitResidentProcessManager::IsNoRequireBigMemory() const
47 {
48 std::lock_guard<ffrt::mutex> lock(mutexLockBigMemory_);
49 return currentBigMemoryState_ == MemoryState::NO_REQUIRE_BIG_MEMORY;
50 }
51
RecordExitResidentBundleName(const std::string & bundleName,int32_t uid)52 bool ExitResidentProcessManager::RecordExitResidentBundleName(const std::string &bundleName, int32_t uid)
53 {
54 std::lock_guard<ffrt::mutex> lock(mutexLock_);
55 if (currentMemorySizeState_ == MemoryState::MEMORY_RECOVERY) {
56 return false;
57 }
58 exitResidentInfos_.emplace_back(bundleName, uid);
59 return true;
60 }
61
RecordExitResidentBundleNameOnRequireBigMemory(const std::string & bundleName,int32_t uid)62 bool ExitResidentProcessManager::RecordExitResidentBundleNameOnRequireBigMemory(
63 const std::string &bundleName, int32_t uid)
64 {
65 std::lock_guard<ffrt::mutex> lock(mutexLockBigMemory_);
66 if (currentBigMemoryState_ == MemoryState::NO_REQUIRE_BIG_MEMORY) {
67 return false;
68 }
69 exitResidentBigMemoryInfos_.emplace_back(bundleName, uid);
70 return true;
71 }
72
RecordExitResidentBundleDependedOnWeb(const std::string & bundleName,int32_t uid)73 void ExitResidentProcessManager::RecordExitResidentBundleDependedOnWeb(const std::string &bundleName, int32_t uid)
74 {
75 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
76 TAG_LOGD(AAFwkTag::APPMGR, "call");
77 exitResidentBundlesDependedOnWeb_.emplace_back(bundleName, uid);
78 }
79
HandleMemorySizeInSufficent()80 int32_t ExitResidentProcessManager::HandleMemorySizeInSufficent()
81 {
82 std::lock_guard<ffrt::mutex> lock(mutexLock_);
83 if (currentMemorySizeState_ != MemoryState::MEMORY_RECOVERY) {
84 TAG_LOGE(AAFwkTag::APPMGR, "memory size is insufficient");
85 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
86 }
87 currentMemorySizeState_ = MemoryState::LOW_MEMORY;
88 return ERR_OK;
89 }
90
HandleRequireBigMemoryOptimization()91 int32_t ExitResidentProcessManager::HandleRequireBigMemoryOptimization()
92 {
93 std::lock_guard<ffrt::mutex> lock(mutexLockBigMemory_);
94 if (currentBigMemoryState_ != MemoryState::NO_REQUIRE_BIG_MEMORY) {
95 TAG_LOGE(AAFwkTag::APPMGR, "REQUIRE_BIG_MEMORY");
96 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
97 }
98 TAG_LOGI(AAFwkTag::APPMGR, "REQUIRE_BIG_MEMORY");
99 currentBigMemoryState_ = MemoryState::REQUIRE_BIG_MEMORY;
100 return ERR_OK;
101 }
102
HandleMemorySizeSufficient(std::vector<ExitResidentProcessInfo> & processInfos)103 int32_t ExitResidentProcessManager::HandleMemorySizeSufficient(std::vector<ExitResidentProcessInfo>& processInfos)
104 {
105 std::lock_guard<ffrt::mutex> lock(mutexLock_);
106 if (currentMemorySizeState_ == MemoryState::MEMORY_RECOVERY) {
107 TAG_LOGE(AAFwkTag::APPMGR, "memory size is sufficient");
108 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
109 }
110 currentMemorySizeState_ = MemoryState::MEMORY_RECOVERY;
111 processInfos = std::move(exitResidentInfos_);
112 return ERR_OK;
113 }
114
HandleExitResidentBundleDependedOnWeb(std::vector<ExitResidentProcessInfo> & bundleNames)115 void ExitResidentProcessManager::HandleExitResidentBundleDependedOnWeb(
116 std::vector<ExitResidentProcessInfo> &bundleNames)
117 {
118 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
119 TAG_LOGD(AAFwkTag::APPMGR, "call");
120 bundleNames = std::move(exitResidentBundlesDependedOnWeb_);
121 }
122
HandleNoRequireBigMemoryOptimization(std::vector<ExitResidentProcessInfo> & processInfos)123 int32_t ExitResidentProcessManager::HandleNoRequireBigMemoryOptimization (
124 std::vector<ExitResidentProcessInfo> &processInfos)
125 {
126 std::lock_guard<ffrt::mutex> lock(mutexLockBigMemory_);
127 if (currentBigMemoryState_ == MemoryState::NO_REQUIRE_BIG_MEMORY) {
128 TAG_LOGE(AAFwkTag::APPMGR, "NO_REQUIRE_BIG_MEMORY");
129 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
130 }
131 TAG_LOGI(AAFwkTag::APPMGR, "NO_REQUIRE_BIG_MEMORY");
132 currentBigMemoryState_ = MemoryState::NO_REQUIRE_BIG_MEMORY;
133 processInfos = std::move(exitResidentBigMemoryInfos_);
134 exitResidentBigMemoryInfos_.clear();
135 return ERR_OK;
136 }
137
QueryExitBundleInfos(const std::vector<ExitResidentProcessInfo> & exitProcessInfos,std::vector<AppExecFwk::BundleInfo> & exitBundleInfos)138 void ExitResidentProcessManager::QueryExitBundleInfos(const std::vector<ExitResidentProcessInfo> &exitProcessInfos,
139 std::vector<AppExecFwk::BundleInfo>& exitBundleInfos)
140 {
141 AppExecFwk::BundleInfo bundleInfo;
142 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
143 if (remoteClientManager == nullptr) {
144 TAG_LOGE(AAFwkTag::APPMGR, "null remoteClientManager");
145 return;
146 }
147 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
148 if (bundleMgrHelper == nullptr) {
149 TAG_LOGE(AAFwkTag::APPMGR, "null bundleMgrHelper");
150 return;
151 }
152 for (const auto &item: exitProcessInfos) {
153 std::string bundleName;
154 int32_t appIndex;
155 if (IN_PROCESS_CALL(bundleMgrHelper->GetNameAndIndexForUid(item.uid, bundleName, appIndex))) {
156 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to get appIndex for %{public}s", item.bundleName.c_str());
157 continue;
158 }
159 auto flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)
160 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE)
161 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY)
162 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
163 if (IN_PROCESS_CALL(bundleMgrHelper->GetCloneBundleInfo(item.bundleName, flags, appIndex, bundleInfo,
164 item.uid / BASE_USER_RANGE)) != ERR_OK) {
165 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail from %{public}s", item.bundleName.c_str());
166 continue;
167 }
168 if (!bundleInfo.isKeepAlive) {
169 TAG_LOGE(AAFwkTag::ABILITYMGR, "not a resident application");
170 continue;
171 }
172 exitBundleInfos.emplace_back(bundleInfo);
173 }
174 }
175
IsKilledForUpgradeWeb(const std::string & bundleName) const176 bool ExitResidentProcessManager::IsKilledForUpgradeWeb(const std::string &bundleName) const
177 {
178 TAG_LOGD(AAFwkTag::APPMGR, "call");
179 std::vector<ExitResidentProcessInfo> bundleNames;
180 {
181 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
182 bundleNames = exitResidentBundlesDependedOnWeb_;
183 }
184 for (const auto &innerBundleName : bundleNames) {
185 if (innerBundleName.bundleName == bundleName) {
186 TAG_LOGD(AAFwkTag::APPMGR, "Is killed for upgrade web.");
187 return true;
188 }
189 }
190 TAG_LOGD(AAFwkTag::APPMGR, "Not killed for upgrade web.");
191 return false;
192 }
193 } // namespace AppExecFwk
194 } // namespace OHOS
195