1 /*
2  * Copyright (c) 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 "file_trans_listener_proxy.h"
17 
18 #include "dfs_error.h"
19 #include "file_trans_listener_interface_code.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "utils_log.h"
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 using namespace std;
27 using namespace OHOS::Storage;
28 using namespace OHOS::FileManagement;
29 
OnFileReceive(uint64_t totalBytes,uint64_t processedBytes)30 int32_t FileTransListenerProxy::OnFileReceive(uint64_t totalBytes, uint64_t processedBytes)
31 {
32     MessageParcel data;
33     MessageParcel reply;
34     MessageOption option;
35     if (!data.WriteInterfaceToken(GetDescriptor())) {
36         LOGE("Failed to write interface token");
37         return E_BROKEN_IPC;
38     }
39     if (!data.WriteUint64(totalBytes)) {
40         LOGE("Failed to send bytesTotal");
41         return E_INVAL_ARG;
42     }
43     if (!data.WriteUint64(processedBytes)) {
44         LOGE("Failed to send bytesUpload");
45         return E_INVAL_ARG;
46     }
47     auto remote = Remote();
48     if (remote == nullptr) {
49         LOGE("remote is nullptr");
50         return E_BROKEN_IPC;
51     }
52     int32_t ret = remote->SendRequest(
53         static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_PROGRESS), data, reply, option);
54     if (ret != E_OK) {
55         LOGE("Failed to send out the request, errno:%{public}d", errno);
56         return E_BROKEN_IPC;
57     }
58     return reply.ReadInt32();
59 }
60 
OnFailed(const std::string & sessionName,int32_t errorCode)61 int32_t FileTransListenerProxy::OnFailed(const std::string &sessionName, int32_t errorCode)
62 {
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option;
66     if (!data.WriteInterfaceToken(GetDescriptor())) {
67         LOGE("Failed to write interface token");
68         return E_BROKEN_IPC;
69     }
70     if (!data.WriteString(sessionName)) {
71         LOGE("Failed to send sessionName");
72         return E_INVAL_ARG;
73     }
74     if (!data.WriteInt32(errorCode)) {
75         LOGE("Failed to send errorCode");
76         return E_INVAL_ARG;
77     }
78     auto remote = Remote();
79     if (remote == nullptr) {
80         LOGE("remote is nullptr");
81         return E_BROKEN_IPC;
82     }
83     int32_t ret = remote->SendRequest(
84         static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FAILED), data, reply, option);
85     if (ret != E_OK) {
86         LOGE("Failed to send out the request, errno:%{public}d", errno);
87         return E_BROKEN_IPC;
88     }
89     return reply.ReadInt32();
90 }
91 
OnFinished(const std::string & sessionName)92 int32_t FileTransListenerProxy::OnFinished(const std::string &sessionName)
93 {
94     MessageParcel data;
95     MessageParcel reply;
96     MessageOption option;
97     if (!data.WriteInterfaceToken(GetDescriptor())) {
98         LOGE("Failed to write interface token");
99         return E_BROKEN_IPC;
100     }
101     if (!data.WriteString(sessionName)) {
102         LOGE("Failed to send sessionName");
103         return E_INVAL_ARG;
104     }
105     auto remote = Remote();
106     if (remote == nullptr) {
107         LOGE("remote is nullptr");
108         return E_BROKEN_IPC;
109     }
110     int32_t ret = remote->SendRequest(
111         static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FINISHED), data, reply, option);
112     if (ret != E_OK) {
113         LOGE("Failed to send out the request, errno:%{public}d", errno);
114         return E_BROKEN_IPC;
115     }
116     return reply.ReadInt32();
117 }
118 
119 } // namespace DistributedFile
120 } // namespace Storage
121 } // namespace OHOS