• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 EVENT_LISTENER_CALLBACK_SERVICE_H
17 #define EVENT_LISTENER_CALLBACK_SERVICE_H
18 
19 #include <string>
20 #include <map>
21 #include <set>
22 
23 #include "event_listener_callback_stub.h"
24 #include "iuser_auth.h"
25 #include "iuser_idm.h"
26 #include "user_auth_client_callback.h"
27 #include "user_idm_client_callback.h"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 class EventListenerCallbackManager {
33 public:
34     static EventListenerCallbackManager &GetInstance();
35     int32_t AddUserAuthSuccessEventListener(const sptr<IUserAuth> &proxy, const std::vector<AuthType> &authTypes,
36         const std::shared_ptr<AuthSuccessEventListener> &listener);
37     int32_t RemoveUserAuthSuccessEventListener(const sptr<IUserAuth> &proxy,
38         const std::shared_ptr<AuthSuccessEventListener> &listener);
39     int32_t AddCredChangeEventListener(const sptr<IUserIdm> &proxy, const std::vector<AuthType> &authTypes,
40         const std::shared_ptr<CredChangeEventListener> &listener);
41     int32_t RemoveCredChangeEventListener(const sptr<IUserIdm> &proxy,
42         const std::shared_ptr<CredChangeEventListener> &listener);
43     void OnServiceDeath();
44 
45 protected:
46     std::set<std::shared_ptr<AuthSuccessEventListener>> GetAuthEventListenerSet(AuthType authType);
47     std::set<std::shared_ptr<CredChangeEventListener>> GetCredEventListenerSet(AuthType authType);
48 
49 private:
50     class EventListenerCallbackImpl : public EventListenerCallbackStub {
51     public:
52         explicit EventListenerCallbackImpl() = default;
53         ~EventListenerCallbackImpl() override = default;
54 
55         int32_t OnNotifyAuthSuccessEvent(int32_t userId, int32_t authType, int32_t callerType,
56             const std::string &callerName) override;
57         int32_t OnNotifyCredChangeEvent(int32_t userId, int32_t authType, int32_t eventType,
58             const IpcCredChangeEventInfo &changeInfo) override;
59         int32_t CallbackEnter([[maybe_unused]] uint32_t code) override;
60         int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override;
61     };
62 
63     EventListenerCallbackManager() = default;
64     ~EventListenerCallbackManager() = default;
65     sptr<EventListenerCallbackImpl> authEventListenerCallbackImpl_ = nullptr;
66     sptr<EventListenerCallbackImpl> credEventListenerCallbackImpl_ = nullptr;
67     std::recursive_mutex eventListenerMutex_;
68     std::map<AuthType, std::set<std::shared_ptr<AuthSuccessEventListener>>> authEventListenerMap_ = {};
69     std::map<AuthType, std::set<std::shared_ptr<CredChangeEventListener>>> credEventListenerMap_ = {};
70 };
71 } // namespace UserAuth
72 } // namespace UserIam
73 } // namespace OHOS
74 #endif // EVENT_LISTENER_CALLBACK_SERVICE_H
75