1 /* 2 * Copyright (C) 2018 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 #ifndef NETD_RESOLV_EVENT_REPORTER_H 18 #define NETD_RESOLV_EVENT_REPORTER_H 19 20 #include <set> 21 22 #include <android-base/thread_annotations.h> 23 24 #include "aidl/android/net/metrics/INetdEventListener.h" 25 #include "aidl/android/net/resolv/aidl/IDnsResolverUnsolicitedEventListener.h" 26 27 /* 28 * This class can be used to get the binder reference to the netd events listener service 29 * via stable runtime ABI which is achieved from libbinder_ndk. It also allows that 30 * register an event listener. 31 */ 32 class ResolverEventReporter { 33 public: 34 ResolverEventReporter(ResolverEventReporter const&) = delete; 35 ResolverEventReporter(ResolverEventReporter&&) = delete; 36 ResolverEventReporter& operator=(ResolverEventReporter const&) = delete; 37 ResolverEventReporter& operator=(ResolverEventReporter&&) = delete; 38 39 using ListenerSet = std::set<std::shared_ptr<aidl::android::net::metrics::INetdEventListener>>; 40 using UnsolEventListenerSet = std::set<std::shared_ptr< 41 aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>>; 42 43 // Get the instance of the singleton ResolverEventReporter. 44 static ResolverEventReporter& getInstance(); 45 46 // Return the binder from the singleton ResolverEventReporter. This method is threadsafe. 47 ListenerSet getListeners() const; 48 49 // Return registered binder services from the singleton ResolverEventReporter. This method is 50 // threadsafe. 51 UnsolEventListenerSet getUnsolEventListeners() const; 52 53 // Add the binder to the singleton ResolverEventReporter. This method is threadsafe. 54 int addListener( 55 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener); 56 57 // Add the binder to the singleton ResolverEventReporter. This method is threadsafe. 58 int addUnsolEventListener( 59 const std::shared_ptr< 60 aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& 61 listener); 62 63 private: 64 ResolverEventReporter() = default; 65 ~ResolverEventReporter() = default; 66 67 void addDefaultListener() EXCLUDES(mMutex); 68 int addListenerImpl( 69 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) 70 EXCLUDES(mMutex); 71 int addListenerImplLocked( 72 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) 73 REQUIRES(mMutex); 74 int addUnsolEventListenerImpl( 75 const std::shared_ptr< 76 aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& 77 listener) EXCLUDES(mMutex); 78 int addUnsolEventListenerImplLocked( 79 const std::shared_ptr< 80 aidl::android::net::resolv::aidl::IDnsResolverUnsolicitedEventListener>& 81 listener) REQUIRES(mMutex); 82 ListenerSet getListenersImpl() const EXCLUDES(mMutex); 83 UnsolEventListenerSet getUnsolEventListenersImpl() const EXCLUDES(mMutex); 84 void handleBinderDied(const void* who) EXCLUDES(mMutex); 85 void handleUnsolEventBinderDied(const void* who) EXCLUDES(mMutex); 86 87 mutable std::mutex mMutex; 88 ListenerSet mListeners GUARDED_BY(mMutex); 89 UnsolEventListenerSet mUnsolEventListeners GUARDED_BY(mMutex); 90 }; 91 92 #endif // NETD_RESOLV_EVENT_REPORTER_H 93