1 /*
2 * Copyright (c) 2025 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 "event_listener_callback_service.h"
17
18 #include "callback_manager.h"
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_ptr.h"
22
23 #define LOG_TAG "USER_AUTH_SDK"
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
AddUserAuthSuccessEventListener(const sptr<IUserAuth> & proxy,const std::vector<AuthType> & authTypes,const std::shared_ptr<AuthSuccessEventListener> & listener)28 int32_t EventListenerCallbackManager::AddUserAuthSuccessEventListener(const sptr<IUserAuth> &proxy,
29 const std::vector<AuthType> &authTypes, const std::shared_ptr<AuthSuccessEventListener> &listener)
30 {
31 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
32 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
33
34 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
35 if (authEventListenerMap_.size() == 0) {
36 authEventListenerCallbackImpl_ = new (std::nothrow) EventListenerCallbackImpl();
37 IF_FALSE_LOGE_AND_RETURN_VAL(authEventListenerCallbackImpl_ != nullptr, GENERAL_ERROR);
38 auto ret = proxy->RegistUserAuthSuccessEventListener(authEventListenerCallbackImpl_);
39 IF_FALSE_LOGE_AND_RETURN_VAL(ret == SUCCESS, ret);
40 }
41
42 for (auto authType : authTypes) {
43 auto addCount = authEventListenerMap_[authType].insert(listener);
44 IAM_LOGI("AddEventListener addCount:%{public}d, authType:%{public}d, listenerSize:%{public}zu",
45 addCount.second, static_cast<int32_t>(authType), authEventListenerMap_[authType].size());
46 }
47 return SUCCESS;
48 }
49
RemoveUserAuthSuccessEventListener(const sptr<IUserAuth> & proxy,const std::shared_ptr<AuthSuccessEventListener> & listener)50 int32_t EventListenerCallbackManager::RemoveUserAuthSuccessEventListener(const sptr<IUserAuth> &proxy,
51 const std::shared_ptr<AuthSuccessEventListener> &listener)
52 {
53 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
54 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
55
56 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
57 auto mapIter = authEventListenerMap_.begin();
58 while (mapIter != authEventListenerMap_.end()) {
59 size_t eraseCount = mapIter->second.erase(listener);
60 IAM_LOGI("RemoveEventListener eraseCount:%{public}zu, authType:%{public}d, listenerSize:%{public}zu",
61 eraseCount, mapIter->first, mapIter->second.size());
62 if (mapIter->second.size() == 0) {
63 mapIter = authEventListenerMap_.erase(mapIter);
64 } else {
65 mapIter++;
66 }
67 }
68
69 if (authEventListenerMap_.size() == 0) {
70 auto ret = proxy->UnRegistUserAuthSuccessEventListener(authEventListenerCallbackImpl_);
71 authEventListenerCallbackImpl_ = nullptr;
72 return ret;
73 }
74 return SUCCESS;
75 }
76
AddCredChangeEventListener(const sptr<IUserIdm> & proxy,const std::vector<AuthType> & authTypes,const std::shared_ptr<CredChangeEventListener> & listener)77 int32_t EventListenerCallbackManager::AddCredChangeEventListener(const sptr<IUserIdm> &proxy,
78 const std::vector<AuthType> &authTypes, const std::shared_ptr<CredChangeEventListener> &listener)
79 {
80 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
81 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
82
83 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
84 if (credEventListenerMap_.size() == 0) {
85 credEventListenerCallbackImpl_ = new (std::nothrow) EventListenerCallbackImpl();
86 IF_FALSE_LOGE_AND_RETURN_VAL(credEventListenerCallbackImpl_ != nullptr, GENERAL_ERROR);
87 auto ret = proxy->RegistCredChangeEventListener(credEventListenerCallbackImpl_);
88 IF_FALSE_LOGE_AND_RETURN_VAL(ret == SUCCESS, ret);
89 }
90
91 for (auto authType : authTypes) {
92 auto addCount = credEventListenerMap_[authType].insert(listener);
93 IAM_LOGI("AddEventListener addCount:%{public}d, authType:%{public}d, listenerSize:%{public}zu",
94 addCount.second, static_cast<int32_t>(authType), credEventListenerMap_[authType].size());
95 }
96 return SUCCESS;
97 }
98
RemoveCredChangeEventListener(const sptr<IUserIdm> & proxy,const std::shared_ptr<CredChangeEventListener> & listener)99 int32_t EventListenerCallbackManager::RemoveCredChangeEventListener(const sptr<IUserIdm> &proxy,
100 const std::shared_ptr<CredChangeEventListener> &listener)
101 {
102 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
103 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, INVALID_PARAMETERS);
104
105 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
106 auto mapIter = credEventListenerMap_.begin();
107 while (mapIter != credEventListenerMap_.end()) {
108 size_t eraseCount = mapIter->second.erase(listener);
109 IAM_LOGI("RemoveEventListener eraseCount:%{public}zu, authType:%{public}d, listenerSize:%{public}zu",
110 eraseCount, mapIter->first, mapIter->second.size());
111 if (mapIter->second.size() == 0) {
112 mapIter = credEventListenerMap_.erase(mapIter);
113 } else {
114 mapIter++;
115 }
116 }
117
118 if (credEventListenerMap_.size() == 0) {
119 auto ret = proxy->UnRegistCredChangeEventListener(credEventListenerCallbackImpl_);
120 credEventListenerCallbackImpl_ = nullptr;
121 return ret;
122 }
123 return SUCCESS;
124 }
125
GetAuthEventListenerSet(AuthType authType)126 std::set<std::shared_ptr<AuthSuccessEventListener>> EventListenerCallbackManager::GetAuthEventListenerSet(
127 AuthType authType)
128 {
129 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
130 if (authEventListenerMap_.find(authType) != authEventListenerMap_.end()) {
131 return authEventListenerMap_[authType];
132 }
133 return {};
134 }
135
GetCredEventListenerSet(AuthType authType)136 std::set<std::shared_ptr<CredChangeEventListener>> EventListenerCallbackManager::GetCredEventListenerSet(
137 AuthType authType)
138 {
139 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
140 if (credEventListenerMap_.find(authType) != credEventListenerMap_.end()) {
141 return credEventListenerMap_[authType];
142 }
143 return {};
144 }
145
OnServiceDeath()146 void EventListenerCallbackManager::OnServiceDeath()
147 {
148 IAM_LOGI("userauthservice death, clear caller map and register again");
149 std::lock_guard<std::recursive_mutex> lock(eventListenerMutex_);
150 authEventListenerMap_.clear();
151 credEventListenerMap_.clear();
152 }
153
GetInstance()154 EventListenerCallbackManager &EventListenerCallbackManager::GetInstance()
155 {
156 static EventListenerCallbackManager eventListenerCallbackManager;
157 return eventListenerCallbackManager;
158 }
159
OnNotifyAuthSuccessEvent(int32_t userId,int32_t authType,int32_t callerType,const std::string & callerName)160 int32_t EventListenerCallbackManager::EventListenerCallbackImpl::OnNotifyAuthSuccessEvent(int32_t userId,
161 int32_t authType, int32_t callerType, const std::string &callerName)
162 {
163 IAM_LOGI("OnNotifyAuthSuccessEvent, userId:%{public}d, authType:%{public}d, callerName:%{public}s,"
164 "callerType:%{public}d", userId, authType, callerName.c_str(), callerType);
165 auto eventListenerSet =
166 EventListenerCallbackManager::GetInstance().GetAuthEventListenerSet(static_cast<AuthType>(authType));
167 for (const auto &listener : eventListenerSet) {
168 if (listener == nullptr) {
169 IAM_LOGE("authListener is nullptr");
170 continue;
171 }
172 listener->OnNotifyAuthSuccessEvent(userId, static_cast<AuthType>(authType), callerType, callerName);
173 }
174 return SUCCESS;
175 }
176
OnNotifyCredChangeEvent(int32_t userId,int32_t authType,int32_t eventType,const IpcCredChangeEventInfo & changeInfo)177 int32_t EventListenerCallbackManager::EventListenerCallbackImpl::OnNotifyCredChangeEvent(int32_t userId,
178 int32_t authType, int32_t eventType, const IpcCredChangeEventInfo &changeInfo)
179 {
180 IAM_LOGI("OnNotifyCredChangeEvent, userId:%{public}d, authType:%{public}d, eventType:%{public}d,"
181 "callerName:%{public}s, credId:%{public}u, lastCredId:%{public}u, isSilentCredChange:%{public}u",
182 userId, authType, eventType, changeInfo.callerName.c_str(), static_cast<uint16_t>(changeInfo.credentialId),
183 static_cast<uint16_t>(changeInfo.lastCredentialId), changeInfo.isSilentCredChange);
184 CredChangeEventInfo info = {changeInfo.callerName, changeInfo.callerType, changeInfo.credentialId,
185 changeInfo.lastCredentialId, changeInfo.isSilentCredChange};
186
187 auto eventListenerSet =
188 EventListenerCallbackManager::GetInstance().GetCredEventListenerSet(static_cast<AuthType>(authType));
189 for (const auto &listener : eventListenerSet) {
190 if (listener == nullptr) {
191 IAM_LOGE("credListener is nullptr");
192 continue;
193 }
194 listener->OnNotifyCredChangeEvent(userId, static_cast<AuthType>(authType),
195 static_cast<CredChangeEventType>(eventType), info);
196 }
197 return SUCCESS;
198 }
199
CallbackEnter(uint32_t code)200 int32_t EventListenerCallbackManager::EventListenerCallbackImpl::CallbackEnter([[maybe_unused]] uint32_t code)
201 {
202 IAM_LOGI("start, code:%{public}u", code);
203 return SUCCESS;
204 }
205
CallbackExit(uint32_t code,int32_t result)206 int32_t EventListenerCallbackManager::EventListenerCallbackImpl::CallbackExit([[maybe_unused]] uint32_t code,
207 [[maybe_unused]] int32_t result)
208 {
209 IAM_LOGI("leave, code:%{public}u, result:%{public}d", code, result);
210 return SUCCESS;
211 }
212 } // namespace UserAuth
213 } // namespace UserIam
214 } // namespace OHOS