1 /*
2 * Copyright (c) 2024 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 "auth_event_listener_manager.h"
17
18 #include <sstream>
19
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23
24 #define LOG_TAG "USER_AUTH_SA"
25
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 using DeathRecipient = IRemoteObject::DeathRecipient;
GetInstance()30 AuthEventListenerManager &AuthEventListenerManager::GetInstance()
31 {
32 IAM_LOGI("start");
33 static AuthEventListenerManager authEventListenerManager;
34 return authEventListenerManager;
35 }
36
RegistUserAuthSuccessEventListener(const std::vector<AuthType> & authType,const sptr<AuthEventListenerInterface> & listener)37 int32_t AuthEventListenerManager::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
38 const sptr<AuthEventListenerInterface> &listener)
39 {
40 IAM_LOGI("start");
41 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
42
43 std::lock_guard<std::recursive_mutex> lock(mutex_);
44 int32_t result = AddDeathRecipient(listener);
45 if (result != SUCCESS) {
46 IAM_LOGE("AddDeathRecipient fail");
47 return result;
48 }
49
50 for (const auto &iter : authType) {
51 AddAuthSuccessEventListener(iter, listener);
52 }
53 IAM_LOGI("RegistUserAuthSuccessEventListener success");
54 return SUCCESS;
55 }
56
UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> & listener)57 int32_t AuthEventListenerManager::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
58 {
59 IAM_LOGI("start");
60 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
61
62 std::lock_guard<std::recursive_mutex> lock(mutex_);
63 int32_t result = RemoveDeathRecipient(listener);
64 if (result != SUCCESS) {
65 IAM_LOGE("RemoveDeathRecipient fail");
66 return result;
67 }
68
69 RemoveAuthSuccessEventListener(AuthType::PIN, listener);
70 RemoveAuthSuccessEventListener(AuthType::FACE, listener);
71 RemoveAuthSuccessEventListener(AuthType::FINGERPRINT, listener);
72 IAM_LOGI("UnRegistUserAuthSuccessEventListener success");
73 return SUCCESS;
74 }
75
AddAuthSuccessEventListener(AuthType authType,const sptr<AuthEventListenerInterface> & listener)76 void AuthEventListenerManager::AddAuthSuccessEventListener(AuthType authType,
77 const sptr<AuthEventListenerInterface> &listener)
78 {
79 IAM_LOGI("AddAuthSuccessEventListener, authType:%{public}d", static_cast<int32_t>(authType));
80 auto iter = std::find_if(eventListenerMap_[authType].begin(), eventListenerMap_[authType].end(),
81 FinderSet(listener->AsObject()));
82 if (iter != eventListenerMap_[authType].end()) {
83 IAM_LOGE("listener is already registed");
84 return;
85 }
86 eventListenerMap_[authType].insert(listener);
87 IAM_LOGI("AddAuthSuccessEventListener success");
88 }
89
RemoveAuthSuccessEventListener(AuthType authType,const sptr<AuthEventListenerInterface> & listener)90 void AuthEventListenerManager::RemoveAuthSuccessEventListener(AuthType authType,
91 const sptr<AuthEventListenerInterface> &listener)
92 {
93 IAM_LOGI("RemoveAuthSuccessEventListener, authType:%{public}d", static_cast<int32_t>(authType));
94 auto iter = std::find_if(eventListenerMap_[authType].begin(), eventListenerMap_[authType].end(),
95 FinderSet(listener->AsObject()));
96 if (iter == eventListenerMap_[authType].end()) {
97 IAM_LOGE("listener is not registed");
98 return;
99 }
100 eventListenerMap_[authType].erase(listener);
101 IAM_LOGI("RemoveAuthSuccessEventListener success");
102 }
103
GetListenerSet(AuthType authType)104 std::set<sptr<AuthEventListenerInterface>> AuthEventListenerManager::GetListenerSet(AuthType authType)
105 {
106 std::lock_guard<std::recursive_mutex> lock(mutex_);
107 std::set<sptr<AuthEventListenerInterface>> listenerSet(eventListenerMap_[authType]);
108 return listenerSet;
109 }
110
OnNotifyAuthSuccessEvent(int32_t userId,AuthType authType,int32_t callerType,std::string & callerName)111 void AuthEventListenerManager::OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType,
112 std::string &callerName)
113 {
114 IAM_LOGI("start");
115 std::set<sptr<AuthEventListenerInterface>> listenerSetTemp = GetListenerSet(authType);
116 for (auto &iter : listenerSetTemp) {
117 if (iter != nullptr) {
118 iter->OnNotifyAuthSuccessEvent(userId, authType, callerType, callerName);
119 IAM_LOGI("OnNotifyAuthSuccessEvent, userId: %{public}d, authType: %{public}d, callerName: %{public}s, "
120 "callerType: %{public}d",
121 userId, static_cast<int32_t>(authType), callerName.c_str(), callerType);
122 }
123 }
124 }
125
AddDeathRecipient(const sptr<AuthEventListenerInterface> & listener)126 int32_t AuthEventListenerManager::AddDeathRecipient(const sptr<AuthEventListenerInterface> &listener)
127 {
128 IAM_LOGI("start");
129 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
130
131 auto obj = listener->AsObject();
132 if (obj == nullptr) {
133 IAM_LOGE("remote object is nullptr");
134 return GENERAL_ERROR;
135 }
136
137 auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(), FinderMap(listener->AsObject()));
138 if (iter != deathRecipientMap_.end()) {
139 IAM_LOGE("deathRecipient is already registed");
140 return SUCCESS;
141 }
142
143 sptr<DeathRecipient> dr(new (std::nothrow) AuthEventListenerDeathRecipient());
144 if ((dr == nullptr) || (!obj->AddDeathRecipient(dr))) {
145 IAM_LOGE("AddDeathRecipient failed");
146 return GENERAL_ERROR;
147 }
148
149 deathRecipientMap_.emplace(listener, dr);
150 IAM_LOGI("AddDeathRecipient success");
151 return SUCCESS;
152 }
153
RemoveDeathRecipient(const sptr<AuthEventListenerInterface> & listener)154 int32_t AuthEventListenerManager::RemoveDeathRecipient(const sptr<AuthEventListenerInterface> &listener)
155 {
156 IAM_LOGI("start");
157 IF_FALSE_LOGE_AND_RETURN_VAL(listener != nullptr, GENERAL_ERROR);
158
159 auto obj = listener->AsObject();
160 if (obj == nullptr) {
161 IAM_LOGE("remote object is nullptr");
162 return GENERAL_ERROR;
163 }
164
165 auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(), FinderMap(listener->AsObject()));
166 if (iter == deathRecipientMap_.end()) {
167 IAM_LOGE("deathRecipient is not registed");
168 return SUCCESS;
169 }
170
171 sptr<DeathRecipient> deathRecipient = iter->second;
172 if (deathRecipient == nullptr) {
173 IAM_LOGE("deathRecipient is nullptr");
174 return GENERAL_ERROR;
175 }
176
177 obj->RemoveDeathRecipient(deathRecipient);
178 deathRecipientMap_.erase(listener);
179 IAM_LOGI("RemoveDeathRecipient success");
180 return SUCCESS;
181 }
182
GetDeathRecipientMap()183 std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> AuthEventListenerManager::GetDeathRecipientMap()
184 {
185 IAM_LOGI("start");
186 std::lock_guard<std::recursive_mutex> lock(mutex_);
187 return deathRecipientMap_;
188 }
189
OnRemoteDied(const wptr<IRemoteObject> & remote)190 void AuthEventListenerManager::AuthEventListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
191 {
192 IAM_LOGI("start");
193 if (remote == nullptr) {
194 IAM_LOGE("remote is nullptr");
195 return;
196 }
197
198 std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> deathRecipientMap =
199 AuthEventListenerManager::GetInstance().GetDeathRecipientMap();
200 for (auto &iter : deathRecipientMap) {
201 if (iter.first != nullptr && remote == iter.first->AsObject()) {
202 int32_t result = AuthEventListenerManager::GetInstance().UnRegistUserAuthSuccessEventListener(iter.first);
203 if (result != SUCCESS) {
204 IAM_LOGE("UnRegistUserAuthSuccessEventListener fail");
205 return;
206 }
207 }
208 }
209 }
210 } // namespace UserAuth
211 } // namespace UserIam
212 } // namespace OHOS