1 /*
2  * Copyright (c) 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_stream_installer_proxy.h"
17 
18 #include <unistd.h>
19 
20 #include "app_log_tag_wrapper.h"
21 #include "app_log_wrapper.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
BundleStreamInstallerProxy(const sptr<IRemoteObject> & object)26 BundleStreamInstallerProxy::BundleStreamInstallerProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<IBundleStreamInstaller>(object)
28 {
29     LOG_D(BMS_TAG_INSTALLER, "create bundle stream installer proxy instance");
30 }
31 
~BundleStreamInstallerProxy()32 BundleStreamInstallerProxy:: ~BundleStreamInstallerProxy()
33 {
34     LOG_D(BMS_TAG_INSTALLER, "destory bundle stream installer proxy instance");
35 }
36 
CreateStream(const std::string & fileName)37 int32_t BundleStreamInstallerProxy::CreateStream(const std::string &fileName)
38 {
39     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create stream begin");
40     int32_t fd = Constants::DEFAULT_STREAM_FD;
41     if (fileName.empty()) {
42         LOG_E(BMS_TAG_INSTALLER, "BundleStreamInstallerProxy create stream faile due to empty fileName");
43         return fd;
44     }
45     MessageParcel data;
46     if (!data.WriteInterfaceToken(BundleStreamInstallerProxy::GetDescriptor())) {
47         LOG_E(BMS_TAG_INSTALLER, "fail to CreateStream due to write interface token fail");
48         return fd;
49     }
50     if (!data.WriteString(fileName)) {
51         LOG_E(BMS_TAG_INSTALLER, "fail to CreateStream due to write fileName fail");
52         return fd;
53     }
54 
55     MessageParcel reply;
56     if (!SendStreamInstallRequest(BundleStreamInstallerInterfaceCode::CREATE_STREAM, data, reply)) {
57         LOG_E(BMS_TAG_INSTALLER, "fail to SendStreamInstallRequest");
58         return fd;
59     }
60 
61     int32_t sharedFd = reply.ReadFileDescriptor();
62     if (sharedFd < 0) {
63         LOG_E(BMS_TAG_INSTALLER, "fail to CreateStream");
64         return fd;
65     }
66 
67     fd = dup(sharedFd);
68     close(sharedFd);
69 
70     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create stream end");
71     return fd;
72 }
73 
CreateSignatureFileStream(const std::string & moduleName,const std::string & fileName)74 int32_t BundleStreamInstallerProxy::CreateSignatureFileStream(const std::string &moduleName,
75     const std::string &fileName)
76 {
77     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create signature file stream begin");
78     int32_t fd = Constants::DEFAULT_STREAM_FD;
79     if (moduleName.empty() || fileName.empty()) {
80         LOG_E(BMS_TAG_INSTALLER, "BundleStreamInstallerProxy create stream faile due to empty fileName or moduleName");
81         return fd;
82     }
83     MessageParcel data;
84     if (!data.WriteInterfaceToken(BundleStreamInstallerProxy::GetDescriptor())) {
85         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSignatureFileStream due to write interface token fail");
86         return fd;
87     }
88     if (!data.WriteString(moduleName)) {
89         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSignatureFileStream due to write moduleName fail");
90         return fd;
91     }
92     if (!data.WriteString(fileName)) {
93         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSignatureFileStream due to write fileName fail");
94         return fd;
95     }
96     MessageParcel reply;
97     if (!SendStreamInstallRequest(BundleStreamInstallerInterfaceCode::CREATE_SIGNATURE_FILE_STREAM, data, reply)) {
98         LOG_E(BMS_TAG_INSTALLER, "fail to SendStreamInstallRequest");
99         return fd;
100     }
101 
102     int32_t sharedFd = reply.ReadFileDescriptor();
103     if (sharedFd < 0) {
104         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSignatureFileStream");
105         return fd;
106     }
107 
108     fd = dup(sharedFd);
109     close(sharedFd);
110 
111     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create stream end");
112     return fd;
113 }
114 
CreateSharedBundleStream(const std::string & hspName,uint32_t index)115 int32_t BundleStreamInstallerProxy::CreateSharedBundleStream(const std::string &hspName, uint32_t index)
116 {
117     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create shared bundle stream begin");
118     int32_t fd = Constants::DEFAULT_STREAM_FD;
119     if (hspName.empty()) {
120         LOG_E(BMS_TAG_INSTALLER, "BundleStreamInstallerProxy create shared bundle stream faile due to empty hspName");
121         return fd;
122     }
123     MessageParcel data;
124     if (!data.WriteInterfaceToken(BundleStreamInstallerProxy::GetDescriptor())) {
125         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSharedBundleStream due to write interface token fail");
126         return fd;
127     }
128     if (!data.WriteString(hspName)) {
129         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSharedBundleStream due to write hspName fail");
130         return fd;
131     }
132     if (!data.WriteUint32(index)) {
133         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSharedBundleStream due to write sharedBundleIdx fail");
134         return fd;
135     }
136 
137     MessageParcel reply;
138     if (!SendStreamInstallRequest(BundleStreamInstallerInterfaceCode::CREATE_SHARED_BUNDLE_STREAM, data, reply)) {
139         LOG_E(BMS_TAG_INSTALLER, "fail to SendStreamInstallRequest");
140         return fd;
141     }
142 
143     int32_t sharedFd = reply.ReadFileDescriptor();
144     if (sharedFd < 0) {
145         LOG_E(BMS_TAG_INSTALLER, "fail to CreateSharedBundleStream");
146         return fd;
147     }
148 
149     fd = dup(sharedFd);
150     close(sharedFd);
151 
152     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy create shared bundle stream end");
153     return fd;
154 }
155 
CreatePgoFileStream(const std::string & moduleName,const std::string & fileName)156 int32_t BundleStreamInstallerProxy::CreatePgoFileStream(const std::string &moduleName,
157     const std::string &fileName)
158 {
159     LOG_D(BMS_TAG_INSTALLER, "create pgo file stream begin");
160     int32_t fd = Constants::DEFAULT_STREAM_FD;
161     if (moduleName.empty() || fileName.empty()) {
162         LOG_E(BMS_TAG_INSTALLER, "create stream faile due to empty fileName or moduleName");
163         return fd;
164     }
165     MessageParcel data;
166     if (!data.WriteInterfaceToken(BundleStreamInstallerProxy::GetDescriptor())) {
167         LOG_E(BMS_TAG_INSTALLER, "fail to CreatePgoFileStream due to write interface token fail");
168         return fd;
169     }
170     if (!data.WriteString(moduleName)) {
171         LOG_E(BMS_TAG_INSTALLER, "fail to CreatePgoFileStream due to write moduleName fail");
172         return fd;
173     }
174     if (!data.WriteString(fileName)) {
175         LOG_E(BMS_TAG_INSTALLER, "fail to CreatePgoFileStream due to write fileName fail");
176         return fd;
177     }
178     MessageParcel reply;
179     if (!SendStreamInstallRequest(BundleStreamInstallerInterfaceCode::CREATE_PGO_FILE_STREAM, data, reply)) {
180         LOG_E(BMS_TAG_INSTALLER, "fail to SendStreamInstallRequest");
181         return fd;
182     }
183 
184     int32_t sharedFd = reply.ReadFileDescriptor();
185     if (sharedFd < 0) {
186         LOG_E(BMS_TAG_INSTALLER, "fail to CreatePgoFileStream");
187         return fd;
188     }
189 
190     fd = dup(sharedFd);
191     close(sharedFd);
192 
193     LOG_D(BMS_TAG_INSTALLER, "create pgo file stream end");
194     return fd;
195 }
196 
Install()197 bool BundleStreamInstallerProxy::Install()
198 {
199     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy install begin");
200     MessageParcel data;
201     if (!data.WriteInterfaceToken(BundleStreamInstallerProxy::GetDescriptor())) {
202         LOG_E(BMS_TAG_INSTALLER, "fail to Install due to write interface token fail");
203         return false;
204     }
205 
206     MessageParcel reply;
207     bool res = SendStreamInstallRequest(BundleStreamInstallerInterfaceCode::STREAM_INSTALL, data, reply);
208     if (!res) {
209         LOG_E(BMS_TAG_INSTALLER, "fail to SendStreamInstallRequest");
210         return res;
211     }
212     LOG_D(BMS_TAG_INSTALLER, "bundle stream installer proxy install end");
213     return true;
214 }
215 
GetInstallerId() const216 uint32_t BundleStreamInstallerProxy::GetInstallerId() const
217 {
218     return installerId_;
219 }
220 
SetInstallerId(uint32_t installerId)221 void BundleStreamInstallerProxy::SetInstallerId(uint32_t installerId)
222 {
223     installerId_ = installerId;
224 }
225 
SendStreamInstallRequest(BundleStreamInstallerInterfaceCode code,MessageParcel & data,MessageParcel & reply)226 bool BundleStreamInstallerProxy::SendStreamInstallRequest(BundleStreamInstallerInterfaceCode code, MessageParcel& data,
227     MessageParcel& reply)
228 {
229     sptr<IRemoteObject> remote = Remote();
230     if (remote == nullptr) {
231         LOG_E(BMS_TAG_INSTALLER, "fail to send request, for remote is nullptr");
232         return false;
233     }
234 
235     MessageOption option(MessageOption::TF_SYNC);
236     int32_t ret = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
237     if (ret != NO_ERROR) {
238         LOG_E(BMS_TAG_INSTALLER, "fail to sendRequest, for transact is failed and error code is: %{public}d", ret);
239         return false;
240     }
241     return true;
242 }
243 } // AppExecFwk
244 } // OHOS