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_ == MemorySizeState::MEMORY_SIZE_SUFFICIENT;
44 }
45
RecordExitResidentBundleName(const std::string & bundleName,int32_t uid)46 bool ExitResidentProcessManager::RecordExitResidentBundleName(const std::string &bundleName, int32_t uid)
47 {
48 std::lock_guard<ffrt::mutex> lock(mutexLock_);
49 if (currentMemorySizeState_ == MemorySizeState::MEMORY_SIZE_SUFFICIENT) {
50 return false;
51 }
52 exitResidentInfos_.emplace_back(bundleName, uid);
53 return true;
54 }
55
RecordExitResidentBundleDependedOnWeb(const std::string & bundleName,int32_t uid)56 void ExitResidentProcessManager::RecordExitResidentBundleDependedOnWeb(const std::string &bundleName, int32_t uid)
57 {
58 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
59 TAG_LOGE(AAFwkTag::APPMGR, "call");
60 exitResidentBundlesDependedOnWeb_.emplace_back(bundleName, uid);
61 }
62
HandleMemorySizeInSufficent()63 int32_t ExitResidentProcessManager::HandleMemorySizeInSufficent()
64 {
65 std::lock_guard<ffrt::mutex> lock(mutexLock_);
66 if (currentMemorySizeState_ != MemorySizeState::MEMORY_SIZE_SUFFICIENT) {
67 TAG_LOGE(AAFwkTag::APPMGR, "memory size is insufficient");
68 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
69 }
70 currentMemorySizeState_ = MemorySizeState::MEMORY_SIZE_INSUFFICIENT;
71 return ERR_OK;
72 }
73
HandleMemorySizeSufficient(std::vector<ExitResidentProcessInfo> & processInfos)74 int32_t ExitResidentProcessManager::HandleMemorySizeSufficient(std::vector<ExitResidentProcessInfo>& processInfos)
75 {
76 std::lock_guard<ffrt::mutex> lock(mutexLock_);
77 if (currentMemorySizeState_ == MemorySizeState::MEMORY_SIZE_SUFFICIENT) {
78 TAG_LOGE(AAFwkTag::APPMGR, "memory size is sufficient");
79 return AAFwk::ERR_NATIVE_MEMORY_SIZE_STATE_UNCHANGED;
80 }
81 currentMemorySizeState_ = MemorySizeState::MEMORY_SIZE_SUFFICIENT;
82 processInfos = std::move(exitResidentInfos_);
83 return ERR_OK;
84 }
85
HandleExitResidentBundleDependedOnWeb(std::vector<ExitResidentProcessInfo> & bundleNames)86 void ExitResidentProcessManager::HandleExitResidentBundleDependedOnWeb(
87 std::vector<ExitResidentProcessInfo> &bundleNames)
88 {
89 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
90 TAG_LOGE(AAFwkTag::APPMGR, "call");
91 bundleNames = std::move(exitResidentBundlesDependedOnWeb_);
92 }
93
QueryExitBundleInfos(const std::vector<ExitResidentProcessInfo> & exitProcessInfos,std::vector<AppExecFwk::BundleInfo> & exitBundleInfos)94 void ExitResidentProcessManager::QueryExitBundleInfos(const std::vector<ExitResidentProcessInfo> &exitProcessInfos,
95 std::vector<AppExecFwk::BundleInfo>& exitBundleInfos)
96 {
97 AppExecFwk::BundleInfo bundleInfo;
98 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
99 if (remoteClientManager == nullptr) {
100 TAG_LOGE(AAFwkTag::APPMGR, "null remoteClientManager");
101 return;
102 }
103 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
104 if (bundleMgrHelper == nullptr) {
105 TAG_LOGE(AAFwkTag::APPMGR, "null bundleMgrHelper");
106 return;
107 }
108 for (const auto &item: exitProcessInfos) {
109 std::string bundleName;
110 int32_t appIndex;
111 if (IN_PROCESS_CALL(bundleMgrHelper->GetNameAndIndexForUid(item.uid, bundleName, appIndex))) {
112 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to get appIndex for %{public}s", item.bundleName.c_str());
113 continue;
114 }
115 auto flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)
116 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE)
117 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY)
118 | static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
119 if (IN_PROCESS_CALL(bundleMgrHelper->GetCloneBundleInfo(item.bundleName, flags, appIndex, bundleInfo,
120 item.uid / BASE_USER_RANGE)) != ERR_OK) {
121 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail from %{public}s", item.bundleName.c_str());
122 continue;
123 }
124 if (!bundleInfo.isKeepAlive) {
125 TAG_LOGE(AAFwkTag::ABILITYMGR, "not a resident application");
126 continue;
127 }
128 exitBundleInfos.emplace_back(bundleInfo);
129 }
130 }
131
IsKilledForUpgradeWeb(const std::string & bundleName) const132 bool ExitResidentProcessManager::IsKilledForUpgradeWeb(const std::string &bundleName) const
133 {
134 TAG_LOGE(AAFwkTag::APPMGR, "call");
135 std::vector<ExitResidentProcessInfo> bundleNames;
136 {
137 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
138 bundleNames = exitResidentBundlesDependedOnWeb_;
139 }
140 for (const auto &innerBundleName : bundleNames) {
141 if (innerBundleName.bundleName == bundleName) {
142 TAG_LOGD(AAFwkTag::APPMGR, "Is killed for upgrade web.");
143 return true;
144 }
145 }
146 TAG_LOGD(AAFwkTag::APPMGR, "Not killed for upgrade web.");
147 return false;
148 }
149 } // namespace AppExecFwk
150 } // namespace OHOS
151