1 /*
2 * Copyright (c) 2021-2022 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 "os_account_subscribe_manager.h"
17
18 #include "account_log_wrapper.h"
19 #include "ios_account_event.h"
20 #include "os_account_subscribe_death_recipient.h"
21
22 namespace OHOS {
23 namespace AccountSA {
OsAccountSubscribeManager()24 OsAccountSubscribeManager::OsAccountSubscribeManager()
25 : subscribeDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
26 new (std::nothrow) OsAccountSubscribeDeathRecipient()))
27 {}
28
~OsAccountSubscribeManager()29 OsAccountSubscribeManager::~OsAccountSubscribeManager()
30 {}
31
SubscribeOsAccount(const std::shared_ptr<OsAccountSubscribeInfo> & subscribeInfoPtr,const sptr<IRemoteObject> & eventListener)32 ErrCode OsAccountSubscribeManager::SubscribeOsAccount(
33 const std::shared_ptr<OsAccountSubscribeInfo> &subscribeInfoPtr, const sptr<IRemoteObject> &eventListener)
34 {
35 if (subscribeInfoPtr == nullptr) {
36 ACCOUNT_LOGE("subscribeInfoPtr is nullptr");
37 return ERR_APPACCOUNT_SERVICE_SUBSCRIBE_INFO_PTR_IS_NULLPTR;
38 }
39
40 if (eventListener == nullptr) {
41 ACCOUNT_LOGE("eventListener is nullptr");
42 return ERR_APPACCOUNT_SERVICE_EVENT_LISTENER_IS_NULLPTR;
43 }
44 auto subscribeRecordPtr = std::make_shared<OsSubscribeRecord>(subscribeInfoPtr, eventListener);
45 if (subscribeRecordPtr == nullptr) {
46 ACCOUNT_LOGE("subscribeRecordPtr is nullptr");
47 return ERR_APPACCOUNT_SERVICE_SUBSCRIBE_RECORD_PTR_IS_NULLPTR;
48 }
49 if (subscribeDeathRecipient_ != nullptr) {
50 eventListener->AddDeathRecipient(subscribeDeathRecipient_);
51 }
52 subscribeRecordPtr->subscribeInfoPtr_ = subscribeInfoPtr;
53 subscribeRecordPtr->eventListener_ = eventListener;
54 return InsertSubscribeRecord(subscribeRecordPtr);
55 }
56
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)57 ErrCode OsAccountSubscribeManager::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
58 {
59 if (eventListener == nullptr) {
60 ACCOUNT_LOGE("eventListener is nullptr");
61 return ERR_APPACCOUNT_SERVICE_EVENT_LISTENER_IS_NULLPTR;
62 }
63
64 if (subscribeDeathRecipient_ != nullptr) {
65 eventListener->RemoveDeathRecipient(subscribeDeathRecipient_);
66 }
67
68 return RemoveSubscribeRecord(eventListener);
69 }
70
InsertSubscribeRecord(const OsSubscribeRecordPtr & subscribeRecordPtr)71 ErrCode OsAccountSubscribeManager::InsertSubscribeRecord(const OsSubscribeRecordPtr &subscribeRecordPtr)
72 {
73 if (subscribeRecordPtr == nullptr) {
74 ACCOUNT_LOGE("subscribeRecordPtr is nullptr");
75 return ERR_APPACCOUNT_SERVICE_SUBSCRIBE_RECORD_PTR_IS_NULLPTR;
76 }
77
78 std::lock_guard<std::mutex> lock(subscribeRecordMutex_);
79
80 subscribeRecords_.emplace_back(subscribeRecordPtr);
81
82 return ERR_OK;
83 }
84
RemoveSubscribeRecord(const sptr<IRemoteObject> & eventListener)85 ErrCode OsAccountSubscribeManager::RemoveSubscribeRecord(const sptr<IRemoteObject> &eventListener)
86 {
87 if (eventListener == nullptr) {
88 ACCOUNT_LOGE("eventListener is nullptr");
89 return ERR_APPACCOUNT_SERVICE_EVENT_LISTENER_IS_NULLPTR;
90 }
91
92 std::lock_guard<std::mutex> lock(subscribeRecordMutex_);
93
94 for (auto it = subscribeRecords_.begin(); it != subscribeRecords_.end(); ++it) {
95 if (eventListener == (*it)->eventListener_) {
96 (*it)->eventListener_ = nullptr;
97 subscribeRecords_.erase(it);
98 break;
99 }
100 }
101
102 return ERR_OK;
103 }
104
GetEventHandler(void)105 ErrCode OsAccountSubscribeManager::GetEventHandler(void)
106 {
107 if (!handler_) {
108 handler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(OHOS::AppExecFwk::EventRunner::Create());
109 if (handler_ == nullptr) {
110 ACCOUNT_LOGE("failed to create event handler");
111 return ERR_OSACCOUNT_SERVICE_CREATE_EVENT_HANDLER;
112 }
113 }
114
115 return ERR_OK;
116 }
117
PublishActivatedOsAccount(const int id)118 ErrCode OsAccountSubscribeManager::PublishActivatedOsAccount(const int id)
119 {
120 uint32_t sendCnt = 0;
121 ErrCode ret = Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED, sendCnt);
122 if (ret != ERR_OK) {
123 ACCOUNT_LOGE("PublishActivatedOsAccount failed! id %{public}d, ret %{public}d.", id, ret);
124 return ret;
125 }
126
127 ACCOUNT_LOGI("PublishActivatedOsAccount succeed! id %{public}d, sendCnt %{public}u.", id, sendCnt);
128 return ERR_OK;
129 }
130
OnAccountsChanged(const OsSubscribeRecordPtr & osSubscribeRecordPtr,const int id)131 bool OsAccountSubscribeManager::OnAccountsChanged(const OsSubscribeRecordPtr &osSubscribeRecordPtr, const int id)
132 {
133 auto osAccountEventProxy = iface_cast<IOsAccountEvent>(osSubscribeRecordPtr->eventListener_);
134 if (osAccountEventProxy == nullptr) {
135 ACCOUNT_LOGE("failed to get app account event proxy");
136 return false;
137 }
138 if (GetEventHandler() != ERR_OK) {
139 ACCOUNT_LOGE("failed to get event handler");
140 return false;
141 }
142 osAccountEventProxy->OnAccountsChanged(id);
143 return true;
144 }
145
PublishActivatingOsAccount(const int id)146 ErrCode OsAccountSubscribeManager::PublishActivatingOsAccount(const int id)
147 {
148 uint32_t sendCnt = 0;
149 ErrCode ret = Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING, sendCnt);
150 if (ret != ERR_OK) {
151 ACCOUNT_LOGE("PublishActivatingOsAccount failed! id %{public}d, ret %{public}d.", id, ret);
152 return ret;
153 }
154
155 ACCOUNT_LOGI("PublishActivatingOsAccount succeed! id %{public}d, sendCnt %{public}u.", id, sendCnt);
156 return ERR_OK;
157 }
158
Publish(const int id,OS_ACCOUNT_SUBSCRIBE_TYPE subscribeType,uint32_t & sendCnt)159 ErrCode OsAccountSubscribeManager::Publish(const int id, OS_ACCOUNT_SUBSCRIBE_TYPE subscribeType, uint32_t& sendCnt)
160 {
161 if (GetEventHandler() != ERR_OK) {
162 ACCOUNT_LOGE("failed to get event handler, id %{public}d, subscribeType %{public}d.", id, subscribeType);
163 return ERR_OSACCOUNT_SERVICE_SUBSCRIBE_GET_EVENT_HANDLE_ERROR;
164 }
165
166 std::lock_guard<std::mutex> lock(subscribeRecordMutex_);
167 for (auto it = subscribeRecords_.begin(); it != subscribeRecords_.end(); ++it) {
168 if ((*it)->subscribeInfoPtr_ == nullptr) {
169 ACCOUNT_LOGE("subscribeInfoPtr_ is null, id %{public}d.", id);
170 continue;
171 }
172 OS_ACCOUNT_SUBSCRIBE_TYPE osAccountSubscribeType;
173 (*it)->subscribeInfoPtr_->GetOsAccountSubscribeType(osAccountSubscribeType);
174 if (osAccountSubscribeType == subscribeType) {
175 OHOS::AppExecFwk::InnerEvent::Callback callback =
176 std::bind(&OsAccountSubscribeManager::OnAccountsChanged, this, (*it), id);
177 if (handler_ == nullptr) {
178 ACCOUNT_LOGE("handler_ is null!");
179 continue;
180 }
181 handler_->PostTask(callback);
182 ++sendCnt;
183 }
184 }
185 return ERR_OK;
186 }
187 } // namespace AccountSA
188 } // namespace OHOS
189