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 "domain_account_status_listener_manager.h"
17 #include "account_error_no.h"
18 #include "account_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AccountSA {
DomainAccountStatusListenerManager()22 DomainAccountStatusListenerManager::DomainAccountStatusListenerManager()
23 {}
24 
~DomainAccountStatusListenerManager()25 DomainAccountStatusListenerManager::~DomainAccountStatusListenerManager()
26 {}
27 
OnResult(const int32_t errCode,Parcel & parcel)28 void DomainAccountStatusListenerManager::OnResult(const int32_t errCode, Parcel &parcel)
29 {
30     if (errCode != ERR_OK) {
31         ACCOUNT_LOGE("OnResult err is %{public}d", errCode);
32         return;
33     }
34     DomainAccountEventData report;
35     if (!report.domainAccountInfo.ReadFromParcel(parcel)) {
36         ACCOUNT_LOGE("ReadFromParcel failed");
37         return;
38     }
39     int32_t event;
40     if (!parcel.ReadInt32(event)) {
41         ACCOUNT_LOGE("Read event failed");
42         return;
43     }
44     report.event = static_cast<DomainAccountEvent>(event);
45     int32_t status;
46     if (!parcel.ReadInt32(status)) {
47         ACCOUNT_LOGE("Read status failed");
48         return;
49     }
50     report.status = static_cast<DomainAccountStatus>(status);
51     int32_t userId;
52     if (!parcel.ReadInt32(userId)) {
53         ACCOUNT_LOGE("Read userId failed");
54         return;
55     }
56     report.userId = userId;
57     std::lock_guard<std::mutex> lock(mutex_);
58     for (auto listener : listenerAll_) {
59         listener->OnStatusChanged(report);
60     }
61 }
62 
InsertRecord(const std::shared_ptr<DomainAccountStatusListener> & listener)63 void DomainAccountStatusListenerManager::InsertRecord(const std::shared_ptr<DomainAccountStatusListener> &listener)
64 {
65     std::lock_guard<std::mutex> lock(mutex_);
66     if (listenerAll_.find(listener) != listenerAll_.end()) {
67         ACCOUNT_LOGI("listener alredy exist");
68         return;
69     }
70     listenerAll_.insert(listener);
71 }
72 
RemoveRecord(const std::shared_ptr<DomainAccountStatusListener> & listener)73 void DomainAccountStatusListenerManager::RemoveRecord(const std::shared_ptr<DomainAccountStatusListener> &listener)
74 {
75     std::lock_guard<std::mutex> lock(mutex_);
76     auto listenerAllIter = listenerAll_.find(listener);
77     if (listenerAllIter == listenerAll_.end()) {
78         return;
79     }
80     listenerAll_.erase(listenerAllIter);
81 }
82 
IsRecordEmpty()83 bool DomainAccountStatusListenerManager::IsRecordEmpty()
84 {
85     std::lock_guard<std::mutex> lock(mutex_);
86     return listenerAll_.empty();
87 }
88 }  // namespace AccountSA
89 }  // namespace OHOS