1 /*
2 * Copyright (c) 2021-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_installer_manager.h"
17
18 #include <cinttypes>
19
20 #include "appexecfwk_errors.h"
21 #include "app_log_wrapper.h"
22 #include "bundle_memory_guard.h"
23 #include "bundle_mgr_service.h"
24 #include "datetime_ex.h"
25 #include "ipc_skeleton.h"
26 #include "xcollie_helper.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string INSTALL_TASK = "Install_Task";
32 const std::string UNINSTALL_TASK = "Uninstall_Task";
33 const std::string RECOVER_TASK = "Recover_Task";
34 const std::string THREAD_POOL_NAME = "InstallerThreadPool";
35 const unsigned int TIME_OUT_SECONDS = 60 * 5;
36 constexpr int32_t MAX_TASK_NUMBER = 10;
37 constexpr int32_t DELAY_INTERVAL_SECONDS = 60;
38 }
39
BundleInstallerManager()40 BundleInstallerManager::BundleInstallerManager()
41 {
42 APP_LOGI("create bundle installer manager instance");
43 }
44
~BundleInstallerManager()45 BundleInstallerManager::~BundleInstallerManager()
46 {
47 APP_LOGI("destroy bundle installer manager instance");
48 }
49
CreateInstallTask(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)50 void BundleInstallerManager::CreateInstallTask(
51 const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
52 {
53 auto installer = CreateInstaller(statusReceiver);
54 if (installer == nullptr) {
55 APP_LOGE("create installer failed");
56 return;
57 }
58 auto task = [installer, bundleFilePath, installParam] {
59 BundleMemoryGuard memoryGuard;
60 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
61 installer->Install(bundleFilePath, installParam);
62 XCollieHelper::CancelTimer(timerId);
63 };
64 AddTask(task, "InstallTask : bundleFilePath : " + bundleFilePath);
65 }
66
CreateRecoverTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)67 void BundleInstallerManager::CreateRecoverTask(
68 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
69 {
70 auto installer = CreateInstaller(statusReceiver);
71 if (installer == nullptr) {
72 APP_LOGE("create installer failed");
73 return;
74 }
75 auto task = [installer, bundleName, installParam] {
76 BundleMemoryGuard memoryGuard;
77 int32_t timerId = XCollieHelper::SetTimer(RECOVER_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
78 installer->Recover(bundleName, installParam);
79 XCollieHelper::CancelTimer(timerId);
80 };
81 AddTask(task, "RecoverTask : bundleName : " + bundleName);
82 }
83
CreateInstallTask(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)84 void BundleInstallerManager::CreateInstallTask(const std::vector<std::string> &bundleFilePaths,
85 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
86 {
87 auto installer = CreateInstaller(statusReceiver);
88 if (installer == nullptr) {
89 APP_LOGE("create installer failed");
90 return;
91 }
92 auto task = [installer, bundleFilePaths, installParam] {
93 BundleMemoryGuard memoryGuard;
94 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
95 installer->Install(bundleFilePaths, installParam);
96 XCollieHelper::CancelTimer(timerId);
97 };
98 std::string paths;
99 for (const auto &bundleFilePath : bundleFilePaths) {
100 paths.append(bundleFilePath).append(" ");
101 }
102 AddTask(task, "InstallTask : bundleFilePaths : " + paths);
103 }
104
CreateInstallByBundleNameTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)105 void BundleInstallerManager::CreateInstallByBundleNameTask(const std::string &bundleName,
106 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
107 {
108 auto installer = CreateInstaller(statusReceiver);
109 if (installer == nullptr) {
110 APP_LOGE("create installer failed");
111 return;
112 }
113
114 auto task = [installer, bundleName, installParam] {
115 BundleMemoryGuard memoryGuard;
116 int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
117 installer->InstallByBundleName(bundleName, installParam);
118 XCollieHelper::CancelTimer(timerId);
119 };
120 AddTask(task, "InstallTask : bundleName : " + bundleName);
121 }
122
CreateUninstallTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)123 void BundleInstallerManager::CreateUninstallTask(
124 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
125 {
126 auto installer = CreateInstaller(statusReceiver);
127 if (installer == nullptr) {
128 APP_LOGE("create installer failed");
129 return;
130 }
131 auto task = [installer, bundleName, installParam] {
132 BundleMemoryGuard memoryGuard;
133 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
134 installer->Uninstall(bundleName, installParam);
135 XCollieHelper::CancelTimer(timerId);
136 };
137 AddTask(task, "UninstallTask : bundleName : " + bundleName);
138 }
139
CreateUninstallTask(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)140 void BundleInstallerManager::CreateUninstallTask(const std::string &bundleName, const std::string &modulePackage,
141 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
142 {
143 auto installer = CreateInstaller(statusReceiver);
144 if (installer == nullptr) {
145 APP_LOGE("create installer failed");
146 return;
147 }
148 auto task = [installer, bundleName, modulePackage, installParam] {
149 BundleMemoryGuard memoryGuard;
150 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
151 installer->Uninstall(bundleName, modulePackage, installParam);
152 XCollieHelper::CancelTimer(timerId);
153 };
154 AddTask(task, "UninstallTask : bundleName : " + bundleName);
155 }
156
CreateUninstallTask(const UninstallParam & uninstallParam,const sptr<IStatusReceiver> & statusReceive)157 void BundleInstallerManager::CreateUninstallTask(const UninstallParam &uninstallParam,
158 const sptr<IStatusReceiver> &statusReceive)
159 {
160 auto installer = CreateInstaller(statusReceive);
161 if (installer == nullptr) {
162 APP_LOGE("create installer failed");
163 return;
164 }
165 auto task = [installer, uninstallParam] {
166 BundleMemoryGuard memoryGuard;
167 int32_t timerId = XCollieHelper::SetTimer(UNINSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr);
168 installer->Uninstall(uninstallParam);
169 XCollieHelper::CancelTimer(timerId);
170 };
171 AddTask(task, "UninstallTask : bundleName : " + uninstallParam.bundleName);
172 }
173
CreateInstaller(const sptr<IStatusReceiver> & statusReceiver)174 std::shared_ptr<BundleInstaller> BundleInstallerManager::CreateInstaller(const sptr<IStatusReceiver> &statusReceiver)
175 {
176 int64_t installerId = GetMicroTickCount();
177 auto installer = std::make_shared<BundleInstaller>(installerId, statusReceiver);
178 installer->SetCallingUid(IPCSkeleton::GetCallingUid());
179 return installer;
180 }
181
AddTask(const ThreadPoolTask & task,const std::string & taskName)182 void BundleInstallerManager::AddTask(const ThreadPoolTask &task, const std::string &taskName)
183 {
184 std::lock_guard<std::mutex> guard(mutex_);
185 APP_LOGI("hold mutex");
186 if (threadPool_ == nullptr) {
187 APP_LOGI("begin to start InstallerThreadPool");
188 threadPool_ = std::make_shared<ThreadPool>(THREAD_POOL_NAME);
189 threadPool_->Start(THREAD_NUMBER);
190 threadPool_->SetMaxTaskNum(MAX_TASK_NUMBER);
191 auto delayCloseTask = std::bind(&BundleInstallerManager::DelayStopThreadPool, shared_from_this());
192 std::thread t(delayCloseTask);
193 t.detach();
194 }
195 APP_LOGI("add task, taskName : %{public}s", taskName.c_str());
196 threadPool_->AddTask(task);
197 }
198
DelayStopThreadPool()199 void BundleInstallerManager::DelayStopThreadPool()
200 {
201 APP_LOGI("DelayStopThreadPool begin");
202 BundleMemoryGuard memoryGuard;
203
204 do {
205 APP_LOGI("sleep for 60s");
206 std::this_thread::sleep_for(std::chrono::seconds(DELAY_INTERVAL_SECONDS));
207 } while (threadPool_ != nullptr && threadPool_->GetCurTaskNum() != 0);
208
209 std::lock_guard<std::mutex> guard(mutex_);
210 if (threadPool_ == nullptr) {
211 APP_LOGI("InstallerThreadPool is null, no need to stop");
212 return;
213 }
214 APP_LOGI("begin to stop InstallerThreadPool");
215 threadPool_->Stop();
216 threadPool_ = nullptr;
217 APP_LOGI("DelayStopThreadPool end");
218 }
219
GetCurTaskNum()220 size_t BundleInstallerManager::GetCurTaskNum()
221 {
222 std::lock_guard<std::mutex> guard(mutex_);
223 if (threadPool_ == nullptr) {
224 return 0;
225 }
226
227 return threadPool_->GetCurTaskNum();
228 }
229 } // namespace AppExecFwk
230 } // namespace OHOS