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 "system_bundle_installer.h"
17
18 #include "app_log_wrapper.h"
19 #include "app_service_fwk_installer.h"
20 #include "bms_key_event_mgr.h"
21 #include "bundle_mgr_service.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
SystemBundleInstaller()25 SystemBundleInstaller::SystemBundleInstaller()
26 {
27 APP_LOGD("system bundle installer instance is created");
28 }
29
~SystemBundleInstaller()30 SystemBundleInstaller::~SystemBundleInstaller()
31 {
32 APP_LOGD("system bundle installer instance is destroyed");
33 }
34
InstallSystemBundle(const std::string & filePath,InstallParam & installParam,Constants::AppType appType)35 ErrCode SystemBundleInstaller::InstallSystemBundle(
36 const std::string &filePath,
37 InstallParam &installParam,
38 Constants::AppType appType)
39 {
40 MarkPreBundleSyeEventBootTag(true);
41 ErrCode result = InstallBundle(filePath, installParam, appType);
42 if (result != ERR_OK) {
43 APP_LOGE("install system bundle fail, error: %{public}d", result);
44 if (result != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON) {
45 BmsKeyEventMgr::ProcessMainBundleInstallFailed(filePath, result);
46 }
47 }
48 return result;
49 }
50
InstallSystemSharedBundle(InstallParam & installParam,bool isOTA,Constants::AppType appType)51 bool SystemBundleInstaller::InstallSystemSharedBundle(
52 InstallParam &installParam,
53 bool isOTA,
54 Constants::AppType appType)
55 {
56 MarkPreBundleSyeEventBootTag(!isOTA);
57 std::vector<std::string> bundlePaths{};
58 ErrCode result = InstallBundle(bundlePaths, installParam, appType);
59 if (result != ERR_OK) {
60 APP_LOGE("install system bundle fail, error: %{public}d", result);
61 return false;
62 }
63 return true;
64 }
65
OTAInstallSystemBundle(const std::vector<std::string> & filePaths,InstallParam & installParam,Constants::AppType appType)66 ErrCode SystemBundleInstaller::OTAInstallSystemBundle(
67 const std::vector<std::string> &filePaths,
68 InstallParam &installParam,
69 Constants::AppType appType)
70 {
71 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
72 if (dataMgr == nullptr) {
73 APP_LOGE("Get dataMgr shared_ptr nullptr");
74 return ERR_APPEXECFWK_NULL_PTR;
75 }
76
77 ErrCode result = ERR_OK;
78 for (auto allUserId : dataMgr->GetAllUser()) {
79 installParam.userId = allUserId;
80 MarkPreBundleSyeEventBootTag(false);
81 otaInstall_ = true;
82 ErrCode errCode = InstallBundle(filePaths, installParam, appType);
83 if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
84 APP_LOGE("install system bundle fail, error: %{public}d", errCode);
85 result = errCode;
86 if (!filePaths.empty()) {
87 BmsKeyEventMgr::ProcessMainBundleInstallFailed(filePaths[0], result);
88 }
89 }
90 ResetInstallProperties();
91 }
92
93 return result;
94 }
95
OTAInstallSystemBundleNeedCheckUser(const std::vector<std::string> & filePaths,InstallParam & installParam,const std::string & bundleName,Constants::AppType appType)96 ErrCode SystemBundleInstaller::OTAInstallSystemBundleNeedCheckUser(
97 const std::vector<std::string> &filePaths,
98 InstallParam &installParam,
99 const std::string &bundleName,
100 Constants::AppType appType)
101 {
102 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
103 if (dataMgr == nullptr) {
104 APP_LOGE("Get dataMgr shared_ptr nullptr");
105 return ERR_APPEXECFWK_NULL_PTR;
106 }
107
108 auto currentBundleUserIds = dataMgr->GetUserIds(bundleName);
109 std::set<int32_t> userIdSet;
110 for (auto userId : currentBundleUserIds) {
111 userIdSet.insert(userId);
112 }
113 if (!installParam.removable) {
114 PreInstallBundleInfo preInstallBundleInfo;
115 if (!(dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo)) ||
116 !preInstallBundleInfo.HasForceUninstalledUser(Constants::START_USERID)) {
117 userIdSet.insert(Constants::START_USERID);
118 }
119 }
120 if (userIdSet.empty() || (userIdSet.find(Constants::DEFAULT_USERID) != userIdSet.end())) {
121 // for singleton hap or no user
122 userIdSet = dataMgr->GetAllUser();
123 } else {
124 // for non-singleton hap
125 userIdSet.insert(Constants::DEFAULT_USERID);
126 }
127 ErrCode result = ERR_OK;
128 for (auto userId : userIdSet) {
129 APP_LOGI_NOFUNC("start ota install -n %{public}s -u %{public}d", bundleName.c_str(), userId);
130 installParam.userId = userId;
131 MarkPreBundleSyeEventBootTag(false);
132 otaInstall_ = true;
133 ErrCode errCode = InstallBundle(filePaths, installParam, appType);
134 if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
135 APP_LOGE("install system bundle %{public}s fail err %{public}d", bundleName.c_str(), errCode);
136 result = errCode;
137 BmsKeyEventMgr::ProcessMainBundleInstallFailed(bundleName, result);
138 }
139 ResetInstallProperties();
140 }
141
142 return result;
143 }
144
OTAInstallSystemBundleTargetUser(const std::vector<std::string> & filePaths,InstallParam & installParam,const std::string & bundleName,Constants::AppType appType,const std::vector<int32_t> & userIds)145 ErrCode SystemBundleInstaller::OTAInstallSystemBundleTargetUser(const std::vector<std::string> &filePaths,
146 InstallParam &installParam, const std::string &bundleName, Constants::AppType appType,
147 const std::vector<int32_t> &userIds)
148 {
149 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
150 if (dataMgr == nullptr) {
151 APP_LOGE("Get dataMgr shared_ptr nullptr");
152 return ERR_APPEXECFWK_NULL_PTR;
153 }
154
155 std::set<int32_t> userIdSet;
156 for (auto userId : userIds) {
157 userIdSet.insert(userId);
158 }
159 if (!installParam.removable) {
160 PreInstallBundleInfo preInstallBundleInfo;
161 if (!(dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo)) ||
162 !preInstallBundleInfo.HasForceUninstalledUser(Constants::START_USERID)) {
163 userIdSet.insert(Constants::START_USERID);
164 }
165 }
166 if (userIdSet.empty() || (userIdSet.find(Constants::DEFAULT_USERID) != userIdSet.end())) {
167 // for singleton hap or no user
168 userIdSet = dataMgr->GetAllUser();
169 } else {
170 // for non-singleton hap
171 userIdSet.insert(Constants::DEFAULT_USERID);
172 }
173 ErrCode result = ERR_OK;
174 for (auto userId : userIdSet) {
175 APP_LOGI_NOFUNC("start ota install -n %{public}s -u %{public}d begin", bundleName.c_str(), userId);
176 installParam.userId = userId;
177 MarkPreBundleSyeEventBootTag(false);
178 otaInstall_ = true;
179 ErrCode errCode = InstallBundle(filePaths, installParam, appType);
180 if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
181 APP_LOGE("install system bundle %{public}s fail error %{public}d", bundleName.c_str(), errCode);
182 result = errCode;
183 BmsKeyEventMgr::ProcessMainBundleInstallFailed(bundleName, result);
184 }
185 ResetInstallProperties();
186 }
187
188 return result;
189 }
190
UninstallSystemBundle(const std::string & bundleName)191 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName)
192 {
193 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
194 if (dataMgr == nullptr) {
195 APP_LOGE("Get dataMgr shared_ptr nullptr");
196 return false;
197 }
198 BundleType type;
199 if (dataMgr->GetBundleType(bundleName, type) && (type == BundleType::APP_SERVICE_FWK)) {
200 AppServiceFwkInstaller installer;
201 return installer.UnInstall(bundleName) == ERR_OK;
202 }
203
204 InstallParam installParam;
205 for (auto userId : dataMgr->GetAllUser()) {
206 installParam.userId = userId;
207 installParam.needSavePreInstallInfo = true;
208 installParam.isPreInstallApp = true;
209 installParam.SetKillProcess(false);
210 installParam.needSendEvent = false;
211 installParam.isKeepData = true;
212 MarkPreBundleSyeEventBootTag(false);
213 ErrCode result = UninstallBundle(bundleName, installParam);
214 if (result != ERR_OK) {
215 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
216 }
217
218 ResetInstallProperties();
219 }
220 return true;
221 }
222
UninstallSystemBundle(const std::string & bundleName,bool isKeepData)223 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, bool isKeepData)
224 {
225 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
226 if (dataMgr == nullptr) {
227 APP_LOGE("Get dataMgr shared_ptr nullptr");
228 return false;
229 }
230
231 InstallParam installParam;
232 for (auto userId : dataMgr->GetAllUser()) {
233 installParam.userId = userId;
234 installParam.needSavePreInstallInfo = true;
235 installParam.isPreInstallApp = true;
236 installParam.SetKillProcess(false);
237 installParam.needSendEvent = false;
238 installParam.isKeepData = isKeepData;
239 MarkPreBundleSyeEventBootTag(false);
240 ErrCode result = UninstallBundle(bundleName, installParam);
241 if (result != ERR_OK) {
242 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
243 }
244
245 ResetInstallProperties();
246 }
247 return true;
248 }
249
UninstallSystemBundle(const std::string & bundleName,const std::string & modulePackage)250 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)
251 {
252 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
253 if (dataMgr == nullptr) {
254 APP_LOGE("Get dataMgr shared_ptr nullptr");
255 return false;
256 }
257
258 InstallParam installParam;
259 for (auto userId : dataMgr->GetAllUser()) {
260 installParam.userId = userId;
261 installParam.needSavePreInstallInfo = true;
262 installParam.isPreInstallApp = true;
263 installParam.SetKillProcess(false);
264 installParam.needSendEvent = false;
265 installParam.isKeepData = true;
266 MarkPreBundleSyeEventBootTag(false);
267 ErrCode result = UninstallBundle(bundleName, modulePackage, installParam);
268 if (result != ERR_OK) {
269 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
270 }
271
272 ResetInstallProperties();
273 }
274 CheckUninstallSystemHsp(bundleName);
275
276 return true;
277 }
278
UninstallSystemBundle(const std::string & bundleName,const InstallParam & installParam)279 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const InstallParam &installParam)
280 {
281 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
282 if (dataMgr == nullptr) {
283 APP_LOGE("Get dataMgr shared_ptr nullptr");
284 return false;
285 }
286 MarkPreBundleSyeEventBootTag(false);
287 ErrCode result = UninstallBundle(bundleName, installParam);
288 if ((result != ERR_OK) && (result != ERR_APPEXECFWK_USER_NOT_INSTALL_HAP)) {
289 APP_LOGW("uninstall system bundle %{public}s userId %{public}d fail, error: %{public}d", bundleName.c_str(),
290 installParam.userId, result);
291 return false;
292 }
293 return true;
294 }
295
CheckUninstallSystemHsp(const std::string & bundleName)296 void SystemBundleInstaller::CheckUninstallSystemHsp(const std::string &bundleName)
297 {
298 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
299 if (dataMgr == nullptr) {
300 APP_LOGE("Get dataMgr shared_ptr nullptr");
301 return;
302 }
303 InnerBundleInfo info;
304 if (!(dataMgr->FetchInnerBundleInfo(bundleName, info))) {
305 APP_LOGD("bundleName %{public}s not existed local", bundleName.c_str());
306 return;
307 }
308 if (info.GetApplicationBundleType() != BundleType::APP_SERVICE_FWK) {
309 APP_LOGD("bundleName %{public}s is not a system hsp", bundleName.c_str());
310 return;
311 }
312 bool isExistHsp = false;
313 for (const auto &item : info.GetInnerModuleInfos()) {
314 if (item.second.distro.moduleType == "shared") {
315 isExistHsp = true;
316 return;
317 }
318 }
319 if (!isExistHsp) {
320 InstallParam installParam;
321 installParam.userId = Constants::DEFAULT_USERID;
322 installParam.needSavePreInstallInfo = true;
323 installParam.isPreInstallApp = true;
324 installParam.SetKillProcess(false);
325 installParam.needSendEvent = false;
326 installParam.isKeepData = true;
327 MarkPreBundleSyeEventBootTag(false);
328 ErrCode result = UninstallBundle(bundleName, installParam);
329 if (result != ERR_OK) {
330 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
331 return;
332 }
333 PreInstallBundleInfo preInstallBundleInfo;
334 if ((dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo))) {
335 dataMgr->DeletePreInstallBundleInfo(bundleName, preInstallBundleInfo);
336 }
337 }
338 }
339 } // namespace AppExecFwk
340 } // namespace OHOS
341