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()22DomainAccountStatusListenerManager::DomainAccountStatusListenerManager() 23 {} 24 ~DomainAccountStatusListenerManager()25DomainAccountStatusListenerManager::~DomainAccountStatusListenerManager() 26 {} 27 OnResult(const int32_t errCode,Parcel & parcel)28void 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 for (auto listener : listenerAll_) { 58 listener->OnStatusChanged(report); 59 } 60 } 61 InsertRecord(const std::shared_ptr<DomainAccountStatusListener> & listener)62void DomainAccountStatusListenerManager::InsertRecord(const std::shared_ptr<DomainAccountStatusListener> &listener) 63 { 64 std::lock_guard<std::mutex> lock(mutex_); 65 if (listenerAll_.find(listener) != listenerAll_.end()) { 66 ACCOUNT_LOGI("listener alredy exist"); 67 return; 68 } 69 listenerAll_.insert(listener); 70 } 71 RemoveRecord(const std::shared_ptr<DomainAccountStatusListener> & listener)72void DomainAccountStatusListenerManager::RemoveRecord(const std::shared_ptr<DomainAccountStatusListener> &listener) 73 { 74 std::lock_guard<std::mutex> lock(mutex_); 75 auto listenerAllIter = listenerAll_.find(listener); 76 if (listenerAllIter == listenerAll_.end()) { 77 return; 78 } 79 listenerAll_.erase(listenerAllIter); 80 } 81 GetListenerAllRecords()82std::set<std::shared_ptr<DomainAccountStatusListener>> DomainAccountStatusListenerManager::GetListenerAllRecords() 83 { 84 return listenerAll_; 85 } 86 IsRecordEmpty()87bool DomainAccountStatusListenerManager::IsRecordEmpty() 88 { 89 return listenerAll_.empty(); 90 } 91 } // namespace AccountSA 92 } // namespace OHOS