• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ReportModuleUpdateStatus(const ModuleUpdateStatus & status)113 int32_t ModuleUpdateProxy::ReportModuleUpdateStatus(const ModuleUpdateStatus &status)
114 {
115     LOG(INFO) << "ReportModuleUpdateStatus process=" << status.process;
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     ModuleIpcHelper::WriteModuleUpdateStatus(data, status);
128 
129     MessageParcel reply;
130     MessageOption option;
131     int32_t ret = remote->SendRequest(
132         static_cast<uint32_t>(ModuleUpdateInterfaceCode::REPORT_MODULE_UPDATE_STATUS), data, reply, option);
133     if (ret != ERR_OK) {
134         LOG(ERROR) << "SendRequest error";
135         return ERR_FLATTEN_OBJECT;
136     }
137 
138     return reply.ReadInt32();
139 }
140 
ExitModuleUpdate()141 int32_t ModuleUpdateProxy::ExitModuleUpdate()
142 {
143     LOG(INFO) << "ExitModuleUpdate";
144     auto remote = Remote();
145     if (remote == nullptr) {
146         LOG(ERROR) << "Can not get remote";
147         return ERR_FLATTEN_OBJECT;
148     }
149 
150     MessageParcel data;
151     if (!data.WriteInterfaceToken(GetDescriptor())) {
152         LOG(ERROR) << "WriteInterfaceToken error";
153         return ERR_FLATTEN_OBJECT;
154     }
155 
156     MessageParcel reply;
157     MessageOption option;
158     int32_t ret = remote->SendRequest(
159         static_cast<uint32_t>(ModuleUpdateInterfaceCode::EXIT_MODULE_UPDATE), data, reply, option);
160     if (ret != ERR_OK) {
161         LOG(ERROR) << "SendRequest error";
162         return ERR_FLATTEN_OBJECT;
163     }
164 
165     return reply.ReadInt32();
166 }
167 } // namespace SysInstaller
168 } // namespace OHOS
169