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 26 /* 27 * This class can be used to get the binder reference to the netd events listener service 28 * via stable runtime ABI which is achieved from libbinder_ndk. It also allows that 29 * register an event listener. 30 */ 31 class ResolverEventReporter { 32 public: 33 ResolverEventReporter(ResolverEventReporter const&) = delete; 34 ResolverEventReporter(ResolverEventReporter&&) = delete; 35 ResolverEventReporter& operator=(ResolverEventReporter const&) = delete; 36 ResolverEventReporter& operator=(ResolverEventReporter&&) = delete; 37 38 using ListenerSet = std::set<std::shared_ptr<aidl::android::net::metrics::INetdEventListener>>; 39 40 // Get the instance of the singleton ResolverEventReporter. 41 static ResolverEventReporter& getInstance(); 42 43 // Return the binder from the singleton ResolverEventReporter. This method is threadsafe. 44 ListenerSet getListeners() const; 45 46 // Add the binder to the singleton ResolverEventReporter. This method is threadsafe. 47 int addListener( 48 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener); 49 50 private: 51 ResolverEventReporter() = default; 52 ~ResolverEventReporter() = default; 53 54 void addDefaultListener() EXCLUDES(mMutex); 55 int addListenerImpl( 56 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) 57 EXCLUDES(mMutex); 58 int addListenerImplLocked( 59 const std::shared_ptr<aidl::android::net::metrics::INetdEventListener>& listener) 60 REQUIRES(mMutex); 61 ListenerSet getListenersImpl() const EXCLUDES(mMutex); 62 void handleBinderDied(const void* who) EXCLUDES(mMutex); 63 64 mutable std::mutex mMutex; 65 ListenerSet mListeners GUARDED_BY(mMutex); 66 }; 67 68 #endif // NETD_RESOLV_EVENT_REPORTER_H 69