1/*
2 * Copyright (C) 2016 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_SERVER_EVENT_REPORTER_H
18#define NETD_SERVER_EVENT_REPORTER_H
19
20#include <atomic>
21#include <binder/IServiceManager.h>
22#include <mutex>
23
24#include "android/net/metrics/INetdEventListener.h"
25
26/*
27 * This class stores the reporting level and can be used to get the event listener service.
28 */
29class EventReporter {
30public:
31    int setMetricsReportingLevel(const int level);
32    int getMetricsReportingLevel() const;
33
34    // Returns the binder reference to the netd events listener service, attempting to fetch it if
35    // we do not have it already. This method is threadsafe.
36    android::sp<android::net::metrics::INetdEventListener> getNetdEventListener();
37
38private:
39    std::atomic_int mReportingLevel{
40            android::net::metrics::INetdEventListener::REPORTING_LEVEL_FULL};
41    // TODO: consider changing this into an atomic type such as
42    // std::atomic<android::net::metrics::INetdEventListener> and deleting the mutex.
43    //
44    // Alternatively, if this locking causes a performance penalty, have each single-threaded
45    // caller (DnsProxyListener, FwmarkServer) keep their own per-thread copy of NetdEventListener
46    // and remove mNetdEventListener entirely.
47    android::sp<android::net::metrics::INetdEventListener> mNetdEventListener;
48    std::mutex mutex;
49
50};
51
52#endif  // NETD_SERVER_EVENT_REPORTER_H
53