Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 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 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 20 | #include <set> |
| 21 | |
Mike Yu | 6d2c892 | 2019-02-15 16:03:02 +0800 | [diff] [blame] | 22 | #include <android-base/thread_annotations.h> |
| 23 | |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 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 |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 28 | * via stable runtime ABI which is achieved from libbinder_ndk. It also allows that |
| 29 | * register an event listener. |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 30 | */ |
| 31 | class ResolverEventReporter { |
| 32 | public: |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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>>; |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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); |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 49 | |
| 50 | private: |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 51 | ResolverEventReporter() = default; |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 52 | ~ResolverEventReporter() = default; |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 53 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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); |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 62 | void handleBinderDied(const void* who) EXCLUDES(mMutex); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 63 | |
| 64 | mutable std::mutex mMutex; |
| 65 | ListenerSet mListeners GUARDED_BY(mMutex); |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | #endif // NETD_RESOLV_EVENT_REPORTER_H |