blob: eb0d9cff9f697ebf2097754afd2e184e60196713 [file] [log] [blame]
Mike Yu39cc6892018-12-14 16:18:27 +08001/*
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 Chena32c8c12019-01-25 10:47:40 +080020#include <set>
21
Mike Yu6d2c8922019-02-15 16:03:02 +080022#include <android-base/thread_annotations.h>
23
Mike Yu39cc6892018-12-14 16:18:27 +080024#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 Chena32c8c12019-01-25 10:47:40 +080028 * via stable runtime ABI which is achieved from libbinder_ndk. It also allows that
29 * register an event listener.
Mike Yu39cc6892018-12-14 16:18:27 +080030 */
31class ResolverEventReporter {
32 public:
Hungming Chena32c8c12019-01-25 10:47:40 +080033 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 Chena32c8c12019-01-25 10:47:40 +080039
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 Yu39cc6892018-12-14 16:18:27 +080049
50 private:
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080051 ResolverEventReporter() = default;
Hungming Chena32c8c12019-01-25 10:47:40 +080052 ~ResolverEventReporter() = default;
Mike Yu39cc6892018-12-14 16:18:27 +080053
Hungming Chena32c8c12019-01-25 10:47:40 +080054 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 Chenfbfa1ce2019-03-26 17:46:46 +080062 void handleBinderDied(const void* who) EXCLUDES(mMutex);
Hungming Chena32c8c12019-01-25 10:47:40 +080063
64 mutable std::mutex mMutex;
65 ListenerSet mListeners GUARDED_BY(mMutex);
Mike Yu39cc6892018-12-14 16:18:27 +080066};
67
68#endif // NETD_RESOLV_EVENT_REPORTER_H