• 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_installer_manager.h"
17 #include <cinttypes>
18 
19 #include "datetime_ex.h"
20 
21 #include "app_log_wrapper.h"
22 #include "appexecfwk_errors.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 
28 const std::string ADD_INSTALLER_FAIL = "fail to add installer in bundle installer manager";
29 
30 }
31 
BundleInstallerManager(const std::shared_ptr<EventRunner> & runner)32 BundleInstallerManager::BundleInstallerManager(const std::shared_ptr<EventRunner> &runner) : EventHandler(runner)
33 {
34     APP_LOGI("create bundle installer manager instance");
35     installersPool_.Start(THREAD_NUMBER);
36     installersPool_.SetMaxTaskNum(MAX_TASK_NUMBER);
37 }
38 
~BundleInstallerManager()39 BundleInstallerManager::~BundleInstallerManager()
40 {
41     APP_LOGI("destroy bundle installer manager instance");
42     installersPool_.Stop();
43 }
44 
ProcessEvent(const InnerEvent::Pointer & event)45 void BundleInstallerManager::ProcessEvent(const InnerEvent::Pointer &event)
46 {
47     APP_LOGI("process event : %{public}u", event->GetInnerEventId());
48     switch (event->GetInnerEventId()) {
49         case REMOVE_BUNDLE_INSTALLER:
50             RemoveInstaller(event->GetParam());
51             break;
52         default:
53             APP_LOGW("the eventId is not supported");
54     }
55 }
56 
CreateInstallTask(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)57 void BundleInstallerManager::CreateInstallTask(
58     const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
59 {
60     auto installer = CreateInstaller(statusReceiver);
61     if (!installer) {
62         APP_LOGE("create installer failed");
63         return;
64     }
65     auto task = [installer, bundleFilePath, installParam] { installer->Install(bundleFilePath, installParam); };
66     installersPool_.AddTask(task);
67 }
68 
CreateUninstallTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)69 void BundleInstallerManager::CreateUninstallTask(
70     const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
71 {
72     auto installer = CreateInstaller(statusReceiver);
73     if (!installer) {
74         APP_LOGE("create installer failed");
75         return;
76     }
77     auto task = [installer, bundleName, installParam] { installer->Uninstall(bundleName, installParam); };
78     installersPool_.AddTask(task);
79 }
80 
CreateUninstallTask(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)81 void BundleInstallerManager::CreateUninstallTask(const std::string &bundleName, const std::string &modulePackage,
82     const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
83 {
84     auto installer = CreateInstaller(statusReceiver);
85     if (!installer) {
86         APP_LOGE("create installer failed");
87         return;
88     }
89     auto task = [installer, bundleName, modulePackage, installParam] {
90         installer->Uninstall(bundleName, modulePackage, installParam);
91     };
92     installersPool_.AddTask(task);
93 }
94 
CreateInstaller(const sptr<IStatusReceiver> & statusReceiver)95 std::shared_ptr<BundleInstaller> BundleInstallerManager::CreateInstaller(const sptr<IStatusReceiver> &statusReceiver)
96 {
97     int64_t installerId = GetMicroTickCount();
98     auto installer = std::make_shared<BundleInstaller>(installerId, shared_from_this(), statusReceiver);
99     if (!installer) {
100         APP_LOGE("create bundle installer failed");
101         return nullptr;
102     }
103     bool isSuccess = false;
104     {
105         std::lock_guard<std::mutex> lock(mutex_);
106         auto result = installers_.try_emplace(installer->GetInstallerId(), installer);
107         isSuccess = result.second;
108     }
109     if (isSuccess) {
110         APP_LOGD("add the specific %{public}" PRId64 " installer", installerId);
111     } else {
112         APP_LOGE("fail to add bundle installer");
113         installer.reset();
114         statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR, ADD_INSTALLER_FAIL);
115     }
116     return installer;
117 }
118 
RemoveInstaller(const int64_t installerId)119 void BundleInstallerManager::RemoveInstaller(const int64_t installerId)
120 {
121     std::lock_guard<std::mutex> lock(mutex_);
122     if (installers_.erase(installerId) > 0) {
123         APP_LOGD("erase the specific %{public}" PRId64 " installer", installerId);
124     }
125 }
126 
127 }  // namespace AppExecFwk
128 }  // namespace OHOS