blob: cbc43ecbb792349563cff4580e9ea578dc78cf75 [file] [log] [blame]
paulhu72742952022-02-09 21:24:09 +08001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <android-base/thread_annotations.h>
20#include <android/net/mdns/aidl/IMDnsEventListener.h>
21
22#include <set>
23
24class MDnsEventReporter final {
25 public:
Paul Hudae10af2022-06-01 20:03:35 +080026 class EventListener : public android::IBinder::DeathRecipient {
27 public:
28 EventListener(MDnsEventReporter* eventReporter,
29 const android::sp<android::net::mdns::aidl::IMDnsEventListener>& listener)
30 : mEventReporter(eventReporter), mListener(listener) {}
31 ~EventListener() override = default;
32 void binderDied(const android::wp<android::IBinder>& /* who */) override {
33 mEventReporter->removeEventListenerImpl(mListener);
34 }
35 android::sp<android::net::mdns::aidl::IMDnsEventListener> getListener() {
36 return mListener;
37 }
38
39 private:
40 MDnsEventReporter* mEventReporter;
41 android::sp<android::net::mdns::aidl::IMDnsEventListener> mListener;
42 };
43
paulhu72742952022-02-09 21:24:09 +080044 MDnsEventReporter(const MDnsEventReporter&) = delete;
45 MDnsEventReporter& operator=(const MDnsEventReporter&) = delete;
46
Paul Hudae10af2022-06-01 20:03:35 +080047 using EventListenerSet = std::set<android::sp<EventListener>>;
paulhu72742952022-02-09 21:24:09 +080048
49 // Get the instance of the singleton MDnsEventReporter.
50 static MDnsEventReporter& getInstance();
51
52 // Return registered binder services from the singleton MDnsEventReporter. This method is
53 // threadsafe.
54 const EventListenerSet& getEventListeners() const;
55
56 // Add the binder to the singleton MDnsEventReporter. This method is threadsafe.
57 int addEventListener(const android::sp<android::net::mdns::aidl::IMDnsEventListener>& listener);
58
59 // Remove the binder from the singleton MDnsEventReporter. This method is threadsafe.
60 int removeEventListener(
61 const android::sp<android::net::mdns::aidl::IMDnsEventListener>& listener);
62
63 private:
64 MDnsEventReporter() = default;
65 ~MDnsEventReporter() = default;
66
67 mutable std::mutex mMutex;
68 EventListenerSet mEventListeners GUARDED_BY(mMutex);
69
70 int addEventListenerImpl(
71 const android::sp<android::net::mdns::aidl::IMDnsEventListener>& listener)
72 EXCLUDES(mMutex);
73 int removeEventListenerImpl(
74 const android::sp<android::net::mdns::aidl::IMDnsEventListener>& listener)
75 EXCLUDES(mMutex);
76 const EventListenerSet& getEventListenersImpl() const EXCLUDES(mMutex);
paulhu72742952022-02-09 21:24:09 +080077};