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