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 bool SoftBusAssetSendListener::isSingleFile_;
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     LOGI("Push asset finished, socketId is %{public}d", socketId);
49     if (fileCnt == 0) {
50         LOGE("fileList has no file");
51         return;
52     }
53     auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(socketId);
54     if (assetObj == nullptr) {
55         LOGE("OnSendAssetFinished get assetObj is nullptr");
56         return;
57     }
58     auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
59     AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_OK);
60     SoftBusHandlerAsset::GetInstance().closeAssetBind(socketId);
61     AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
62     SoftBusHandlerAsset::GetInstance().RemoveFile(fileList[0], !SoftBusAssetSendListener::isSingleFile_);
63 }
64 
OnSendAssetError(int32_t socketId,const char ** fileList,int32_t fileCnt,int32_t errorCode)65 void SoftBusAssetSendListener::OnSendAssetError(int32_t socketId,
66                                                 const char **fileList,
67                                                 int32_t fileCnt,
68                                                 int32_t errorCode)
69 {
70     LOGE("SendAssetError, socketId is %{public}d, errorCode %{public}d", socketId, errorCode);
71     if (fileCnt == 0) {
72         LOGE("fileList has no file");
73         return;
74     }
75     auto assetObj = SoftBusHandlerAsset::GetInstance().GetAssetObj(socketId);
76     if (assetObj == nullptr) {
77         LOGE("OnSendAssetError  get assetObj is nullptr");
78         return;
79     }
80     auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
81     AssetCallbackManager::GetInstance().NotifyAssetSendResult(taskId, assetObj, FileManagement::E_SEND_FILE);
82     SoftBusHandlerAsset::GetInstance().closeAssetBind(socketId);
83     AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
84     SoftBusHandlerAsset::GetInstance().RemoveFile(fileList[0], !SoftBusAssetSendListener::isSingleFile_);
85 }
86 
OnSendShutdown(int32_t sessionId,ShutdownReason reason)87 void SoftBusAssetSendListener::OnSendShutdown(int32_t sessionId, ShutdownReason reason)
88 {
89     LOGI("OnSessionClosed, sessionId = %{public}d, reason = %{public}d", sessionId, reason);
90 }
91 
92 } // namespace DistributedFile
93 } // namespace Storage
94 } // namespace OHOS