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