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_stub.h"
17 #include "sys_installer_sa_ipc_interface_code.h"
18
19 #include "log/log.h"
20 #include "string_ex.h"
21
22 namespace OHOS {
23 namespace SysInstaller {
24 using namespace std;
25 using namespace Updater;
26 using namespace std::placeholders;
27
ModuleUpdateStub()28 ModuleUpdateStub::ModuleUpdateStub()
29 {
30 requestFuncMap_.emplace(ModuleUpdateInterfaceCode::INSTALL_MODULE_PACKAGE,
31 bind(&ModuleUpdateStub::InstallModulePackageStub, this, _1, _2, _3, _4));
32 requestFuncMap_.emplace(ModuleUpdateInterfaceCode::UNINSTALL_MODULE_PACKAGE,
33 bind(&ModuleUpdateStub::UninstallModulePackageStub, this, _1, _2, _3, _4));
34 requestFuncMap_.emplace(ModuleUpdateInterfaceCode::GET_MODULE_PACKAGE_INFO,
35 bind(&ModuleUpdateStub::GetModulePackageInfoStub, this, _1, _2, _3, _4));
36 requestFuncMap_.emplace(ModuleUpdateInterfaceCode::REPORT_MODULE_UPDATE_STATUS,
37 bind(&ModuleUpdateStub::ReportModuleUpdateStatusStub, this, _1, _2, _3, _4));
38 requestFuncMap_.emplace(ModuleUpdateInterfaceCode::EXIT_MODULE_UPDATE,
39 bind(&ModuleUpdateStub::ExitModuleUpdateStub, this, _1, _2, _3, _4));
40 }
41
~ModuleUpdateStub()42 ModuleUpdateStub::~ModuleUpdateStub()
43 {
44 requestFuncMap_.clear();
45 }
46
InstallModulePackageStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const47 int32_t ModuleUpdateStub::InstallModulePackageStub(ModuleUpdateStub *service,
48 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
49 {
50 if (service == nullptr) {
51 LOG(ERROR) << "Invalid param";
52 return -1;
53 }
54 string pkgPath = Str16ToStr8(data.ReadString16());
55 int32_t ret = service->InstallModulePackage(pkgPath);
56 reply.WriteInt32(ret);
57 return 0;
58 }
59
UninstallModulePackageStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const60 int32_t ModuleUpdateStub::UninstallModulePackageStub(ModuleUpdateStub *service,
61 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
62 {
63 if (service == nullptr) {
64 LOG(ERROR) << "Invalid param";
65 return -1;
66 }
67 string hmpName = Str16ToStr8(data.ReadString16());
68 int32_t ret = service->UninstallModulePackage(hmpName);
69 reply.WriteInt32(ret);
70 return 0;
71 }
72
GetModulePackageInfoStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const73 int32_t ModuleUpdateStub::GetModulePackageInfoStub(ModuleUpdateStub *service,
74 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
75 {
76 if (service == nullptr) {
77 LOG(ERROR) << "Invalid param";
78 return -1;
79 }
80 string hmpName = Str16ToStr8(data.ReadString16());
81 std::list<ModulePackageInfo> infos;
82 int32_t ret = service->GetModulePackageInfo(hmpName, infos);
83 ModuleIpcHelper::WriteModulePackageInfos(reply, infos);
84 reply.WriteInt32(ret);
85 return 0;
86 }
87
ReportModuleUpdateStatusStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const88 int32_t ModuleUpdateStub::ReportModuleUpdateStatusStub(ModuleUpdateStub *service,
89 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
90 {
91 if (service == nullptr) {
92 LOG(ERROR) << "Invalid param";
93 return -1;
94 }
95 ModuleUpdateStatus status;
96 ModuleIpcHelper::ReadModuleUpdateStatus(data, status);
97 int32_t ret = service->ReportModuleUpdateStatus(status);
98 reply.WriteInt32(ret);
99 return 0;
100 }
101
ExitModuleUpdateStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const102 int32_t ModuleUpdateStub::ExitModuleUpdateStub(ModuleUpdateStub *service,
103 MessageParcel &data, MessageParcel &reply, MessageOption &option) const
104 {
105 if (service == nullptr) {
106 LOG(ERROR) << "Invalid param";
107 return -1;
108 }
109 int32_t ret = service->ExitModuleUpdate();
110 reply.WriteInt32(ret);
111 return 0;
112 }
113
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int32_t ModuleUpdateStub::OnRemoteRequest(uint32_t code,
115 MessageParcel &data, MessageParcel &reply, MessageOption &option)
116 {
117 if (data.ReadInterfaceToken() != GetDescriptor()) {
118 LOG(ERROR) << "ModuleUpdateStub ReadInterfaceToken fail";
119 return -1;
120 }
121
122 LOG(INFO) << "OnRemoteRequest func code " << code;
123 auto inter = requestFuncMap_.find(code);
124 if (inter != requestFuncMap_.end()) {
125 return inter->second(this, data, reply, option);
126 }
127
128 LOG(INFO) << "ModuleUpdateStub OnRemoteRequest code " << code << "not found";
129 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
130 }
131 } // namespace SysInstaller
132 } // namespace OHOS