• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "aot_handler.h"
19 #include "bms_extension_data_mgr.h"
20 #include "bms_key_event_mgr.h"
21 #include "bundle_mgr_service.h"
22 #include "hitrace_meter.h"
23 #include "installd_client.h"
24 #include "ipc_skeleton.h"
25 #include "parameters.h"
26 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
27 #include "default_app_mgr.h"
28 #endif
29 #ifdef WINDOW_ENABLE
30 #include "scene_board_judgement.h"
31 #endif
32 #include "status_receiver_host.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 std::atomic_uint g_installedHapNum = 0;
37 constexpr const char* DATA_PRELOAD_APP = "/data/preload/app/";
38 constexpr uint8_t FACTOR = 8;
39 constexpr uint8_t INTERVAL = 6;
40 constexpr const char* QUICK_FIX_APP_PATH = "/data/update/quickfix/app/temp/cold";
41 constexpr const char* ACCESSTOKEN_PROCESS_NAME = "accesstoken_service";
42 constexpr const char* PRELOAD_APP = "/preload/app/";
43 constexpr const char* MULTIUSER_INSTALL_THIRD_PRELOAD_APP = "const.bms.multiUserInstallThirdPreloadApp";
44 constexpr const char* LOG_PATH = "/log/";
45 
46 class UserReceiverImpl : public StatusReceiverHost {
47 public:
UserReceiverImpl(const std::string & bundleName,bool needReInstall)48     UserReceiverImpl(const std::string &bundleName, bool needReInstall)
49         : bundleName_(bundleName), needReInstall_(needReInstall) {};
50     virtual ~UserReceiverImpl() override = default;
51 
SetBundlePromise(const std::shared_ptr<BundlePromise> & bundlePromise)52     void SetBundlePromise(const std::shared_ptr<BundlePromise>& bundlePromise)
53     {
54         bundlePromise_ = bundlePromise;
55     }
56 
SetTotalHapNum(int32_t totalHapNum)57     void SetTotalHapNum(int32_t totalHapNum)
58     {
59         totalHapNum_ = totalHapNum;
60     }
61 
SavePreInstallException(const std::string & bundleName)62     void SavePreInstallException(const std::string &bundleName)
63     {
64         auto preInstallExceptionMgr =
65             DelayedSingleton<BundleMgrService>::GetInstance()->GetPreInstallExceptionMgr();
66         if (preInstallExceptionMgr == nullptr) {
67             APP_LOGE("preInstallExceptionMgr is nullptr");
68             return;
69         }
70 
71         preInstallExceptionMgr->SavePreInstallExceptionBundleName(bundleName);
72     }
73 
OnStatusNotify(const int progress)74     virtual void OnStatusNotify(const int progress) override {}
OnFinished(const int32_t resultCode,const std::string & resultMsg)75     virtual void OnFinished(const int32_t resultCode, const std::string &resultMsg) override
76     {
77         g_installedHapNum++;
78         APP_LOGI("OnFinished, resultCode : %{public}d, resultMsg : %{public}s, count : %{public}u",
79             resultCode, resultMsg.c_str(), g_installedHapNum.load());
80         if (static_cast<int32_t>(g_installedHapNum) >= totalHapNum_ && bundlePromise_ != nullptr) {
81             bundlePromise_->NotifyAllTasksExecuteFinished();
82         }
83 
84         if (resultCode != ERR_OK && resultCode !=
85             ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON && needReInstall_) {
86             APP_LOGI("needReInstall bundleName: %{public}s", bundleName_.c_str());
87             BmsKeyEventMgr::ProcessMainBundleInstallFailed(bundleName_, resultCode);
88             SavePreInstallException(bundleName_);
89         }
90     }
91 private:
92     std::shared_ptr<BundlePromise> bundlePromise_ = nullptr;
93     int32_t totalHapNum_ = INT32_MAX;
94     std::string bundleName_;
95     bool needReInstall_ = false;
96 };
97 
SkipThirdPreloadAppInstallation(const int32_t userId,const PreInstallBundleInfo & preInfo)98 bool BundleUserMgrHostImpl::SkipThirdPreloadAppInstallation(const int32_t userId, const PreInstallBundleInfo &preInfo)
99 {
100     if (userId == Constants::START_USERID) {
101         return false;
102     }
103     const std::vector<std::string> bundlePaths = preInfo.GetBundlePaths();
104     if (bundlePaths.empty()) {
105         return false;
106     }
107     const std::string firstBundlePath = bundlePaths.front();
108     if (firstBundlePath.rfind(DATA_PRELOAD_APP, 0) == 0) {
109         APP_LOGI("-n %{public}s -u %{public}d not install data preload app", preInfo.GetBundleName().c_str(), userId);
110         return true;
111     }
112     if (firstBundlePath.rfind(PRELOAD_APP, 0) == 0 &&
113         !OHOS::system::GetBoolParameter(MULTIUSER_INSTALL_THIRD_PRELOAD_APP, true)) {
114         APP_LOGI("-n %{public}s -u %{public}d not install preload app", preInfo.GetBundleName().c_str(), userId);
115         return true;
116     }
117     return false;
118 }
119 
CreateNewUser(int32_t userId,const std::vector<std::string> & disallowList)120 ErrCode BundleUserMgrHostImpl::CreateNewUser(int32_t userId, const std::vector<std::string> &disallowList)
121 {
122     HITRACE_METER(HITRACE_TAG_APP);
123     EventReport::SendCpuSceneEvent(ACCESSTOKEN_PROCESS_NAME, 1 << 1); // second scene
124     APP_LOGI("CreateNewUser user(%{public}d) start", userId);
125     BmsExtensionDataMgr bmsExtensionDataMgr;
126     bool needToSkipPreBundleInstall = bmsExtensionDataMgr.IsNeedToSkipPreBundleInstall();
127     if (needToSkipPreBundleInstall) {
128         APP_LOGI("need to skip pre bundle install");
129         EventReport::SendUserSysEvent(UserEventType::CREATE_WITH_SKIP_PRE_INSTALL_START, userId);
130     } else {
131         EventReport::SendUserSysEvent(UserEventType::CREATE_START, userId);
132     }
133     std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
134     if (CheckInitialUser() != ERR_OK) {
135         APP_LOGE("CheckInitialUser failed");
136         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
137     }
138     BeforeCreateNewUser(userId);
139     OnCreateNewUser(userId, needToSkipPreBundleInstall, disallowList);
140     UninstallBackupUninstallList(userId, needToSkipPreBundleInstall);
141     AfterCreateNewUser(userId);
142     if (needToSkipPreBundleInstall) {
143         EventReport::SendUserSysEvent(UserEventType::CREATE_WITH_SKIP_PRE_INSTALL_END, userId);
144     } else {
145         EventReport::SendUserSysEvent(UserEventType::CREATE_END, userId);
146     }
147     APP_LOGI("CreateNewUser end userId: (%{public}d)", userId);
148     if (userId == Constants::START_USERID) {
149         CheckBackUpFirstBootLog();
150     }
151     return ERR_OK;
152 }
153 
BeforeCreateNewUser(int32_t userId)154 void BundleUserMgrHostImpl::BeforeCreateNewUser(int32_t userId)
155 {
156     ClearBundleEvents();
157     InstalldClient::GetInstance()->AddUserDirDeleteDfx(userId);
158 }
159 
OnCreateNewUser(int32_t userId,bool needToSkipPreBundleInstall,const std::vector<std::string> & disallowList)160 void BundleUserMgrHostImpl::OnCreateNewUser(int32_t userId, bool needToSkipPreBundleInstall,
161     const std::vector<std::string> &disallowList)
162 {
163     auto dataMgr = GetDataMgrFromService();
164     if (dataMgr == nullptr) {
165         APP_LOGE("DataMgr is nullptr");
166         return;
167     }
168 
169     auto installer = GetBundleInstaller();
170     if (installer == nullptr) {
171         APP_LOGE("installer is nullptr");
172         return;
173     }
174 
175     if (dataMgr->HasUserId(userId)) {
176         APP_LOGW("Has create user %{public}d", userId);
177         ErrCode ret = InnerRemoveUser(userId, false); // no need lock
178         if (ret != ERR_OK) {
179             APP_LOGW("remove user %{public}d failed, error %{public}d", userId, ret);
180         }
181     }
182 
183     dataMgr->AddUserId(userId);
184     dataMgr->CreateAppInstallDir(userId);
185     std::set<PreInstallBundleInfo> preInstallBundleInfos;
186     if (!GetAllPreInstallBundleInfos(disallowList, userId, needToSkipPreBundleInstall, preInstallBundleInfos)) {
187         APP_LOGE("GetAllPreInstallBundleInfos failed %{public}d.", userId);
188         return;
189     }
190     GetAdditionalBundleInfos(preInstallBundleInfos);
191     g_installedHapNum = 0;
192     std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
193     int32_t totalHapNum = static_cast<int32_t>(preInstallBundleInfos.size());
194     std::string identity = IPCSkeleton::ResetCallingIdentity();
195     bool needReinstall = userId == Constants::START_USERID;
196     // Read apps installed by other users that are visible to all users
197     for (const auto &info : preInstallBundleInfos) {
198         InstallParam installParam;
199         installParam.userId = userId;
200         installParam.isPreInstallApp = !info.GetIsAdditionalApp();
201         installParam.installFlag = InstallFlag::NORMAL;
202         installParam.preinstallSourceFlag = ApplicationInfoFlag::FLAG_BOOT_INSTALLED;
203         sptr<UserReceiverImpl> userReceiverImpl(
204             new (std::nothrow) UserReceiverImpl(info.GetBundleName(), needReinstall));
205         if (userReceiverImpl == nullptr) {
206             APP_LOGE("userReceiverImpl is nullptr, -n %{public}s", info.GetBundleName().c_str());
207             g_installedHapNum++;
208             if (needReinstall) {
209                 SavePreInstallException(info.GetBundleName());
210             }
211             continue;
212         }
213         userReceiverImpl->SetBundlePromise(bundlePromise);
214         userReceiverImpl->SetTotalHapNum(totalHapNum);
215         installer->InstallByBundleName(info.GetBundleName(), installParam, userReceiverImpl);
216     }
217     if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
218         bundlePromise->WaitForAllTasksExecute();
219         APP_LOGI("OnCreateNewUser wait complete");
220     }
221     // process keep alive bundle
222     if (userId == Constants::START_USERID) {
223         BMSEventHandler::ProcessRebootQuickFixBundleInstall(QUICK_FIX_APP_PATH, false);
224     }
225     IPCSkeleton::SetCallingIdentity(identity);
226 }
227 
GetAllPreInstallBundleInfos(const std::vector<std::string> & disallowList,int32_t userId,bool needToSkipPreBundleInstall,std::set<PreInstallBundleInfo> & preInstallBundleInfos)228 bool BundleUserMgrHostImpl::GetAllPreInstallBundleInfos(
229     const std::vector<std::string> &disallowList,
230     int32_t userId, bool needToSkipPreBundleInstall,
231     std::set<PreInstallBundleInfo> &preInstallBundleInfos)
232 {
233     auto dataMgr = GetDataMgrFromService();
234     if (dataMgr == nullptr) {
235         APP_LOGE("DataMgr is nullptr");
236         return false;
237     }
238 
239     bool isStartUser = userId == Constants::START_USERID;
240     std::vector<PreInstallBundleInfo> allPreInstallBundleInfos = dataMgr->GetAllPreInstallBundleInfos();
241     // Scan preset applications and parse package information.
242     for (auto &preInfo : allPreInstallBundleInfos) {
243         InnerBundleInfo innerBundleInfo;
244         if (dataMgr->FetchInnerBundleInfo(preInfo.GetBundleName(), innerBundleInfo)
245             && innerBundleInfo.IsSingleton()) {
246             APP_LOGI("BundleName is IsSingleton %{public}s", preInfo.GetBundleName().c_str());
247             continue;
248         }
249         if (std::find(disallowList.begin(), disallowList.end(),
250             preInfo.GetBundleName()) != disallowList.end()) {
251             APP_LOGI("BundleName is same as black list %{public}s", preInfo.GetBundleName().c_str());
252             continue;
253         }
254         if (needToSkipPreBundleInstall && !preInfo.GetBundlePaths().empty() &&
255             (preInfo.GetBundlePaths().front().find(PRELOAD_APP) == 0)) {
256             APP_LOGI("-n %{public}s -u %{public}d skip install", preInfo.GetBundleName().c_str(), userId);
257             continue;
258         }
259         if (SkipThirdPreloadAppInstallation(userId, preInfo)) {
260             continue;
261         }
262         if (isStartUser) {
263             preInfo.CalculateHapTotalSize();
264         }
265         preInstallBundleInfos.insert(preInfo);
266     }
267 
268     return !preInstallBundleInfos.empty();
269 }
270 
GetAdditionalBundleInfos(std::set<PreInstallBundleInfo> & preInstallBundleInfos)271 void BundleUserMgrHostImpl::GetAdditionalBundleInfos(std::set<PreInstallBundleInfo> &preInstallBundleInfos)
272 {
273     auto dataMgr = GetDataMgrFromService();
274     if (dataMgr == nullptr) {
275         APP_LOGE("DataMgr is nullptr");
276         return;
277     }
278     // get all non pre-installed driver apps to install for new user
279     std::vector<std::string> bundleNames = dataMgr->GetBundleNamesForNewUser();
280     for (auto &bundleName : bundleNames) {
281         PreInstallBundleInfo preInstallBundleInfo;
282         preInstallBundleInfo.SetBundleName(bundleName);
283         preInstallBundleInfo.SetIsAdditionalApp(true);
284         preInstallBundleInfos.insert(preInstallBundleInfo);
285     }
286 }
287 
AfterCreateNewUser(int32_t userId)288 void BundleUserMgrHostImpl::AfterCreateNewUser(int32_t userId)
289 {
290 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
291     DefaultAppMgr::GetInstance().HandleCreateUser(userId);
292 #endif
293     HandleSceneBoard(userId);
294     RdbDataManager::ClearCache();
295     if (userId == Constants::START_USERID) {
296         DelayedSingleton<BundleMgrService>::GetInstance()->NotifyBundleScanStatus();
297         // need process main bundle status
298         BmsKeyEventMgr::ProcessMainBundleStatusFinally();
299     }
300 }
301 
RemoveUser(int32_t userId)302 ErrCode BundleUserMgrHostImpl::RemoveUser(int32_t userId)
303 {
304     return InnerRemoveUser(userId, true);
305 }
306 
InnerRemoveUser(int32_t userId,bool needLock)307 ErrCode BundleUserMgrHostImpl::InnerRemoveUser(int32_t userId, bool needLock)
308 {
309     HITRACE_METER(HITRACE_TAG_APP);
310     EventReport::SendUserSysEvent(UserEventType::REMOVE_START, userId);
311     EventReport::SendCpuSceneEvent(ACCESSTOKEN_PROCESS_NAME, 1 << 1); // second scene
312     APP_LOGI("RemoveUser user(%{public}d) start needLock %{public}d", userId, needLock);
313     if (needLock) {
314         std::lock_guard<std::mutex> lock(bundleUserMgrMutex_);
315         return ProcessRemoveUser(userId);
316     }
317     return ProcessRemoveUser(userId);
318 }
319 
ProcessRemoveUser(int32_t userId)320 ErrCode BundleUserMgrHostImpl::ProcessRemoveUser(int32_t userId)
321 {
322     auto dataMgr = GetDataMgrFromService();
323     if (dataMgr == nullptr) {
324         APP_LOGE("DataMgr is nullptr");
325         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
326     }
327 
328     auto installer = GetBundleInstaller();
329     if (installer == nullptr) {
330         APP_LOGE("installer is nullptr");
331         return ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR;
332     }
333 
334     if (!dataMgr->HasUserId(userId)) {
335         APP_LOGE("Has remove user %{public}d", userId);
336         return ERR_APPEXECFWK_USER_NOT_EXIST;
337     }
338 
339     std::vector<BundleInfo> bundleInfos;
340     if (!dataMgr->GetBundleInfos(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfos, userId)) {
341         APP_LOGE("get all bundle info failed when userId is %{public}d", userId);
342         RemoveArkProfile(userId);
343         RemoveAsanLogDirectory(userId);
344         dataMgr->RemoveUserId(userId);
345         dataMgr->RemoveAppInstallDir(userId);
346         dataMgr->DeleteFirstInstallBundleInfo(userId);
347         return ERR_OK;
348     }
349 
350     ClearBundleEvents();
351     InnerUninstallBundle(userId, bundleInfos);
352     RemoveArkProfile(userId);
353     RemoveAsanLogDirectory(userId);
354     dataMgr->RemoveUserId(userId);
355     dataMgr->RemoveAppInstallDir(userId);
356     dataMgr->DeleteFirstInstallBundleInfo(userId);
357 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
358     DefaultAppMgr::GetInstance().HandleRemoveUser(userId);
359 #endif
360     EventReport::SendUserSysEvent(UserEventType::REMOVE_END, userId);
361     HandleNotifyBundleEventsAsync();
362     APP_LOGI("RemoveUser end userId: (%{public}d)", userId);
363     return ERR_OK;
364 }
365 
RemoveArkProfile(int32_t userId)366 void BundleUserMgrHostImpl::RemoveArkProfile(int32_t userId)
367 {
368     std::string arkProfilePath = AOTHandler::BuildArkProfilePath(userId);
369     APP_LOGI("DeleteArkProfile %{public}s when remove user", arkProfilePath.c_str());
370     InstalldClient::GetInstance()->RemoveDir(arkProfilePath);
371 }
372 
RemoveAsanLogDirectory(int32_t userId)373 void BundleUserMgrHostImpl::RemoveAsanLogDirectory(int32_t userId)
374 {
375     std::string asanLogDir = std::string(ServiceConstants::BUNDLE_ASAN_LOG_DIR) + ServiceConstants::PATH_SEPARATOR
376         + std::to_string(userId);
377     APP_LOGI("remove asan log directory %{public}s when remove user", asanLogDir.c_str());
378     InstalldClient::GetInstance()->RemoveDir(asanLogDir);
379 }
380 
CheckInitialUser()381 ErrCode BundleUserMgrHostImpl::CheckInitialUser()
382 {
383     auto dataMgr = GetDataMgrFromService();
384     if (dataMgr == nullptr) {
385         APP_LOGE("DataMgr is nullptr");
386         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
387     }
388 
389     if (!dataMgr->HasInitialUserCreated()) {
390         APP_LOGI("Bms initial user do not created successfully and wait");
391         std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
392         dataMgr->SetBundlePromise(bundlePromise);
393         bundlePromise->WaitForAllTasksExecute();
394         APP_LOGI("Bms initial user created successfully");
395     }
396     return ERR_OK;
397 }
398 
GetDataMgrFromService()399 const std::shared_ptr<BundleDataMgr> BundleUserMgrHostImpl::GetDataMgrFromService()
400 {
401     return DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
402 }
403 
GetBundleInstaller()404 const sptr<IBundleInstaller> BundleUserMgrHostImpl::GetBundleInstaller()
405 {
406     return DelayedSingleton<BundleMgrService>::GetInstance()->GetBundleInstaller();
407 }
408 
InnerUninstallBundle(int32_t userId,const std::vector<BundleInfo> & bundleInfos)409 void BundleUserMgrHostImpl::InnerUninstallBundle(
410     int32_t userId,
411     const std::vector<BundleInfo> &bundleInfos)
412 {
413     APP_LOGI("InnerUninstallBundle for userId: %{public}d start", userId);
414     auto installer = GetBundleInstaller();
415     if (installer == nullptr) {
416         APP_LOGE("InnerUninstallBundle installer is nullptr");
417         return;
418     }
419     std::string identity = IPCSkeleton::ResetCallingIdentity();
420     g_installedHapNum = 0;
421     std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
422     int32_t totalHapNum = static_cast<int32_t>(bundleInfos.size());
423     for (const auto &info : bundleInfos) {
424         InstallParam installParam;
425         installParam.userId = userId;
426         installParam.SetForceExecuted(true);
427         installParam.concentrateSendEvent = true;
428         installParam.isPreInstallApp = info.isPreInstallApp;
429         installParam.installFlag = InstallFlag::NORMAL;
430         installParam.isRemoveUser = true;
431         sptr<UserReceiverImpl> userReceiverImpl(
432             new (std::nothrow) UserReceiverImpl(info.name, false));
433         if (userReceiverImpl == nullptr) {
434             APP_LOGE("userReceiverImpl is nullptr, -n %{public}s", info.name.c_str());
435             g_installedHapNum++;
436             continue;
437         }
438         userReceiverImpl->SetBundlePromise(bundlePromise);
439         userReceiverImpl->SetTotalHapNum(totalHapNum);
440         installer->Uninstall(info.name, installParam, userReceiverImpl);
441     }
442     if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
443         bundlePromise->WaitForAllTasksExecute();
444     }
445     IPCSkeleton::SetCallingIdentity(identity);
446     APP_LOGI("InnerUninstallBundle for userId: %{public}d end", userId);
447 }
448 
ClearBundleEvents()449 void BundleUserMgrHostImpl::ClearBundleEvents()
450 {
451     std::lock_guard<std::mutex> uninstallEventLock(bundleEventMutex_);
452     bundleEvents_.clear();
453 }
454 
AddNotifyBundleEvents(const NotifyBundleEvents & notifyBundleEvents)455 void BundleUserMgrHostImpl::AddNotifyBundleEvents(const NotifyBundleEvents &notifyBundleEvents)
456 {
457     std::lock_guard<std::mutex> lock(bundleEventMutex_);
458     bundleEvents_.emplace_back(notifyBundleEvents);
459 }
460 
HandleNotifyBundleEventsAsync()461 void BundleUserMgrHostImpl::HandleNotifyBundleEventsAsync()
462 {
463     auto task = [this] {
464         HandleNotifyBundleEvents();
465     };
466     std::thread taskThread(task);
467     taskThread.detach();
468 }
469 
HandleNotifyBundleEvents()470 void BundleUserMgrHostImpl::HandleNotifyBundleEvents()
471 {
472     APP_LOGI("HandleNotifyBundleEvents");
473     std::vector<NotifyBundleEvents> bundleEvents;
474     {
475         std::lock_guard<std::mutex> lock(bundleEventMutex_);
476         bundleEvents = bundleEvents_;
477         bundleEvents_.clear();
478     }
479     auto dataMgr = GetDataMgrFromService();
480     if (dataMgr == nullptr) {
481         APP_LOGE("DataMgr is nullptr");
482         return;
483     }
484 
485     std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
486     for (size_t i = 0; i < bundleEvents.size(); ++i) {
487         commonEventMgr->NotifyBundleStatus(bundleEvents[i], dataMgr);
488         if ((i != 0) && (i % FACTOR == 0)) {
489             std::this_thread::sleep_for(std::chrono::milliseconds(INTERVAL));
490         }
491     }
492 }
493 
HandleSceneBoard(int32_t userId) const494 void BundleUserMgrHostImpl::HandleSceneBoard(int32_t userId) const
495 {
496 #ifdef WINDOW_ENABLE
497     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
498     if (dataMgr == nullptr) {
499         APP_LOGE("dataMgr is null");
500         return;
501     }
502     bool sceneBoardEnable = Rosen::SceneBoardJudgement::IsSceneBoardEnabled();
503     APP_LOGI("userId : %{public}d, sceneBoardEnable : %{public}d", userId, sceneBoardEnable);
504     dataMgr->SetApplicationEnabled(Constants::SCENE_BOARD_BUNDLE_NAME, 0, sceneBoardEnable,
505         ServiceConstants::CALLER_NAME_BMS, userId);
506     dataMgr->SetApplicationEnabled(ServiceConstants::LAUNCHER_BUNDLE_NAME, 0, !sceneBoardEnable,
507         ServiceConstants::CALLER_NAME_BMS, userId);
508 #endif
509 }
510 
InnerProcessSkipPreInstallBundles(const std::set<std::string> & uninstallList,bool needToSkipPreBundleInstall)511 bool BundleUserMgrHostImpl::InnerProcessSkipPreInstallBundles(
512     const std::set<std::string> &uninstallList, bool needToSkipPreBundleInstall)
513 {
514     if (uninstallList.empty() || !needToSkipPreBundleInstall) {
515         return true;
516     }
517     APP_LOGI("process skip pre bundle install start");
518     auto dataMgr = GetDataMgrFromService();
519     if (dataMgr == nullptr) {
520         APP_LOGE("DataMgr is nullptr");
521         return false;
522     }
523     bool ret = true;
524     for (const auto &name : uninstallList) {
525         PreInstallBundleInfo preInfo;
526         preInfo.SetBundleName(name);
527         if (!dataMgr->GetPreInstallBundleInfo(name, preInfo)) {
528             APP_LOGI("no pre bundleInfo %{public}s in db", name.c_str());
529             continue;
530         }
531         if (!preInfo.GetBundlePaths().empty() && (preInfo.GetBundlePaths().front().find(PRELOAD_APP) == 0)) {
532             preInfo.SetIsUninstalled(true);
533             if (!dataMgr->SavePreInstallBundleInfo(name, preInfo)) {
534                 APP_LOGE("save pre bundle %{public}s failed", name.c_str());
535                 ret = false;
536             }
537         }
538     }
539     APP_LOGI("process skip pre bundle install end");
540     return ret;
541 }
542 
UninstallBackupUninstallList(int32_t userId,bool needToSkipPreBundleInstall)543 void BundleUserMgrHostImpl::UninstallBackupUninstallList(int32_t userId, bool needToSkipPreBundleInstall)
544 {
545     BmsExtensionDataMgr bmsExtensionDataMgr;
546     std::set<std::string> uninstallList;
547     bmsExtensionDataMgr.GetBackupUninstallList(userId, uninstallList);
548     if (uninstallList.empty()) {
549         APP_LOGI("userId : %{public}d, back uninstall list is empty", userId);
550         return;
551     }
552     auto installer = GetBundleInstaller();
553     if (installer == nullptr) {
554         APP_LOGE("installer is nullptr");
555         return;
556     }
557 
558     std::string identity = IPCSkeleton::ResetCallingIdentity();
559     g_installedHapNum = 0;
560     std::shared_ptr<BundlePromise> bundlePromise = std::make_shared<BundlePromise>();
561     int32_t totalHapNum = static_cast<int32_t>(uninstallList.size());
562     for (const auto &bundleName : uninstallList) {
563         InstallParam installParam;
564         installParam.userId = userId;
565         installParam.needSendEvent = false;
566         installParam.isPreInstallApp = true;
567         sptr<UserReceiverImpl> userReceiverImpl(
568             new (std::nothrow) UserReceiverImpl(bundleName, false));
569         if (userReceiverImpl == nullptr) {
570             APP_LOGE("userReceiverImpl is nullptr, -n %{public}s", bundleName.c_str());
571             g_installedHapNum++;
572             continue;
573         }
574         userReceiverImpl->SetBundlePromise(bundlePromise);
575         userReceiverImpl->SetTotalHapNum(totalHapNum);
576         installer->Uninstall(bundleName, installParam, userReceiverImpl);
577     }
578     if (static_cast<int32_t>(g_installedHapNum) < totalHapNum) {
579         bundlePromise->WaitForAllTasksExecute();
580     }
581     IPCSkeleton::SetCallingIdentity(identity);
582     bmsExtensionDataMgr.ClearBackupUninstallFile(userId);
583     (void)InnerProcessSkipPreInstallBundles(uninstallList, needToSkipPreBundleInstall);
584 }
585 
SavePreInstallException(const std::string & bundleName)586 void BundleUserMgrHostImpl::SavePreInstallException(const std::string &bundleName)
587 {
588     auto preInstallExceptionMgr =
589         DelayedSingleton<BundleMgrService>::GetInstance()->GetPreInstallExceptionMgr();
590     if (preInstallExceptionMgr == nullptr) {
591         APP_LOGE("preInstallExceptionMgr is nullptr, -n %{public}s save failed", bundleName.c_str());
592         return;
593     }
594 
595     preInstallExceptionMgr->SavePreInstallExceptionBundleName(bundleName);
596 }
597 
CheckBackUpFirstBootLog()598 void BundleUserMgrHostImpl::CheckBackUpFirstBootLog()
599 {
600     APP_LOGI("start");
601     InstalldClient::GetInstance()->BackUpFirstBootLog();
602     return;
603 }
604 }  // namespace AppExecFwk
605 }  // namespace OHOS
606