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