1600cc4afSopenharmony_ci/*
2600cc4afSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License.
5600cc4afSopenharmony_ci * You may obtain a copy of the License at
6600cc4afSopenharmony_ci *
7600cc4afSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8600cc4afSopenharmony_ci *
9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and
13600cc4afSopenharmony_ci * limitations under the License.
14600cc4afSopenharmony_ci */
15600cc4afSopenharmony_ci
16600cc4afSopenharmony_ci#include "bundle_installer.h"
17600cc4afSopenharmony_ci
18600cc4afSopenharmony_ci#include <cinttypes>
19600cc4afSopenharmony_ci
20600cc4afSopenharmony_ci#include "app_log_tag_wrapper.h"
21600cc4afSopenharmony_ci#include "bundle_mgr_service.h"
22600cc4afSopenharmony_ci
23600cc4afSopenharmony_cinamespace OHOS {
24600cc4afSopenharmony_cinamespace AppExecFwk {
25600cc4afSopenharmony_ciBundleInstaller::BundleInstaller(const int64_t installerId, const sptr<IStatusReceiver> &statusReceiver)
26600cc4afSopenharmony_ci    : installerId_(installerId), statusReceiver_(statusReceiver)
27600cc4afSopenharmony_ci{
28600cc4afSopenharmony_ci    LOG_NOFUNC_I(BMS_TAG_INSTALLER, "create bundle installer instance id:%{public}" PRId64 "", installerId_);
29600cc4afSopenharmony_ci}
30600cc4afSopenharmony_ci
31600cc4afSopenharmony_ciBundleInstaller::~BundleInstaller()
32600cc4afSopenharmony_ci{
33600cc4afSopenharmony_ci    LOG_NOFUNC_I(BMS_TAG_INSTALLER, "destroy installer id:%{public}" PRId64 "", installerId_);
34600cc4afSopenharmony_ci}
35600cc4afSopenharmony_ci
36600cc4afSopenharmony_civoid BundleInstaller::Install(const std::string &bundleFilePath, const InstallParam &installParam)
37600cc4afSopenharmony_ci{
38600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
39600cc4afSopenharmony_ci    if (installParam.userId == Constants::ALL_USERID) {
40600cc4afSopenharmony_ci        auto userInstallParam = installParam;
41600cc4afSopenharmony_ci        userInstallParam.allUser = true;
42600cc4afSopenharmony_ci        for (auto userId : GetExistsCommonUserIds()) {
43600cc4afSopenharmony_ci            userInstallParam.userId = userId;
44600cc4afSopenharmony_ci            userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
45600cc4afSopenharmony_ci            resultCode = InstallBundle(
46600cc4afSopenharmony_ci                bundleFilePath, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
47600cc4afSopenharmony_ci            ResetInstallProperties();
48600cc4afSopenharmony_ci        }
49600cc4afSopenharmony_ci
50600cc4afSopenharmony_ci        NotifyAllBundleStatus();
51600cc4afSopenharmony_ci    } else {
52600cc4afSopenharmony_ci        resultCode = InstallBundle(
53600cc4afSopenharmony_ci            bundleFilePath, installParam, Constants::AppType::THIRD_PARTY_APP);
54600cc4afSopenharmony_ci        InstallDriverForAllUsers(bundleFilePath, installParam);
55600cc4afSopenharmony_ci    }
56600cc4afSopenharmony_ci    std::string resultMsg = GetCheckResultMsg();
57600cc4afSopenharmony_ci    SetCheckResultMsg("");
58600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
59600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, resultMsg);
60600cc4afSopenharmony_ci    }
61600cc4afSopenharmony_ci}
62600cc4afSopenharmony_ci
63600cc4afSopenharmony_civoid BundleInstaller::Recover(const std::string &bundleName, const InstallParam &installParam)
64600cc4afSopenharmony_ci{
65600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
66600cc4afSopenharmony_ci    if (installParam.userId == Constants::ALL_USERID) {
67600cc4afSopenharmony_ci        auto userInstallParam = installParam;
68600cc4afSopenharmony_ci        for (auto userId : GetExistsCommonUserIds()) {
69600cc4afSopenharmony_ci            userInstallParam.userId = userId;
70600cc4afSopenharmony_ci            userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
71600cc4afSopenharmony_ci            resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
72600cc4afSopenharmony_ci            ResetInstallProperties();
73600cc4afSopenharmony_ci        }
74600cc4afSopenharmony_ci    } else {
75600cc4afSopenharmony_ci        resultCode = BaseBundleInstaller::Recover(bundleName, installParam);
76600cc4afSopenharmony_ci        RecoverDriverForAllUsers(bundleName, installParam);
77600cc4afSopenharmony_ci    }
78600cc4afSopenharmony_ci
79600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
80600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
81600cc4afSopenharmony_ci    }
82600cc4afSopenharmony_ci}
83600cc4afSopenharmony_ci
84600cc4afSopenharmony_civoid BundleInstaller::Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam)
85600cc4afSopenharmony_ci{
86600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
87600cc4afSopenharmony_ci    if (installParam.userId == Constants::ALL_USERID) {
88600cc4afSopenharmony_ci        auto userInstallParam = installParam;
89600cc4afSopenharmony_ci        userInstallParam.allUser = true;
90600cc4afSopenharmony_ci        for (auto userId : GetExistsCommonUserIds()) {
91600cc4afSopenharmony_ci            userInstallParam.userId = userId;
92600cc4afSopenharmony_ci            userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
93600cc4afSopenharmony_ci            resultCode = InstallBundle(
94600cc4afSopenharmony_ci                bundleFilePaths, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
95600cc4afSopenharmony_ci            ResetInstallProperties();
96600cc4afSopenharmony_ci        }
97600cc4afSopenharmony_ci
98600cc4afSopenharmony_ci        NotifyAllBundleStatus();
99600cc4afSopenharmony_ci    } else {
100600cc4afSopenharmony_ci        resultCode = InstallBundle(bundleFilePaths, installParam, Constants::AppType::THIRD_PARTY_APP);
101600cc4afSopenharmony_ci        InstallDriverForAllUsers(bundleFilePaths, installParam);
102600cc4afSopenharmony_ci    }
103600cc4afSopenharmony_ci    std::string resultMsg = GetCheckResultMsg();
104600cc4afSopenharmony_ci    SetCheckResultMsg("");
105600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
106600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, resultMsg);
107600cc4afSopenharmony_ci    }
108600cc4afSopenharmony_ci}
109600cc4afSopenharmony_ci
110600cc4afSopenharmony_civoid BundleInstaller::InstallByBundleName(const std::string &bundleName, const InstallParam &installParam)
111600cc4afSopenharmony_ci{
112600cc4afSopenharmony_ci    ErrCode resultCode = InstallBundleByBundleName(bundleName, installParam);
113600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
114600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
115600cc4afSopenharmony_ci    }
116600cc4afSopenharmony_ci}
117600cc4afSopenharmony_ci
118600cc4afSopenharmony_civoid BundleInstaller::Uninstall(const std::string &bundleName, const InstallParam &installParam)
119600cc4afSopenharmony_ci{
120600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
121600cc4afSopenharmony_ci    if (installParam.userId == Constants::ALL_USERID ||
122600cc4afSopenharmony_ci        (!installParam.isRemoveUser && HasDriverExtensionAbility(bundleName))) {
123600cc4afSopenharmony_ci        std::vector<ErrCode> errCode;
124600cc4afSopenharmony_ci        auto userInstallParam = installParam;
125600cc4afSopenharmony_ci        for (auto userId : GetExistsCommonUserIds()) {
126600cc4afSopenharmony_ci            userInstallParam.userId = userId;
127600cc4afSopenharmony_ci            resultCode = UninstallBundle(bundleName, userInstallParam);
128600cc4afSopenharmony_ci            errCode.push_back(resultCode);
129600cc4afSopenharmony_ci            ResetInstallProperties();
130600cc4afSopenharmony_ci        }
131600cc4afSopenharmony_ci        if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
132600cc4afSopenharmony_ci            for (const auto &err : errCode) {
133600cc4afSopenharmony_ci                if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
134600cc4afSopenharmony_ci                    err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
135600cc4afSopenharmony_ci                    resultCode = err;
136600cc4afSopenharmony_ci                    break;
137600cc4afSopenharmony_ci                }
138600cc4afSopenharmony_ci                resultCode = ERR_OK;
139600cc4afSopenharmony_ci            }
140600cc4afSopenharmony_ci        } else {
141600cc4afSopenharmony_ci            resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
142600cc4afSopenharmony_ci        }
143600cc4afSopenharmony_ci    } else {
144600cc4afSopenharmony_ci        resultCode = UninstallBundle(bundleName, installParam);
145600cc4afSopenharmony_ci    }
146600cc4afSopenharmony_ci
147600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
148600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
149600cc4afSopenharmony_ci    }
150600cc4afSopenharmony_ci}
151600cc4afSopenharmony_ci
152600cc4afSopenharmony_civoid BundleInstaller::Uninstall(const UninstallParam &uninstallParam)
153600cc4afSopenharmony_ci{
154600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
155600cc4afSopenharmony_ci    resultCode = UninstallBundleByUninstallParam(uninstallParam);
156600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
157600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
158600cc4afSopenharmony_ci    }
159600cc4afSopenharmony_ci}
160600cc4afSopenharmony_ci
161600cc4afSopenharmony_civoid BundleInstaller::Uninstall(
162600cc4afSopenharmony_ci    const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam)
163600cc4afSopenharmony_ci{
164600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
165600cc4afSopenharmony_ci    if (installParam.userId == Constants::ALL_USERID || HasDriverExtensionAbility(bundleName)) {
166600cc4afSopenharmony_ci        std::vector<ErrCode> errCode;
167600cc4afSopenharmony_ci        auto userInstallParam = installParam;
168600cc4afSopenharmony_ci        for (auto userId : GetExistsCommonUserIds()) {
169600cc4afSopenharmony_ci            userInstallParam.userId = userId;
170600cc4afSopenharmony_ci            resultCode = UninstallBundle(bundleName, modulePackage, userInstallParam);
171600cc4afSopenharmony_ci            errCode.push_back(resultCode);
172600cc4afSopenharmony_ci            ResetInstallProperties();
173600cc4afSopenharmony_ci        }
174600cc4afSopenharmony_ci        if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
175600cc4afSopenharmony_ci            for (const auto &err : errCode) {
176600cc4afSopenharmony_ci                if (!(err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE ||
177600cc4afSopenharmony_ci                    err == ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_MODULE ||
178600cc4afSopenharmony_ci                    err == ERR_APPEXECFWK_USER_NOT_INSTALL_HAP || err == ERR_OK)) {
179600cc4afSopenharmony_ci                    resultCode = err;
180600cc4afSopenharmony_ci                    break;
181600cc4afSopenharmony_ci                }
182600cc4afSopenharmony_ci                resultCode = ERR_OK;
183600cc4afSopenharmony_ci            }
184600cc4afSopenharmony_ci        } else {
185600cc4afSopenharmony_ci            resultCode = (errCode.size() > 0) ? errCode[0] : ERR_OK;
186600cc4afSopenharmony_ci        }
187600cc4afSopenharmony_ci    } else {
188600cc4afSopenharmony_ci        resultCode = UninstallBundle(bundleName, modulePackage, installParam);
189600cc4afSopenharmony_ci    }
190600cc4afSopenharmony_ci
191600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
192600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
193600cc4afSopenharmony_ci    }
194600cc4afSopenharmony_ci}
195600cc4afSopenharmony_ci
196600cc4afSopenharmony_civoid BundleInstaller::UpdateInstallerState(const InstallerState state)
197600cc4afSopenharmony_ci{
198600cc4afSopenharmony_ci    LOG_I(BMS_TAG_INSTALLER, "state: %{public}d", static_cast<int32_t>(state));
199600cc4afSopenharmony_ci    SetInstallerState(state);
200600cc4afSopenharmony_ci    if (statusReceiver_) {
201600cc4afSopenharmony_ci        statusReceiver_->OnStatusNotify(static_cast<int>(state));
202600cc4afSopenharmony_ci    }
203600cc4afSopenharmony_ci}
204600cc4afSopenharmony_ci
205600cc4afSopenharmony_cistd::set<int32_t> BundleInstaller::GetExistsCommonUserIds()
206600cc4afSopenharmony_ci{
207600cc4afSopenharmony_ci    std::set<int32_t> userIds;
208600cc4afSopenharmony_ci    auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
209600cc4afSopenharmony_ci    if (dataMgr == nullptr) {
210600cc4afSopenharmony_ci        LOG_E(BMS_TAG_INSTALLER, "Get dataMgr shared_ptr nullptr");
211600cc4afSopenharmony_ci        return userIds;
212600cc4afSopenharmony_ci    }
213600cc4afSopenharmony_ci
214600cc4afSopenharmony_ci    for (auto userId : dataMgr->GetAllUser()) {
215600cc4afSopenharmony_ci        if (userId >= Constants::START_USERID) {
216600cc4afSopenharmony_ci            userIds.insert(userId);
217600cc4afSopenharmony_ci        }
218600cc4afSopenharmony_ci    }
219600cc4afSopenharmony_ci    return userIds;
220600cc4afSopenharmony_ci}
221600cc4afSopenharmony_ci
222600cc4afSopenharmony_civoid BundleInstaller::InstallDriverForAllUsers(const std::string &bundleFilePath, const InstallParam &installParam)
223600cc4afSopenharmony_ci{
224600cc4afSopenharmony_ci    std::vector<std::string> bundleFilePaths { bundleFilePath };
225600cc4afSopenharmony_ci    InstallDriverForAllUsers(bundleFilePaths, installParam);
226600cc4afSopenharmony_ci}
227600cc4afSopenharmony_ci
228600cc4afSopenharmony_civoid BundleInstaller::InstallDriverForAllUsers(const std::vector<std::string> &bundleFilePaths,
229600cc4afSopenharmony_ci    const InstallParam &installParam)
230600cc4afSopenharmony_ci{
231600cc4afSopenharmony_ci    std::string bundleName = GetCurrentBundleName();
232600cc4afSopenharmony_ci    if (!HasDriverExtensionAbility(bundleName)) {
233600cc4afSopenharmony_ci        APP_LOGD("bundle %{public}s has no driver extension ability", bundleName.c_str());
234600cc4afSopenharmony_ci        return;
235600cc4afSopenharmony_ci    }
236600cc4afSopenharmony_ci    APP_LOGI("bundle %{public}s has driver extension ability, need to install for all users", bundleName.c_str());
237600cc4afSopenharmony_ci    ResetInstallProperties();
238600cc4afSopenharmony_ci    auto userInstallParam = installParam;
239600cc4afSopenharmony_ci    userInstallParam.allUser = true;
240600cc4afSopenharmony_ci    for (auto userId : GetExistsCommonUserIds()) {
241600cc4afSopenharmony_ci        if (userId == installParam.userId) {
242600cc4afSopenharmony_ci            continue;
243600cc4afSopenharmony_ci        }
244600cc4afSopenharmony_ci        userInstallParam.userId = userId;
245600cc4afSopenharmony_ci        userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
246600cc4afSopenharmony_ci        ErrCode resultCode = InstallBundle(
247600cc4afSopenharmony_ci            bundleFilePaths, userInstallParam, Constants::AppType::THIRD_PARTY_APP);
248600cc4afSopenharmony_ci        ResetInstallProperties();
249600cc4afSopenharmony_ci        if (resultCode != ERR_OK) {
250600cc4afSopenharmony_ci            APP_LOGE("install driver for user %{public}d failed, resultCode: %{public}d", userId, resultCode);
251600cc4afSopenharmony_ci        }
252600cc4afSopenharmony_ci    }
253600cc4afSopenharmony_ci    NotifyAllBundleStatus();
254600cc4afSopenharmony_ci}
255600cc4afSopenharmony_ci
256600cc4afSopenharmony_civoid BundleInstaller::RecoverDriverForAllUsers(const std::string &bundleName, const InstallParam &installParam)
257600cc4afSopenharmony_ci{
258600cc4afSopenharmony_ci    if (!HasDriverExtensionAbility(bundleName)) {
259600cc4afSopenharmony_ci        APP_LOGD("bundle %{public}s has no driver extension ability", bundleName.c_str());
260600cc4afSopenharmony_ci        return;
261600cc4afSopenharmony_ci    }
262600cc4afSopenharmony_ci    APP_LOGI("bundle %{public}s has driver extension ability, need to recover for all users", bundleName.c_str());
263600cc4afSopenharmony_ci    ResetInstallProperties();
264600cc4afSopenharmony_ci    auto userInstallParam = installParam;
265600cc4afSopenharmony_ci    for (auto userId : GetExistsCommonUserIds()) {
266600cc4afSopenharmony_ci        if (userId == installParam.userId) {
267600cc4afSopenharmony_ci            continue;
268600cc4afSopenharmony_ci        }
269600cc4afSopenharmony_ci        userInstallParam.userId = userId;
270600cc4afSopenharmony_ci        userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
271600cc4afSopenharmony_ci        ErrCode resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
272600cc4afSopenharmony_ci        ResetInstallProperties();
273600cc4afSopenharmony_ci        if (resultCode != ERR_OK) {
274600cc4afSopenharmony_ci            APP_LOGE("recover driver for user %{public}d failed, resultCode: %{public}d", userId, resultCode);
275600cc4afSopenharmony_ci        }
276600cc4afSopenharmony_ci    }
277600cc4afSopenharmony_ci}
278600cc4afSopenharmony_ci
279600cc4afSopenharmony_civoid BundleInstaller::UninstallAndRecover(const std::string &bundleName, const InstallParam &installParam)
280600cc4afSopenharmony_ci{
281600cc4afSopenharmony_ci    ErrCode resultCode = ERR_OK;
282600cc4afSopenharmony_ci    std::vector<ErrCode> errCode;
283600cc4afSopenharmony_ci    auto userInstallParam = installParam;
284600cc4afSopenharmony_ci    std::vector<int32_t> userIds;
285600cc4afSopenharmony_ci    auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
286600cc4afSopenharmony_ci    if (dataMgr != nullptr) {
287600cc4afSopenharmony_ci        userIds = dataMgr->GetUserIds(bundleName);
288600cc4afSopenharmony_ci    }
289600cc4afSopenharmony_ci    for (auto userId : userIds) {
290600cc4afSopenharmony_ci        userInstallParam.userId = userId;
291600cc4afSopenharmony_ci        userInstallParam.SetIsUninstallAndRecover(true);
292600cc4afSopenharmony_ci        resultCode = UninstallBundle(bundleName, userInstallParam);
293600cc4afSopenharmony_ci        errCode.push_back(resultCode);
294600cc4afSopenharmony_ci        ResetInstallProperties();
295600cc4afSopenharmony_ci    }
296600cc4afSopenharmony_ci    for (auto userId : userIds) {
297600cc4afSopenharmony_ci        userInstallParam.userId = userId;
298600cc4afSopenharmony_ci        userInstallParam.installFlag = InstallFlag::REPLACE_EXISTING;
299600cc4afSopenharmony_ci        resultCode = BaseBundleInstaller::Recover(bundleName, userInstallParam);
300600cc4afSopenharmony_ci        errCode.push_back(resultCode);
301600cc4afSopenharmony_ci        ResetInstallProperties();
302600cc4afSopenharmony_ci    }
303600cc4afSopenharmony_ci
304600cc4afSopenharmony_ci    if (std::find(errCode.begin(), errCode.end(), ERR_OK) != errCode.end()) {
305600cc4afSopenharmony_ci        for (const auto &err : errCode) {
306600cc4afSopenharmony_ci            if (err != ERR_OK) {
307600cc4afSopenharmony_ci                resultCode = err;
308600cc4afSopenharmony_ci                break;
309600cc4afSopenharmony_ci            }
310600cc4afSopenharmony_ci            resultCode = ERR_OK;
311600cc4afSopenharmony_ci        }
312600cc4afSopenharmony_ci    } else {
313600cc4afSopenharmony_ci        resultCode = (errCode.size() > 0) ? errCode[0] : ERR_APPEXECFWK_UNINSTALL_MISSING_INSTALLED_BUNDLE;
314600cc4afSopenharmony_ci    }
315600cc4afSopenharmony_ci    if (statusReceiver_ != nullptr) {
316600cc4afSopenharmony_ci        statusReceiver_->OnFinished(resultCode, "");
317600cc4afSopenharmony_ci    }
318600cc4afSopenharmony_ci}
319600cc4afSopenharmony_ci}  // namespace AppExecFwk
320600cc4afSopenharmony_ci}  // namespace OHOS