• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_mgr_service_event_handler.h"
17 
18 #include <future>
19 
20 #include "app_log_wrapper.h"
21 #include "bundle_mgr_service.h"
22 #include "bundle_scanner.h"
23 #include "perf_profile.h"
24 #include "system_bundle_installer.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 
BMSEventHandler(const std::shared_ptr<EventRunner> & runner)29 BMSEventHandler::BMSEventHandler(const std::shared_ptr<EventRunner> &runner) : EventHandler(runner)
30 {
31     APP_LOGI("instance is created");
32 }
33 
~BMSEventHandler()34 BMSEventHandler::~BMSEventHandler()
35 {
36     APP_LOGI("instance is destroyed");
37 }
38 
ProcessEvent(const InnerEvent::Pointer & event)39 void BMSEventHandler::ProcessEvent(const InnerEvent::Pointer &event)
40 {
41     switch (event->GetInnerEventId()) {
42         case BUNDLE_SCAN_START: {
43             auto future = std::async(std::launch::async, [this] {
44                 ProcessSystemBundleInstall(Constants::AppType::SYSTEM_APP);
45                 ProcessSystemBundleInstall(Constants::AppType::THIRD_SYSTEM_APP);
46             });
47             future.get();
48             SetAllInstallFlag();
49             break;
50         }
51         case BUNDLE_SCAN_FINISHED:
52             break;
53         case BMS_START_FINISHED:
54             break;
55         default:
56             APP_LOGE("the eventId is not supported");
57             break;
58     }
59 }
60 
ProcessSystemBundleInstall(Constants::AppType appType) const61 void BMSEventHandler::ProcessSystemBundleInstall(Constants::AppType appType) const
62 {
63     APP_LOGI("scan thread start");
64     auto scanner = std::make_unique<BundleScanner>();
65     if (!scanner) {
66         APP_LOGE("make scanner failed");
67         return;
68     }
69     std::string scanDir = (appType == Constants::AppType::SYSTEM_APP) ? Constants::SYSTEM_APP_SCAN_PATH
70                                                                       : Constants::THIRD_SYSTEM_APP_SCAN_PATH;
71     APP_LOGI("scanDir %{public}s", scanDir.c_str());
72     std::list<std::string> bundleList = scanner->Scan(scanDir);
73     for (const auto &item : bundleList) {
74         SystemBundleInstaller installer(item);
75         APP_LOGI("scan item %{public}s", item.c_str());
76         if (!installer.InstallSystemBundle(appType)) {
77             APP_LOGW("Install System app:%{public}s error", item.c_str());
78         }
79     }
80     PerfProfile::GetInstance().Dump();
81 }
82 
SetAllInstallFlag() const83 void BMSEventHandler::SetAllInstallFlag() const
84 {
85     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
86     if (dataMgr == nullptr) {
87         APP_LOGE("DataMgr is nullptr");
88         return;
89     }
90     dataMgr->SetAllInstallFlag(true);
91 }
92 
93 }  // namespace AppExecFwk
94 }  // namespace OHOS
95