1 /*
2  * Copyright (c) 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 "module_update_proxy.h"
17 
18 #include "log/log.h"
19 #include "module_ipc_helper.h"
20 #include "string_ex.h"
21 #include "sys_installer_sa_ipc_interface_code.h"
22 
23 namespace OHOS {
24 namespace SysInstaller {
25 using namespace Updater;
26 
InstallModulePackage(const std::string & pkgPath)27 int32_t ModuleUpdateProxy::InstallModulePackage(const std::string &pkgPath)
28 {
29     LOG(INFO) << "InstallModulePackage " << pkgPath;
30     auto remote = Remote();
31     if (remote == nullptr) {
32         LOG(ERROR) << "Can not get remote";
33         return ERR_FLATTEN_OBJECT;
34     }
35 
36     MessageParcel data;
37     if (!data.WriteInterfaceToken(GetDescriptor())) {
38         LOG(ERROR) << "WriteInterfaceToken error";
39         return ERR_FLATTEN_OBJECT;
40     }
41     data.WriteString16(Str8ToStr16(pkgPath));
42 
43     MessageParcel reply;
44     MessageOption option;
45     int32_t ret = remote->SendRequest(
46         static_cast<uint32_t>(ModuleUpdateInterfaceCode::INSTALL_MODULE_PACKAGE), data, reply, option);
47     if (ret != ERR_OK) {
48         LOG(ERROR) << "SendRequest error";
49         return ERR_FLATTEN_OBJECT;
50     }
51 
52     return reply.ReadInt32();
53 }
54 
UninstallModulePackage(const std::string & hmpName)55 int32_t ModuleUpdateProxy::UninstallModulePackage(const std::string &hmpName)
56 {
57     LOG(INFO) << "UninstallModulePackage " << hmpName;
58     auto remote = Remote();
59     if (remote == nullptr) {
60         LOG(ERROR) << "Can not get remote";
61         return ERR_FLATTEN_OBJECT;
62     }
63 
64     MessageParcel data;
65     if (!data.WriteInterfaceToken(GetDescriptor())) {
66         LOG(ERROR) << "WriteInterfaceToken error";
67         return ERR_FLATTEN_OBJECT;
68     }
69     data.WriteString16(Str8ToStr16(hmpName));
70 
71     MessageParcel reply;
72     MessageOption option;
73     int32_t ret = remote->SendRequest(
74         static_cast<uint32_t>(ModuleUpdateInterfaceCode::UNINSTALL_MODULE_PACKAGE), data, reply, option);
75     if (ret != ERR_OK) {
76         LOG(ERROR) << "SendRequest error";
77         return ERR_FLATTEN_OBJECT;
78     }
79 
80     return reply.ReadInt32();
81 }
82 
GetModulePackageInfo(const std::string & hmpName,std::list<ModulePackageInfo> & modulePackageInfos)83 int32_t ModuleUpdateProxy::GetModulePackageInfo(const std::string &hmpName,
84     std::list<ModulePackageInfo> &modulePackageInfos)
85 {
86     LOG(INFO) << "GetModulePackageInfo " << hmpName;
87     auto remote = Remote();
88     if (remote == nullptr) {
89         LOG(ERROR) << "Can not get remote";
90         return ERR_FLATTEN_OBJECT;
91     }
92 
93     MessageParcel data;
94     if (!data.WriteInterfaceToken(GetDescriptor())) {
95         LOG(ERROR) << "WriteInterfaceToken error";
96         return ERR_FLATTEN_OBJECT;
97     }
98     data.WriteString16(Str8ToStr16(hmpName));
99 
100     MessageParcel reply;
101     MessageOption option;
102     int32_t ret = remote->SendRequest(
103         static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_MODULE_PACKAGE_INFO), data, reply, option);
104     if (ret != ERR_OK) {
105         LOG(ERROR) << "SendRequest error";
106         return ERR_FLATTEN_OBJECT;
107     }
108 
109     ModuleIpcHelper::ReadModulePackageInfos(reply, modulePackageInfos);
110     return reply.ReadInt32();
111 }
112 
ExitModuleUpdate()113 int32_t ModuleUpdateProxy::ExitModuleUpdate()
114 {
115     LOG(INFO) << "ExitModuleUpdate";
116     auto remote = Remote();
117     if (remote == nullptr) {
118         LOG(ERROR) << "Can not get remote";
119         return ERR_FLATTEN_OBJECT;
120     }
121 
122     MessageParcel data;
123     if (!data.WriteInterfaceToken(GetDescriptor())) {
124         LOG(ERROR) << "WriteInterfaceToken error";
125         return ERR_FLATTEN_OBJECT;
126     }
127 
128     MessageParcel reply;
129     MessageOption option;
130     int32_t ret = remote->SendRequest(
131         static_cast<uint32_t>(ModuleUpdateInterfaceCode::EXIT_MODULE_UPDATE), data, reply, option);
132     if (ret != ERR_OK) {
133         LOG(ERROR) << "SendRequest error";
134         return ERR_FLATTEN_OBJECT;
135     }
136 
137     return reply.ReadInt32();
138 }
139 
GetHmpVersionInfo()140 std::vector<HmpVersionInfo> ModuleUpdateProxy::GetHmpVersionInfo()
141 {
142     LOG(INFO) << "GetHmpVersionInfo";
143     std::vector<HmpVersionInfo> versionInfo {};
144     auto remote = Remote();
145     if (remote == nullptr) {
146         LOG(ERROR) << "Can not get remote";
147         return versionInfo;
148     }
149 
150     MessageParcel data;
151     if (!data.WriteInterfaceToken(GetDescriptor())) {
152         LOG(ERROR) << "WriteInterfaceToken error";
153         return versionInfo;
154     }
155 
156     MessageParcel reply;
157     MessageOption option;
158     int32_t ret = remote->SendRequest(
159         static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_HMP_VERSION_INFO), data, reply, option);
160     if (ret != ERR_OK) {
161         LOG(ERROR) << "SendRequest error";
162         return versionInfo;
163     }
164 
165     int32_t count = reply.ReadInt32();
166     for (int32_t i = 0; i < count; i++) {
167         std::unique_ptr<HmpVersionInfo> infoPtr(reply.ReadParcelable<HmpVersionInfo>());
168         if (infoPtr != nullptr) {
169             versionInfo.push_back(*infoPtr);
170         }
171     }
172     return versionInfo;
173 }
174 
StartUpdateHmpPackage(const std::string & path,const sptr<ISysInstallerCallback> & updateCallback)175 int32_t ModuleUpdateProxy::StartUpdateHmpPackage(const std::string &path,
176     const sptr<ISysInstallerCallback> &updateCallback)
177 {
178     LOG(INFO) << "StartUpdateHmpPackage";
179     if (updateCallback == nullptr) {
180         LOG(ERROR) <<  "Invalid param";
181         return ERR_INVALID_VALUE;
182     }
183     auto remote = Remote();
184     if (remote == nullptr) {
185         LOG(ERROR) << "Can not get remote";
186         return ERR_FLATTEN_OBJECT;
187     }
188 
189     MessageParcel data;
190     if (!data.WriteInterfaceToken(GetDescriptor())) {
191         LOG(ERROR) << "WriteInterfaceToken error";
192         return ERR_FLATTEN_OBJECT;
193     }
194 
195     if (!data.WriteRemoteObject(updateCallback->AsObject())) {
196         LOG(ERROR) << "WriteRemoteObject fail";
197         return ERR_FLATTEN_OBJECT;
198     }
199     data.WriteString16(Str8ToStr16(path));
200 
201     MessageParcel reply;
202     MessageOption option;
203     int32_t ret = remote->SendRequest(
204         static_cast<uint32_t>(ModuleUpdateInterfaceCode::START_UPDATE_HMP_PACKAGE), data, reply, option);
205     if (ret != ERR_OK) {
206         LOG(ERROR) << "SendRequest error";
207         return ERR_FLATTEN_OBJECT;
208     }
209 
210     return reply.ReadInt32();
211 }
212 
GetHmpUpdateResult()213 std::vector<HmpUpdateInfo> ModuleUpdateProxy::GetHmpUpdateResult()
214 {
215     LOG(INFO) << "GetHmpUpdateResult";
216     std::vector<HmpUpdateInfo> updateInfo {};
217     auto remote = Remote();
218     if (remote == nullptr) {
219         LOG(ERROR) << "Can not get remote";
220         return updateInfo;
221     }
222 
223     MessageParcel data;
224     if (!data.WriteInterfaceToken(GetDescriptor())) {
225         LOG(ERROR) << "WriteInterfaceToken error";
226         return updateInfo;
227     }
228 
229     MessageParcel reply;
230     MessageOption option;
231     int32_t ret = remote->SendRequest(
232         static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_HMP_UPDATE_RESULT), data, reply, option);
233     if (ret != ERR_OK) {
234         LOG(ERROR) << "SendRequest error";
235         return updateInfo;
236     }
237 
238     int32_t count = reply.ReadInt32();
239     for (int32_t i = 0; i < count; i++) {
240         std::unique_ptr<HmpUpdateInfo> infoPtr(reply.ReadParcelable<HmpUpdateInfo>());
241         if (infoPtr != nullptr) {
242             updateInfo.push_back(*infoPtr);
243         }
244     }
245     return updateInfo;
246 }
247 } // namespace SysInstaller
248 } // namespace OHOS
249