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.h"
17
18 #include <cinttypes>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_mgr_service.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
BundleInstaller(const int64_t installerId,const sptr<IStatusReceiver> & statusReceiver)25 BundleInstaller::BundleInstaller(const int64_t installerId, const sptr<IStatusReceiver> &statusReceiver)
26 : installerId_(installerId), statusReceiver_(statusReceiver)
27 {
28 APP_LOGI("create bundle installer instance, the installer id is %{public}" PRId64 "", installerId_);
29 }
30
~BundleInstaller()31 BundleInstaller::~BundleInstaller()
32 {
33 APP_LOGI("destroy bundle installer instance, the installer id is %{public}" PRId64 "", installerId_);
34 }
35
Install(const std::string & bundleFilePath,const InstallParam & installParam)36 void BundleInstaller::Install(const std::string &bundleFilePath, const InstallParam &installParam)
37 {
38 ErrCode resultCode = ERR_OK;
39 if (installParam.userId == Constants::ALL_USERID) {
40 auto userInstallParam = installParam;
41 userInstallParam.allUser = true;
42 for (auto userId : GetExistsCommonUserIds()) {
43 userInstallParam.userId = userId;
44 userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
45 resultCode = InstallBundle(
46 bundleFilePath, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
47 ResetInstallProperties();
48 }
49
50 NotifyAllBundleStatus();
51 } else {
52 resultCode = InstallBundle(
53 bundleFilePath, installParam, Constants::AppType::THIRD_PARTY_APP);
54 }
55
56 if (statusReceiver_ != nullptr) {
57 statusReceiver_->OnFinished(resultCode, "");
58 }
59 }
60
Recover(const std::string & bundleName,const InstallParam & installParam)61 void BundleInstaller::Recover(const std::string &bundleName, const InstallParam &installParam)
62 {
63 ErrCode resultCode = ERR_OK;
64 if (installParam.userId == Constants::ALL_USERID) {
65 auto userInstallParam = installParam;
66 for (auto userId : GetExistsCommonUserIds()) {
67 userInstallParam.userId = userId;
68 userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
69 resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
70 ResetInstallProperties();
71 }
72 } else {
73 resultCode = BaseBundleInstaller::Recover(bundleName, installParam);
74 }
75
76 if (statusReceiver_ != nullptr) {
77 statusReceiver_->OnFinished(resultCode, "");
78 }
79 }
80
Install(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam)81 void BundleInstaller::Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam)
82 {
83 ErrCode resultCode = ERR_OK;
84 if (installParam.userId == Constants::ALL_USERID) {
85 auto userInstallParam = installParam;
86 userInstallParam.allUser = true;
87 for (auto userId : GetExistsCommonUserIds()) {
88 userInstallParam.userId = userId;
89 userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
90 resultCode = InstallBundle(
91 bundleFilePaths, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
92 ResetInstallProperties();
93 }
94
95 NotifyAllBundleStatus();
96 } else {
97 resultCode = InstallBundle(bundleFilePaths, installParam, Constants::AppType::THIRD_PARTY_APP);
98 }
99 if (statusReceiver_ != nullptr) {
100 statusReceiver_->OnFinished(resultCode, "");
101 }
102 }
103
InstallByBundleName(const std::string & bundleName,const InstallParam & installParam)104 void BundleInstaller::InstallByBundleName(const std::string &bundleName, const InstallParam &installParam)
105 {
106 ErrCode resultCode = InstallBundleByBundleName(bundleName, installParam);
107 if (statusReceiver_ != nullptr) {
108 statusReceiver_->OnFinished(resultCode, "");
109 }
110 }
111
Uninstall(const std::string & bundleName,const InstallParam & installParam)112 void BundleInstaller::Uninstall(const std::string &bundleName, const InstallParam &installParam)
113 {
114 ErrCode resultCode = ERR_OK;
115 if (installParam.userId == Constants::ALL_USERID) {
116 std::vector<ErrCode> errCode;
117 auto userInstallParam = installParam;
118 for (auto userId : GetExistsCommonUserIds()) {
119 userInstallParam.userId = userId;
120 resultCode = UninstallBundle(bundleName, userInstallParam);
121 errCode.push_back(resultCode);
122 ResetInstallProperties();
123 }
124 if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
125 for (const auto &err : errCode) {
126 if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
127 err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
128 resultCode = err;
129 break;
130 }
131 resultCode = ERR_OK;
132 }
133 } else {
134 resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
135 }
136 } else {
137 resultCode = UninstallBundle(bundleName, installParam);
138 }
139
140 if (statusReceiver_ != nullptr) {
141 statusReceiver_->OnFinished(resultCode, "");
142 }
143 }
144
Uninstall(const UninstallParam & uninstallParam)145 void BundleInstaller::Uninstall(const UninstallParam &uninstallParam)
146 {
147 ErrCode resultCode = ERR_OK;
148 resultCode = UninstallBundleByUninstallParam(uninstallParam);
149 if (statusReceiver_ != nullptr) {
150 statusReceiver_->OnFinished(resultCode, "");
151 }
152 }
153
Uninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam)154 void BundleInstaller::Uninstall(
155 const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam)
156 {
157 ErrCode resultCode = ERR_OK;
158 if (installParam.userId == Constants::ALL_USERID) {
159 std::vector<ErrCode> errCode;
160 auto userInstallParam = installParam;
161 for (auto userId : GetExistsCommonUserIds()) {
162 userInstallParam.userId = userId;
163 resultCode = UninstallBundle(bundleName, modulePackage, userInstallParam);
164 errCode.push_back(resultCode);
165 ResetInstallProperties();
166 }
167 if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
168 for (const auto &err : errCode) {
169 if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
170 err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_MODULE ||
171 err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
172 resultCode = err;
173 break;
174 }
175 resultCode = ERR_OK;
176 }
177 } else {
178 resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
179 }
180 } else {
181 resultCode = UninstallBundle(bundleName, modulePackage, installParam);
182 }
183
184 if (statusReceiver_ != nullptr) {
185 statusReceiver_->OnFinished(resultCode, "");
186 }
187 }
188
UpdateInstallerState(const InstallerState state)189 void BundleInstaller::UpdateInstallerState(const InstallerState state)
190 {
191 APP_LOGI("UpdateInstallerState in bundleInstaller state %{public}d", state);
192 SetInstallerState(state);
193 if (statusReceiver_) {
194 statusReceiver_->OnStatusNotify(static_cast<int>(state));
195 }
196 }
197
GetExistsCommonUserIds()198 std::set<int32_t> BundleInstaller::GetExistsCommonUserIds()
199 {
200 std::set<int32_t> userIds;
201 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
202 if (dataMgr == nullptr) {
203 APP_LOGE("Get dataMgr shared_ptr nullptr");
204 return userIds;
205 }
206
207 for (auto userId : dataMgr->GetAllUser()) {
208 if (userId >= Constants::START_USERID) {
209 userIds.insert(userId);
210 }
211 }
212 return userIds;
213 }
214 } // namespace AppExecFwk
215 } // namespace OHOS