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
IsMemorySizeSufficent() const40 bool ExitResidentProcessManager::IsMemorySizeSufficent() 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, "The remoteClientManager is nullptr.");
101 return;
102 }
103 auto bundleMgrHelper = remoteClientManager->GetBundleManagerHelper();
104 if (bundleMgrHelper == nullptr) {
105 TAG_LOGE(AAFwkTag::APPMGR, "The bundleMgrHelper is nullptr.");
106 return;
107 }
108 for (const auto &item: exitProcessInfos) {
109 if (!IN_PROCESS_CALL(bundleMgrHelper->GetBundleInfo(item.bundleName,
110 AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, item.uid / BASE_USER_RANGE))) {
111 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail from %{public}s", item.bundleName.c_str());
112 continue;
113 }
114 if (!bundleInfo.isKeepAlive) {
115 TAG_LOGE(AAFwkTag::ABILITYMGR, "Not a resident application.");
116 continue;
117 }
118 exitBundleInfos.emplace_back(bundleInfo);
119 }
120 }
121
IsKilledForUpgradeWeb(const std::string & bundleName) const122 bool ExitResidentProcessManager::IsKilledForUpgradeWeb(const std::string &bundleName) const
123 {
124 TAG_LOGE(AAFwkTag::APPMGR, "call");
125 std::vector<ExitResidentProcessInfo> bundleNames;
126 {
127 std::lock_guard<ffrt::mutex> lock(webMutexLock_);
128 bundleNames = exitResidentBundlesDependedOnWeb_;
129 }
130 for (const auto &innerBundleName : bundleNames) {
131 if (innerBundleName.bundleName == bundleName) {
132 TAG_LOGD(AAFwkTag::APPMGR, "Is killed for upgrade web.");
133 return true;
134 }
135 }
136 TAG_LOGD(AAFwkTag::APPMGR, "Not killed for upgrade web.");
137 return false;
138 }
139 } // namespace AppExecFwk
140 } // namespace OHOS
141