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 "account_log_wrapper.h"
17 #include "distributed_account_event_service.h"
18 
19 namespace OHOS {
20 namespace AccountSA {
DistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const std::shared_ptr<DistributedAccountSubscribeCallback> & callback)21 DistributedAccountEventService::DistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
22     const std::shared_ptr<DistributedAccountSubscribeCallback> &callback)
23     : distributedAccountSubscribeCallback_(callback)
24 {
25     types_.insert(type);
26 }
27 
~DistributedAccountEventService()28 DistributedAccountEventService::~DistributedAccountEventService()
29 {}
30 
IsTypeExist(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)31 bool DistributedAccountEventService::IsTypeExist(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)
32 {
33     std::lock_guard<std::mutex> lock(typesLock_);
34     return types_.find(type) != types_.end();
35 }
36 
AddType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)37 void DistributedAccountEventService::AddType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)
38 {
39     std::lock_guard<std::mutex> lock(typesLock_);
40     types_.insert(type);
41 }
42 
DeleteType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)43 void DistributedAccountEventService::DeleteType(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type)
44 {
45     std::lock_guard<std::mutex> lock(typesLock_);
46     types_.erase(type);
47 }
48 
GetTypeSize()49 int32_t DistributedAccountEventService::GetTypeSize()
50 {
51     std::lock_guard<std::mutex> lock(typesLock_);
52     return types_.size();
53 }
54 
GetAllType(std::vector<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> & typeList)55 void DistributedAccountEventService::GetAllType(std::vector<DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE> &typeList)
56 {
57     std::lock_guard<std::mutex> lock(typesLock_);
58     for (auto it : types_) {
59         typeList.push_back(it);
60     }
61 }
62 
OnAccountsChanged(const DistributedAccountEventData & eventData)63 void DistributedAccountEventService::OnAccountsChanged(const DistributedAccountEventData &eventData)
64 {
65     if (distributedAccountSubscribeCallback_ == nullptr) {
66         ACCOUNT_LOGE("Callback_ is nullptr");
67         return;
68     }
69     distributedAccountSubscribeCallback_->OnAccountsChanged(eventData);
70 }
71 }  // namespace AccountSA
72 }  // namespace OHOS
73