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/svc_extension_proxy.h"
17
18 #include "b_error/b_error.h"
19 #include "b_error/b_excep_utils.h"
20 #include "filemgmt_libhilog.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
GetFileHandle(const string & fileName)27 UniqueFd SvcExtensionProxy::GetFileHandle(const string &fileName)
28 {
29 HILOGI("Start");
30 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
31 MessageParcel data;
32 data.WriteInterfaceToken(GetDescriptor());
33
34 if (!data.WriteString(fileName)) {
35 BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
36 return UniqueFd(-1);
37 }
38
39 MessageParcel reply;
40 MessageOption option;
41 int32_t ret =
42 Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_FILE_HANDLE), data, reply, option);
43 if (ret != NO_ERROR) {
44 HILOGE("Received error %{public}d when doing IPC", ret);
45 return UniqueFd(-ret);
46 }
47
48 HILOGI("Successful");
49 UniqueFd fd(reply.ReadFileDescriptor());
50 return UniqueFd(fd.Release());
51 }
52
HandleClear()53 ErrCode SvcExtensionProxy::HandleClear()
54 {
55 HILOGI("Start");
56 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
57 MessageParcel data;
58 data.WriteInterfaceToken(GetDescriptor());
59
60 MessageParcel reply;
61 MessageOption option;
62 int32_t ret =
63 Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_CLAER), data, reply, option);
64 if (ret != NO_ERROR) {
65 HILOGE("Received error %{public}d when doing IPC", ret);
66 return ErrCode(ret);
67 }
68
69 HILOGI("Successful");
70 return reply.ReadInt32();
71 }
72
HandleBackup()73 ErrCode SvcExtensionProxy::HandleBackup()
74 {
75 HILOGI("Start");
76 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
77 MessageParcel data;
78 data.WriteInterfaceToken(GetDescriptor());
79
80 MessageParcel reply;
81 MessageOption option;
82 int32_t ret =
83 Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_BACKUP), data, reply, option);
84 if (ret != NO_ERROR) {
85 HILOGE("Received error %{public}d when doing IPC", ret);
86 return ErrCode(ret);
87 }
88
89 HILOGI("Successful");
90 return reply.ReadInt32();
91 }
92
PublishFile(const string & fileName)93 ErrCode SvcExtensionProxy::PublishFile(const string &fileName)
94 {
95 HILOGI("Start");
96 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
97 MessageParcel data;
98 data.WriteInterfaceToken(GetDescriptor());
99
100 if (!data.WriteString(fileName)) {
101 BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
102 return ErrCode(EPERM);
103 }
104
105 MessageParcel reply;
106 MessageOption option;
107 int32_t ret =
108 Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_PUBLISH_FILE), data, reply, option);
109 if (ret != NO_ERROR) {
110 HILOGE("Received error %{public}d when doing IPC", ret);
111 return ErrCode(ret);
112 }
113
114 HILOGI("Successful");
115 return reply.ReadInt32();
116 }
117 } // namespace OHOS::FileManagement::Backup