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 "status_listener_manager.h"
17
18 #include <datetime_ex.h>
19 #include <future>
20 #include <pthread.h>
21 #include <thread>
22
23 #include "account_error_no.h"
24 #include "account_event_provider.h"
25 #include "account_log_wrapper.h"
26 #ifdef HAS_CES_PART
27 #include "common_event_support.h"
28 #endif // HAS_CES_PART
29 #include "domain_account_callback_proxy.h"
30 #include "status_listener_death_recipient.h"
31
32 namespace OHOS {
33 namespace AccountSA {
34 namespace {
35 #ifdef HAS_CES_PART
36 static const int INVALID_USERID = -1;
37 #endif // HAS_CES_PART
38 } // namespace
39
GetInstance()40 StatusListenerManager& StatusListenerManager::GetInstance()
41 {
42 static StatusListenerManager *instance = new (std::nothrow) StatusListenerManager();
43 return *instance;
44 }
45
StatusListenerManager()46 StatusListenerManager::StatusListenerManager() : listenerDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
47 new (std::nothrow) StatusListenerDeathRecipient()))
48 {
49 }
50
~StatusListenerManager()51 StatusListenerManager::~StatusListenerManager()
52 {}
53
InsertListenerToRecords(const sptr<IRemoteObject> & listener)54 ErrCode StatusListenerManager::InsertListenerToRecords(const sptr<IRemoteObject> &listener)
55 {
56 if (listener == nullptr) {
57 ACCOUNT_LOGE("listener is nullptr");
58 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
59 }
60 std::lock_guard<std::mutex> lock(mutex_);
61 if (listenerAll_.find(listener) != listenerAll_.end()) {
62 ACCOUNT_LOGI("listener is already exist");
63 return ERR_OK;
64 }
65 if ((listener->IsProxyObject()) && (listenerDeathRecipient_ != nullptr) &&
66 (!listener->AddDeathRecipient(listenerDeathRecipient_))) {
67 ACCOUNT_LOGE("AddDeathRecipient failed");
68 return ERR_ACCOUNT_COMMON_ADD_DEATH_RECIPIENT;
69 }
70 listenerAll_.insert(listener);
71 return ERR_OK;
72 }
73
RemoveListenerByListener(const sptr<IRemoteObject> & listener)74 ErrCode StatusListenerManager::RemoveListenerByListener(const sptr<IRemoteObject> &listener)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 auto listenerToAllIt = listenerAll_.find(listener);
78 if (listenerToAllIt != listenerAll_.end()) {
79 listenerAll_.erase(listenerToAllIt);
80 }
81 if ((listener->IsProxyObject()) && (listenerDeathRecipient_ != nullptr)) {
82 listener->RemoveDeathRecipient(listenerDeathRecipient_);
83 }
84 return ERR_OK;
85 }
86
DomainAccountEventParcel(const DomainAccountEventData & report,Parcel & parcel)87 void StatusListenerManager::DomainAccountEventParcel(const DomainAccountEventData &report, Parcel &parcel)
88 {
89 if (!report.domainAccountInfo.Marshalling(parcel)) {
90 ACCOUNT_LOGE("write domainAccountInfo failed.");
91 return;
92 }
93 if (!parcel.WriteInt32(report.event)) {
94 ACCOUNT_LOGE("write event failed.");
95 return;
96 }
97 if (!parcel.WriteInt32(report.status)) {
98 ACCOUNT_LOGE("write status failed.");
99 return;
100 }
101 if (!parcel.WriteInt32(report.userId)) {
102 ACCOUNT_LOGE("write userId failed.");
103 return;
104 }
105 return;
106 }
107
NotifyEventAsync(const DomainAccountEventData & report)108 void StatusListenerManager::NotifyEventAsync(const DomainAccountEventData &report)
109 {
110 ACCOUNT_LOGI("report.event %{public}d, report.status %{public}d", report.event, report.status);
111 #ifdef HAS_CES_PART
112 AccountEventProvider::EventPublish(
113 EventFwk::CommonEventSupport::COMMON_EVENT_DOMAIN_ACCOUNT_STATUS_CHANGED, INVALID_USERID, &report);
114 #else // HAS_CES_PART
115 ACCOUNT_LOGI("No common event part! Publish nothing!");
116 #endif // HAS_CES_PART
117 std::lock_guard<std::mutex> lock(mutex_);
118 for (auto record : listenerAll_) {
119 auto callback = iface_cast<IDomainAccountCallback>(record);
120 if (callback == nullptr) {
121 ACCOUNT_LOGE("callback is nullptr!");
122 continue;
123 }
124 Parcel parcel;
125 DomainAccountEventParcel(report, parcel);
126 callback->OnResult(ERR_OK, parcel);
127 }
128 }
129 } // namespace AccountSA
130 } // namespace OHOS
131