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 #include "bundle_user_mgr_host_impl.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_mgr_service.h"
20 #include "bundle_permission_mgr.h"
21 #include "bundle_promise.h"
22 #include "bundle_util.h"
23 #include "event_report.h"
24 #include "hitrace_meter.h"
25 #include "installd_client.h"
26 #include "rdb_data_manager.h"
27 #include "status_receiver_host.h"
28 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
29 #include "default_app_mgr.h"
30 #endif
31 #include "ipc_skeleton.h"
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 std::atomic_uint g_installedHapNum = 0;
36 const std::string ARK_PROFILE_PATH = "/data/local/ark-profile/";
37
38 class UserReceiverImpl : public StatusReceiverHost {
39 public:
40 UserReceiverImpl() = default;
41 virtual ~UserReceiverImpl() override = default;
42
SetBundlePromise(const std::shared_ptr<BundlePromise> & bundlePromise)43 void SetBundlePromise(const std::shared_ptr<BundlePromise>& bundlePromise)
44 {
45 bundlePromise_ = bundlePromise;
46 }
47
SetTotalHapNum(int32_t totalHapNum)48 void SetTotalHapNum(int32_t totalHapNum)
49 {
50 totalHapNum_ = totalHapNum;
51 }
52
OnStatusNotify(const int progress)53 virtual void OnStatusNotify(const int progress) override {}
OnFinished(const int32_t resultCode,const std::string & resultMsg)54 virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg) override
55 {
56 g_installedHapNum++;
57 APP_LOGI("OnFinished, resultCode : %{public}d, resultMsg : %{public}s, count : %{public}u",
58 resultCode, resultMsg.c_str(), g_installedHapNum.load());
59 if (static_cast<int32_t>(g_installedHapNum) >= totalHapNum_ && bundlePromise_ != nullptr) {
60 bundlePromise_->NotifyAllTasksExecuteFinished();
61 }
62 }
63 private:
64 std::shared_ptr<BundlePromise> bundlePromise_ = nullptr;
65 int32_t totalHapNum_ = INT32_MAX;
66 };
67
CreateNewUser(int32_t userId)68 void BundleUserMgrHostImpl::CreateNewUser(int32_t userId)
69 {
70 HITRACE_METER(HITRACE_TAG_APP);
71 EventReport::SendUserSysEvent(UserEventType::CREATE_START, userId);
72 APP_LOGI("CreateNewUser user(%{public}d) start.", userId);
73 std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
74 CheckInitialUser();
75 BeforeCreateNewUser(userId);
76 OnCreateNewUser(userId);
77 AfterCreateNewUser(userId);
78 EventReport::SendUserSysEvent(UserEventType::CREATE_END, userId);
79 APP_LOGI("CreateNewUser end userId: (%{public}d)", userId);
80 }
81
BeforeCreateNewUser(int32_t userId)82 void BundleUserMgrHostImpl::BeforeCreateNewUser(int32_t userId)
83 {
84 if (!BundlePermissionMgr::Init()) {
85 APP_LOGW("BundlePermissionMgr::Init failed");
86 }
87 }
88
OnCreateNewUser(int32_t userId)89 void BundleUserMgrHostImpl::OnCreateNewUser(int32_t userId)
90 {
91 auto dataMgr = GetDataMgrFromService();
92 if (dataMgr == nullptr) {
93 APP_LOGE("DataMgr is nullptr");
94 return;
95 }
96
97 auto installer = GetBundleInstaller();
98 if (installer == nullptr) {
99 APP_LOGE("installer is nullptr");
100 return;
101 }
102
103 if (dataMgr->HasUserId(userId)) {
104 APP_LOGE("Has create user %{public}d.", userId);
105 return;
106 }
107
108 dataMgr->AddUserId(userId);
109 // Scan preset applications and parse package information.
110 std::vector<PreInstallBundleInfo> preInstallBundleInfos = dataMgr->GetAllPreInstallBundleInfos();
111 g_installedHapNum = 0;
112 std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
113 int32_t totalHapNum = static_cast<int32_t>(preInstallBundleInfos.size());
114 std::string identity = IPCSkeleton::ResetCallingIdentity();
115 // Read apps installed by other users that are visible to all users
116 for (const auto &info : preInstallBundleInfos) {
117 InstallParam installParam;
118 installParam.userId = userId;
119 installParam.isPreInstallApp = true;
120 installParam.installFlag = InstallFlag::NORMAL;
121 sptr<UserReceiverImpl> userReceiverImpl(new (std::nothrow) UserReceiverImpl());
122 userReceiverImpl->SetBundlePromise(bundlePromise);
123 userReceiverImpl->SetTotalHapNum(totalHapNum);
124 installer->InstallByBundleName(info.GetBundleName(), installParam, userReceiverImpl);
125 }
126 if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
127 bundlePromise->WaitForAllTasksExecute();
128 APP_LOGI("OnCreateNewUser wait complete");
129 }
130 IPCSkeleton::SetCallingIdentity(identity);
131 }
132
AfterCreateNewUser(int32_t userId)133 void BundleUserMgrHostImpl::AfterCreateNewUser(int32_t userId)
134 {
135 if (userId == Constants::START_USERID) {
136 DelayedSingleton<BundleMgrService>::GetInstance()->NotifyBundleScanStatus();
137 }
138
139 BundlePermissionMgr::UnInit();
140 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
141 DefaultAppMgr::GetInstance().HandleCreateUser(userId);
142 #endif
143 RdbDataManager::ClearCache();
144 }
145
RemoveUser(int32_t userId)146 void BundleUserMgrHostImpl::RemoveUser(int32_t userId)
147 {
148 HITRACE_METER(HITRACE_TAG_APP);
149 EventReport::SendUserSysEvent(UserEventType::REMOVE_START, userId);
150 APP_LOGD("RemoveUser user(%{public}d) start.", userId);
151 std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
152 auto dataMgr = GetDataMgrFromService();
153 if (dataMgr == nullptr) {
154 APP_LOGE("DataMgr is nullptr");
155 return;
156 }
157
158 auto installer = GetBundleInstaller();
159 if (installer == nullptr) {
160 APP_LOGE("installer is nullptr");
161 return;
162 }
163
164 if (!dataMgr->HasUserId(userId)) {
165 APP_LOGE("Has remove user %{public}d.", userId);
166 return;
167 }
168
169 std::vector<BundleInfo> bundleInfos;
170 if (!dataMgr->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, userId)) {
171 APP_LOGE("get all bundle info failed when userId is %{public}d.", userId);
172 RemoveArkProfile(userId);
173 RemoveAsanLogDirectory(userId);
174 dataMgr->RemoveUserId(userId);
175 return;
176 }
177
178 InnerUninstallBundle(userId, bundleInfos);
179
180 RemoveArkProfile(userId);
181 RemoveAsanLogDirectory(userId);
182 dataMgr->RemoveUserId(userId);
183 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
184 DefaultAppMgr::GetInstance().HandleRemoveUser(userId);
185 #endif
186 EventReport::SendUserSysEvent(UserEventType::REMOVE_END, userId);
187 APP_LOGD("RemoveUser end userId: (%{public}d)", userId);
188 }
189
RemoveArkProfile(int32_t userId)190 void BundleUserMgrHostImpl::RemoveArkProfile(int32_t userId)
191 {
192 std::string arkProfilePath;
193 arkProfilePath.append(ARK_PROFILE_PATH).append(std::to_string(userId));
194 APP_LOGI("DeleteArkProfile %{public}s when remove user", arkProfilePath.c_str());
195 InstalldClient::GetInstance()->RemoveDir(arkProfilePath);
196 }
197
RemoveAsanLogDirectory(int32_t userId)198 void BundleUserMgrHostImpl::RemoveAsanLogDirectory(int32_t userId)
199 {
200 std::string asanLogDir = Constants::BUNDLE_ASAN_LOG_DIR + Constants::PATH_SEPARATOR
201 + std::to_string(userId);
202 APP_LOGI("remove asan log directory %{public}s when remove user", asanLogDir.c_str());
203 InstalldClient::GetInstance()->RemoveDir(asanLogDir);
204 }
205
CheckInitialUser()206 void BundleUserMgrHostImpl::CheckInitialUser()
207 {
208 auto dataMgr = GetDataMgrFromService();
209 if (dataMgr == nullptr) {
210 APP_LOGE("DataMgr is nullptr");
211 return;
212 }
213
214 if (!dataMgr->HasInitialUserCreated()) {
215 APP_LOGD("Bms initial user do not created successfully and wait.");
216 std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
217 dataMgr->SetBundlePromise(bundlePromise);
218 bundlePromise->WaitForAllTasksExecute();
219 APP_LOGD("Bms initial user created successfully.");
220 }
221 }
222
GetDataMgrFromService()223 const std::shared_ptr<BundleDataMgr> BundleUserMgrHostImpl::GetDataMgrFromService()
224 {
225 return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
226 }
227
GetBundleInstaller()228 const sptr<IBundleInstaller> BundleUserMgrHostImpl::GetBundleInstaller()
229 {
230 return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
231 }
232
InnerUninstallBundle(int32_t userId,const std::vector<BundleInfo> & bundleInfos)233 void BundleUserMgrHostImpl::InnerUninstallBundle(
234 int32_t userId,
235 const std::vector<BundleInfo> &bundleInfos)
236 {
237 APP_LOGD("InnerUninstallBundle for userId: %{public}d start", userId);
238 auto installer = GetBundleInstaller();
239 if (installer == nullptr) {
240 APP_LOGE("InnerUninstallBundle installer is nullptr");
241 return;
242 }
243 std::string identity = IPCSkeleton::ResetCallingIdentity();
244 g_installedHapNum = 0;
245 std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
246 int32_t totalHapNum = static_cast<int32_t>(bundleInfos.size());
247 for (const auto &info : bundleInfos) {
248 InstallParam installParam;
249 installParam.userId = userId;
250 installParam.forceExecuted = true;
251 installParam.isPreInstallApp = info.isPreInstallApp;
252 installParam.installFlag = InstallFlag::NORMAL;
253 sptr<UserReceiverImpl> userReceiverImpl(new UserReceiverImpl());
254 userReceiverImpl->SetBundlePromise(bundlePromise);
255 userReceiverImpl->SetTotalHapNum(totalHapNum);
256 installer->Uninstall(info.name, installParam, userReceiverImpl);
257 }
258 if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
259 bundlePromise->WaitForAllTasksExecute();
260 }
261 IPCSkeleton::SetCallingIdentity(identity);
262 APP_LOGD("InnerUninstallBundle for userId: %{public}d end", userId);
263 }
264 } // namespace AppExecFwk
265 } // namespace OHOS
266