blob: db9021ae9fc2869d12572457ed552adc4c457698 [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#define LOG_TAG "MDnsEventReporter"
18
19#include "MDnsEventReporter.h"
20
21using android::net::mdns::aidl::IMDnsEventListener;
22
23MDnsEventReporter& MDnsEventReporter::getInstance() {
24 // It should be initialized only once.
25 static MDnsEventReporter instance;
26 return instance;
27}
28
29const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListeners() const {
30 return getEventListenersImpl();
31}
32
33int MDnsEventReporter::addEventListener(const android::sp<IMDnsEventListener>& listener) {
34 return addEventListenerImpl(listener);
35}
36
37int MDnsEventReporter::removeEventListener(const android::sp<IMDnsEventListener>& listener) {
38 return removeEventListenerImpl(listener);
39}
40
41const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListenersImpl() const {
42 std::lock_guard lock(mMutex);
43 return mEventListeners;
44}
45
46int MDnsEventReporter::addEventListenerImpl(const android::sp<IMDnsEventListener>& listener) {
47 if (listener == nullptr) {
48 ALOGE("The event listener should not be null");
49 return -EINVAL;
50 }
51
52 std::lock_guard lock(mMutex);
53
54 for (const auto& it : mEventListeners) {
55 if (android::IInterface::asBinder(it).get() ==
56 android::IInterface::asBinder(listener).get()) {
57 ALOGW("The event listener was already subscribed");
58 return -EEXIST;
59 }
60 }
61
62 // Create the death listener.
63 class DeathRecipient : public android::IBinder::DeathRecipient {
64 public:
65 DeathRecipient(MDnsEventReporter* eventReporter,
66 const android::sp<IMDnsEventListener>& listener)
67 : mEventReporter(eventReporter), mListener(listener) {}
68 ~DeathRecipient() override = default;
69 void binderDied(const android::wp<android::IBinder>& /* who */) override {
70 mEventReporter->removeEventListenerImpl(mListener);
71 }
72
73 private:
74 MDnsEventReporter* mEventReporter;
75 android::sp<IMDnsEventListener> mListener;
76 };
77
78 android::sp<android::IBinder::DeathRecipient> deathRecipient =
79 new DeathRecipient(this, listener);
80
81 android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
82
83 mEventListeners.insert(listener);
84 return 0;
85}
86
87int MDnsEventReporter::removeEventListenerImpl(const android::sp<IMDnsEventListener>& listener) {
88 if (listener == nullptr) {
89 ALOGE("The event listener should not be null");
90 return -EINVAL;
91 }
92
93 std::lock_guard lock(mMutex);
94
95 mEventListeners.erase(listener);
96 return 0;
97}