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 "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 (auto callback : recvCallbackList_) {
38         if (recvCallback->AsObject() == callback->AsObject()) {
39             LOGI("recvCallback registered!");
40             return;
41         }
42     }
43     recvCallbackList_.emplace_back(recvCallback);
44 }
45 
RemoveRecvCallback(const sptr<IAssetRecvCallback> & recvCallback)46 void AssetCallbackManager::RemoveRecvCallback(const sptr<IAssetRecvCallback> &recvCallback)
47 {
48     if (recvCallback == nullptr) {
49         LOGE("recvCallback is nullptr");
50         return;
51     }
52     std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
53     for (auto iter = recvCallbackList_.begin(); iter != recvCallbackList_.end();) {
54         if ((*iter)->AsObject() == recvCallback->AsObject()) {
55             iter = recvCallbackList_.erase(iter);
56         } else {
57             iter++;
58         }
59     }
60 }
61 
AddSendCallback(const std::string & taskId,const sptr<IAssetSendCallback> & sendCallback)62 void AssetCallbackManager::AddSendCallback(const std::string &taskId, const sptr<IAssetSendCallback> &sendCallback)
63 {
64     if (taskId.empty()) {
65         LOGI("taskId is empty");
66         return;
67     }
68     std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
69     auto iter = sendCallbackMap_.find(taskId);
70     if (iter != sendCallbackMap_.end()) {
71         LOGI("taskId exist, taskId %{public}s", taskId.c_str());
72         return;
73     }
74     sendCallbackMap_.insert(std::pair<std::string, sptr<IAssetSendCallback>>(taskId, sendCallback));
75 }
76 
RemoveSendCallback(const std::string & taskId)77 void AssetCallbackManager::RemoveSendCallback(const std::string &taskId)
78 {
79     std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
80     auto iter = sendCallbackMap_.find(taskId);
81     if (iter == sendCallbackMap_.end()) {
82         LOGE("taskId not exist, taskId %{public}s", taskId.c_str());
83         return;
84     }
85     sendCallbackMap_.erase(iter);
86 }
87 
NotifyAssetRecvStart(const std::string & srcNetworkId,const std::string & dstNetworkId,const std::string & sessionId,const std::string & dstBundleName)88 void AssetCallbackManager::NotifyAssetRecvStart(const std::string &srcNetworkId,
89                                                 const std::string &dstNetworkId,
90                                                 const std::string &sessionId,
91                                                 const std::string &dstBundleName)
92 {
93     LOGI("NotifyAssetRecvStart.");
94     std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
95     for (auto callback : recvCallbackList_) {
96         if (callback != nullptr) {
97             callback->OnStart(srcNetworkId, dstNetworkId, sessionId, dstBundleName);
98         } else {
99             LOGE("IAssetRecvCallback is empty, sessionId is %{public}s, dstBundleName is %{public}s",
100                  sessionId.c_str(), dstBundleName.c_str());
101         }
102     }
103 }
104 
NotifyAssetRecvFinished(const std::string & srcNetworkId,const sptr<AssetObj> & assetObj,int32_t result)105 void AssetCallbackManager::NotifyAssetRecvFinished(const std::string &srcNetworkId,
106                                                    const sptr<AssetObj> &assetObj,
107                                                    int32_t result)
108 {
109     LOGI("NotifyAssetRecvFinished.");
110     std::lock_guard<std::mutex> lock(recvCallbackListMutex_);
111     for (auto callback : recvCallbackList_) {
112         if (callback == nullptr) {
113             LOGE("IAssetRecvCallback is empty, sessionId is %{public}s, dstBundleName is %{public}s",
114                  assetObj->sessionId_.c_str(), assetObj->dstBundleName_.c_str());
115         } else {
116             callback->OnFinished(srcNetworkId, assetObj, result);
117         }
118     }
119 }
120 
NotifyAssetSendResult(const std::string & taskId,const sptr<AssetObj> & assetObj,int32_t result)121 void AssetCallbackManager::NotifyAssetSendResult(const std::string &taskId,
122                                                  const sptr<AssetObj> &assetObj,
123                                                  int32_t result)
124 {
125     LOGI("NotifyAssetSendResult.");
126     std::lock_guard<std::mutex> lock(sendCallbackMapMutex_);
127     auto iter = sendCallbackMap_.find(taskId);
128     if (iter == sendCallbackMap_.end()) {
129         LOGE("taskId not exist, taskId %{public}s", taskId.c_str());
130         return;
131     }
132     if (iter->second == nullptr) {
133         LOGE("IAssetSendCallback is empty!");
134         return;
135     }
136     iter->second->OnSendResult(assetObj, result);
137 }
138 } // namespace DistributedFile
139 } // namespace Storage
140 } // namespace OHOS