• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ffrt.h"
26 #include "ipc_skeleton.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
BundleInstallerManager()30 BundleInstallerManager::BundleInstallerManager()
31 {
32     APP_LOGI("create bundle installer manager instance");
33 }
34 
~BundleInstallerManager()35 BundleInstallerManager::~BundleInstallerManager()
36 {
37     APP_LOGI("destroy bundle installer manager instance");
38 }
39 
CreateInstallTask(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)40 void BundleInstallerManager::CreateInstallTask(
41     const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
42 {
43     auto installer = CreateInstaller(statusReceiver);
44     if (installer == nullptr) {
45         APP_LOGE("create installer failed");
46         return;
47     }
48     auto task = [installer, bundleFilePath, installParam] {
49         BundleMemoryGuard memoryGuard;
50         installer->Install(bundleFilePath, installParam);
51     };
52     AddTask(task, "InstallTask : bundleFilePath : " + bundleFilePath);
53 }
54 
CreateRecoverTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)55 void BundleInstallerManager::CreateRecoverTask(
56     const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
57 {
58     auto installer = CreateInstaller(statusReceiver);
59     if (installer == nullptr) {
60         APP_LOGE("create installer failed");
61         return;
62     }
63     auto task = [installer, bundleName, installParam] {
64         BundleMemoryGuard memoryGuard;
65         installer->Recover(bundleName, installParam);
66     };
67     AddTask(task, "RecoverTask : bundleName : " + bundleName);
68 }
69 
CreateInstallTask(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)70 void BundleInstallerManager::CreateInstallTask(const std::vector<std::string> &bundleFilePaths,
71     const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
72 {
73     auto installer = CreateInstaller(statusReceiver);
74     if (installer == nullptr) {
75         APP_LOGE("create installer failed");
76         return;
77     }
78     auto task = [installer, bundleFilePaths, installParam] {
79         BundleMemoryGuard memoryGuard;
80         installer->Install(bundleFilePaths, installParam);
81     };
82     std::string paths;
83     for (const auto &bundleFilePath : bundleFilePaths) {
84         paths.append(bundleFilePath).append(" ");
85     }
86     AddTask(task, "InstallTask : bundleFilePaths : " + paths);
87 }
88 
CreateInstallByBundleNameTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)89 void BundleInstallerManager::CreateInstallByBundleNameTask(const std::string &bundleName,
90     const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
91 {
92     auto installer = CreateInstaller(statusReceiver);
93     if (installer == nullptr) {
94         APP_LOGE("create installer failed");
95         return;
96     }
97 
98     auto task = [installer, bundleName, installParam] {
99         BundleMemoryGuard memoryGuard;
100         installer->InstallByBundleName(bundleName, installParam);
101     };
102     AddTask(task, "InstallTask : bundleName : " + bundleName);
103 }
104 
CreateUninstallTask(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)105 void BundleInstallerManager::CreateUninstallTask(
106     const std::string &bundleName, 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     auto task = [installer, bundleName, installParam] {
114         BundleMemoryGuard memoryGuard;
115         installer->Uninstall(bundleName, installParam);
116     };
117     AddTask(task, "UninstallTask : bundleName : " + bundleName);
118 }
119 
CreateUninstallTask(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)120 void BundleInstallerManager::CreateUninstallTask(const std::string &bundleName, const std::string &modulePackage,
121     const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
122 {
123     auto installer = CreateInstaller(statusReceiver);
124     if (installer == nullptr) {
125         APP_LOGE("create installer failed");
126         return;
127     }
128     auto task = [installer, bundleName, modulePackage, installParam] {
129         BundleMemoryGuard memoryGuard;
130         installer->Uninstall(bundleName, modulePackage, installParam);
131     };
132     AddTask(task, "UninstallTask : bundleName : " + bundleName);
133 }
134 
CreateUninstallTask(const UninstallParam & uninstallParam,const sptr<IStatusReceiver> & statusReceive)135 void BundleInstallerManager::CreateUninstallTask(const UninstallParam &uninstallParam,
136     const sptr<IStatusReceiver> &statusReceive)
137 {
138     auto installer = CreateInstaller(statusReceive);
139     if (installer == nullptr) {
140         APP_LOGE("create installer failed");
141         return;
142     }
143     auto task = [installer, uninstallParam] {
144         BundleMemoryGuard memoryGuard;
145         installer->Uninstall(uninstallParam);
146     };
147     AddTask(task, "UninstallTask : bundleName : " + uninstallParam.bundleName);
148 }
149 
CreateInstaller(const sptr<IStatusReceiver> & statusReceiver)150 std::shared_ptr<BundleInstaller> BundleInstallerManager::CreateInstaller(const sptr<IStatusReceiver> &statusReceiver)
151 {
152     int64_t installerId = GetMicroTickCount();
153     auto installer = std::make_shared<BundleInstaller>(installerId, statusReceiver);
154     installer->SetCallingUid(IPCSkeleton::GetCallingUid());
155     return installer;
156 }
157 
AddTask(const ThreadPoolTask & task,const std::string & taskName)158 void BundleInstallerManager::AddTask(const ThreadPoolTask &task, const std::string &taskName)
159 {
160     APP_LOGI("submit task, taskName : %{public}s", taskName.c_str());
161     ffrt::submit(task, {}, {}, ffrt::task_attr().qos(ffrt::qos_user_initiated).name(taskName.c_str()));
162 }
163 }  // namespace AppExecFwk
164 }  // namespace OHOS