• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*
17  * 注意:
18  *     - 注意点1:本文件原则上只处理与IPC模块的IO,具体业务逻辑实现在service.cpp19  *     - 注意点2:所有调用开头处打印 Begin 字样,通过BError返回正常结果/错误码,这是出于防抵赖的目的
20  */
21 
22 #include "module_ipc/service_stub.h"
23 
24 #include <sstream>
25 
26 #include "b_error/b_error.h"
27 #include "b_error/b_excep_utils.h"
28 #include "filemgmt_libhilog.h"
29 #include "module_ipc/service_reverse_proxy.h"
30 
31 namespace OHOS::FileManagement::Backup {
32 using namespace std;
33 
ServiceStub()34 ServiceStub::ServiceStub()
35 {
36     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION)] =
37         &ServiceStub::CmdInitRestoreSession;
38     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION)] =
39         &ServiceStub::CmdInitBackupSession;
40     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] =
41         &ServiceStub::CmdGetLocalCapabilities;
42     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] =
43         &ServiceStub::CmdPublishFile;
44     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] =
45         &ServiceStub::CmdAppFileReady;
46     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE)] = &ServiceStub::CmdAppDone;
47     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START)] = &ServiceStub::CmdStart;
48     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME)] =
49         &ServiceStub::CmdGetFileHandle;
50     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION)] =
51         &ServiceStub::CmdAppendBundlesRestoreSession;
52     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] =
53         &ServiceStub::CmdAppendBundlesBackupSession;
54     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish;
55 }
56 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
58 {
59     const std::u16string descriptor = ServiceStub::GetDescriptor();
60     const std::u16string remoteDescriptor = data.ReadInterfaceToken();
61     if (descriptor != remoteDescriptor) {
62         return BError(BError::Codes::SA_INVAL_ARG, "Invalid remote descriptor");
63     }
64 
65     HILOGI("Begin to call procedure indexed %{public}u", code);
66     auto interfaceIndex = opToInterfaceMap_.find(code);
67     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
68         HILOGE("Cannot response request %{public}d : unknown procedure", code);
69         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70     }
71 
72     return BExcepUltils::ExceptionCatcherLocked(
73         [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); });
74 }
75 
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)76 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
77 {
78     HILOGI("Begin");
79     auto remote = data.ReadRemoteObject();
80     if (!remote) {
81         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
82     }
83     auto iremote = iface_cast<IServiceReverse>(remote);
84     if (!iremote) {
85         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
86     }
87 
88     int32_t res = InitRestoreSession(iremote);
89     if (!reply.WriteInt32(res)) {
90         stringstream ss;
91         ss << "Failed to send the result " << res;
92         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
93     }
94     return BError(BError::Codes::OK);
95 }
96 
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)97 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
98 {
99     HILOGI("Begin");
100     auto remote = data.ReadRemoteObject();
101     if (!remote) {
102         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
103     }
104     auto iremote = iface_cast<IServiceReverse>(remote);
105     if (!iremote) {
106         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
107     }
108 
109     int res = InitBackupSession(iremote);
110     if (!reply.WriteInt32(res)) {
111         stringstream ss;
112         ss << "Failed to send the result " << res;
113         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
114     }
115     return BError(BError::Codes::OK);
116 }
117 
CmdStart(MessageParcel & data,MessageParcel & reply)118 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
119 {
120     int res = Start();
121     if (!reply.WriteInt32(res)) {
122         stringstream ss;
123         ss << "Failed to send the result " << res;
124         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
125     }
126     return BError(BError::Codes::OK);
127 }
128 
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)129 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
130 {
131     HILOGI("Begin");
132     UniqueFd fd(GetLocalCapabilities());
133     if (!reply.WriteFileDescriptor(fd)) {
134         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
135     }
136     return BError(BError::Codes::OK);
137 }
138 
CmdPublishFile(MessageParcel & data,MessageParcel & reply)139 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
140 {
141     HILOGI("Begin");
142     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
143     if (!fileInfo) {
144         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
145     }
146     int res = PublishFile(*fileInfo);
147     if (!reply.WriteInt32(res)) {
148         stringstream ss;
149         ss << "Failed to send the result " << res;
150         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
151     }
152     return BError(BError::Codes::OK);
153 }
154 
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)155 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
156 {
157     HILOGI("Begin");
158     string fileName;
159     if (!data.ReadString(fileName)) {
160         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName");
161     }
162     UniqueFd fd(data.ReadFileDescriptor());
163     if (fd < 0) {
164         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
165     }
166 
167     int res = AppFileReady(fileName, move(fd));
168     if (!reply.WriteInt32(res)) {
169         stringstream ss;
170         ss << "Failed to send the result " << res;
171         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
172     }
173     return BError(BError::Codes::OK);
174 }
175 
CmdAppDone(MessageParcel & data,MessageParcel & reply)176 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
177 {
178     HILOGI("Begin");
179     bool success;
180     if (!data.ReadBool(success)) {
181         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bool flag");
182     }
183     int res = AppDone(success);
184     if (!reply.WriteInt32(res)) {
185         stringstream ss;
186         ss << "Failed to send the result " << res;
187         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
188     }
189     return BError(BError::Codes::OK);
190 }
191 
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)192 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
193 {
194     HILOGI("Begin");
195     string bundleName;
196     if (!data.ReadString(bundleName)) {
197         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
198     }
199     string fileName;
200     if (!data.ReadString(fileName)) {
201         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode();
202     }
203 
204     return GetFileHandle(bundleName, fileName);
205 }
206 
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)207 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
208 {
209     HILOGI("Begin");
210     UniqueFd fd(data.ReadFileDescriptor());
211     if (fd < 0) {
212         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
213     }
214 
215     vector<string> bundleNames;
216     if (!data.ReadStringVector(&bundleNames)) {
217         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
218     }
219     int32_t type;
220     if (!data.ReadInt32(type)) {
221         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType");
222     }
223     RestoreTypeEnum restoreType = static_cast<RestoreTypeEnum>(type);
224     int32_t userId;
225     if (!data.ReadInt32(userId)) {
226         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId");
227     }
228 
229     int res = AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId);
230     if (!reply.WriteInt32(res)) {
231         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
232     }
233     return BError(BError::Codes::OK);
234 }
235 
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)236 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
237 {
238     HILOGI("Begin");
239     vector<string> bundleNames;
240     if (!data.ReadStringVector(&bundleNames)) {
241         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
242     }
243 
244     int32_t res = AppendBundlesBackupSession(bundleNames);
245     if (!reply.WriteInt32(res)) {
246         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
247     }
248     return BError(BError::Codes::OK);
249 }
250 
CmdFinish(MessageParcel & data,MessageParcel & reply)251 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
252 {
253     HILOGI("Begin");
254     int res = Finish();
255     if (!reply.WriteInt32(res)) {
256         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
257     }
258     return BError(BError::Codes::OK);
259 }
260 } // namespace OHOS::FileManagement::Backup
261