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 "sys_installer_proxy.h"
17 
18 #include <string_ex.h>
19 
20 #include "log/log.h"
21 #include "securec.h"
22 #include "sys_installer_sa_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace SysInstaller {
26 using namespace Updater;
27 
SysInstallerInit()28 int32_t SysInstallerProxy::SysInstallerInit()
29 {
30     LOG(INFO) << "SysInstallerInit";
31     auto remote = Remote();
32     if (remote == nullptr) {
33         LOG(ERROR) << "Can not get remote";
34         return ERR_FLATTEN_OBJECT;
35     }
36 
37     MessageParcel data;
38     if (!data.WriteInterfaceToken(GetDescriptor())) {
39         return ERR_FLATTEN_OBJECT;
40     }
41     MessageParcel reply;
42     MessageOption option;
43     int32_t ret = remote->SendRequest(
44         static_cast<uint32_t>(SysInstallerInterfaceCode::SYS_INSTALLER_INIT), data, reply, option);
45     if (ret != ERR_OK) {
46         LOG(ERROR) << "SendRequest error";
47         return ERR_FLATTEN_OBJECT;
48     }
49 
50     return reply.ReadInt32();
51 }
52 
StartUpdatePackageZip(const std::string & pkgPath)53 int32_t SysInstallerProxy::StartUpdatePackageZip(const std::string &pkgPath)
54 {
55     LOG(INFO) << "StartUpdatePackageZip";
56     auto remote = Remote();
57     if (remote == nullptr) {
58         LOG(ERROR) << "Can not get remote";
59         return ERR_FLATTEN_OBJECT;
60     }
61 
62     MessageParcel data;
63     if (!data.WriteInterfaceToken(GetDescriptor())) {
64         LOG(ERROR) << "WriteInterfaceToken error";
65         return ERR_FLATTEN_OBJECT;
66     }
67     data.WriteString16(Str8ToStr16(pkgPath));
68 
69     MessageParcel reply;
70     MessageOption option { MessageOption::TF_ASYNC};
71     int32_t ret = remote->SendRequest(
72         static_cast<uint32_t>(SysInstallerInterfaceCode::UPDATE_PACKAGE), data, reply, option);
73     if (ret != ERR_OK) {
74         LOG(ERROR) << "SendRequest error";
75         return ERR_FLATTEN_OBJECT;
76     }
77 
78     return reply.ReadInt32();
79 }
80 
SetUpdateCallback(const sptr<ISysInstallerCallback> & cb)81 int32_t SysInstallerProxy::SetUpdateCallback(const sptr<ISysInstallerCallback> &cb)
82 {
83     if (cb == nullptr) {
84         LOG(ERROR) <<  "Invalid param";
85         return ERR_INVALID_VALUE;
86     }
87     LOG(INFO) << "RegisterUpdateCallback";
88 
89     auto remote = Remote();
90     if (remote == nullptr) {
91         LOG(ERROR) << "Can not get remote";
92         return ERR_FLATTEN_OBJECT;
93     }
94 
95     MessageParcel data;
96     if (!data.WriteInterfaceToken(GetDescriptor())) {
97         LOG(ERROR) << "WriteInterfaceToken fail";
98         return -1;
99     }
100 
101     bool ret = data.WriteRemoteObject(cb->AsObject());
102     if (!ret) {
103         LOG(ERROR) << "WriteRemoteObject error";
104         return ERR_FLATTEN_OBJECT;
105     }
106     MessageParcel reply;
107     MessageOption option { MessageOption::TF_ASYNC};
108     int32_t res = remote->SendRequest(
109         static_cast<uint32_t>(SysInstallerInterfaceCode::SET_UPDATE_CALLBACK), data, reply, option);
110     if (res != ERR_OK) {
111         LOG(ERROR) << "SendRequest error";
112         return ERR_FLATTEN_OBJECT;
113     }
114     return reply.ReadInt32();
115 }
116 
GetUpdateStatus()117 int32_t SysInstallerProxy::GetUpdateStatus()
118 {
119     LOG(INFO) << "GetUpdateStatus";
120     auto remote = Remote();
121     if (remote == nullptr) {
122         LOG(ERROR) << "Can not get remote";
123         return ERR_FLATTEN_OBJECT;
124     }
125 
126     MessageParcel data;
127     if (!data.WriteInterfaceToken(GetDescriptor())) {
128         return -1;
129     }
130     MessageParcel reply;
131     MessageOption option { MessageOption::TF_ASYNC};
132     int32_t ret = remote->SendRequest(
133         static_cast<uint32_t>(SysInstallerInterfaceCode::GET_UPDATE_STATUS), data, reply, option);
134     if (ret != ERR_OK) {
135         LOG(ERROR) << "SendRequest error";
136         return ERR_FLATTEN_OBJECT;
137     }
138 
139     return reply.ReadInt32();
140 }
141 
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)142 int32_t SysInstallerProxy::StartUpdateParaZip(const std::string &pkgPath,
143     const std::string &location, const std::string &cfgDir)
144 {
145     LOG(INFO) << "StartUpdateParaZip";
146     auto remote = Remote();
147     if (remote == nullptr) {
148         LOG(ERROR) << "Can not get remote";
149         return ERR_FLATTEN_OBJECT;
150     }
151 
152     MessageParcel data;
153     if (!data.WriteInterfaceToken(GetDescriptor())) {
154         LOG(ERROR) << "WriteInterfaceToken error";
155         return ERR_FLATTEN_OBJECT;
156     }
157     data.WriteString16(Str8ToStr16(pkgPath));
158     data.WriteString16(Str8ToStr16(location));
159     data.WriteString16(Str8ToStr16(cfgDir));
160 
161     MessageParcel reply;
162     MessageOption option;
163     int32_t ret = remote->SendRequest(
164         static_cast<uint32_t>(SysInstallerInterfaceCode::UPDATE_PARA_PACKAGE), data, reply, option);
165     if (ret != ERR_OK) {
166         LOG(ERROR) << "SendRequest error";
167         return ERR_FLATTEN_OBJECT;
168     }
169 
170     return reply.ReadInt32();
171 }
172 
StartDeleteParaZip(const std::string & location,const std::string & cfgDir)173 int32_t SysInstallerProxy::StartDeleteParaZip(const std::string &location, const std::string &cfgDir)
174 {
175     LOG(INFO) << "StartDeleteParaZip";
176     auto remote = Remote();
177     if (remote == nullptr) {
178         LOG(ERROR) << "Can not get remote";
179         return ERR_FLATTEN_OBJECT;
180     }
181 
182     MessageParcel data;
183     if (!data.WriteInterfaceToken(GetDescriptor())) {
184         LOG(ERROR) << "WriteInterfaceToken error";
185         return ERR_FLATTEN_OBJECT;
186     }
187     data.WriteString16(Str8ToStr16(location));
188     data.WriteString16(Str8ToStr16(cfgDir));
189 
190     MessageParcel reply;
191     MessageOption option;
192     int32_t ret = remote->SendRequest(
193         static_cast<uint32_t>(SysInstallerInterfaceCode::DELETE_PARA_PACKAGE), data, reply, option);
194     if (ret != ERR_OK) {
195         LOG(ERROR) << "SendRequest error";
196         return ERR_FLATTEN_OBJECT;
197     }
198 
199     return reply.ReadInt32();
200 }
201 
AccDecompressAndVerifyPkg(const std::string & srcPath,const std::string & dstPath,const uint32_t type)202 int32_t SysInstallerProxy::AccDecompressAndVerifyPkg(const std::string &srcPath,
203     const std::string &dstPath, const uint32_t type)
204 {
205     LOG(INFO) << "AccDecompressAndVerifyPkg";
206     auto remote = Remote();
207     if (remote == nullptr) {
208         LOG(ERROR) << "Can not get remote";
209         return ERR_FLATTEN_OBJECT;
210     }
211 
212     MessageParcel data;
213     if (!data.WriteInterfaceToken(GetDescriptor())) {
214         LOG(ERROR) << "WriteInterfaceToken error";
215         return ERR_FLATTEN_OBJECT;
216     }
217     data.WriteString16(Str8ToStr16(srcPath));
218     data.WriteString16(Str8ToStr16(dstPath));
219     data.WriteInt32(static_cast<int32_t>(type));
220 
221     MessageParcel reply;
222     MessageOption option;
223     int32_t ret = remote->SendRequest(
224         static_cast<uint32_t>(SysInstallerInterfaceCode::DECOMPRESS_ACC_PACKAGE), data, reply, option);
225     if (ret != ERR_OK) {
226         LOG(ERROR) << "SendRequest error";
227         return ERR_FLATTEN_OBJECT;
228     }
229 
230     return reply.ReadInt32();
231 }
232 
AccDeleteDir(const std::string & dstPath)233 int32_t SysInstallerProxy::AccDeleteDir(const std::string &dstPath)
234 {
235     LOG(INFO) << "AccDeleteDir";
236     auto remote = Remote();
237     if (remote == nullptr) {
238         LOG(ERROR) << "Can not get remote";
239         return ERR_FLATTEN_OBJECT;
240     }
241 
242     MessageParcel data;
243     if (!data.WriteInterfaceToken(GetDescriptor())) {
244         LOG(ERROR) << "WriteInterfaceToken error";
245         return ERR_FLATTEN_OBJECT;
246     }
247     data.WriteString16(Str8ToStr16(dstPath));
248 
249     MessageParcel reply;
250     MessageOption option;
251     int32_t ret = remote->SendRequest(
252         static_cast<uint32_t>(SysInstallerInterfaceCode::DELETE_ACC_PACKAGE), data, reply, option);
253     if (ret != ERR_OK) {
254         LOG(ERROR) << "SendRequest error";
255         return ERR_FLATTEN_OBJECT;
256     }
257 
258     return reply.ReadInt32();
259 }
260 }
261 } // namespace OHOS
262