• 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.h"
17 
18 #include <cinttypes>
19 
20 #include "app_log_tag_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     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "create bundle installer instance id:%{public}" PRId64 "", installerId_);
29 }
30 
~BundleInstaller()31 BundleInstaller::~BundleInstaller()
32 {
33     LOG_NOFUNC_I(BMS_TAG_INSTALLER, "destroy installer id:%{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         InstallForAllUsers(installParam);
55     }
56     std::string resultMsg = GetCheckResultMsg();
57     SetCheckResultMsg("");
58     if (statusReceiver_ != nullptr) {
59         statusReceiver_->OnFinished(resultCode, resultMsg);
60     }
61 }
62 
Recover(const std::string & bundleName,const InstallParam & installParam)63 void BundleInstaller::Recover(const std::string &bundleName, const InstallParam &installParam)
64 {
65     ErrCode resultCode = ERR_OK;
66     if (installParam.userId == Constants::ALL_USERID) {
67         auto userInstallParam = installParam;
68         for (auto userId : GetExistsCommonUserIds()) {
69             userInstallParam.userId = userId;
70             userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
71             resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
72             ResetInstallProperties();
73         }
74     } else {
75         resultCode = BaseBundleInstaller::Recover(bundleName, installParam);
76         RecoverDriverForAllUsers(bundleName, installParam);
77     }
78 
79     if (statusReceiver_ != nullptr) {
80         statusReceiver_->OnFinished(resultCode, "");
81     }
82 }
83 
Install(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam)84 void BundleInstaller::Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam)
85 {
86     ErrCode resultCode = ERR_OK;
87     if (installParam.userId == Constants::ALL_USERID) {
88         auto userInstallParam = installParam;
89         userInstallParam.allUser = true;
90         for (auto userId : GetExistsCommonUserIds()) {
91             userInstallParam.userId = userId;
92             userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
93             resultCode = InstallBundle(
94                 bundleFilePaths, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
95             ResetInstallProperties();
96         }
97 
98         NotifyAllBundleStatus();
99     } else {
100         resultCode = InstallBundle(bundleFilePaths, installParam, Constants::AppType::THIRD_PARTY_APP);
101         InstallForAllUsers(installParam);
102     }
103     std::string resultMsg = GetCheckResultMsg();
104     SetCheckResultMsg("");
105     if (statusReceiver_ != nullptr) {
106         statusReceiver_->OnFinished(resultCode, resultMsg);
107     }
108 }
109 
InstallByBundleName(const std::string & bundleName,const InstallParam & installParam)110 void BundleInstaller::InstallByBundleName(const std::string &bundleName, const InstallParam &installParam)
111 {
112     ErrCode resultCode = InstallBundleByBundleName(bundleName, installParam);
113     if (statusReceiver_ != nullptr) {
114         statusReceiver_->OnFinished(resultCode, "");
115     }
116 }
117 
Uninstall(const std::string & bundleName,const InstallParam & installParam)118 void BundleInstaller::Uninstall(const std::string &bundleName, const InstallParam &installParam)
119 {
120     ErrCode resultCode = ERR_OK;
121     bool isDriver = IsDriverForAllUser(bundleName);
122     int32_t userId = GetDriverInstallUser(bundleName);
123     if (isDriver && (userId == Constants::DEFAULT_USERID || userId == Constants::U1)) {
124         InstallParam param = installParam;
125         param.userId = userId;
126         resultCode = UninstallBundle(bundleName, param);
127         if (statusReceiver_ != nullptr) {
128             statusReceiver_->OnFinished(resultCode, "");
129         }
130         return;
131     }
132     if (installParam.userId == Constants::ALL_USERID ||
133         (!installParam.isRemoveUser && isDriver)) {
134         std::vector<ErrCode> errCode;
135         auto userInstallParam = installParam;
136         for (auto userId : GetExistsCommonUserIds()) {
137             userInstallParam.userId = userId;
138             resultCode = UninstallBundle(bundleName, userInstallParam);
139             errCode.push_back(resultCode);
140             ResetInstallProperties();
141         }
142         if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
143             for (const auto &err : errCode) {
144                 if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
145                     err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
146                     resultCode = err;
147                     break;
148                 }
149                 resultCode = ERR_OK;
150             }
151         } else {
152             resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
153         }
154     } else {
155         resultCode = UninstallBundle(bundleName, installParam);
156     }
157 
158     if (statusReceiver_ != nullptr) {
159         statusReceiver_->OnFinished(resultCode, "");
160     }
161 }
162 
Uninstall(const UninstallParam & uninstallParam)163 void BundleInstaller::Uninstall(const UninstallParam &uninstallParam)
164 {
165     ErrCode resultCode = ERR_OK;
166     resultCode = UninstallBundleByUninstallParam(uninstallParam);
167     if (statusReceiver_ != nullptr) {
168         statusReceiver_->OnFinished(resultCode, "");
169     }
170 }
171 
Uninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam)172 void BundleInstaller::Uninstall(
173     const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam)
174 {
175     ErrCode resultCode = ERR_OK;
176     bool isDriver = IsDriverForAllUser(bundleName);
177     int32_t userId = GetDriverInstallUser(bundleName);
178     if (isDriver && (userId == Constants::DEFAULT_USERID || userId == Constants::U1)) {
179         InstallParam param = installParam;
180         param.userId = userId;
181         resultCode = UninstallBundle(bundleName, modulePackage, param);
182         if (statusReceiver_ != nullptr) {
183             statusReceiver_->OnFinished(resultCode, "");
184         }
185         return;
186     }
187     if (installParam.userId == Constants::ALL_USERID || isDriver) {
188         std::vector<ErrCode> errCode;
189         auto userInstallParam = installParam;
190         for (auto userId : GetExistsCommonUserIds()) {
191             userInstallParam.userId = userId;
192             resultCode = UninstallBundle(bundleName, modulePackage, userInstallParam);
193             errCode.push_back(resultCode);
194             ResetInstallProperties();
195         }
196         if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
197             for (const auto &err : errCode) {
198                 if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
199                     err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_MODULE ||
200                     err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
201                     resultCode = err;
202                     break;
203                 }
204                 resultCode = ERR_OK;
205             }
206         } else {
207             resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
208         }
209     } else {
210         resultCode = UninstallBundle(bundleName, modulePackage, installParam);
211     }
212 
213     if (statusReceiver_ != nullptr) {
214         statusReceiver_->OnFinished(resultCode, "");
215     }
216 }
217 
UpdateInstallerState(const InstallerState state)218 void BundleInstaller::UpdateInstallerState(const InstallerState state)
219 {
220     LOG_D(BMS_TAG_INSTALLER, "state: %{public}d", static_cast<int32_t>(state));
221     SetInstallerState(state);
222     if (statusReceiver_) {
223         statusReceiver_->OnStatusNotify(static_cast<int>(state));
224     }
225 }
226 
GetExistsCommonUserIds()227 std::set<int32_t> BundleInstaller::GetExistsCommonUserIds()
228 {
229     std::set<int32_t> userIds;
230     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
231     if (dataMgr == nullptr) {
232         LOG_E(BMS_TAG_INSTALLER, "Get dataMgr shared_ptr nullptr");
233         return userIds;
234     }
235 
236     for (auto userId : dataMgr->GetAllUser()) {
237         if (userId >= Constants::START_USERID) {
238             userIds.insert(userId);
239         }
240     }
241     return userIds;
242 }
243 
InstallForAllUsers(const InstallParam & installParam)244 void BundleInstaller::InstallForAllUsers(const InstallParam &installParam)
245 {
246     std::string bundleName = GetCurrentBundleName();
247     if (!IsDriverForAllUser(bundleName) && !IsEnterpriseForAllUser(installParam, bundleName)) {
248         APP_LOGD("bundle %{public}s has no driver extension ability", bundleName.c_str());
249         return;
250     }
251     APP_LOGI("bundle %{public}s need to install for all users", bundleName.c_str());
252     ResetInstallProperties();
253     auto userInstallParam = installParam;
254     for (auto userId : GetExistsCommonUserIds()) {
255         if (userId == installParam.userId) {
256             continue;
257         }
258         userInstallParam.userId = userId;
259         userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
260         ErrCode resultCode = InstallBundleByBundleName(bundleName, userInstallParam);
261         ResetInstallProperties();
262         if (resultCode != ERR_OK) {
263             APP_LOGE("install for user %{public}d failed, resultCode: %{public}d", userId, resultCode);
264         }
265     }
266     NotifyAllBundleStatus();
267 }
268 
RecoverDriverForAllUsers(const std::string & bundleName,const InstallParam & installParam)269 void BundleInstaller::RecoverDriverForAllUsers(const std::string &bundleName, const InstallParam &installParam)
270 {
271     if (!IsDriverForAllUser(bundleName)) {
272         APP_LOGD("bundle %{public}s has no driver extension ability", bundleName.c_str());
273         return;
274     }
275     APP_LOGI("bundle %{public}s has driver extension ability, need to recover for all users", bundleName.c_str());
276     ResetInstallProperties();
277     auto userInstallParam = installParam;
278     for (auto userId : GetExistsCommonUserIds()) {
279         if (userId == installParam.userId) {
280             continue;
281         }
282         userInstallParam.userId = userId;
283         userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
284         ErrCode resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
285         ResetInstallProperties();
286         if (resultCode != ERR_OK) {
287             APP_LOGE("recover driver for user %{public}d failed, resultCode: %{public}d", userId, resultCode);
288         }
289     }
290 }
291 
UninstallAndRecover(const std::string & bundleName,const InstallParam & installParam)292 void BundleInstaller::UninstallAndRecover(const std::string &bundleName, const InstallParam &installParam)
293 {
294     ErrCode resultCode = ERR_OK;
295     std::vector<ErrCode> errCode;
296     auto userInstallParam = installParam;
297     std::vector<int32_t> userIds;
298     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
299     if (dataMgr != nullptr) {
300         userIds = dataMgr->GetUserIds(bundleName);
301     }
302     for (auto userId : userIds) {
303         userInstallParam.userId = userId;
304         userInstallParam.SetIsUninstallAndRecover(true);
305         resultCode = UninstallBundle(bundleName, userInstallParam);
306         errCode.push_back(resultCode);
307         ResetInstallProperties();
308     }
309     for (auto userId : userIds) {
310         userInstallParam.userId = userId;
311         userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
312         resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
313         errCode.push_back(resultCode);
314         ResetInstallProperties();
315     }
316 
317     if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
318         for (const auto &err : errCode) {
319             if (err != ERR_OK) {
320                 resultCode = err;
321                 break;
322             }
323             resultCode = ERR_OK;
324         }
325     } else {
326         resultCode = (errCode.size() > 0) ? errCode[0] : ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE;
327     }
328     if (statusReceiver_ != nullptr) {
329         statusReceiver_->OnFinished(resultCode, "");
330     }
331 }
332 }  // namespace AppExecFwk
333 }  // namespace OHOS