• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "progress_notifier_impl.h"
17 
18 #include "anonymous.h"
19 #include "logger.h"
20 #include "objectstore_errors.h"
21 
22 namespace OHOS::ObjectStore {
23 
GetInstance()24 std::shared_ptr<ProgressNotifierImpl> ProgressNotifierImpl::GetInstance()
25 {
26     static std::shared_ptr<ProgressNotifierImpl> instance;
27     static std::mutex instanceLock;
28     std::lock_guard<std::mutex> lockGuard(instanceLock);
29     if (instance == nullptr) {
30         instance = std::make_shared<ProgressNotifierImpl>();
31         DistributedObjectStore *storeInstance = DistributedObjectStore::GetInstance();
32         if (storeInstance == nullptr) {
33             LOG_ERROR("Get store instance nullptr");
34             return instance;
35         }
36         auto ret = storeInstance->SetProgressNotifier(instance);
37         if (ret != SUCCESS) {
38             LOG_ERROR("SetProgressNotifier %{public}d error", ret);
39         } else {
40             LOG_INFO("SetProgressNotifier success");
41         }
42     }
43     return instance;
44 }
45 
AddWatcher(const std::string & sessionId,std::weak_ptr<JSWatcher> watcher)46 void ProgressNotifierImpl::AddWatcher(const std::string &sessionId, std::weak_ptr<JSWatcher> watcher)
47 {
48     std::lock_guard<std::mutex> lock(mutex_);
49     watchers_.insert_or_assign(sessionId, watcher);
50 }
51 
DelWatcher(const std::string & sessionId)52 void ProgressNotifierImpl::DelWatcher(const std::string &sessionId)
53 {
54     std::lock_guard<std::mutex> lock(mutex_);
55     watchers_.erase(sessionId);
56 }
57 
OnChanged(const std::string & sessionId,int32_t progress)58 void ProgressNotifierImpl::OnChanged(const std::string &sessionId, int32_t progress)
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     if (watchers_.count(sessionId) != 0) {
62         LOG_INFO("start emit %{public}s %{public}d", Anonymous::Change(sessionId).c_str(), progress);
63         std::shared_ptr<JSWatcher> lockedWatcher = watchers_.at(sessionId).lock();
64         if (lockedWatcher) {
65             lockedWatcher->Emit("progressChanged", sessionId, progress);
66             LOG_INFO("end emit %{public}s %{public}d", Anonymous::Change(sessionId).c_str(), progress);
67         } else {
68             LOG_ERROR("watcher expired");
69         }
70     }
71 }
72 
~ProgressNotifierImpl()73 ProgressNotifierImpl::~ProgressNotifierImpl()
74 {
75 }
76 
77 } // namespace OHOS::ObjectStore
78