• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "account_log_wrapper.h"
17 #include "distributed_account_event_service.h"
18 
19 namespace OHOS {
20 namespace AccountSA {
DistributedAccountEventService()21 DistributedAccountEventService::DistributedAccountEventService()
22 {}
23 
~DistributedAccountEventService()24 DistributedAccountEventService::~DistributedAccountEventService()
25 {}
26 
GetCallbackSize()27 int32_t DistributedAccountEventService::GetCallbackSize()
28 {
29     std::lock_guard<std::mutex> lock(mapLock_);
30     return callbackMap_.size();
31 }
32 
IsTypeExist(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)33 bool DistributedAccountEventService::IsTypeExist(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
34     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
35 {
36     std::lock_guard<std::mutex> lock(mapLock_);
37     auto it = callbackMap_.find(callback);
38     if (it == callbackMap_.end()) {
39         return false;
40     }
41     auto types = it->second;
42     return types.find(type) != types.end();
43 }
44 
AddType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)45 void DistributedAccountEventService::AddType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
46     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
47 {
48     if (callback == nullptr) {
49         return;
50     }
51     std::lock_guard<std::mutex> lock(mapLock_);
52     auto it = callbackMap_.find(callback);
53     if (it == callbackMap_.end()) {
54         callbackMap_[callback] = {type};
55     } else {
56         it->second.insert(type);
57     }
58 
59     auto itemType = typeMap_.find(type);
60     if (itemType == typeMap_.end()) {
61         typeMap_[type] = {callback};
62     } else {
63         itemType->second.insert(callback);
64     }
65     ACCOUNT_LOGI("Distributed client subscribe, type size=%{public}zu, callback size=%{public}zu.",
66         typeMap_.size(), callbackMap_.size());
67 }
68 
DeleteType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)69 void DistributedAccountEventService::DeleteType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
70     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
71 {
72     if (callback == nullptr) {
73         return;
74     }
75     std::lock_guard<std::mutex> lock(mapLock_);
76     auto it = callbackMap_.find(callback);
77     if (it == callbackMap_.end()) {
78         return;
79     }
80     it->second.erase(type);
81 
82     if (it->second.size() == 0) {
83         callbackMap_.erase(it);
84     }
85 
86     auto itemType = typeMap_.find(type);
87     if (itemType == typeMap_.end()) {
88         return;
89     }
90     itemType->second.erase(callback);
91     if (itemType->second.size() == 0) {
92         typeMap_.erase(itemType);
93     }
94     ACCOUNT_LOGI("Distributed client unsubscribe, type size=%{public}zu, callback size=%{public}zu.",
95         typeMap_.size(), callbackMap_.size());
96 }
97 
GetAllType(std::set<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> & typeList)98 void DistributedAccountEventService::GetAllType(std::set<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> &typeList)
99 {
100     std::lock_guard<std::mutex> lock(mapLock_);
101     for (auto &item : typeMap_) {
102         typeList.insert(item.first);
103     }
104 }
105 
OnAccountsChanged(const DistributedAccountEventData & eventData)106 ErrCode DistributedAccountEventService::OnAccountsChanged(const DistributedAccountEventData &eventData)
107 {
108     std::lock_guard<std::mutex> lock(mapLock_);
109     auto it = typeMap_.find(eventData.type_);
110     if (it == typeMap_.end()) {
111         ACCOUNT_LOGI("callback is empty");
112         return ERR_OK;
113     }
114     for (const auto &item : it->second) {
115         item->OnAccountsChanged(eventData);
116     }
117     return ERR_OK;
118 }
119 
GetInstance()120 DistributedAccountEventService *DistributedAccountEventService::GetInstance()
121 {
122     static sptr<DistributedAccountEventService> instance = new (std::nothrow) DistributedAccountEventService();
123     return instance.GetRefPtr();
124 }
125 }  // namespace AccountSA
126 }  // namespace OHOS
127