1 /*
2 * Copyright (c) 2023 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 "trans_mananger.h"
17
18 #include "utils_log.h"
19
20 namespace OHOS {
21 namespace Storage {
22 namespace DistributedFile {
GetInstance()23 TransManager &TransManager::GetInstance()
24 {
25 static TransManager instance;
26 return instance;
27 }
28
NotifyFileProgress(const std::string & sessionName,uint64_t total,uint64_t processed)29 void TransManager::NotifyFileProgress(const std::string &sessionName, uint64_t total, uint64_t processed)
30 {
31 std::lock_guard<std::mutex> lock(sessionTransMapMutex_);
32 auto iter = sessionTransMap_.find(sessionName);
33 if (iter == sessionTransMap_.end()) {
34 LOGE("sessionName not registered!");
35 return;
36 }
37 if (iter->second == nullptr) {
38 LOGE("IFileTransListener is empty!");
39 return;
40 }
41 iter->second->OnFileReceive(total, processed);
42 }
43
NotifyFileFailed(const std::string & sessionName,int32_t errorCode)44 void TransManager::NotifyFileFailed(const std::string &sessionName, int32_t errorCode)
45 {
46 std::lock_guard<std::mutex> lock(sessionTransMapMutex_);
47 auto iter = sessionTransMap_.find(sessionName);
48 if (iter == sessionTransMap_.end()) {
49 LOGE("sessionName not registered!");
50 return;
51 }
52 if (iter->second == nullptr) {
53 LOGE("IFileTransListener is empty!");
54 return;
55 }
56 iter->second->OnFailed(sessionName, errorCode);
57 }
58
NotifyFileFinished(const std::string & sessionName)59 void TransManager::NotifyFileFinished(const std::string &sessionName)
60 {
61 std::lock_guard<std::mutex> lock(sessionTransMapMutex_);
62 auto iter = sessionTransMap_.find(sessionName);
63 if (iter == sessionTransMap_.end()) {
64 LOGE("sessionName not registered!");
65 return;
66 }
67 if (iter->second == nullptr) {
68 LOGE("IFileTransListener is empty!");
69 return;
70 }
71 iter->second->OnFinished(sessionName);
72 }
73
AddTransTask(const std::string & sessionName,const sptr<IFileTransListener> & listener)74 void TransManager::AddTransTask(const std::string &sessionName, const sptr<IFileTransListener> &listener)
75 {
76 std::lock_guard<std::mutex> lock(sessionTransMapMutex_);
77 auto iter = sessionTransMap_.find(sessionName);
78 if (iter != sessionTransMap_.end()) {
79 LOGE("sessionName registered!");
80 return;
81 }
82 sessionTransMap_.insert(std::make_pair(sessionName, listener));
83 }
84
DeleteTransTask(const std::string & sessionName)85 void TransManager::DeleteTransTask(const std::string &sessionName)
86 {
87 std::lock_guard<std::mutex> lock(sessionTransMapMutex_);
88 auto iter = sessionTransMap_.find(sessionName);
89 if (iter == sessionTransMap_.end()) {
90 LOGE("sessionName not registered!");
91 return;
92 }
93 sessionTransMap_.erase(iter);
94 }
95
96 } // namespace DistributedFile
97 } // namespace Storage
98 } // namespace OHOS