1 /*
2  * Copyright (c) 2023-2024 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 "network/softbus/softbus_file_receive_listener.h"
17 
18 #include <cinttypes>
19 
20 #include "dfs_daemon_event_dfx.h"
21 #include "dfs_error.h"
22 #include "network/softbus/softbus_handler.h"
23 #include "sandbox_helper.h"
24 #include "trans_mananger.h"
25 #include "utils_directory.h"
26 #include "utils_log.h"
27 
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 using namespace FileManagement;
32 std::string SoftBusFileReceiveListener::path_ = "";
OnFile(int32_t socket,FileEvent * event)33 void SoftBusFileReceiveListener::OnFile(int32_t socket, FileEvent *event)
34 {
35     if (event == nullptr) {
36         LOGE("invalid parameter");
37         return;
38     }
39     switch (event->type) {
40         case FILE_EVENT_RECV_START:
41             OnReceiveFileStarted(socket, event->fileCnt);
42             break;
43         case FILE_EVENT_RECV_PROCESS:
44             OnReceiveFileProcess(socket, event->bytesProcessed, event->bytesTotal);
45             break;
46         case FILE_EVENT_RECV_FINISH:
47             OnReceiveFileFinished(socket, event->fileCnt);
48             break;
49         case FILE_EVENT_RECV_ERROR:
50             OnFileTransError(socket, event->errorCode);
51             break;
52         case FILE_EVENT_TRANS_STATUS:
53             OnReceiveFileReport(socket, event->statusList, event->errorCode);
54             break;
55         case FILE_EVENT_RECV_UPDATE_PATH:
56             event->UpdateRecvPath = GetRecvPath;
57             break;
58         default:
59             LOGI("Other situations");
60             break;
61     }
62 }
63 
GetRecvPath()64 const char* SoftBusFileReceiveListener::GetRecvPath()
65 {
66     const char* path = path_.c_str();
67     LOGI("path: %s", path);
68     return path;
69 }
70 
SetRecvPath(const std::string & physicalPath)71 void SoftBusFileReceiveListener::SetRecvPath(const std::string &physicalPath)
72 {
73     if (physicalPath.empty()) {
74         LOGI("SetRecvPath physicalPath is empty.");
75         return;
76     }
77     LOGI("SetRecvPath physicalPath: %{public}s", GetAnonyString(physicalPath).c_str());
78     if (!AppFileService::SandboxHelper::CheckValidPath(physicalPath)) {
79         LOGE("invalid path.");
80         RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
81             RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
82             RadarReporter::CREAT_VALID_PATH_ERROR, RadarReporter::PACKAGE_NAME, RadarReporter::appFileService);
83         path_ = "";
84         return;
85     }
86     path_ = physicalPath;
87 }
88 
OnCopyReceiveBind(int32_t socketId,PeerSocketInfo info)89 void SoftBusFileReceiveListener::OnCopyReceiveBind(int32_t socketId, PeerSocketInfo info)
90 {
91     LOGI("OnCopyReceiveBind begin, socketId %{public}d", socketId);
92     SoftBusHandler::OnSinkSessionOpened(socketId, info);
93 }
94 
GetLocalSessionName(int32_t sessionId)95 std::string SoftBusFileReceiveListener::GetLocalSessionName(int32_t sessionId)
96 {
97     std::string sessionName = "";
98     sessionName = SoftBusHandler::GetSessionName(sessionId);
99     return sessionName;
100 }
101 
OnReceiveFileStarted(int32_t sessionId,int32_t fileCnt)102 void SoftBusFileReceiveListener::OnReceiveFileStarted(int32_t sessionId, int32_t fileCnt)
103 {
104     LOGI("OnReceiveFileStarted, sessionId = %{public}d, fileCnt = %{public}d", sessionId, fileCnt);
105 }
106 
OnReceiveFileProcess(int32_t sessionId,uint64_t bytesUpload,uint64_t bytesTotal)107 void SoftBusFileReceiveListener::OnReceiveFileProcess(int32_t sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
108 {
109     LOGI("OnReceiveFileProcess, sessionId = %{public}d, bytesUpload = %{public}" PRIu64 ","
110          "bytesTotal = %{public}" PRIu64 "", sessionId, bytesUpload, bytesTotal);
111     std::string sessionName = GetLocalSessionName(sessionId);
112     if (sessionName.empty()) {
113         LOGE("sessionName is empty");
114         return;
115     }
116     TransManager::GetInstance().NotifyFileProgress(sessionName, bytesTotal, bytesUpload);
117 }
118 
OnReceiveFileFinished(int32_t sessionId,int32_t fileCnt)119 void SoftBusFileReceiveListener::OnReceiveFileFinished(int32_t sessionId, int32_t fileCnt)
120 {
121     LOGI("OnReceiveFileFinished, sessionId = %{public}d, fileCnt = %{public}d", sessionId, fileCnt);
122     std::string sessionName = GetLocalSessionName(sessionId);
123     if (sessionName.empty()) {
124         LOGE("sessionName is empty");
125         return;
126     }
127     TransManager::GetInstance().NotifyFileFinished(sessionName);
128     TransManager::GetInstance().DeleteTransTask(sessionName);
129     SoftBusHandler::GetInstance().ChangeOwnerIfNeeded(sessionId, sessionName);
130 }
131 
OnFileTransError(int32_t sessionId,int32_t errorCode)132 void SoftBusFileReceiveListener::OnFileTransError(int32_t sessionId, int32_t errorCode)
133 {
134     LOGE("OnFileTransError");
135     RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
136         RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
137         RadarReporter::FILE_TRANS_ERROR, RadarReporter::PACKAGE_NAME,
138         RadarReporter::dSoftBus + std::to_string(errorCode));
139     std::string sessionName = GetLocalSessionName(sessionId);
140     if (sessionName.empty()) {
141         LOGE("sessionName is empty");
142         return;
143     }
144     TransManager::GetInstance().NotifyFileFailed(sessionName, errorCode);
145     TransManager::GetInstance().DeleteTransTask(sessionName);
146     SoftBusHandler::GetInstance().CloseSession(sessionId, sessionName);
147 }
148 
OnReceiveFileReport(int32_t sessionId,FileStatusList statusList,int32_t errorCode)149 void SoftBusFileReceiveListener::OnReceiveFileReport(int32_t sessionId, FileStatusList statusList, int32_t errorCode)
150 {
151     LOGE("OnReceiveFileReport");
152     std::string sessionName = GetLocalSessionName(sessionId);
153     if (sessionName.empty()) {
154         LOGE("sessionName is empty");
155         return;
156     }
157     TransManager::GetInstance().NotifyFileFailed(sessionName, errorCode);
158 }
159 
OnReceiveFileShutdown(int32_t sessionId,ShutdownReason reason)160 void SoftBusFileReceiveListener::OnReceiveFileShutdown(int32_t sessionId, ShutdownReason reason)
161 {
162     LOGI("OnReceiveFileShutdown, sessionId is %{public}d", sessionId);
163     std::string sessionName = GetLocalSessionName(sessionId);
164     if (sessionName.empty()) {
165         LOGE("sessionName is empty");
166         return;
167     }
168     TransManager::GetInstance().NotifyFileFailed(sessionName, E_SOFTBUS_SESSION_FAILED);
169     TransManager::GetInstance().DeleteTransTask(sessionName);
170     SoftBusHandler::GetInstance().CloseSession(sessionId, sessionName);
171 }
172 } // namespace DistributedFile
173 } // namespace Storage
174 } // namespace OHOS