• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hitrace_meter.h"
24 #include "installd_client.h"
25 #ifdef BMS_RDB_ENABLE
26 #include "rdb_data_manager.h"
27 #endif
28 #include "status_receiver_host.h"
29 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
30 #include "default_app_mgr.h"
31 #endif
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_LOGD("OnFinished::resultCode:(%{public}d) resultMsg:(%{public}s).",
58             resultCode, resultMsg.c_str());
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     APP_LOGD("CreateNewUser user(%{public}d) start.", userId);
72     std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
73     CheckInitialUser();
74     BeforeCreateNewUser(userId);
75     OnCreateNewUser(userId);
76     AfterCreateNewUser(userId);
77     APP_LOGD("CreateNewUser end userId: (%{public}d)", userId);
78 }
79 
BeforeCreateNewUser(int32_t userId)80 void BundleUserMgrHostImpl::BeforeCreateNewUser(int32_t userId)
81 {
82     if (!BundlePermissionMgr::Init()) {
83         APP_LOGW("BundlePermissionMgr::Init failed");
84     }
85 }
86 
OnCreateNewUser(int32_t userId)87 void BundleUserMgrHostImpl::OnCreateNewUser(int32_t userId)
88 {
89     auto dataMgr = GetDataMgrFromService();
90     if (dataMgr == nullptr) {
91         APP_LOGE("DataMgr is nullptr");
92         return;
93     }
94 
95     auto installer = GetBundleInstaller();
96     if (installer == nullptr) {
97         APP_LOGE("installer is nullptr");
98         return;
99     }
100 
101     if (dataMgr->HasUserId(userId)) {
102         APP_LOGE("Has create user %{public}d.", userId);
103         return;
104     }
105 
106     dataMgr->AddUserId(userId);
107     // Scan preset applications and parse package information.
108     std::vector<PreInstallBundleInfo> preInstallBundleInfos = dataMgr->GetAllPreInstallBundleInfos();
109     g_installedHapNum = 0;
110     std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
111     int32_t totalHapNum = static_cast<int32_t>(preInstallBundleInfos.size());
112 
113     // Read apps installed by other users that are visible to all users
114     for (const auto &info : preInstallBundleInfos) {
115         InstallParam installParam;
116         installParam.userId = userId;
117         installParam.isPreInstallApp = true;
118         installParam.installFlag = InstallFlag::NORMAL;
119         sptr<UserReceiverImpl> userReceiverImpl(new (std::nothrow) UserReceiverImpl());
120         userReceiverImpl->SetBundlePromise(bundlePromise);
121         userReceiverImpl->SetTotalHapNum(totalHapNum);
122         installer->InstallByBundleName(info.GetBundleName(), installParam, userReceiverImpl);
123     }
124 
125     if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
126         bundlePromise->WaitForAllTasksExecute();
127     }
128 }
129 
AfterCreateNewUser(int32_t userId)130 void BundleUserMgrHostImpl::AfterCreateNewUser(int32_t userId)
131 {
132     if (userId == Constants::START_USERID) {
133         DelayedSingleton<BundleMgrService>::GetInstance()->NotifyBundleScanStatus();
134     }
135 
136     BundlePermissionMgr::UnInit();
137 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
138     DefaultAppMgr::GetInstance().HandleCreateUser(userId);
139 #endif
140 #ifdef BMS_RDB_ENABLE
141         RdbDataManager::ClearCache();
142 #endif
143 }
144 
RemoveUser(int32_t userId)145 void BundleUserMgrHostImpl::RemoveUser(int32_t userId)
146 {
147     HITRACE_METER(HITRACE_TAG_APP);
148     APP_LOGD("RemoveUser user(%{public}d) start.", userId);
149     std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
150     auto dataMgr = GetDataMgrFromService();
151     if (dataMgr == nullptr) {
152         APP_LOGE("DataMgr is nullptr");
153         return;
154     }
155 
156     auto installer = GetBundleInstaller();
157     if (installer == nullptr) {
158         APP_LOGE("installer is nullptr");
159         return;
160     }
161 
162     if (!dataMgr->HasUserId(userId)) {
163         APP_LOGE("Has remove user %{public}d.", userId);
164         return;
165     }
166 
167     std::vector<BundleInfo> bundleInfos;
168     if (!dataMgr->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, userId)) {
169         APP_LOGE("get all bundle info failed when userId is %{public}d.", userId);
170         RemoveArkProfile(userId);
171         RemoveAsanLogDirectory(userId);
172         dataMgr->RemoveUserId(userId);
173         return;
174     }
175 
176     g_installedHapNum = 0;
177     std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
178     int32_t totalHapNum = static_cast<int32_t>(bundleInfos.size());
179     for (const auto &info : bundleInfos) {
180         InstallParam installParam;
181         installParam.userId = userId;
182         installParam.forceExecuted = true;
183         installParam.isPreInstallApp = true;
184         installParam.installFlag = InstallFlag::NORMAL;
185         sptr<UserReceiverImpl> userReceiverImpl(new UserReceiverImpl());
186         userReceiverImpl->SetBundlePromise(bundlePromise);
187         userReceiverImpl->SetTotalHapNum(totalHapNum);
188         installer->Uninstall(info.name, installParam, userReceiverImpl);
189     }
190 
191     if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
192         bundlePromise->WaitForAllTasksExecute();
193     }
194 
195     RemoveArkProfile(userId);
196     RemoveAsanLogDirectory(userId);
197     dataMgr->RemoveUserId(userId);
198 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
199     DefaultAppMgr::GetInstance().HandleRemoveUser(userId);
200 #endif
201     APP_LOGD("RemoveUser end userId: (%{public}d)", userId);
202 }
203 
RemoveArkProfile(int32_t userId)204 void BundleUserMgrHostImpl::RemoveArkProfile(int32_t userId)
205 {
206     std::string arkProfilePath;
207     arkProfilePath.append(ARK_PROFILE_PATH).append(std::to_string(userId));
208     APP_LOGI("DeleteArkProfile %{public}s when remove user", arkProfilePath.c_str());
209     InstalldClient::GetInstance()->RemoveDir(arkProfilePath);
210 }
211 
RemoveAsanLogDirectory(int32_t userId)212 void BundleUserMgrHostImpl::RemoveAsanLogDirectory(int32_t userId)
213 {
214     std::string asanLogDir = Constants::BUNDLE_ASAN_LOG_DIR + Constants::PATH_SEPARATOR
215         + std::to_string(userId);
216     APP_LOGI("remove asan log directory %{public}s when remove user", asanLogDir.c_str());
217     InstalldClient::GetInstance()->RemoveDir(asanLogDir);
218 }
219 
CheckInitialUser()220 void BundleUserMgrHostImpl::CheckInitialUser()
221 {
222     auto dataMgr = GetDataMgrFromService();
223     if (dataMgr == nullptr) {
224         APP_LOGE("DataMgr is nullptr");
225         return;
226     }
227 
228     if (!dataMgr->HasInitialUserCreated()) {
229         APP_LOGD("Bms initial user do not created successfully and wait.");
230         std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
231         dataMgr->SetBundlePromise(bundlePromise);
232         bundlePromise->WaitForAllTasksExecute();
233         APP_LOGD("Bms initial user created successfully.");
234     }
235 }
236 
GetDataMgrFromService()237 const std::shared_ptr<BundleDataMgr> BundleUserMgrHostImpl::GetDataMgrFromService()
238 {
239     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
240 }
241 
GetBundleInstaller()242 const sptr<IBundleInstaller> BundleUserMgrHostImpl::GetBundleInstaller()
243 {
244     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
245 }
246 }  // namespace AppExecFwk
247 }  // namespace OHOS
248