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 "bundle_mgr_service.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
SystemBundleInstaller()23 SystemBundleInstaller::SystemBundleInstaller()
24 {
25 APP_LOGI("system bundle installer instance is created");
26 }
27
~SystemBundleInstaller()28 SystemBundleInstaller::~SystemBundleInstaller()
29 {
30 APP_LOGI("system bundle installer instance is destroyed");
31 }
32
InstallSystemBundle(const std::string & filePath,InstallParam & installParam,Constants::AppType appType)33 ErrCode SystemBundleInstaller::InstallSystemBundle(
34 const std::string &filePath,
35 InstallParam &installParam,
36 Constants::AppType appType)
37 {
38 MarkPreBundleSyeEventBootTag(true);
39 ErrCode result = InstallBundle(filePath, installParam, appType);
40 if (result != ERR_OK) {
41 APP_LOGE("install system bundle fail, error: %{public}d", result);
42 }
43 return result;
44 }
45
InstallSystemSharedBundle(InstallParam & installParam,bool isOTA,Constants::AppType appType)46 bool SystemBundleInstaller::InstallSystemSharedBundle(
47 InstallParam &installParam,
48 bool isOTA,
49 Constants::AppType appType)
50 {
51 MarkPreBundleSyeEventBootTag(!isOTA);
52 std::vector<std::string> bundlePaths{};
53 ErrCode result = InstallBundle(bundlePaths, installParam, appType);
54 if (result != ERR_OK) {
55 APP_LOGE("install system bundle fail, error: %{public}d", result);
56 return false;
57 }
58 return true;
59 }
60
OTAInstallSystemBundle(const std::vector<std::string> & filePaths,InstallParam & installParam,Constants::AppType appType)61 ErrCode SystemBundleInstaller::OTAInstallSystemBundle(
62 const std::vector<std::string> &filePaths,
63 InstallParam &installParam,
64 Constants::AppType appType)
65 {
66 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
67 if (dataMgr == nullptr) {
68 APP_LOGE("Get dataMgr shared_ptr nullptr");
69 return ERR_APPEXECFWK_INSTALL_BUNDLE_MGR_SERVICE_ERROR;
70 }
71
72 ErrCode result = ERR_OK;
73 for (auto allUserId : dataMgr->GetAllUser()) {
74 installParam.userId = allUserId;
75 MarkPreBundleSyeEventBootTag(false);
76 otaInstall_ = true;
77 ErrCode errCode = InstallBundle(filePaths, installParam, appType);
78 if ((errCode != ERR_OK) && (errCode != ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON)) {
79 APP_LOGE("install system bundle fail, error: %{public}d", errCode);
80 result = errCode;
81 }
82 ResetInstallProperties();
83 }
84
85 return result;
86 }
87
UninstallSystemBundle(const std::string & bundleName)88 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName)
89 {
90 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
91 if (dataMgr == nullptr) {
92 APP_LOGE("Get dataMgr shared_ptr nullptr");
93 return false;
94 }
95
96 InstallParam installParam;
97 for (auto userId : dataMgr->GetAllUser()) {
98 installParam.userId = userId;
99 installParam.needSavePreInstallInfo = true;
100 installParam.isPreInstallApp = true;
101 installParam.noSkipsKill = false;
102 installParam.needSendEvent = false;
103 MarkPreBundleSyeEventBootTag(false);
104 ErrCode result = UninstallBundle(bundleName, installParam);
105 if (result != ERR_OK) {
106 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
107 }
108
109 ResetInstallProperties();
110 }
111 return true;
112 }
113
UninstallSystemBundle(const std::string & bundleName,bool isKeepData)114 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, bool isKeepData)
115 {
116 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
117 if (dataMgr == nullptr) {
118 APP_LOGE("Get dataMgr shared_ptr nullptr!");
119 return false;
120 }
121
122 InstallParam installParam;
123 for (auto userId : dataMgr->GetAllUser()) {
124 installParam.userId = userId;
125 installParam.needSavePreInstallInfo = true;
126 installParam.isPreInstallApp = true;
127 installParam.noSkipsKill = false;
128 installParam.needSendEvent = false;
129 installParam.isKeepData = isKeepData;
130 MarkPreBundleSyeEventBootTag(false);
131 ErrCode result = UninstallBundle(bundleName, installParam);
132 if (result != ERR_OK) {
133 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
134 }
135
136 ResetInstallProperties();
137 }
138 return true;
139 }
140
UninstallSystemBundle(const std::string & bundleName,const std::string & modulePackage)141 bool SystemBundleInstaller::UninstallSystemBundle(const std::string &bundleName, const std::string &modulePackage)
142 {
143 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
144 if (dataMgr == nullptr) {
145 APP_LOGE("Get dataMgr shared_ptr nullptr");
146 return false;
147 }
148
149 InstallParam installParam;
150 for (auto userId : dataMgr->GetAllUser()) {
151 installParam.userId = userId;
152 installParam.needSavePreInstallInfo = true;
153 installParam.isPreInstallApp = true;
154 installParam.noSkipsKill = false;
155 installParam.needSendEvent = false;
156 MarkPreBundleSyeEventBootTag(false);
157 ErrCode result = UninstallBundle(bundleName, modulePackage, installParam);
158 if (result != ERR_OK) {
159 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
160 }
161
162 ResetInstallProperties();
163 }
164 CheckUninstallSystemHsp(bundleName);
165
166 return true;
167 }
168
CheckUninstallSystemHsp(const std::string & bundleName)169 void SystemBundleInstaller::CheckUninstallSystemHsp(const std::string &bundleName)
170 {
171 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
172 if (dataMgr == nullptr) {
173 APP_LOGE("Get dataMgr shared_ptr nullptr");
174 return;
175 }
176 InnerBundleInfo info;
177 if (!(dataMgr->FetchInnerBundleInfo(bundleName, info))) {
178 APP_LOGD("bundleName %{public}s not existed local", bundleName.c_str());
179 return;
180 }
181 if (info.GetApplicationBundleType() != BundleType::APP_SERVICE_FWK) {
182 APP_LOGD("bundleName %{public}s is not a system hsp", bundleName.c_str());
183 return;
184 }
185 bool isExistHsp = false;
186 for (const auto &item : info.GetInnerModuleInfos()) {
187 if (item.second.distro.moduleType == "shared") {
188 isExistHsp = true;
189 return;
190 }
191 }
192 if (!isExistHsp) {
193 InstallParam installParam;
194 installParam.userId = Constants::DEFAULT_USERID;
195 installParam.needSavePreInstallInfo = true;
196 installParam.isPreInstallApp = true;
197 installParam.noSkipsKill = false;
198 installParam.needSendEvent = false;
199 installParam.isKeepData = true;
200 MarkPreBundleSyeEventBootTag(false);
201 ErrCode result = UninstallBundle(bundleName, installParam);
202 if (result != ERR_OK) {
203 APP_LOGW("uninstall system bundle fail, error: %{public}d", result);
204 return;
205 }
206 PreInstallBundleInfo preInstallBundleInfo;
207 if ((dataMgr->GetPreInstallBundleInfo(bundleName, preInstallBundleInfo))) {
208 dataMgr->DeletePreInstallBundleInfo(bundleName, preInstallBundleInfo);
209 }
210 }
211 }
212 } // namespace AppExecFwk
213 } // namespace OHOS
214