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 if (count > IPC_MAX_SIZE || count < IPC_MIN_SIZE) {
167 LOG(ERROR) << "Not support such a large number of modules: " << count;
168 return versionInfo;
169 }
170 for (int32_t i = 0; i < count; i++) {
171 std::unique_ptr<HmpVersionInfo> infoPtr(reply.ReadParcelable<HmpVersionInfo>());
172 if (infoPtr != nullptr) {
173 versionInfo.push_back(*infoPtr);
174 }
175 }
176 return versionInfo;
177 }
178
StartUpdateHmpPackage(const std::string & path,const sptr<ISysInstallerCallback> & updateCallback)179 int32_t ModuleUpdateProxy::StartUpdateHmpPackage(const std::string &path,
180 const sptr<ISysInstallerCallback> &updateCallback)
181 {
182 LOG(INFO) << "StartUpdateHmpPackage";
183 if (updateCallback == nullptr) {
184 LOG(ERROR) << "Invalid param";
185 return ERR_INVALID_VALUE;
186 }
187 auto remote = Remote();
188 if (remote == nullptr) {
189 LOG(ERROR) << "Can not get remote";
190 return ERR_FLATTEN_OBJECT;
191 }
192
193 MessageParcel data;
194 if (!data.WriteInterfaceToken(GetDescriptor())) {
195 LOG(ERROR) << "WriteInterfaceToken error";
196 return ERR_FLATTEN_OBJECT;
197 }
198
199 if (!data.WriteRemoteObject(updateCallback->AsObject())) {
200 LOG(ERROR) << "WriteRemoteObject fail";
201 return ERR_FLATTEN_OBJECT;
202 }
203 data.WriteString16(Str8ToStr16(path));
204
205 MessageParcel reply;
206 MessageOption option;
207 int32_t ret = remote->SendRequest(
208 static_cast<uint32_t>(ModuleUpdateInterfaceCode::START_UPDATE_HMP_PACKAGE), data, reply, option);
209 if (ret != ERR_OK) {
210 LOG(ERROR) << "SendRequest error";
211 return ERR_FLATTEN_OBJECT;
212 }
213
214 return reply.ReadInt32();
215 }
216
GetHmpUpdateResult()217 std::vector<HmpUpdateInfo> ModuleUpdateProxy::GetHmpUpdateResult()
218 {
219 LOG(INFO) << "GetHmpUpdateResult";
220 std::vector<HmpUpdateInfo> updateInfo {};
221 auto remote = Remote();
222 if (remote == nullptr) {
223 LOG(ERROR) << "Can not get remote";
224 return updateInfo;
225 }
226
227 MessageParcel data;
228 if (!data.WriteInterfaceToken(GetDescriptor())) {
229 LOG(ERROR) << "WriteInterfaceToken error";
230 return updateInfo;
231 }
232
233 MessageParcel reply;
234 MessageOption option;
235 int32_t ret = remote->SendRequest(
236 static_cast<uint32_t>(ModuleUpdateInterfaceCode::GET_HMP_UPDATE_RESULT), data, reply, option);
237 if (ret != ERR_OK) {
238 LOG(ERROR) << "SendRequest error";
239 return updateInfo;
240 }
241
242 int32_t count = reply.ReadInt32();
243 if (count > IPC_MAX_SIZE || count < IPC_MIN_SIZE) {
244 LOG(ERROR) << "Not support such a large number of results: " << count;
245 return updateInfo;
246 }
247 for (int32_t i = 0; i < count; i++) {
248 std::unique_ptr<HmpUpdateInfo> infoPtr(reply.ReadParcelable<HmpUpdateInfo>());
249 if (infoPtr != nullptr) {
250 updateInfo.push_back(*infoPtr);
251 }
252 }
253 return updateInfo;
254 }
255 } // namespace SysInstaller
256 } // namespace OHOS
257