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 "asset_callback_manager.h"
17
18 #include "network/softbus/softbus_handler_asset.h"
19 #include "utils_log.h"
20
21 namespace OHOS {
22 namespace Storage {
23 namespace DistributedFile {
GetInstance()24 AssetCallbackManager &AssetCallbackManager::GetInstance()
25 {
26 static AssetCallbackManager instance;
27 return instance;
28 }
29
AddRecvCallback(const sptr<IAssetRecvCallback> & recvCallback)30 void AssetCallbackManager::AddRecvCallback(const sptr<IAssetRecvCallback> &recvCallback)
31 {
32 if (recvCallback == nullptr) {
33 LOGE("recvCallback is nullptr");
34 return;
35 }
36 std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
37 for (const auto &callback : recvCallbackList_) {
38 if (callback == nullptr) {
39 LOGE("callback is nullptr");
40 continue;
41 }
42 if (recvCallback->AsObject() == callback->AsObject()) {
43 LOGI("recvCallback registered!");
44 return;
45 }
46 }
47 recvCallbackList_.emplace_back(recvCallback);
48 }
49
RemoveRecvCallback(const sptr<IAssetRecvCallback> & recvCallback)50 void AssetCallbackManager::RemoveRecvCallback(const sptr<IAssetRecvCallback> &recvCallback)
51 {
52 if (recvCallback == nullptr) {
53 LOGE("recvCallback is nullptr");
54 return;
55 }
56 std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
57 for (auto iter = recvCallbackList_.begin(); iter != recvCallbackList_.end();) {
58 if ((*iter)->AsObject() == recvCallback->AsObject()) {
59 iter = recvCallbackList_.erase(iter);
60 } else {
61 iter++;
62 }
63 }
64 }
65
AddSendCallback(const std::string & taskId,const sptr<IAssetSendCallback> & sendCallback)66 void AssetCallbackManager::AddSendCallback(const std::string &taskId, const sptr<IAssetSendCallback> &sendCallback)
67 {
68 if (taskId.empty()) {
69 LOGI("taskId is empty");
70 return;
71 }
72 std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
73 auto iter = sendCallbackMap_.find(taskId);
74 if (iter != sendCallbackMap_.end()) {
75 LOGI("taskId exist, taskId %{public}s", taskId.c_str());
76 return;
77 }
78 sendCallbackMap_.insert(std::pair<std::string, sptr<IAssetSendCallback>>(taskId, sendCallback));
79 }
80
RemoveSendCallback(const std::string & taskId)81 void AssetCallbackManager::RemoveSendCallback(const std::string &taskId)
82 {
83 std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
84 auto iter = sendCallbackMap_.find(taskId);
85 if (iter == sendCallbackMap_.end()) {
86 LOGE("taskId not exist, taskId %{public}s", taskId.c_str());
87 return;
88 }
89 sendCallbackMap_.erase(iter);
90 }
91
NotifyAssetRecvStart(const std::string & srcNetworkId,const std::string & dstNetworkId,const std::string & sessionId,const std::string & dstBundleName)92 void AssetCallbackManager::NotifyAssetRecvStart(const std::string &srcNetworkId,
93 const std::string &dstNetworkId,
94 const std::string &sessionId,
95 const std::string &dstBundleName)
96 {
97 LOGI("NotifyAssetRecvStart.");
98 std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
99 for (const auto &callback : recvCallbackList_) {
100 if (callback != nullptr) {
101 callback->OnStart(srcNetworkId, dstNetworkId, sessionId, dstBundleName);
102 } else {
103 LOGE("IAssetRecvCallback is empty, sessionId is %{public}s, dstBundleName is %{public}s",
104 sessionId.c_str(), dstBundleName.c_str());
105 }
106 }
107 }
108
NotifyAssetRecvProgress(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,uint64_t total,uint64_t processed)109 void AssetCallbackManager::NotifyAssetRecvProgress(const std::string &srcNetworkId,
110 const sptr<AssetObj> &assetObj,
111 uint64_t total,
112 uint64_t processed)
113 {
114 LOGD("NotifyAssetRecvProgress.");
115 std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
116 for (const auto &callback : recvCallbackList_) {
117 if (callback == nullptr) {
118 LOGE("IAssetRecvCallback is empty");
119 if (assetObj != nullptr) {
120 LOGE("SessionId is %{public}s, dstBundleName is %{public}s",
121 assetObj->sessionId_.c_str(), assetObj->dstBundleName_.c_str());
122 }
123 continue;
124 }
125 callback->OnRecvProgress(srcNetworkId, assetObj, total, processed);
126 }
127 }
128
NotifyAssetRecvFinished(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,int32_t result)129 void AssetCallbackManager::NotifyAssetRecvFinished(const std::string &srcNetworkId,
130 const sptr<AssetObj> &assetObj,
131 int32_t result)
132 {
133 LOGI("NotifyAssetRecvFinished.");
134 std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
135 for (const auto &callback : recvCallbackList_) {
136 if (callback == nullptr) {
137 LOGE("IAssetRecvCallback is empty");
138 if (assetObj != nullptr) {
139 LOGE("SessionId is %{public}s, dstBundleName is %{public}s",
140 assetObj->sessionId_.c_str(), assetObj->dstBundleName_.c_str());
141 }
142 } else {
143 callback->OnFinished(srcNetworkId, assetObj, result);
144 }
145 }
146 }
147
NotifyAssetSendResult(const std::string & taskId,const sptr<AssetObj> & assetObj,int32_t result)148 void AssetCallbackManager::NotifyAssetSendResult(const std::string &taskId,
149 const sptr<AssetObj> &assetObj,
150 int32_t result)
151 {
152 LOGI("NotifyAssetSendResult.");
153 std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
154 auto iter = sendCallbackMap_.find(taskId);
155 if (iter == sendCallbackMap_.end()) {
156 LOGE("taskId not exist, taskId %{public}s", taskId.c_str());
157 return;
158 }
159 if (iter->second == nullptr) {
160 LOGE("IAssetSendCallback is empty!");
161 return;
162 }
163 iter->second->OnSendResult(assetObj, result);
164 }
165 } // namespace DistributedFile
166 } // namespace Storage
167 } // namespace OHOS