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 "ext_extension_stub.h"
17
18 #include <cstdint>
19 #include <sstream>
20
21 #include "b_error/b_error.h"
22 #include "b_error/b_excep_utils.h"
23 #include "filemgmt_libhilog.h"
24
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27
ExtExtensionStub()28 ExtExtensionStub::ExtExtensionStub()
29 {
30 opToInterfaceMap_[static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_FILE_HANDLE)] =
31 &ExtExtensionStub::CmdGetFileHandle;
32 opToInterfaceMap_[static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_CLAER)] =
33 &ExtExtensionStub::CmdHandleClear;
34 opToInterfaceMap_[static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_BACKUP)] =
35 &ExtExtensionStub::CmdHandleBackup;
36 opToInterfaceMap_[static_cast<uint32_t>(IExtensionInterfaceCode::CMD_PUBLISH_FILE)] =
37 &ExtExtensionStub::CmdPublishFile;
38 }
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code,
41 MessageParcel &data,
42 MessageParcel &reply,
43 MessageOption &option)
44 {
45 const std::u16string descriptor = ExtExtensionStub::GetDescriptor();
46 const std::u16string remoteDescriptor = data.ReadInterfaceToken();
47 if (descriptor != remoteDescriptor) {
48 return BError(BError::Codes::EXT_INVAL_ARG, "Invalid remote descriptor");
49 }
50
51 HILOGI("Begin to call procedure indexed %{public}u", code);
52 auto interfaceIndex = opToInterfaceMap_.find(code);
53 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
54 HILOGE("Cannot response request %{public}d : unknown procedure", code);
55 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56 }
57
58 return BExcepUltils::ExceptionCatcherLocked(
59 [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); });
60 }
61
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)62 ErrCode ExtExtensionStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
63 {
64 HILOGI("Begin");
65 string fileName;
66 if (!data.ReadString(fileName)) {
67 return BError(BError::Codes::EXT_INVAL_ARG, "Failed to receive fileName").GetCode();
68 }
69
70 UniqueFd fd = GetFileHandle(fileName);
71 if (!reply.WriteFileDescriptor(fd)) {
72 return BError(BError::Codes::EXT_BROKEN_IPC, "Failed to send out the file").GetCode();
73 }
74 return BError(BError::Codes::OK);
75 }
76
CmdHandleClear(MessageParcel & data,MessageParcel & reply)77 ErrCode ExtExtensionStub::CmdHandleClear(MessageParcel &data, MessageParcel &reply)
78 {
79 HILOGI("Begin");
80 ErrCode res = HandleClear();
81 if (!reply.WriteInt32(res)) {
82 stringstream ss;
83 ss << "Failed to send the result " << res;
84 return BError(BError::Codes::EXT_BROKEN_IPC, ss.str()).GetCode();
85 }
86 return BError(BError::Codes::OK);
87 }
88
CmdHandleBackup(MessageParcel & data,MessageParcel & reply)89 ErrCode ExtExtensionStub::CmdHandleBackup(MessageParcel &data, MessageParcel &reply)
90 {
91 HILOGI("Begin");
92 ErrCode res = HandleBackup();
93 if (!reply.WriteInt32(res)) {
94 stringstream ss;
95 ss << "Failed to send the result " << res;
96 return BError(BError::Codes::EXT_BROKEN_IPC, ss.str()).GetCode();
97 }
98 return BError(BError::Codes::OK);
99 }
100
CmdPublishFile(MessageParcel & data,MessageParcel & reply)101 ErrCode ExtExtensionStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
102 {
103 HILOGI("Begin");
104 string fileName;
105 if (!data.ReadString(fileName)) {
106 return BError(BError::Codes::EXT_INVAL_ARG, "Failed to receive fileName");
107 }
108
109 ErrCode res = PublishFile(fileName);
110 if (!reply.WriteInt32(res)) {
111 stringstream ss;
112 ss << "Failed to send the result " << res;
113 return BError(BError::Codes::EXT_BROKEN_IPC, ss.str()).GetCode();
114 }
115 return BError(BError::Codes::OK);
116 }
117 } // namespace OHOS::FileManagement::Backup