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