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_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
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_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
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 (userIdSet.empty() || (userIdSet.find(Constants::DEFAULT_USERID) != userIdSet.end())) {
114         // for singleton hap or no user
115         userIdSet = dataMgr->GetAllUser();
116     } else {
117         // for non-singleton hap
118         userIdSet.insert(Constants::DEFAULT_USERID);
119     }
120     ErrCode result = ERR_OK;
121     for (auto userId : userIdSet) {
122         APP_LOGI_NOFUNC("start ota install -n %{public}s -u %{public}d", bundleName.c_str(), userId);
123         installParam.userId = userId;
124         MarkPreBundleSyeEventBootTag(false);
125         otaInstall_ = true;
126         ErrCode errCode = InstallBundle(filePaths, installParam, appType);
127         if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
128             APP_LOGE("install system bundle %{public}s fail err %{public}d", bundleName.c_str(), errCode);
129             result = errCode;
130             BmsKeyEventMgr::ProcessMainBundleInstallFailed(bundleName, result);
131         }
132         ResetInstallProperties();
133     }
134 
135     return result;
136 }
137 
UninstallSystemBundle(const std::string &bundleName)138 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName)
139 {
140     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
141     if (dataMgr == nullptr) {
142         APP_LOGE("Get dataMgr shared_ptr nullptr");
143         return false;
144     }
145     BundleType type;
146     if (dataMgr->GetBundleType(bundleName, type) && (type == BundleType::APP_SERVICE_FWK)) {
147         AppServiceFwkInstaller installer;
148         return installer.UnInstall(bundleName) == ERR_OK;
149     }
150 
151     InstallParam installParam;
152     for (auto userId : dataMgr->GetAllUser()) {
153         installParam.userId = userId;
154         installParam.needSavePreInstallInfo = true;
155         installParam.isPreInstallApp = true;
156         installParam.SetKillProcess(false);
157         installParam.needSendEvent = false;
158         installParam.isKeepData = true;
159         MarkPreBundleSyeEventBootTag(false);
160         ErrCode result = UninstallBundle(bundleName, installParam);
161         if (result != ERR_OK) {
162             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
163         }
164 
165         ResetInstallProperties();
166     }
167     return true;
168 }
169 
UninstallSystemBundle(const std::string &bundleName, bool isKeepData)170 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, bool isKeepData)
171 {
172     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
173     if (dataMgr == nullptr) {
174         APP_LOGE("Get dataMgr shared_ptr nullptr");
175         return false;
176     }
177 
178     InstallParam installParam;
179     for (auto userId : dataMgr->GetAllUser()) {
180         installParam.userId = userId;
181         installParam.needSavePreInstallInfo = true;
182         installParam.isPreInstallApp = true;
183         installParam.SetKillProcess(false);
184         installParam.needSendEvent = false;
185         installParam.isKeepData = isKeepData;
186         MarkPreBundleSyeEventBootTag(false);
187         ErrCode result = UninstallBundle(bundleName, installParam);
188         if (result != ERR_OK) {
189             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
190         }
191 
192         ResetInstallProperties();
193     }
194     return true;
195 }
196 
UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)197 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)
198 {
199     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
200     if (dataMgr == nullptr) {
201         APP_LOGE("Get dataMgr shared_ptr nullptr");
202         return false;
203     }
204 
205     InstallParam installParam;
206     for (auto userId : dataMgr->GetAllUser()) {
207         installParam.userId = userId;
208         installParam.needSavePreInstallInfo = true;
209         installParam.isPreInstallApp = true;
210         installParam.SetKillProcess(false);
211         installParam.needSendEvent = false;
212         installParam.isKeepData = true;
213         MarkPreBundleSyeEventBootTag(false);
214         ErrCode result = UninstallBundle(bundleName, modulePackage, installParam);
215         if (result != ERR_OK) {
216             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
217         }
218 
219         ResetInstallProperties();
220     }
221     CheckUninstallSystemHsp(bundleName);
222 
223     return true;
224 }
225 
UninstallSystemBundle(const std::string &bundleName, const InstallParam &installParam)226 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const InstallParam &installParam)
227 {
228     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
229     if (dataMgr == nullptr) {
230         APP_LOGE("Get dataMgr shared_ptr nullptr");
231         return false;
232     }
233     MarkPreBundleSyeEventBootTag(false);
234     ErrCode result = UninstallBundle(bundleName, installParam);
235     if ((result != ERR_OK) && (result != ERR_APPEXECFWK_USER_NOT_INSTALL_HAP)) {
236         APP_LOGW("uninstall system bundle %{public}s userId %{public}d fail, error: %{public}d", bundleName.c_str(),
237             installParam.userId, result);
238         return false;
239     }
240     return true;
241 }
242 
CheckUninstallSystemHsp(const std::string &bundleName)243 void SystemBundleInstaller::CheckUninstallSystemHsp(const std::string &bundleName)
244 {
245     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
246     if (dataMgr == nullptr) {
247         APP_LOGE("Get dataMgr shared_ptr nullptr");
248         return;
249     }
250     InnerBundleInfo info;
251     if (!(dataMgr->FetchInnerBundleInfo(bundleName, info))) {
252         APP_LOGD("bundleName %{public}s not existed local", bundleName.c_str());
253         return;
254     }
255     if (info.GetApplicationBundleType() != BundleType::APP_SERVICE_FWK) {
256         APP_LOGD("bundleName %{public}s is not a system hsp", bundleName.c_str());
257         return;
258     }
259     bool isExistHsp = false;
260     for (const auto &item : info.GetInnerModuleInfos()) {
261         if (item.second.distro.moduleType == "shared") {
262             isExistHsp = true;
263             return;
264         }
265     }
266     if (!isExistHsp) {
267         InstallParam installParam;
268         installParam.userId = Constants::DEFAULT_USERID;
269         installParam.needSavePreInstallInfo = true;
270         installParam.isPreInstallApp = true;
271         installParam.SetKillProcess(false);
272         installParam.needSendEvent = false;
273         installParam.isKeepData = true;
274         MarkPreBundleSyeEventBootTag(false);
275         ErrCode result = UninstallBundle(bundleName, installParam);
276         if (result != ERR_OK) {
277             APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
278             return;
279         }
280         PreInstallBundleInfo preInstallBundleInfo;
281         if ((dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo))) {
282             dataMgr->DeletePreInstallBundleInfo(bundleName, preInstallBundleInfo);
283         }
284     }
285 }
286 }  // namespace AppExecFwk
287 }  // namespace OHOS
288