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 #ifndef SCREENLOCK_STRONGAUTH_LISTENER_MANAGER_H 17 #define SCREENLOCK_STRONGAUTH_LISTENER_MANAGER_H 18 19 #include <map> 20 #include <mutex> 21 #include <set> 22 23 #include "iremote_object.h" 24 #include "screenlock_common.h" 25 #include "screenlock_inner_listener_interface.h" 26 27 namespace OHOS { 28 namespace ScreenLock { 29 using DeathRecipient = IRemoteObject::DeathRecipient; 30 class InnerListenerManager : public RefBase { 31 public: 32 static sptr<InnerListenerManager> GetInstance(); 33 int32_t RegisterInnerListener(int32_t userId, const ListenType listenType, 34 const sptr<InnerListenerIf> &listener); 35 int32_t UnRegisterInnerListener(const ListenType listenType, const sptr<InnerListenerIf> &listener); 36 int32_t AddDeathRecipient(const ListenType listenType, const sptr<InnerListenerIf> &listener); 37 int32_t RemoveDeathRecipient(const sptr<InnerListenerIf> &listener); 38 std::map<sptr<InnerListenerIf>, std::pair<ListenType, sptr<DeathRecipient>>> GetDeathRecipient(); 39 void OnStrongAuthChanged(int32_t userId, int32_t strongAuth); 40 void OnDeviceLockStateChanged(int32_t userId, int32_t lockState); 41 42 protected: 43 class InnerListenerDeathRecipient : public IRemoteObject::DeathRecipient, public NoCopyable { 44 public: 45 InnerListenerDeathRecipient() = default; 46 ~InnerListenerDeathRecipient() override = default; 47 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 48 }; 49 50 InnerListenerManager() = default; 51 ~InnerListenerManager() = default; 52 int32_t AddInnerListener(int32_t userId, const ListenType listenType, 53 const sptr<InnerListenerIf> &listener); 54 int32_t RemoveInnerListener(const ListenType listenType, const sptr<InnerListenerIf> &listener); 55 bool HasListenerSet(int32_t userId, ListenType listenType); 56 std::recursive_mutex mutex_; 57 std::map<sptr<InnerListenerIf>, std::pair<ListenType, sptr<DeathRecipient>>> deathRecipientMap_; 58 std::map<ListenType, std::map<int32_t, std::set<sptr<InnerListenerIf>>>> innerListenMap_; 59 60 private: 61 static std::mutex instanceLock_; 62 static sptr<InnerListenerManager> instance_; 63 std::set<sptr<InnerListenerIf>> getListenerSet(int32_t userId, ListenType listenType); 64 void OnStateChanged(int32_t userId, int32_t lockState, ListenType listenType); 65 struct FinderSet { FinderSetFinderSet66 explicit FinderSet(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) 67 { 68 } operatorFinderSet69 bool operator()(sptr<InnerListenerIf> listener) 70 { 71 return listener->AsObject() == remoteObject_; 72 } 73 sptr<IRemoteObject> remoteObject_ {nullptr}; 74 }; 75 76 struct FinderMap { FinderMapFinderMap77 explicit FinderMap(sptr<IRemoteObject> remoteObject) : remoteObject_(remoteObject) 78 { 79 } operatorFinderMap80 bool operator()(std::map<sptr<InnerListenerIf>, std::pair<ListenType, sptr<DeathRecipient>>>::value_type &pair) 81 { 82 return pair.first->AsObject() == remoteObject_; 83 } 84 sptr<IRemoteObject> remoteObject_ {nullptr}; 85 }; 86 }; 87 } 88 } 89 #endif // SCREENLOCK_STRONGAUTH_LISTENER_MANAGER_H