1 /*
2 * Copyright (c) 2022-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_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 }
48
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51 auto interfaceIndex = opToInterfaceMap_.find(code);
52 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
53 return BError(BError::Codes::OK);
54 }
55
56 const std::u16string descriptor = ServiceStub::GetDescriptor();
57 const std::u16string remoteDescriptor = data.ReadInterfaceToken();
58 if (descriptor != remoteDescriptor) {
59 return BError(BError::Codes::OK);
60 }
61 return (this->*(interfaceIndex->second))(data, reply);
62 }
63
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)64 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
65 {
66 auto remote = data.ReadRemoteObject();
67 auto iremote = iface_cast<IServiceReverse>(remote);
68
69 int32_t res = InitRestoreSession(iremote);
70 reply.WriteInt32(res);
71 return BError(BError::Codes::OK);
72 }
73
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)74 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
75 {
76 auto remote = data.ReadRemoteObject();
77 auto iremote = iface_cast<IServiceReverse>(remote);
78
79 int res = InitBackupSession(iremote);
80 reply.WriteInt32(res);
81 return BError(BError::Codes::OK);
82 }
83
CmdStart(MessageParcel & data,MessageParcel & reply)84 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
85 {
86 int res = Start();
87 reply.WriteInt32(res);
88 return BError(BError::Codes::OK);
89 }
90
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)91 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
92 {
93 UniqueFd fd(GetLocalCapabilities());
94 reply.WriteFileDescriptor(fd);
95 return BError(BError::Codes::OK);
96 }
97
CmdPublishFile(MessageParcel & data,MessageParcel & reply)98 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
99 {
100 unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
101 int res = PublishFile(*fileInfo);
102 reply.WriteInt32(res);
103 return BError(BError::Codes::OK);
104 }
105
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)106 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
107 {
108 string fileName;
109 data.ReadString(fileName);
110 UniqueFd fd(data.ReadFileDescriptor());
111 int res = AppFileReady(fileName, move(fd));
112 reply.WriteInt32(res);
113 return BError(BError::Codes::OK);
114 }
115
CmdAppDone(MessageParcel & data,MessageParcel & reply)116 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
117 {
118 bool success;
119 data.ReadBool(success);
120 int res = AppDone(success);
121 reply.WriteInt32(res);
122 return BError(BError::Codes::OK);
123 }
124
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)125 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
126 {
127 string bundleName;
128 data.ReadString(bundleName);
129 string fileName;
130 data.ReadString(fileName);
131
132 return GetFileHandle(bundleName, fileName);
133 }
134
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)135 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
136 {
137 UniqueFd fd(data.ReadFileDescriptor());
138 std::vector<string> bundleNames;
139 data.ReadStringVector(&bundleNames);
140
141 int res = AppendBundlesRestoreSession(move(fd), bundleNames);
142 reply.WriteInt32(res);
143 return BError(BError::Codes::OK);
144 }
145
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)146 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
147 {
148 std::vector<string> bundleNames;
149 data.ReadStringVector(&bundleNames);
150
151 int res = AppendBundlesBackupSession(bundleNames);
152 reply.WriteInt32(res);
153 return BError(BError::Codes::OK);
154 }
155
CmdFinish(MessageParcel & data,MessageParcel & reply)156 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
157 {
158 int res = Finish();
159 reply.WriteInt32(res);
160 return BError(BError::Codes::OK);
161 }
162 } // namespace OHOS::FileManagement::Backup
163