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 "b_resources/b_constants.h"
22 #include "module_ipc/service_reverse_proxy.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
ServiceStub()27 ServiceStub::ServiceStub()
28 {
29 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish;
30 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] =
31 &ServiceStub::CmdRelease;
32 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION)] =
33 &ServiceStub::CmdInitRestoreSession;
34 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION)] =
35 &ServiceStub::CmdInitBackupSession;
36 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] =
37 &ServiceStub::CmdGetLocalCapabilities;
38 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] =
39 &ServiceStub::CmdAppFileReady;
40 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] =
41 &ServiceStub::CmdPublishFile;
42 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE)] = &ServiceStub::CmdAppDone;
43 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START)] = &ServiceStub::CmdStart;
44 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME)] =
45 &ServiceStub::CmdGetFileHandle;
46 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION)] =
47 &ServiceStub::CmdAppendBundlesRestoreSession;
48 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION_DETAIL)] =
49 &ServiceStub::CmdAppendBundlesDetailsRestoreSession;
50 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] =
51 &ServiceStub::CmdAppendBundlesBackupSession;
52 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] =
53 &ServiceStub::CmdGetLocalCapabilitiesIncremental;
54 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] =
55 &ServiceStub::CmdInitIncrementalBackupSession;
56 opToInterfaceMap_[static_cast<uint32_t>(
57 IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS)] =
58 &ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession;
59 opToInterfaceMap_[static_cast<uint32_t>(
60 IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] =
61 &ServiceStub::CmdAppendBundlesIncrementalBackupSession;
62 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] =
63 &ServiceStub::CmdPublishIncrementalFile;
64 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] =
65 &ServiceStub::CmdAppIncrementalFileReady;
66 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] =
67 &ServiceStub::CmdGetIncrementalFileHandle;
68 ServiceStubSupplement();
69 opToInterfaceMap_[static_cast<uint32_t>(
70 IServiceInterfaceCode::SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP)] =
71 &ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup;
72 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_STOP_EXT_TIMER)] =
73 &ServiceStub::CmdStopExtTimer;
74 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_REFRESH_DATA_SIZE)] =
75 &ServiceStub::CmdRefreshDataSize;
76 }
77
ServiceStubSupplement()78 void ServiceStub::ServiceStubSupplement()
79 {
80 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
81 &ServiceStub::CmdGetBackupInfo;
82 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER)] =
83 &ServiceStub::CmdUpdateTimer;
84 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE)] =
85 &ServiceStub::CmdUpdateSendRate;
86 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO)] =
87 &ServiceStub::CmdReportAppProcessInfo;
88 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] =
89 &ServiceStub::CmdInitRestoreSessionMsg;
90 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG)] =
91 &ServiceStub::CmdInitBackupSessionMsg;
92 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG)] =
93 &ServiceStub::CmdInitIncrementalBackupSessionMsg;
94 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] =
95 &ServiceStub::CmdCancel;
96 opToInterfaceMap_[static_cast<uint32_t>(
97 IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BUNDLE_INFOS)] =
98 &ServiceStub::CmdGetLocalCapabilitiesForBdInfos;
99 opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_DATA_SIZE)] =
100 &ServiceStub::CmdGetBackupDataSize;
101 }
102
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)103 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
104 {
105 auto interfaceIndex = opToInterfaceMap_.find(code);
106 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
107 return BError(BError::Codes::OK);
108 }
109
110 const std::u16string descriptor = ServiceStub::GetDescriptor();
111 const std::u16string remoteDescriptor = data.ReadInterfaceToken();
112 if (descriptor != remoteDescriptor) {
113 return BError(BError::Codes::OK);
114 }
115 return (this->*(interfaceIndex->second))(data, reply);
116 }
117
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)118 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
119 {
120 auto remote = data.ReadRemoteObject();
121 auto iremote = iface_cast<IServiceReverse>(remote);
122
123 int32_t res = InitRestoreSession(iremote);
124 reply.WriteInt32(res);
125 return BError(BError::Codes::OK);
126 }
127
CmdInitRestoreSessionMsg(MessageParcel & data,MessageParcel & reply)128 int32_t ServiceStub::CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply)
129 {
130 auto remote = data.ReadRemoteObject();
131 auto iremote = iface_cast<IServiceReverse>(remote);
132 std::string errMsg;
133 int32_t res = InitRestoreSession(iremote, errMsg);
134 reply.WriteString(errMsg);
135 reply.WriteInt32(res);
136 return BError(BError::Codes::OK);
137 }
138
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)139 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
140 {
141 auto remote = data.ReadRemoteObject();
142 auto iremote = iface_cast<IServiceReverse>(remote);
143
144 int res = InitBackupSession(iremote);
145 reply.WriteInt32(res);
146 return BError(BError::Codes::OK);
147 }
148
CmdInitBackupSessionMsg(MessageParcel & data,MessageParcel & reply)149 int32_t ServiceStub::CmdInitBackupSessionMsg(MessageParcel &data, MessageParcel &reply)
150 {
151 auto remote = data.ReadRemoteObject();
152 auto iremote = iface_cast<IServiceReverse>(remote);
153 std::string errMsg;
154 int res = InitBackupSession(iremote, errMsg);
155 reply.WriteString(errMsg);
156 reply.WriteInt32(res);
157 return BError(BError::Codes::OK);
158 }
159
CmdStart(MessageParcel & data,MessageParcel & reply)160 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
161 {
162 int res = Start();
163 reply.WriteInt32(res);
164 return BError(BError::Codes::OK);
165 }
166
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)167 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
168 {
169 UniqueFd fd(GetLocalCapabilities());
170 reply.WriteFileDescriptor(fd);
171 return BError(BError::Codes::OK);
172 }
173
CmdPublishFile(MessageParcel & data,MessageParcel & reply)174 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
175 {
176 unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
177 int res = PublishFile(*fileInfo);
178 reply.WriteInt32(res);
179 return BError(BError::Codes::OK);
180 }
181
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)182 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
183 {
184 string fileName;
185 data.ReadString(fileName);
186 UniqueFd fd(data.ReadFileDescriptor());
187 int res = AppFileReady(fileName, move(fd), 0);
188 reply.WriteInt32(res);
189 return BError(BError::Codes::OK);
190 }
191
CmdAppDone(MessageParcel & data,MessageParcel & reply)192 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
193 {
194 ErrCode errCode;
195 data.ReadInt32(errCode);
196 int res = AppDone(errCode);
197 reply.WriteInt32(res);
198 return BError(BError::Codes::OK);
199 }
200
CmdResultReport(MessageParcel & data,MessageParcel & reply)201 int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply)
202 {
203 std::string restoreRetInfo;
204 data.ReadString(restoreRetInfo);
205 int32_t scenario;
206 data.ReadInt32(scenario);
207 ErrCode errCode;
208 data.ReadInt32(errCode);
209 BackupRestoreScenario type = static_cast<BackupRestoreScenario>(scenario);
210 int res = ServiceResultReport(restoreRetInfo, type, errCode);
211 reply.WriteInt32(res);
212 return BError(BError::Codes::OK);
213 }
214
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)215 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
216 {
217 string bundleName;
218 data.ReadString(bundleName);
219 string fileName;
220 data.ReadString(fileName);
221
222 return GetFileHandle(bundleName, fileName);
223 }
224
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)225 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
226 {
227 UniqueFd fd(data.ReadFileDescriptor());
228 std::vector<string> bundleNames;
229 data.ReadStringVector(&bundleNames);
230 int res = AppendBundlesRestoreSession(move(fd), bundleNames);
231 reply.WriteInt32(res);
232 return BError(BError::Codes::OK);
233 }
234
CmdAppendBundlesDetailsRestoreSession(MessageParcel & data,MessageParcel & reply)235 int32_t ServiceStub::CmdAppendBundlesDetailsRestoreSession(MessageParcel &data, MessageParcel &reply)
236 {
237 UniqueFd fd(data.ReadFileDescriptor());
238 std::vector<string> bundleNames;
239 data.ReadStringVector(&bundleNames);
240 std::vector<string> detailInfos;
241 data.ReadStringVector(&detailInfos);
242 int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos);
243 reply.WriteInt32(res);
244 return BError(BError::Codes::OK);
245 }
246
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)247 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
248 {
249 std::vector<string> bundleNames;
250 data.ReadStringVector(&bundleNames);
251 int res = AppendBundlesBackupSession(bundleNames);
252 reply.WriteInt32(res);
253 return BError(BError::Codes::OK);
254 }
255
CmdFinish(MessageParcel & data,MessageParcel & reply)256 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
257 {
258 int res = Finish();
259 reply.WriteInt32(res);
260 return BError(BError::Codes::OK);
261 }
262
CmdGetBackupInfo(MessageParcel & data,MessageParcel & reply)263 int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
264 {
265 int ret = ERR_OK;
266 string bundleName;
267 if (!data.ReadString(bundleName)) {
268 return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
269 }
270 std::string result;
271 ret = GetBackupInfo(bundleName, result);
272 return ret;
273 }
274
CmdUpdateTimer(MessageParcel & data,MessageParcel & reply)275 int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
276 {
277 int ret = ERR_OK;
278 string bundleName;
279 if (!data.ReadString(bundleName)) {
280 return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
281 }
282 uint32_t timeout;
283 if (!data.ReadUint32(timeout)) {
284 return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeout"));
285 }
286 bool result;
287 ret = UpdateTimer(bundleName, timeout, result);
288 return BError(BError::Codes::OK);
289 }
290
CmdUpdateSendRate(MessageParcel & data,MessageParcel & reply)291 int32_t ServiceStub::CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply)
292 {
293 int ret = ERR_OK;
294 string bundleName;
295 if (!data.ReadString(bundleName)) {
296 return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
297 }
298 int32_t sendRate;
299 if (!data.ReadInt32(sendRate)) {
300 return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive sendRate"));
301 }
302 bool result;
303 ret = UpdateSendRate(bundleName, sendRate, result);
304 return BError(BError::Codes::OK);
305 }
306
CmdRelease(MessageParcel & data,MessageParcel & reply)307 int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
308 {
309 int res = Release();
310 reply.WriteInt32(res);
311 return BError(BError::Codes::OK);
312 }
313
CmdCancel(MessageParcel & data,MessageParcel & reply)314 int32_t ServiceStub::CmdCancel(MessageParcel &data, MessageParcel &reply)
315 {
316 int ret = ERR_OK;
317 string bundleName;
318 if (!data.ReadString(bundleName)) {
319 return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName");
320 }
321 int result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
322 ret = Cancel(bundleName, result);
323 reply.WriteInt32(result);
324 return ret;
325 }
326
CmdGetLocalCapabilitiesIncremental(MessageParcel & data,MessageParcel & reply)327 int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply)
328 {
329 return BError(BError::Codes::OK);
330 }
331
CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel & data,MessageParcel & reply)332 int32_t ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply)
333 {
334 return BError(BError::Codes::OK);
335 }
336
CmdInitIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)337 int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
338 {
339 return BError(BError::Codes::OK);
340 }
341
CmdInitIncrementalBackupSessionMsg(MessageParcel & data,MessageParcel & reply)342 int32_t ServiceStub::CmdInitIncrementalBackupSessionMsg(MessageParcel &data, MessageParcel &reply)
343 {
344 return BError(BError::Codes::OK);
345 }
346
CmdAppendBundlesIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)347 int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
348 {
349 return BError(BError::Codes::OK);
350 }
351
CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)352 int32_t ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
353 {
354 return BError(BError::Codes::OK);
355 }
356
CmdPublishIncrementalFile(MessageParcel & data,MessageParcel & reply)357 int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply)
358 {
359 return BError(BError::Codes::OK);
360 }
361
CmdAppIncrementalFileReady(MessageParcel & data,MessageParcel & reply)362 int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply)
363 {
364 return BError(BError::Codes::OK);
365 }
366
CmdGetIncrementalFileHandle(MessageParcel & data,MessageParcel & reply)367 int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply)
368 {
369 return BError(BError::Codes::OK);
370 }
371
CmdReportAppProcessInfo(MessageParcel & data,MessageParcel & reply)372 int32_t ServiceStub::CmdReportAppProcessInfo(MessageParcel &data, MessageParcel &reply)
373 {
374 return BError(BError::Codes::OK);
375 }
376
CmdStopExtTimer(MessageParcel & data,MessageParcel & reply)377 int32_t ServiceStub::CmdStopExtTimer(MessageParcel &data, MessageParcel &reply)
378 {
379 return BError(BError::Codes::OK);
380 }
381
CmdRefreshDataSize(MessageParcel & data,MessageParcel & reply)382 int32_t ServiceStub::CmdRefreshDataSize(MessageParcel &data, MessageParcel &reply)
383 {
384 return BError(BError::Codes::OK);
385 }
386
CmdGetLocalCapabilitiesForBdInfos(MessageParcel & data,MessageParcel & reply)387 int32_t ServiceStub::CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply)
388 {
389 return BError(BError::Codes::OK);
390 }
391
CmdGetBackupDataSize(MessageParcel & data,MessageParcel & reply)392 int32_t ServiceStub::CmdGetBackupDataSize(MessageParcel &data, MessageParcel &reply)
393 {
394 return BError(BError::Codes::OK);
395 }
396 } // namespace OHOS::FileManagement::Backup
397