1 /* 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 21 using android::IInterface; 22 using android::sp; 23 using android::net::mdns::aidl::IMDnsEventListener; 24 getInstance()25MDnsEventReporter& MDnsEventReporter::getInstance() { 26 // It should be initialized only once. 27 static MDnsEventReporter instance; 28 return instance; 29 } 30 getEventListeners() const31const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListeners() const { 32 return getEventListenersImpl(); 33 } 34 addEventListener(const sp<IMDnsEventListener> & listener)35int MDnsEventReporter::addEventListener(const sp<IMDnsEventListener>& listener) { 36 return addEventListenerImpl(listener); 37 } 38 removeEventListener(const sp<IMDnsEventListener> & listener)39int MDnsEventReporter::removeEventListener(const sp<IMDnsEventListener>& listener) { 40 return removeEventListenerImpl(listener); 41 } 42 getEventListenersImpl() const43const MDnsEventReporter::EventListenerSet& MDnsEventReporter::getEventListenersImpl() const { 44 return mEventListeners; 45 } 46 addEventListenerImpl(const sp<IMDnsEventListener> & listener)47int MDnsEventReporter::addEventListenerImpl(const sp<IMDnsEventListener>& listener) { 48 if (listener == nullptr) { 49 ALOGE("The event listener should not be null"); 50 return -EINVAL; 51 } 52 53 std::lock_guard lock(mMutex); 54 55 for (const auto& it : mEventListeners) { 56 if (IInterface::asBinder(it->getListener()).get() == IInterface::asBinder(listener).get()) { 57 ALOGW("The event listener was already subscribed"); 58 return -EEXIST; 59 } 60 } 61 62 auto eventListener = sp<EventListener>::make(this, listener); 63 IInterface::asBinder(listener)->linkToDeath(eventListener); 64 mEventListeners.insert(eventListener); 65 return 0; 66 } 67 removeEventListenerImpl(const sp<IMDnsEventListener> & listener)68int MDnsEventReporter::removeEventListenerImpl(const sp<IMDnsEventListener>& listener) { 69 if (listener == nullptr) { 70 ALOGE("The event listener should not be null"); 71 return -EINVAL; 72 } 73 74 std::lock_guard lock(mMutex); 75 76 for (const auto& it : mEventListeners) { 77 const auto& binder = IInterface::asBinder(it->getListener()); 78 if (binder == IInterface::asBinder(listener)) { 79 binder->unlinkToDeath(it); 80 mEventListeners.erase(it); 81 return 0; 82 } 83 } 84 ALOGE("The event listener does not exist"); 85 return -ENOENT; 86 }