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_stub.h"
17 
18 #include "dfs_error.h"
19 #include "file_trans_listener_interface_code.h"
20 #include "utils_log.h"
21 
22 namespace OHOS {
23 namespace Storage {
24 namespace DistributedFile {
25 using namespace FileManagement;
FileTransListenerStub()26 FileTransListenerStub::FileTransListenerStub()
27 {
28     opToInterfaceMap_[static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_PROGRESS)] =
29         &FileTransListenerStub::HandleOnFileReceive;
30     opToInterfaceMap_[static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FAILED)] =
31         &FileTransListenerStub::HandleOnFailed;
32     opToInterfaceMap_[static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FINISHED)] =
33         &FileTransListenerStub::HandleOnFinished;
34 }
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t FileTransListenerStub::OnRemoteRequest(uint32_t code,
37                                                MessageParcel &data,
38                                                MessageParcel &reply,
39                                                MessageOption &option)
40 {
41     if (data.ReadInterfaceToken() != GetDescriptor()) {
42         return File_Trans_Listener_DESCRIPTOR_IS_EMPTY;
43     }
44     switch (code) {
45         case static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_PROGRESS):
46             return HandleOnFileReceive(data, reply);
47         case static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FAILED):
48             return HandleOnFailed(data, reply);
49         case static_cast<uint32_t>(FileTransListenerInterfaceCode::FILE_TRANS_LISTENER_ON_FINISHED):
50             return HandleOnFinished(data, reply);
51         default:
52             LOGE("Cannot response request %d: unknown tranction", code);
53             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54     }
55 }
56 
HandleOnFileReceive(MessageParcel & data,MessageParcel & reply)57 int32_t FileTransListenerStub::HandleOnFileReceive(MessageParcel &data, MessageParcel &reply)
58 {
59     uint64_t totalBytes = 0;
60     if (!data.ReadUint64(totalBytes)) {
61         LOGE("read totalBytes failed");
62         return E_INVAL_ARG;
63     }
64     uint64_t processedBytes = 0;
65     if (!data.ReadUint64(processedBytes)) {
66         LOGE("read processedBytes failed");
67         return E_INVAL_ARG;
68     }
69     int32_t res = OnFileReceive(totalBytes, processedBytes);
70     if (res != E_OK) {
71         LOGE("OnFileReceive call failed");
72         return E_BROKEN_IPC;
73     }
74     reply.WriteInt32(res);
75     return res;
76 }
77 
HandleOnFailed(MessageParcel & data,MessageParcel & reply)78 int32_t FileTransListenerStub::HandleOnFailed(MessageParcel &data, MessageParcel &reply)
79 {
80     std::string sessionName;
81     if (!data.ReadString(sessionName)) {
82         LOGE("read sessionName failed");
83         return E_INVAL_ARG;
84     }
85     std::int32_t errorCode;
86     if (!data.ReadInt32(errorCode)) {
87         LOGE("read errorCode failed");
88         return E_INVAL_ARG;
89     }
90     int32_t res = OnFailed(sessionName, errorCode);
91     if (res != E_OK) {
92         LOGE("OnFailed call failed");
93         return E_BROKEN_IPC;
94     }
95     reply.WriteInt32(res);
96     return res;
97 }
98 
HandleOnFinished(MessageParcel & data,MessageParcel & reply)99 int32_t FileTransListenerStub::HandleOnFinished(MessageParcel &data, MessageParcel &reply)
100 {
101     std::string sessionName;
102     if (!data.ReadString(sessionName)) {
103         LOGE("read sessionName failed");
104         return E_INVAL_ARG;
105     }
106     int32_t res = OnFinished(sessionName);
107     if (res != E_OK) {
108         LOGE("OnFinished call failed");
109         return E_BROKEN_IPC;
110     }
111     reply.WriteInt32(res);
112     return res;
113 }
114 
115 } // namespace DistributedFile
116 } // namespace Storage
117 } // namespace OHOS