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 "bundle_installer_host.h"
17
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 #include "app_log_wrapper.h"
22 #include "appexecfwk_errors.h"
23 #include "bundle_constants.h"
24 #include "bundle_permission_mgr.h"
25 #include "bundle_util.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const std::string INSTALL_THREAD = "install_thread";
31 const std::string GET_MANAGER_FAIL = "fail to get bundle installer manager";
32 } // namespace
33
BundleInstallerHost()34 BundleInstallerHost::BundleInstallerHost()
35 {
36 APP_LOGI("create bundle installer host instance");
37 }
38
~BundleInstallerHost()39 BundleInstallerHost::~BundleInstallerHost()
40 {
41 APP_LOGI("destroy bundle installer host instance");
42 }
43
Init()44 bool BundleInstallerHost::Init()
45 {
46 APP_LOGD("begin to init");
47 auto installRunner = EventRunner::Create(INSTALL_THREAD);
48 if (!installRunner) {
49 APP_LOGE("create install runner fail");
50 return false;
51 }
52 manager_ = std::make_shared<BundleInstallerManager>(installRunner);
53 APP_LOGD("init successfully");
54 return true;
55 }
56
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int BundleInstallerHost::OnRemoteRequest(
58 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
59 {
60 APP_LOGD("bundle installer host onReceived message, the message code is %{public}u", code);
61 std::u16string descripter = GetDescriptor();
62 std::u16string remoteDescripter = data.ReadInterfaceToken();
63 if (descripter != remoteDescripter) {
64 APP_LOGE("fail to write reply message in bundle mgr host due to the reply is nullptr");
65 return OBJECT_NULL;
66 }
67
68 switch (code) {
69 case static_cast<uint32_t>(IBundleInstaller::Message::INSTALL):
70 HandleInstallMessage(data);
71 break;
72 case static_cast<uint32_t>(IBundleInstaller::Message::INSTALL_MULTIPLE_HAPS):
73 HandleInstallMultipleHapsMessage(data);
74 break;
75 case static_cast<uint32_t>(IBundleInstaller::Message::UNINSTALL):
76 HandleUninstallMessage(data);
77 break;
78 case static_cast<uint32_t>(IBundleInstaller::Message::UNINSTALL_MODULE):
79 HandleUninstallModuleMessage(data);
80 break;
81 case static_cast<uint32_t>(IBundleInstaller::Message::RECOVER):
82 HandleRecoverMessage(data);
83 break;
84 default:
85 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
86 }
87 return NO_ERROR;
88 }
89
HandleInstallMessage(Parcel & data)90 void BundleInstallerHost::HandleInstallMessage(Parcel &data)
91 {
92 APP_LOGD("handle install message");
93 std::string bundlePath = Str16ToStr8(data.ReadString16());
94 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
95 if (!installParam) {
96 APP_LOGE("ReadParcelable<InstallParam> failed");
97 return;
98 }
99 sptr<IRemoteObject> object = data.ReadObject<IRemoteObject>();
100 if (object == nullptr) {
101 APP_LOGE("read failed");
102 return;
103 }
104 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
105
106 Install(bundlePath, *installParam, statusReceiver);
107 APP_LOGD("handle install message finished");
108 }
109
110
HandleRecoverMessage(Parcel & data)111 void BundleInstallerHost::HandleRecoverMessage(Parcel &data)
112 {
113 APP_LOGD("handle install message by bundleName");
114 std::string bundleName = Str16ToStr8(data.ReadString16());
115 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
116 if (!installParam) {
117 APP_LOGE("ReadParcelable<InstallParam> failed");
118 return;
119 }
120 sptr<IRemoteObject> object = data.ReadObject<IRemoteObject>();
121 if (object == nullptr) {
122 APP_LOGE("read failed");
123 return;
124 }
125 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
126
127 Recover(bundleName, *installParam, statusReceiver);
128 APP_LOGD("handle install message by bundleName finished");
129 }
130
HandleInstallMultipleHapsMessage(Parcel & data)131 void BundleInstallerHost::HandleInstallMultipleHapsMessage(Parcel &data)
132 {
133 APP_LOGD("handle install multiple haps message");
134 int32_t size = data.ReadInt32();
135 std::vector<std::string> pathVec;
136 for (int i = 0; i < size; ++i) {
137 std::string path(Str16ToStr8(data.ReadString16()));
138 if (std::find(pathVec.begin(), pathVec.end(), path) == pathVec.end()) {
139 pathVec.emplace_back(path);
140 }
141 }
142 if (size == 0 || pathVec.empty()) {
143 APP_LOGE("inputted bundlepath vector is empty");
144 return;
145 }
146 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
147 if (!installParam) {
148 APP_LOGE("ReadParcelable<InstallParam> failed");
149 return;
150 }
151 sptr<IRemoteObject> object = data.ReadObject<IRemoteObject>();
152 if (object == nullptr) {
153 APP_LOGE("read failed");
154 return;
155 }
156 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
157
158 Install(pathVec, *installParam, statusReceiver);
159 APP_LOGD("handle install multiple haps finished");
160 }
161
HandleUninstallMessage(Parcel & data)162 void BundleInstallerHost::HandleUninstallMessage(Parcel &data)
163 {
164 APP_LOGD("handle uninstall message");
165 std::string bundleName = Str16ToStr8(data.ReadString16());
166 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
167 if (!installParam) {
168 APP_LOGE("ReadParcelable<InstallParam> failed");
169 return;
170 }
171 sptr<IRemoteObject> object = data.ReadObject<IRemoteObject>();
172 if (object == nullptr) {
173 APP_LOGE("read failed");
174 return;
175 }
176 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
177
178 Uninstall(bundleName, *installParam, statusReceiver);
179 APP_LOGD("handle uninstall message finished");
180 }
181
HandleUninstallModuleMessage(Parcel & data)182 void BundleInstallerHost::HandleUninstallModuleMessage(Parcel &data)
183 {
184 APP_LOGD("handle uninstall module message");
185 std::string bundleName = Str16ToStr8(data.ReadString16());
186 std::string modulePackage = Str16ToStr8(data.ReadString16());
187 std::unique_ptr<InstallParam> installParam(data.ReadParcelable<InstallParam>());
188 if (!installParam) {
189 APP_LOGE("ReadParcelable<InstallParam> failed");
190 return;
191 }
192 sptr<IRemoteObject> object = data.ReadObject<IRemoteObject>();
193 if (object == nullptr) {
194 APP_LOGE("read failed");
195 return;
196 }
197 sptr<IStatusReceiver> statusReceiver = iface_cast<IStatusReceiver>(object);
198
199 Uninstall(bundleName, modulePackage, *installParam, statusReceiver);
200 APP_LOGD("handle uninstall message finished");
201 }
202
Install(const std::string & bundleFilePath,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)203 bool BundleInstallerHost::Install(
204 const std::string &bundleFilePath, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
205 {
206 if (!CheckBundleInstallerManager(statusReceiver)) {
207 APP_LOGE("statusReceiver invalid");
208 return false;
209 }
210 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
211 APP_LOGE("install permission denied");
212 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
213 return false;
214 }
215
216 manager_->CreateInstallTask(bundleFilePath, CheckInstallParam(installParam), statusReceiver);
217 return true;
218 }
219
Install(const std::vector<std::string> & bundleFilePaths,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)220 bool BundleInstallerHost::Install(const std::vector<std::string> &bundleFilePaths, const InstallParam &installParam,
221 const sptr<IStatusReceiver> &statusReceiver)
222 {
223 if (!CheckBundleInstallerManager(statusReceiver)) {
224 APP_LOGE("statusReceiver invalid");
225 return false;
226 }
227 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
228 APP_LOGE("install permission denied");
229 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
230 return false;
231 }
232
233 manager_->CreateInstallTask(bundleFilePaths, CheckInstallParam(installParam), statusReceiver);
234 return true;
235 }
236
Recover(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)237 bool BundleInstallerHost::Recover(
238 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
239 {
240 if (!CheckBundleInstallerManager(statusReceiver)) {
241 APP_LOGE("statusReceiver invalid");
242 return false;
243 }
244 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
245 APP_LOGE("install permission denied");
246 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_PERMISSION_DENIED, "");
247 return false;
248 }
249 manager_->CreateRecoverTask(bundleName, CheckInstallParam(installParam), statusReceiver);
250 return true;
251 }
252
Uninstall(const std::string & bundleName,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)253 bool BundleInstallerHost::Uninstall(
254 const std::string &bundleName, const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
255 {
256 if (!CheckBundleInstallerManager(statusReceiver)) {
257 APP_LOGE("statusReceiver invalid");
258 return false;
259 }
260 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
261 APP_LOGE("uninstall permission denied");
262 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_PERMISSION_DENIED, "");
263 return false;
264 }
265 manager_->CreateUninstallTask(bundleName, CheckInstallParam(installParam), statusReceiver);
266 return true;
267 }
268
Uninstall(const std::string & bundleName,const std::string & modulePackage,const InstallParam & installParam,const sptr<IStatusReceiver> & statusReceiver)269 bool BundleInstallerHost::Uninstall(const std::string &bundleName, const std::string &modulePackage,
270 const InstallParam &installParam, const sptr<IStatusReceiver> &statusReceiver)
271 {
272 if (!CheckBundleInstallerManager(statusReceiver)) {
273 APP_LOGE("statusReceiver invalid");
274 return false;
275 }
276 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
277 APP_LOGE("uninstall permission denied");
278 statusReceiver->OnFinished(ERR_APPEXECFWK_UNINSTALL_PERMISSION_DENIED, "");
279 return false;
280 }
281 manager_->CreateUninstallTask(
282 bundleName, modulePackage, CheckInstallParam(installParam), statusReceiver);
283 return true;
284 }
285
CheckBundleInstallerManager(const sptr<IStatusReceiver> & statusReceiver) const286 bool BundleInstallerHost::CheckBundleInstallerManager(const sptr<IStatusReceiver> &statusReceiver) const
287 {
288 if (!statusReceiver) {
289 APP_LOGE("the receiver is nullptr");
290 return false;
291 }
292 if (!manager_) {
293 APP_LOGE("the bundle installer manager is nullptr");
294 statusReceiver->OnFinished(ERR_APPEXECFWK_INSTALL_INTERNAL_ERROR, GET_MANAGER_FAIL);
295 return false;
296 }
297 return true;
298 }
299
CheckInstallParam(const InstallParam & installParam)300 InstallParam BundleInstallerHost::CheckInstallParam(const InstallParam &installParam)
301 {
302 if (installParam.userId == Constants::UNSPECIFIED_USERID) {
303 APP_LOGI("installParam userId is unspecified and get calling userId by callingUid");
304 InstallParam callInstallParam = installParam;
305 callInstallParam.userId = BundleUtil::GetUserIdByCallingUid();
306 return callInstallParam;
307 }
308
309 return installParam;
310 }
311
312 } // namespace AppExecFwk
313 } // namespace OHOS