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 #ifndef IAM_AUTH_EVENT_LISTENER_MANAGER_H 17 #define IAM_AUTH_EVENT_LISTENER_MANAGER_H 18 19 #include "user_auth_interface.h" 20 #include <map> 21 #include <mutex> 22 #include <set> 23 24 namespace OHOS { 25 namespace UserIam { 26 namespace UserAuth { 27 using DeathRecipient = IRemoteObject::DeathRecipient; 28 class AuthEventListenerManager { 29 public: 30 static AuthEventListenerManager &GetInstance(); 31 int32_t RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType, 32 const sptr<AuthEventListenerInterface> &listener); 33 int32_t UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener); 34 void OnNotifyAuthSuccessEvent(int32_t userId, AuthType authType, int32_t callerType, std::string &callerName); 35 int32_t AddDeathRecipient(const sptr<AuthEventListenerInterface> &listener); 36 int32_t RemoveDeathRecipient(const sptr<AuthEventListenerInterface> &listener); 37 std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> GetDeathRecipientMap(); 38 39 protected: 40 class AuthEventListenerDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable { 41 public: 42 AuthEventListenerDeathRecipient() = default; 43 ~AuthEventListenerDeathRecipient() override = default; 44 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 45 }; 46 47 AuthEventListenerManager() = default; 48 ~AuthEventListenerManager() = default; 49 void AddAuthSuccessEventListener(AuthType authType, const sptr<AuthEventListenerInterface> &listener); 50 void RemoveAuthSuccessEventListener(AuthType authType, const sptr<AuthEventListenerInterface> &listener); 51 std::set<sptr<AuthEventListenerInterface>> GetListenerSet(AuthType authType); 52 std::recursive_mutex mutex_; 53 std::map<AuthType, std::set<sptr<AuthEventListenerInterface>>> eventListenerMap_; 54 std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>> deathRecipientMap_; 55 56 private: 57 struct FinderSet { FinderSetFinderSet58 explicit FinderSet(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) 59 { 60 } operatorFinderSet61 bool operator()(sptr<AuthEventListenerInterface> listener) 62 { 63 return listener->AsObject() == remoteObject_; 64 } 65 sptr<IRemoteObject> remoteObject_ {nullptr}; 66 }; 67 68 struct FinderMap { FinderMapFinderMap69 explicit FinderMap(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) 70 { 71 } operatorFinderMap72 bool operator()(std::map<sptr<AuthEventListenerInterface>, sptr<DeathRecipient>>::value_type &pair) 73 { 74 return pair.first->AsObject() == remoteObject_; 75 } 76 sptr<IRemoteObject> remoteObject_ {nullptr}; 77 }; 78 }; 79 } // namespace UserAuth 80 } // namespace UserIam 81 } // namespace OHOS 82 #endif // IAM_AUTH_EVENT_LISTENER_MANAGER_H