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