1 /*
2 * Copyright (c) 2022-2024 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_ipc/service_stub.h"
17
18 #include <sstream>
19
20 #include "b_error/b_error.h"
21 #include "module_ipc/service_reverse_proxy.h"
22
23 namespace OHOS::FileManagement::Backup {
24 using namespace std;
25
ServiceStub()26 ServiceStub::ServiceStub()
27 {
28 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION)] =
29 &ServiceStub::CmdInitRestoreSession;
30 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION)] =
31 &ServiceStub::CmdInitBackupSession;
32 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] =
33 &ServiceStub::CmdGetLocalCapabilities;
34 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] =
35 &ServiceStub::CmdPublishFile;
36 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] =
37 &ServiceStub::CmdAppFileReady;
38 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE)] = &ServiceStub::CmdAppDone;
39 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START)] = &ServiceStub::CmdStart;
40 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME)] =
41 &ServiceStub::CmdGetFileHandle;
42 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION)] =
43 &ServiceStub::CmdAppendBundlesRestoreSession;
44 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] =
45 &ServiceStub::CmdAppendBundlesBackupSession;
46 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish;
47 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] =
48 &ServiceStub::CmdRelease;
49 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] =
50 &ServiceStub::CmdGetLocalCapabilitiesIncremental;
51 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] =
52 &ServiceStub::CmdInitIncrementalBackupSession;
53 opToInterfaceMap_[static_cast<uint32_t>(
54 IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] =
55 &ServiceStub::CmdAppendBundlesIncrementalBackupSession;
56 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] =
57 &ServiceStub::CmdPublishIncrementalFile;
58 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] =
59 &ServiceStub::CmdAppIncrementalFileReady;
60 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] =
61 &ServiceStub::CmdGetIncrementalFileHandle;
62 }
63
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
65 {
66 auto interfaceIndex = opToInterfaceMap_.find(code);
67 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
68 return BError(BError::Codes::OK);
69 }
70
71 const std::u16string descriptor = ServiceStub::GetDescriptor();
72 const std::u16string remoteDescriptor = data.ReadInterfaceToken();
73 if (descriptor != remoteDescriptor) {
74 return BError(BError::Codes::OK);
75 }
76 return (this->*(interfaceIndex->second))(data, reply);
77 }
78
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)79 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
80 {
81 auto remote = data.ReadRemoteObject();
82 auto iremote = iface_cast<IServiceReverse>(remote);
83
84 int32_t res = InitRestoreSession(iremote);
85 reply.WriteInt32(res);
86 return BError(BError::Codes::OK);
87 }
88
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)89 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
90 {
91 auto remote = data.ReadRemoteObject();
92 auto iremote = iface_cast<IServiceReverse>(remote);
93
94 int res = InitBackupSession(iremote);
95 reply.WriteInt32(res);
96 return BError(BError::Codes::OK);
97 }
98
CmdStart(MessageParcel & data,MessageParcel & reply)99 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
100 {
101 int res = Start();
102 reply.WriteInt32(res);
103 return BError(BError::Codes::OK);
104 }
105
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)106 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
107 {
108 UniqueFd fd(GetLocalCapabilities());
109 reply.WriteFileDescriptor(fd);
110 return BError(BError::Codes::OK);
111 }
112
CmdPublishFile(MessageParcel & data,MessageParcel & reply)113 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
114 {
115 unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
116 int res = PublishFile(*fileInfo);
117 reply.WriteInt32(res);
118 return BError(BError::Codes::OK);
119 }
120
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)121 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
122 {
123 string fileName;
124 data.ReadString(fileName);
125 UniqueFd fd(data.ReadFileDescriptor());
126 int res = AppFileReady(fileName, move(fd));
127 reply.WriteInt32(res);
128 return BError(BError::Codes::OK);
129 }
130
CmdAppDone(MessageParcel & data,MessageParcel & reply)131 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
132 {
133 bool success;
134 data.ReadBool(success);
135 int res = AppDone(success);
136 reply.WriteInt32(res);
137 return BError(BError::Codes::OK);
138 }
139
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)140 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
141 {
142 string bundleName;
143 data.ReadString(bundleName);
144 string fileName;
145 data.ReadString(fileName);
146
147 return GetFileHandle(bundleName, fileName);
148 }
149
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)150 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
151 {
152 UniqueFd fd(data.ReadFileDescriptor());
153 std::vector<string> bundleNames;
154 data.ReadStringVector(&bundleNames);
155
156 int res = AppendBundlesRestoreSession(move(fd), bundleNames);
157 reply.WriteInt32(res);
158 return BError(BError::Codes::OK);
159 }
160
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)161 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
162 {
163 std::vector<string> bundleNames;
164 data.ReadStringVector(&bundleNames);
165
166 int res = AppendBundlesBackupSession(bundleNames);
167 reply.WriteInt32(res);
168 return BError(BError::Codes::OK);
169 }
170
CmdFinish(MessageParcel & data,MessageParcel & reply)171 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
172 {
173 int res = Finish();
174 reply.WriteInt32(res);
175 return BError(BError::Codes::OK);
176 }
177
CmdRelease(MessageParcel & data,MessageParcel & reply)178 int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
179 {
180 int res = Release();
181 reply.WriteInt32(res);
182 return BError(BError::Codes::OK);
183 }
184
CmdGetLocalCapabilitiesIncremental(MessageParcel & data,MessageParcel & reply)185 int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply)
186 {
187 return BError(BError::Codes::OK);
188 }
189
CmdInitIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)190 int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
191 {
192 return BError(BError::Codes::OK);
193 }
194
CmdAppendBundlesIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)195 int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
196 {
197 return BError(BError::Codes::OK);
198 }
199
CmdPublishIncrementalFile(MessageParcel & data,MessageParcel & reply)200 int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply)
201 {
202 return BError(BError::Codes::OK);
203 }
204
CmdAppIncrementalFileReady(MessageParcel & data,MessageParcel & reply)205 int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply)
206 {
207 return BError(BError::Codes::OK);
208 }
209
CmdGetIncrementalFileHandle(MessageParcel & data,MessageParcel & reply)210 int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply)
211 {
212 return BError(BError::Codes::OK);
213 }
214 } // namespace OHOS::FileManagement::Backup
215