1cb7eb8c9Sopenharmony_ci/*
2cb7eb8c9Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at
6cb7eb8c9Sopenharmony_ci *
7cb7eb8c9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb7eb8c9Sopenharmony_ci *
9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and
13cb7eb8c9Sopenharmony_ci * limitations under the License.
14cb7eb8c9Sopenharmony_ci */
15cb7eb8c9Sopenharmony_ci
16cb7eb8c9Sopenharmony_ci#include "ipc/daemon_stub.h"
17cb7eb8c9Sopenharmony_ci
18cb7eb8c9Sopenharmony_ci#include "dfs_error.h"
19cb7eb8c9Sopenharmony_ci#include "dfsu_access_token_helper.h"
20cb7eb8c9Sopenharmony_ci#include "dm_device_info.h"
21cb7eb8c9Sopenharmony_ci#include "ipc/distributed_file_daemon_ipc_interface_code.h"
22cb7eb8c9Sopenharmony_ci#include "ipc_skeleton.h"
23cb7eb8c9Sopenharmony_ci#include "utils_log.h"
24cb7eb8c9Sopenharmony_ci
25cb7eb8c9Sopenharmony_cinamespace OHOS {
26cb7eb8c9Sopenharmony_cinamespace Storage {
27cb7eb8c9Sopenharmony_cinamespace DistributedFile {
28cb7eb8c9Sopenharmony_ciusing namespace OHOS::FileManagement;
29cb7eb8c9Sopenharmony_ciconst int32_t UID = 1009;
30cb7eb8c9Sopenharmony_ciconst int32_t DATA_UID = 3012;
31cb7eb8c9Sopenharmony_ciDaemonStub::DaemonStub()
32cb7eb8c9Sopenharmony_ci{
33cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION)] =
34cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleOpenP2PConnection;
35cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
36cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION)] =
37cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleCloseP2PConnection;
38cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
39cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION_EX)] =
40cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleOpenP2PConnectionEx;
41cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
42cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION_EX)] =
43cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleCloseP2PConnectionEx;
44cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PREPARE_SESSION)] =
45cb7eb8c9Sopenharmony_ci        &DaemonStub::HandlePrepareSession;
46cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CANCEL_COPY_TASK)] =
47cb7eb8c9Sopenharmony_ci            &DaemonStub::HandleCancelCopyTask;
48cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE)] =
49cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleRequestSendFile;
50cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
51cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO)] =
52cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleGetRemoteCopyInfo;
53cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
54cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REGISTER_ASSET_CALLBACK)] =
55cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleRegisterRecvCallback;
56cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
57cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_UN_REGISTER_ASSET_CALLBACK)] =
58cb7eb8c9Sopenharmony_ci        &DaemonStub::HandleUnRegisterRecvCallback;
59cb7eb8c9Sopenharmony_ci    opToInterfaceMap_[static_cast<uint32_t>(
60cb7eb8c9Sopenharmony_ci        DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PUSH_ASSET)] =
61cb7eb8c9Sopenharmony_ci        &DaemonStub::HandlePushAsset;
62cb7eb8c9Sopenharmony_ci}
63cb7eb8c9Sopenharmony_ci
64cb7eb8c9Sopenharmony_ciint32_t DaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
65cb7eb8c9Sopenharmony_ci{
66cb7eb8c9Sopenharmony_ci    if (data.ReadInterfaceToken() != GetDescriptor()) {
67cb7eb8c9Sopenharmony_ci        return DFS_DAEMON_DESCRIPTOR_IS_EMPTY;
68cb7eb8c9Sopenharmony_ci    }
69cb7eb8c9Sopenharmony_ci    switch (code) {
70cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION):
71cb7eb8c9Sopenharmony_ci            return HandleOpenP2PConnection(data, reply);
72cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION):
73cb7eb8c9Sopenharmony_ci            return HandleCloseP2PConnection(data, reply);
74cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION_EX):
75cb7eb8c9Sopenharmony_ci            return HandleOpenP2PConnectionEx(data, reply);
76cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION_EX):
77cb7eb8c9Sopenharmony_ci            return HandleCloseP2PConnectionEx(data, reply);
78cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PREPARE_SESSION):
79cb7eb8c9Sopenharmony_ci            return HandlePrepareSession(data, reply);
80cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CANCEL_COPY_TASK):
81cb7eb8c9Sopenharmony_ci            return HandleCancelCopyTask(data, reply);
82cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE):
83cb7eb8c9Sopenharmony_ci            return HandleRequestSendFile(data, reply);
84cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO):
85cb7eb8c9Sopenharmony_ci            return HandleGetRemoteCopyInfo(data, reply);
86cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REGISTER_ASSET_CALLBACK):
87cb7eb8c9Sopenharmony_ci            return HandleRegisterRecvCallback(data, reply);
88cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_UN_REGISTER_ASSET_CALLBACK):
89cb7eb8c9Sopenharmony_ci            return HandleUnRegisterRecvCallback(data, reply);
90cb7eb8c9Sopenharmony_ci        case static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PUSH_ASSET):
91cb7eb8c9Sopenharmony_ci            return HandlePushAsset(data, reply);
92cb7eb8c9Sopenharmony_ci        default:
93cb7eb8c9Sopenharmony_ci            LOGE("Cannot response request %d: unknown tranction", code);
94cb7eb8c9Sopenharmony_ci            return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
95cb7eb8c9Sopenharmony_ci    }
96cb7eb8c9Sopenharmony_ci}
97cb7eb8c9Sopenharmony_ci
98cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleOpenP2PConnection(MessageParcel &data, MessageParcel &reply)
99cb7eb8c9Sopenharmony_ci{
100cb7eb8c9Sopenharmony_ci    LOGI("Begin OpenP2PConnection");
101cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
102cb7eb8c9Sopenharmony_ci        LOGE("[HandleOpenP2PConnection] DATASYNC permission denied");
103cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
104cb7eb8c9Sopenharmony_ci    }
105cb7eb8c9Sopenharmony_ci    DistributedHardware::DmDeviceInfo deviceInfo;
106cb7eb8c9Sopenharmony_ci    auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
107cb7eb8c9Sopenharmony_ci    if (ret != 0) {
108cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source device id failed, ret is %{public}d", ret);
109cb7eb8c9Sopenharmony_ci        return -1;
110cb7eb8c9Sopenharmony_ci    }
111cb7eb8c9Sopenharmony_ci    ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
112cb7eb8c9Sopenharmony_ci    if (ret != 0) {
113cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source device name failed, ret is %{public}d", ret);
114cb7eb8c9Sopenharmony_ci        return -1;
115cb7eb8c9Sopenharmony_ci    }
116cb7eb8c9Sopenharmony_ci    ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
117cb7eb8c9Sopenharmony_ci    if (ret != 0) {
118cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source network id failed, ret is %{public}d", ret);
119cb7eb8c9Sopenharmony_ci        return -1;
120cb7eb8c9Sopenharmony_ci    }
121cb7eb8c9Sopenharmony_ci    deviceInfo.deviceTypeId = data.ReadUint16();
122cb7eb8c9Sopenharmony_ci    deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
123cb7eb8c9Sopenharmony_ci    deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
124cb7eb8c9Sopenharmony_ci
125cb7eb8c9Sopenharmony_ci    int32_t res = OpenP2PConnection(deviceInfo);
126cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
127cb7eb8c9Sopenharmony_ci    LOGI("End OpenP2PConnection, res = %{public}d", res);
128cb7eb8c9Sopenharmony_ci    return res;
129cb7eb8c9Sopenharmony_ci}
130cb7eb8c9Sopenharmony_ci
131cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleCloseP2PConnection(MessageParcel &data, MessageParcel &reply)
132cb7eb8c9Sopenharmony_ci{
133cb7eb8c9Sopenharmony_ci    LOGI("Begin CloseP2PConnection");
134cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
135cb7eb8c9Sopenharmony_ci        LOGE("[HandleCloseP2PConnection] DATASYNC permission denied");
136cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
137cb7eb8c9Sopenharmony_ci    }
138cb7eb8c9Sopenharmony_ci    DistributedHardware::DmDeviceInfo deviceInfo;
139cb7eb8c9Sopenharmony_ci    auto ret = strcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
140cb7eb8c9Sopenharmony_ci    if (ret != 0) {
141cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source device id failed, ret is %{public}d", ret);
142cb7eb8c9Sopenharmony_ci        return -1;
143cb7eb8c9Sopenharmony_ci    }
144cb7eb8c9Sopenharmony_ci    ret = strcpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, data.ReadCString());
145cb7eb8c9Sopenharmony_ci    if (ret != 0) {
146cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source device name failed, ret is %{public}d", ret);
147cb7eb8c9Sopenharmony_ci        return -1;
148cb7eb8c9Sopenharmony_ci    }
149cb7eb8c9Sopenharmony_ci    ret = strcpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, data.ReadCString());
150cb7eb8c9Sopenharmony_ci    if (ret != 0) {
151cb7eb8c9Sopenharmony_ci        LOGE("strcpy for source network id failed, ret is %{public}d", ret);
152cb7eb8c9Sopenharmony_ci        return -1;
153cb7eb8c9Sopenharmony_ci    }
154cb7eb8c9Sopenharmony_ci    deviceInfo.deviceTypeId = data.ReadUint16();
155cb7eb8c9Sopenharmony_ci    deviceInfo.range = static_cast<int32_t>(data.ReadUint32());
156cb7eb8c9Sopenharmony_ci    deviceInfo.authForm = static_cast<DistributedHardware::DmAuthForm>(data.ReadInt32());
157cb7eb8c9Sopenharmony_ci
158cb7eb8c9Sopenharmony_ci    int32_t res = CloseP2PConnection(deviceInfo);
159cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
160cb7eb8c9Sopenharmony_ci    LOGI("End CloseP2PConnection");
161cb7eb8c9Sopenharmony_ci    return res;
162cb7eb8c9Sopenharmony_ci}
163cb7eb8c9Sopenharmony_ci
164cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleOpenP2PConnectionEx(MessageParcel &data, MessageParcel &reply)
165cb7eb8c9Sopenharmony_ci{
166cb7eb8c9Sopenharmony_ci    LOGI("DaemonStub::Begin OpenP2PConnectionEx");
167cb7eb8c9Sopenharmony_ci    std::string networkId;
168cb7eb8c9Sopenharmony_ci    if (!data.ReadString(networkId)) {
169cb7eb8c9Sopenharmony_ci        LOGE("read networkId failed");
170cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
171cb7eb8c9Sopenharmony_ci    }
172cb7eb8c9Sopenharmony_ci    auto remote = data.ReadRemoteObject();
173cb7eb8c9Sopenharmony_ci    if (remote == nullptr) {
174cb7eb8c9Sopenharmony_ci        LOGE("read remoteObject failed");
175cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
176cb7eb8c9Sopenharmony_ci    }
177cb7eb8c9Sopenharmony_ci    auto remoteReverseObj = iface_cast<IFileDfsListener>(remote);
178cb7eb8c9Sopenharmony_ci    if (remoteReverseObj == nullptr) {
179cb7eb8c9Sopenharmony_ci        LOGE("remoteReverseObj is null");
180cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
181cb7eb8c9Sopenharmony_ci    }
182cb7eb8c9Sopenharmony_ci    int32_t res = OpenP2PConnectionEx(networkId, remoteReverseObj);
183cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
184cb7eb8c9Sopenharmony_ci    LOGI("DaemonStub::End OpenP2PConnection, res = %{public}d.", res);
185cb7eb8c9Sopenharmony_ci    return res;
186cb7eb8c9Sopenharmony_ci}
187cb7eb8c9Sopenharmony_ci
188cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleCloseP2PConnectionEx(MessageParcel &data, MessageParcel &reply)
189cb7eb8c9Sopenharmony_ci{
190cb7eb8c9Sopenharmony_ci    LOGI("DaemonStub::Begin CloseP2PConnection.");
191cb7eb8c9Sopenharmony_ci    std::string networkId;
192cb7eb8c9Sopenharmony_ci    if (!data.ReadString(networkId)) {
193cb7eb8c9Sopenharmony_ci        LOGE("read networkId failed");
194cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
195cb7eb8c9Sopenharmony_ci    }
196cb7eb8c9Sopenharmony_ci
197cb7eb8c9Sopenharmony_ci    int32_t res = CloseP2PConnectionEx(networkId);
198cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
199cb7eb8c9Sopenharmony_ci    LOGI("DaemonStub::End CloseP2PConnection");
200cb7eb8c9Sopenharmony_ci    return res;
201cb7eb8c9Sopenharmony_ci}
202cb7eb8c9Sopenharmony_ci
203cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandlePrepareSession(MessageParcel &data, MessageParcel &reply)
204cb7eb8c9Sopenharmony_ci{
205cb7eb8c9Sopenharmony_ci    std::string srcUri;
206cb7eb8c9Sopenharmony_ci    if (!data.ReadString(srcUri)) {
207cb7eb8c9Sopenharmony_ci        LOGE("read srcUri failed");
208cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
209cb7eb8c9Sopenharmony_ci    }
210cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckUriPermission(srcUri)) {
211cb7eb8c9Sopenharmony_ci        LOGE("permission verify failed");
212cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
213cb7eb8c9Sopenharmony_ci    }
214cb7eb8c9Sopenharmony_ci    std::string dstUri;
215cb7eb8c9Sopenharmony_ci    if (!data.ReadString(dstUri)) {
216cb7eb8c9Sopenharmony_ci        LOGE("read dstUri failed");
217cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
218cb7eb8c9Sopenharmony_ci    }
219cb7eb8c9Sopenharmony_ci    std::string srcDeviceId;
220cb7eb8c9Sopenharmony_ci    if (!data.ReadString(srcDeviceId)) {
221cb7eb8c9Sopenharmony_ci        LOGE("read srcDeviceId failed");
222cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
223cb7eb8c9Sopenharmony_ci    }
224cb7eb8c9Sopenharmony_ci    auto listener = data.ReadRemoteObject();
225cb7eb8c9Sopenharmony_ci    if (listener == nullptr) {
226cb7eb8c9Sopenharmony_ci        LOGE("read listener failed");
227cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
228cb7eb8c9Sopenharmony_ci    }
229cb7eb8c9Sopenharmony_ci    HmdfsInfo info{};
230cb7eb8c9Sopenharmony_ci    if (!data.ReadString(info.copyPath)) {
231cb7eb8c9Sopenharmony_ci        LOGE("read info.copyPath failed");
232cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
233cb7eb8c9Sopenharmony_ci    }
234cb7eb8c9Sopenharmony_ci    if (!data.ReadBool(info.dirExistFlag)) {
235cb7eb8c9Sopenharmony_ci        LOGE("read info.dirExistFlag failed");
236cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
237cb7eb8c9Sopenharmony_ci    }
238cb7eb8c9Sopenharmony_ci    int32_t res = PrepareSession(srcUri, dstUri, srcDeviceId, listener, info);
239cb7eb8c9Sopenharmony_ci    if (!reply.WriteString(info.sessionName)) {
240cb7eb8c9Sopenharmony_ci        LOGE("Write sessionName failed");
241cb7eb8c9Sopenharmony_ci        return E_IPC_WRITE_FAILED;
242cb7eb8c9Sopenharmony_ci    }
243cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
244cb7eb8c9Sopenharmony_ci    LOGD("End PrepareSession, ret = %{public}d.", res);
245cb7eb8c9Sopenharmony_ci    return res;
246cb7eb8c9Sopenharmony_ci}
247cb7eb8c9Sopenharmony_ci
248cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleRequestSendFile(MessageParcel &data, MessageParcel &reply)
249cb7eb8c9Sopenharmony_ci{
250cb7eb8c9Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
251cb7eb8c9Sopenharmony_ci    if (uid != UID) {
252cb7eb8c9Sopenharmony_ci        LOGE("Permission denied, caller is not dfs!");
253cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
254cb7eb8c9Sopenharmony_ci    }
255cb7eb8c9Sopenharmony_ci    std::string srcUri;
256cb7eb8c9Sopenharmony_ci    if (!data.ReadString(srcUri)) {
257cb7eb8c9Sopenharmony_ci        LOGE("read srcUri failed");
258cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
259cb7eb8c9Sopenharmony_ci    }
260cb7eb8c9Sopenharmony_ci    std::string dstPath;
261cb7eb8c9Sopenharmony_ci    if (!data.ReadString(dstPath)) {
262cb7eb8c9Sopenharmony_ci        LOGE("read dstPath failed");
263cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
264cb7eb8c9Sopenharmony_ci    }
265cb7eb8c9Sopenharmony_ci    std::string dstDeviceId;
266cb7eb8c9Sopenharmony_ci    if (!data.ReadString(dstDeviceId)) {
267cb7eb8c9Sopenharmony_ci        LOGE("read remoteDeviceId failed");
268cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
269cb7eb8c9Sopenharmony_ci    }
270cb7eb8c9Sopenharmony_ci    std::string sessionName;
271cb7eb8c9Sopenharmony_ci    if (!data.ReadString(sessionName)) {
272cb7eb8c9Sopenharmony_ci        LOGE("read sessionName failed");
273cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
274cb7eb8c9Sopenharmony_ci    }
275cb7eb8c9Sopenharmony_ci    auto res = RequestSendFile(srcUri, dstPath, dstDeviceId, sessionName);
276cb7eb8c9Sopenharmony_ci    reply.WriteInt32(res);
277cb7eb8c9Sopenharmony_ci    LOGD("End RequestSendFile, ret = %{public}d.", res);
278cb7eb8c9Sopenharmony_ci    return res;
279cb7eb8c9Sopenharmony_ci}
280cb7eb8c9Sopenharmony_ci
281cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleGetRemoteCopyInfo(MessageParcel &data, MessageParcel &reply)
282cb7eb8c9Sopenharmony_ci{
283cb7eb8c9Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
284cb7eb8c9Sopenharmony_ci    if (uid != UID) {
285cb7eb8c9Sopenharmony_ci        LOGE("Permission denied, caller is not dfs!");
286cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
287cb7eb8c9Sopenharmony_ci    }
288cb7eb8c9Sopenharmony_ci    std::string srcUri;
289cb7eb8c9Sopenharmony_ci    if (!data.ReadString(srcUri)) {
290cb7eb8c9Sopenharmony_ci        LOGE("read srcUri failed");
291cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
292cb7eb8c9Sopenharmony_ci    }
293cb7eb8c9Sopenharmony_ci    bool isFile = false;
294cb7eb8c9Sopenharmony_ci    bool isDir = false;
295cb7eb8c9Sopenharmony_ci    auto res = GetRemoteCopyInfo(srcUri, isFile, isDir);
296cb7eb8c9Sopenharmony_ci    if (res != E_OK) {
297cb7eb8c9Sopenharmony_ci        LOGE("GetRemoteCopyInfo failed");
298cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
299cb7eb8c9Sopenharmony_ci    }
300cb7eb8c9Sopenharmony_ci    if (!reply.WriteBool(isFile)) {
301cb7eb8c9Sopenharmony_ci        LOGE("Write isFile failed");
302cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
303cb7eb8c9Sopenharmony_ci    }
304cb7eb8c9Sopenharmony_ci    if (!reply.WriteBool(isDir)) {
305cb7eb8c9Sopenharmony_ci        LOGE("Write isDir failed");
306cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
307cb7eb8c9Sopenharmony_ci    }
308cb7eb8c9Sopenharmony_ci    if (!reply.WriteInt32(res)) {
309cb7eb8c9Sopenharmony_ci        LOGE("Write res failed");
310cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
311cb7eb8c9Sopenharmony_ci    }
312cb7eb8c9Sopenharmony_ci    LOGD("End GetRemoteCopyInfo, ret = %{public}d.", res);
313cb7eb8c9Sopenharmony_ci    return res;
314cb7eb8c9Sopenharmony_ci}
315cb7eb8c9Sopenharmony_ci
316cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleCancelCopyTask(MessageParcel &data, MessageParcel &reply)
317cb7eb8c9Sopenharmony_ci{
318cb7eb8c9Sopenharmony_ci    std::string sessionName;
319cb7eb8c9Sopenharmony_ci    if (!data.ReadString(sessionName)) {
320cb7eb8c9Sopenharmony_ci        LOGE("read sessionName failed");
321cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
322cb7eb8c9Sopenharmony_ci    }
323cb7eb8c9Sopenharmony_ci    return CancelCopyTask(sessionName);
324cb7eb8c9Sopenharmony_ci}
325cb7eb8c9Sopenharmony_ci
326cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleRegisterRecvCallback(MessageParcel &data, MessageParcel &reply)
327cb7eb8c9Sopenharmony_ci{
328cb7eb8c9Sopenharmony_ci    LOGI("Begin RegisterRecvCallback");
329cb7eb8c9Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
330cb7eb8c9Sopenharmony_ci    if (uid != DATA_UID) {
331cb7eb8c9Sopenharmony_ci        LOGE("Permission denied, caller is not data!");
332cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
333cb7eb8c9Sopenharmony_ci    }
334cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
335cb7eb8c9Sopenharmony_ci        LOGE("[RegisterRecvCallback] DATASYNC permission denied");
336cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
337cb7eb8c9Sopenharmony_ci    }
338cb7eb8c9Sopenharmony_ci    auto object = data.ReadRemoteObject();
339cb7eb8c9Sopenharmony_ci    if (object == nullptr) {
340cb7eb8c9Sopenharmony_ci        LOGE("RegisterRecvCallback failed, object is nullptr.");
341cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
342cb7eb8c9Sopenharmony_ci    }
343cb7eb8c9Sopenharmony_ci    auto recvCallback = iface_cast<IAssetRecvCallback>(object);
344cb7eb8c9Sopenharmony_ci    if (recvCallback == nullptr) {
345cb7eb8c9Sopenharmony_ci        LOGE("RegisterRecvCallback failed, Callback is nullptr");
346cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
347cb7eb8c9Sopenharmony_ci    }
348cb7eb8c9Sopenharmony_ci    int32_t res = RegisterAssetCallback(recvCallback);
349cb7eb8c9Sopenharmony_ci    if (!reply.WriteInt32(res)) {
350cb7eb8c9Sopenharmony_ci        LOGE("RegisterRecvCallback write res failed, res is %{public}d", res);
351cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
352cb7eb8c9Sopenharmony_ci    }
353cb7eb8c9Sopenharmony_ci    return res;
354cb7eb8c9Sopenharmony_ci}
355cb7eb8c9Sopenharmony_ci
356cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandleUnRegisterRecvCallback(MessageParcel &data, MessageParcel &reply)
357cb7eb8c9Sopenharmony_ci{
358cb7eb8c9Sopenharmony_ci    LOGI("Begin UnRegisterRecvCallback");
359cb7eb8c9Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
360cb7eb8c9Sopenharmony_ci    if (uid != DATA_UID) {
361cb7eb8c9Sopenharmony_ci        LOGE("Permission denied, caller is not data!");
362cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
363cb7eb8c9Sopenharmony_ci    }
364cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
365cb7eb8c9Sopenharmony_ci        LOGE("[UnRegisterRecvCallback] DATASYNC permission denied");
366cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
367cb7eb8c9Sopenharmony_ci    }
368cb7eb8c9Sopenharmony_ci    auto object = data.ReadRemoteObject();
369cb7eb8c9Sopenharmony_ci    if (object == nullptr) {
370cb7eb8c9Sopenharmony_ci        LOGE("UnRegisterRecvCallback failed, object is nullptr.");
371cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
372cb7eb8c9Sopenharmony_ci    }
373cb7eb8c9Sopenharmony_ci    auto recvCallback = iface_cast<IAssetRecvCallback>(object);
374cb7eb8c9Sopenharmony_ci    if (recvCallback == nullptr) {
375cb7eb8c9Sopenharmony_ci        LOGE("UnRegisterRecvCallback failed, Callback is nullptr");
376cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
377cb7eb8c9Sopenharmony_ci    }
378cb7eb8c9Sopenharmony_ci    int32_t res = UnRegisterAssetCallback(recvCallback);
379cb7eb8c9Sopenharmony_ci    if (!reply.WriteInt32(res)) {
380cb7eb8c9Sopenharmony_ci        LOGE("UnRegisterRecvCallback write res failed, res is %{public}d", res);
381cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
382cb7eb8c9Sopenharmony_ci    }
383cb7eb8c9Sopenharmony_ci    return res;
384cb7eb8c9Sopenharmony_ci}
385cb7eb8c9Sopenharmony_ci
386cb7eb8c9Sopenharmony_ciint32_t DaemonStub::HandlePushAsset(MessageParcel &data, MessageParcel &reply)
387cb7eb8c9Sopenharmony_ci{
388cb7eb8c9Sopenharmony_ci    LOGI("Begin PushAsset");
389cb7eb8c9Sopenharmony_ci    auto uid = IPCSkeleton::GetCallingUid();
390cb7eb8c9Sopenharmony_ci    if (uid != DATA_UID) {
391cb7eb8c9Sopenharmony_ci        LOGE("Permission denied, caller is not data!");
392cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
393cb7eb8c9Sopenharmony_ci    }
394cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
395cb7eb8c9Sopenharmony_ci        LOGE("[PushAsset] DATASYNC permission denied");
396cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
397cb7eb8c9Sopenharmony_ci    }
398cb7eb8c9Sopenharmony_ci    int32_t userId;
399cb7eb8c9Sopenharmony_ci    if (!data.ReadInt32(userId)) {
400cb7eb8c9Sopenharmony_ci        LOGE("read userId failed");
401cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
402cb7eb8c9Sopenharmony_ci    }
403cb7eb8c9Sopenharmony_ci
404cb7eb8c9Sopenharmony_ci    sptr<AssetObj> assetObj = data.ReadParcelable<AssetObj>();
405cb7eb8c9Sopenharmony_ci    if (!assetObj) {
406cb7eb8c9Sopenharmony_ci        LOGE("object of AssetObj is nullptr");
407cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
408cb7eb8c9Sopenharmony_ci    }
409cb7eb8c9Sopenharmony_ci
410cb7eb8c9Sopenharmony_ci    auto object = data.ReadRemoteObject();
411cb7eb8c9Sopenharmony_ci    if (object == nullptr) {
412cb7eb8c9Sopenharmony_ci        LOGE("PushAsset failed, object is nullptr.");
413cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
414cb7eb8c9Sopenharmony_ci    }
415cb7eb8c9Sopenharmony_ci    auto sendCallback = iface_cast<IAssetSendCallback>(object);
416cb7eb8c9Sopenharmony_ci    if (sendCallback == nullptr) {
417cb7eb8c9Sopenharmony_ci        LOGE("PushAsset failed, Callback is nullptr");
418cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
419cb7eb8c9Sopenharmony_ci    }
420cb7eb8c9Sopenharmony_ci
421cb7eb8c9Sopenharmony_ci    int32_t res = PushAsset(userId, assetObj, sendCallback);
422cb7eb8c9Sopenharmony_ci    if (!reply.WriteInt32(res)) {
423cb7eb8c9Sopenharmony_ci        LOGE("PushAsset write res failed, res is %{public}d", res);
424cb7eb8c9Sopenharmony_ci        return E_IPC_READ_FAILED;
425cb7eb8c9Sopenharmony_ci    }
426cb7eb8c9Sopenharmony_ci    return res;
427cb7eb8c9Sopenharmony_ci}
428cb7eb8c9Sopenharmony_ci} // namespace DistributedFile
429cb7eb8c9Sopenharmony_ci} // namespace Storage
430cb7eb8c9Sopenharmony_ci} // namespace OHOS