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