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