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 REMOTE_CONNECT_LISTENER_MANAGER_H
17 #define REMOTE_CONNECT_LISTENER_MANAGER_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include "iam_common_defines.h"
23 #include "remote_connect_listener.h"
24 
25 #define LOG_TAG "USER_AUTH_SA"
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 class RemoteConnectListenerManager {
30 public:
31     static RemoteConnectListenerManager &GetInstance();
32     RemoteConnectListenerManager() = default;
33     ~RemoteConnectListenerManager() = default;
34 
35     ResultCode RegisterListener(const std::string &connectionName, const std::string &endPointName,
36         const std::shared_ptr<ConnectionListener> &listener);
37     ResultCode RegisterListener(const std::string &endPointName, const std::shared_ptr<ConnectionListener> &listener);
38     ResultCode UnregisterListener(const std::string &connectionName, const std::string &endPointName);
39     ResultCode UnregisterListener(const std::string &endPointName);
40 
41     std::shared_ptr<ConnectionListener> FindListener(const std::string &connectionName,
42         const std::string &endPointName);
43     void OnConnectionDown(const std::string &connectionName);
44     void OnConnectionUp(const std::string &connectionName);
45 
46     struct ListenerInfo {
47         std::string connectionName;
48         std::string endPointName;
49         std::shared_ptr<ConnectionListener> listener {nullptr};
50 
51         bool operator==(const ListenerInfo &other) const;
52     };
53 
54 private:
55     std::vector<ListenerInfo> listeners_;
56     std::recursive_mutex listenerMutex_;
57 };
58 } // namespace UserAuth
59 } // namespace UserIam
60 } // namespace OHOS
61 #endif // REMOTE_CONNECT_LISTENER_MANAGER_H