1 /*
2 * Copyright (c) 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_asset_send_listener.h"
17
18 #include "asset_callback_manager.h"
19 #include "dfs_error.h"
20 #include "network/softbus/softbus_handler_asset.h"
21 #include "utils_log.h"
22
23 namespace OHOS {
24 namespace Storage {
25 namespace DistributedFile {
26 std::map<std::string, std::pair<std::string, bool>> SoftBusAssetSendListener::taskIsSingleFileMap_;
OnFile(int32_t socket,FileEvent * event)27 void SoftBusAssetSendListener::OnFile(int32_t socket, FileEvent *event)
28 {
29 if (event == nullptr) {
30 LOGE("invalid paramter");
31 return;
32 }
33 switch (event->type) {
34 case FILE_EVENT_SEND_FINISH:
35 OnSendAssetFinished(socket, event->files, event->fileCnt);
36 break;
37 case FILE_EVENT_SEND_ERROR:
38 OnSendAssetError(socket, event->files, event->fileCnt, event->errorCode);
39 break;
40 default:
41 LOGI("Other situations");
42 break;
43 }
44 }
45
OnSendAssetFinished(int32_t socketId,const char ** fileList,int32_t fileCnt)46 void SoftBusAssetSendListener::OnSendAssetFinished(int32_t socketId, const char **fileList, int32_t fileCnt)
47 {
48 std::lock_guard<std::recursive_mutex> lock(mtx_);
49 LOGI("Push asset finished, socketId is %{public}d", socketId);
50 if (fileCnt == 0) {
51 LOGE("fileList has no file");
52 return;
53 }
54 auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(socketId);
55 if (assetObj == nullptr) {
56 LOGE("OnSendAssetFinished get assetObj is nullptr");
57 return;
58 }
59 auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
60 AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_OK);
61 SoftBusHandlerAsset::GetInstance().closeAssetBind(socketId);
62 AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
63 SoftBusHandlerAsset::GetInstance().RemoveFile(fileList[0], GetIsZipFile(taskId));
64 RemoveFileMap(taskId);
65 }
66
OnSendAssetError(int32_t socketId,const char ** fileList,int32_t fileCnt,int32_t errorCode)67 void SoftBusAssetSendListener::OnSendAssetError(int32_t socketId,
68 const char **fileList,
69 int32_t fileCnt,
70 int32_t errorCode)
71 {
72 std::lock_guard<std::recursive_mutex> lock(mtx_);
73 LOGE("SendAssetError, socketId is %{public}d, errorCode %{public}d", socketId, errorCode);
74 if (fileCnt == 0) {
75 LOGE("fileList has no file");
76 return;
77 }
78 auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(socketId);
79 if (assetObj == nullptr) {
80 LOGE("OnSendAssetError get assetObj is nullptr");
81 return;
82 }
83 auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
84 AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_SEND_FILE);
85 SoftBusHandlerAsset::GetInstance().closeAssetBind(socketId);
86 AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
87 SoftBusHandlerAsset::GetInstance().RemoveFile(fileList[0], GetIsZipFile(taskId));
88 RemoveFileMap(taskId);
89 }
90
DisConnectByAllConnect(const std::string & peerNetworkId)91 void SoftBusAssetSendListener::DisConnectByAllConnect(const std::string &peerNetworkId)
92 {
93 std::lock_guard<std::recursive_mutex> lock(mtx_);
94 auto socketIds = SoftBusHandlerAsset::GetInstance().GetSocketIdFromAssetObj(peerNetworkId);
95 for (auto socketId : socketIds) {
96 auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(socketId);
97 if (assetObj == nullptr) {
98 LOGE("OnSendAssetError get assetObj is nullptr");
99 continue;
100 }
101 auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
102 AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_SEND_FILE);
103 SoftBusHandlerAsset::GetInstance().closeAssetBind(socketId);
104 AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
105 auto it = taskIsSingleFileMap_.find(taskId);
106 if (it != taskIsSingleFileMap_.end()) {
107 SoftBusHandlerAsset::GetInstance().RemoveFile(it->second.first, !it->second.second);
108 taskIsSingleFileMap_.erase(it);
109 }
110 }
111 }
112
OnSendShutdown(int32_t sessionId,ShutdownReason reason)113 void SoftBusAssetSendListener::OnSendShutdown(int32_t sessionId, ShutdownReason reason)
114 {
115 std::lock_guard<std::recursive_mutex> lock(mtx_);
116 LOGE("OnSessionClosed, sessionId = %{public}d, reason = %{public}d", sessionId, reason);
117 auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(sessionId);
118 if (assetObj == nullptr) {
119 LOGW("get assetObj is nullptr");
120 return;
121 }
122 auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
123 AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_SEND_FILE);
124 SoftBusHandlerAsset::GetInstance().closeAssetBind(sessionId);
125 AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
126 auto it = taskIsSingleFileMap_.find(taskId);
127 if (it != taskIsSingleFileMap_.end()) {
128 SoftBusHandlerAsset::GetInstance().RemoveFile(it->second.first, !it->second.second);
129 taskIsSingleFileMap_.erase(it);
130 }
131 }
132
AddFileMap(const std::string & taskId,const std::string & filePath,bool isSingleFile)133 void SoftBusAssetSendListener::AddFileMap(const std::string &taskId, const std::string &filePath, bool isSingleFile)
134 {
135 std::lock_guard<std::recursive_mutex> lock(mtx_);
136 auto it = taskIsSingleFileMap_.find(taskId);
137 if (it != taskIsSingleFileMap_.end()) {
138 LOGE("taskId already exist, %{public}s", taskId.c_str());
139 return;
140 }
141 taskIsSingleFileMap_.insert(std::make_pair(taskId, std::make_pair(filePath, isSingleFile)));
142 }
143
GetIsZipFile(const std::string & taskId)144 bool SoftBusAssetSendListener::GetIsZipFile(const std::string &taskId)
145 {
146 std::lock_guard<std::recursive_mutex> lock(mtx_);
147 auto it = taskIsSingleFileMap_.find(taskId);
148 if (it == taskIsSingleFileMap_.end()) {
149 LOGI("taskId not exist, %{public}s", taskId.c_str());
150 return false;
151 }
152 return !it->second.second;
153 }
154
RemoveFileMap(const std::string & taskId)155 void SoftBusAssetSendListener::RemoveFileMap(const std::string &taskId)
156 {
157 std::lock_guard<std::recursive_mutex> lock(mtx_);
158 auto it = taskIsSingleFileMap_.find(taskId);
159 if (it == taskIsSingleFileMap_.end()) {
160 LOGI("taskId not exist, %{public}s", taskId.c_str());
161 return;
162 }
163 taskIsSingleFileMap_.erase(it);
164 }
165
166 } // namespace DistributedFile
167 } // namespace Storage
168 } // namespace OHOS