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