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 }
73 
ServiceStubSupplement()74 void ServiceStub::ServiceStubSupplement()
75 {
76     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
77         &ServiceStub::CmdGetBackupInfo;
78     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER)] =
79         &ServiceStub::CmdUpdateTimer;
80     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE)] =
81         &ServiceStub::CmdUpdateSendRate;
82     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO)] =
83         &ServiceStub::CmdReportAppProcessInfo;
84 }
85 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)86 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
87 {
88     auto interfaceIndex = opToInterfaceMap_.find(code);
89     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
90         return BError(BError::Codes::OK);
91     }
92 
93     const std::u16string descriptor = ServiceStub::GetDescriptor();
94     const std::u16string remoteDescriptor = data.ReadInterfaceToken();
95     if (descriptor != remoteDescriptor) {
96         return BError(BError::Codes::OK);
97     }
98     return (this->*(interfaceIndex->second))(data, reply);
99 }
100 
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)101 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
102 {
103     auto remote = data.ReadRemoteObject();
104     auto iremote = iface_cast<IServiceReverse>(remote);
105 
106     int32_t res = InitRestoreSession(iremote);
107     reply.WriteInt32(res);
108     return BError(BError::Codes::OK);
109 }
110 
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)111 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
112 {
113     auto remote = data.ReadRemoteObject();
114     auto iremote = iface_cast<IServiceReverse>(remote);
115 
116     int res = InitBackupSession(iremote);
117     reply.WriteInt32(res);
118     return BError(BError::Codes::OK);
119 }
120 
CmdStart(MessageParcel & data,MessageParcel & reply)121 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
122 {
123     int res = Start();
124     reply.WriteInt32(res);
125     return BError(BError::Codes::OK);
126 }
127 
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)128 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
129 {
130     UniqueFd fd(GetLocalCapabilities());
131     reply.WriteFileDescriptor(fd);
132     return BError(BError::Codes::OK);
133 }
134 
CmdPublishFile(MessageParcel & data,MessageParcel & reply)135 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
136 {
137     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
138     int res = PublishFile(*fileInfo);
139     reply.WriteInt32(res);
140     return BError(BError::Codes::OK);
141 }
142 
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)143 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
144 {
145     string fileName;
146     data.ReadString(fileName);
147     UniqueFd fd(data.ReadFileDescriptor());
148     int res = AppFileReady(fileName, move(fd), 0);
149     reply.WriteInt32(res);
150     return BError(BError::Codes::OK);
151 }
152 
CmdAppDone(MessageParcel & data,MessageParcel & reply)153 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
154 {
155     ErrCode errCode;
156     data.ReadInt32(errCode);
157     int res = AppDone(errCode);
158     reply.WriteInt32(res);
159     return BError(BError::Codes::OK);
160 }
161 
CmdResultReport(MessageParcel & data,MessageParcel & reply)162 int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply)
163 {
164     std::string restoreRetInfo;
165     data.ReadString(restoreRetInfo);
166     int32_t scenario;
167     data.ReadInt32(scenario);
168     ErrCode errCode;
169     data.ReadInt32(errCode);
170     BackupRestoreScenario type = static_cast<BackupRestoreScenario>(scenario);
171     int res = ServiceResultReport(restoreRetInfo, type, errCode);
172     reply.WriteInt32(res);
173     return BError(BError::Codes::OK);
174 }
175 
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)176 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
177 {
178     string bundleName;
179     data.ReadString(bundleName);
180     string fileName;
181     data.ReadString(fileName);
182 
183     return GetFileHandle(bundleName, fileName);
184 }
185 
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)186 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
187 {
188     UniqueFd fd(data.ReadFileDescriptor());
189     std::vector<string> bundleNames;
190     data.ReadStringVector(&bundleNames);
191 
192     int res = AppendBundlesRestoreSession(move(fd), bundleNames);
193     reply.WriteInt32(res);
194     return BError(BError::Codes::OK);
195 }
196 
CmdAppendBundlesDetailsRestoreSession(MessageParcel & data,MessageParcel & reply)197 int32_t ServiceStub::CmdAppendBundlesDetailsRestoreSession(MessageParcel &data, MessageParcel &reply)
198 {
199     UniqueFd fd(data.ReadFileDescriptor());
200     std::vector<string> bundleNames;
201     data.ReadStringVector(&bundleNames);
202     std::vector<string> detailInfos;
203     data.ReadStringVector(&detailInfos);
204     int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos);
205     reply.WriteInt32(res);
206     return BError(BError::Codes::OK);
207 }
208 
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)209 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
210 {
211     std::vector<string> bundleNames;
212     data.ReadStringVector(&bundleNames);
213 
214     int res = AppendBundlesBackupSession(bundleNames);
215     reply.WriteInt32(res);
216     return BError(BError::Codes::OK);
217 }
218 
CmdFinish(MessageParcel & data,MessageParcel & reply)219 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
220 {
221     int res = Finish();
222     reply.WriteInt32(res);
223     return BError(BError::Codes::OK);
224 }
225 
CmdGetBackupInfo(MessageParcel & data,MessageParcel & reply)226 int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
227 {
228     int ret = ERR_OK;
229     string bundleName;
230     if (!data.ReadString(bundleName)) {
231         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
232     }
233     std::string result;
234     ret = GetBackupInfo(bundleName, result);
235     return ret;
236 }
237 
CmdUpdateTimer(MessageParcel & data,MessageParcel & reply)238 int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
239 {
240     int ret = ERR_OK;
241     string bundleName;
242     if (!data.ReadString(bundleName)) {
243         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
244     }
245     uint32_t timeout;
246     if (!data.ReadUint32(timeout)) {
247         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeout"));
248     }
249     bool result;
250     ret = UpdateTimer(bundleName, timeout, result);
251     return BError(BError::Codes::OK);
252 }
253 
CmdUpdateSendRate(MessageParcel & data,MessageParcel & reply)254 int32_t ServiceStub::CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply)
255 {
256     int ret = ERR_OK;
257     string bundleName;
258     if (!data.ReadString(bundleName)) {
259         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
260     }
261     int32_t sendRate;
262     if (!data.ReadInt32(sendRate)) {
263         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive sendRate"));
264     }
265     bool result;
266     ret = UpdateSendRate(bundleName, sendRate, result);
267     return BError(BError::Codes::OK);
268 }
269 
CmdRelease(MessageParcel & data,MessageParcel & reply)270 int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
271 {
272     int res = Release();
273     reply.WriteInt32(res);
274     return BError(BError::Codes::OK);
275 }
276 
CmdGetLocalCapabilitiesIncremental(MessageParcel & data,MessageParcel & reply)277 int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply)
278 {
279     return BError(BError::Codes::OK);
280 }
281 
CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel & data,MessageParcel & reply)282 int32_t ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply)
283 {
284     return BError(BError::Codes::OK);
285 }
286 
CmdInitIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)287 int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
288 {
289     return BError(BError::Codes::OK);
290 }
291 
CmdAppendBundlesIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)292 int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
293 {
294     return BError(BError::Codes::OK);
295 }
296 
CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)297 int32_t ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
298 {
299     return BError(BError::Codes::OK);
300 }
301 
CmdPublishIncrementalFile(MessageParcel & data,MessageParcel & reply)302 int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply)
303 {
304     return BError(BError::Codes::OK);
305 }
306 
CmdAppIncrementalFileReady(MessageParcel & data,MessageParcel & reply)307 int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply)
308 {
309     return BError(BError::Codes::OK);
310 }
311 
CmdGetIncrementalFileHandle(MessageParcel & data,MessageParcel & reply)312 int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply)
313 {
314     return BError(BError::Codes::OK);
315 }
316 
CmdReportAppProcessInfo(MessageParcel & data,MessageParcel & reply)317 int32_t ServiceStub::CmdReportAppProcessInfo(MessageParcel &data, MessageParcel &reply)
318 {
319     return BError(BError::Codes::OK);
320 }
321 } // namespace OHOS::FileManagement::Backup
322