1 /*
2 * Copyright (c) 2022-2023 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_stream_installer_host.h"
17
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_framework_core_ipc_interface_code.h"
21 #include "bundle_memory_guard.h"
22 #include "ipc_types.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
BundleStreamInstallerHost()26 BundleStreamInstallerHost::BundleStreamInstallerHost()
27 {
28 APP_LOGD("create bundle stream installer host instance");
29 init();
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int BundleStreamInstallerHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
33 MessageOption &option)
34 {
35 BundleMemoryGuard memoryGuard;
36 APP_LOGD("bundle stream installer host onReceived message, the message code is %{public}u", code);
37 std::u16string descriptor = BundleStreamInstallerHost::GetDescriptor();
38 std::u16string remoteDescriptor = data.ReadInterfaceToken();
39 if (descriptor != remoteDescriptor) {
40 APP_LOGW("[OnRemoteRequest] fail: invalid interface token!");
41 return OBJECT_NULL;
42 }
43
44 if (funcMap_.find(code) == funcMap_.end()) {
45 APP_LOGW("[OnRemoteRequest] fail: unknown code!");
46 return IRemoteStub<IBundleStreamInstaller>::OnRemoteRequest(code, data, reply, option);
47 }
48
49 return funcMap_[code](data, reply);
50 }
51
HandleCreateStream(MessageParcel & data,MessageParcel & reply)52 ErrCode BundleStreamInstallerHost::HandleCreateStream(MessageParcel &data, MessageParcel &reply)
53 {
54 std::string fileName = data.ReadString();
55 if (fileName.empty()) {
56 APP_LOGE("HandleCreateStream param fileName is empty");
57 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
58 }
59 int32_t fd = CreateStream(fileName);
60 if (!reply.WriteFileDescriptor(fd)) {
61 APP_LOGE("write fd failed");
62 return ERR_APPEXECFWK_PARCEL_ERROR;
63 }
64 return ERR_OK;
65 }
66
HandleCreateSignatureFileStream(MessageParcel & data,MessageParcel & reply)67 ErrCode BundleStreamInstallerHost::HandleCreateSignatureFileStream(MessageParcel &data, MessageParcel &reply)
68 {
69 std::string moduleName = data.ReadString();
70 std::string fileName = data.ReadString();
71 if (moduleName.empty() || fileName.empty()) {
72 APP_LOGE("HandleCreateSignatureFileStream params are invalid");
73 return ERR_APPEXECFWK_INSTALL_PARAM_ERROR;
74 }
75 int32_t fd = CreateSignatureFileStream(moduleName, fileName);
76 if (!reply.WriteFileDescriptor(fd)) {
77 APP_LOGE("write fd failed");
78 return ERR_APPEXECFWK_PARCEL_ERROR;
79 }
80 return ERR_OK;
81 }
82
HandleCreateSharedBundleStream(MessageParcel & data,MessageParcel & reply)83 ErrCode BundleStreamInstallerHost::HandleCreateSharedBundleStream(MessageParcel &data, MessageParcel &reply)
84 {
85 std::string hspName = data.ReadString();
86 uint32_t sharedBundleIdx = data.ReadUint32();
87 int32_t fd = CreateSharedBundleStream(hspName, sharedBundleIdx);
88 if (!reply.WriteFileDescriptor(fd)) {
89 APP_LOGE("write fd failed");
90 return ERR_APPEXECFWK_PARCEL_ERROR;
91 }
92 return ERR_OK;
93 }
94
HandleInstall(MessageParcel & data,MessageParcel & reply)95 ErrCode BundleStreamInstallerHost::HandleInstall(MessageParcel &data, MessageParcel &reply)
96 {
97 if (!Install()) {
98 reply.WriteBool(false);
99 APP_LOGE("stream install failed");
100 return ERR_APPEXECFWK_PARCEL_ERROR;
101 }
102 reply.WriteBool(true);
103 return ERR_OK;
104 }
105
init()106 void BundleStreamInstallerHost::init()
107 {
108 funcMap_.emplace(static_cast<uint32_t>(BundleStreamInstallerInterfaceCode::CREATE_STREAM),
109 [this](MessageParcel &data, MessageParcel &reply)->ErrCode {
110 return this->HandleCreateStream(data, reply);
111 });
112 funcMap_.emplace(static_cast<uint32_t>(BundleStreamInstallerInterfaceCode::CREATE_SHARED_BUNDLE_STREAM),
113 [this](MessageParcel &data, MessageParcel &reply)->ErrCode {
114 return this->HandleCreateSharedBundleStream(data, reply);
115 });
116 funcMap_.emplace(static_cast<uint32_t>(BundleStreamInstallerInterfaceCode::STREAM_INSTALL),
117 [this](MessageParcel &data, MessageParcel &reply)->ErrCode {
118 return this->HandleInstall(data, reply);
119 });
120 funcMap_.emplace(static_cast<uint32_t>(BundleStreamInstallerInterfaceCode::CREATE_SIGNATURE_FILE_STREAM),
121 [this](MessageParcel &data, MessageParcel &reply)->ErrCode {
122 return this->HandleCreateSignatureFileStream(data, reply);
123 });
124 }
125 } // AppExecFwk
126 } // OHOS