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