blob: 5459cd63e9586105ff6dea70e6228379c9fa78ef [file] [log] [blame]
honghaiz023f3ef2015-10-19 09:39:32 -07001/*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_NETWORKMONITOR_H_
12#define WEBRTC_BASE_NETWORKMONITOR_H_
13
14#include "webrtc/base/logging.h"
honghaiz023f3ef2015-10-19 09:39:32 -070015#include "webrtc/base/sigslot.h"
16#include "webrtc/base/thread.h"
17
18namespace rtc {
honghaizcec0a082016-01-15 14:49:09 -080019
20class IPAddress;
21
22// Error values are negative.
23enum NetworkBindingResults {
24 NETWORK_BIND_SUCCESS = 0, // No error
25 NETWORK_BIND_FAILURE = -1, // Generic error
26 NETWORK_BIND_NOT_IMPLEMENTED = -2,
27 NETWORK_BIND_ADDRESS_NOT_FOUND = -3,
28 NETWORK_BIND_NETWORK_CHANGED = -4
29};
30
honghaiza7ad7c32016-02-02 12:54:14 -080031enum AdapterType {
32 // This enum resembles the one in Chromium net::ConnectionType.
33 ADAPTER_TYPE_UNKNOWN = 0,
34 ADAPTER_TYPE_ETHERNET = 1 << 0,
35 ADAPTER_TYPE_WIFI = 1 << 1,
36 ADAPTER_TYPE_CELLULAR = 1 << 2,
37 ADAPTER_TYPE_VPN = 1 << 3,
38 ADAPTER_TYPE_LOOPBACK = 1 << 4
39};
40
honghaizcec0a082016-01-15 14:49:09 -080041class NetworkBinderInterface {
42 public:
43 // Binds a socket to the network that is attached to |address| so that all
44 // packets on the socket |socket_fd| will be sent via that network.
45 // This is needed because some operating systems (like Android) require a
46 // special bind call to put packets on a non-default network interface.
47 virtual int BindSocketToNetwork(int socket_fd, const IPAddress& address) = 0;
honghaiza0c44ea2016-03-23 16:07:48 -070048 virtual ~NetworkBinderInterface() {}
honghaizcec0a082016-01-15 14:49:09 -080049};
50
honghaiz023f3ef2015-10-19 09:39:32 -070051/*
52 * Receives network-change events via |OnNetworksChanged| and signals the
53 * networks changed event.
54 *
55 * Threading consideration:
56 * It is expected that all upstream operations (from native to Java) are
57 * performed from the worker thread. This includes creating, starting and
58 * stopping the monitor. This avoids the potential race condition when creating
59 * the singleton Java NetworkMonitor class. Downstream operations can be from
60 * any thread, but this class will forward all the downstream operations onto
61 * the worker thread.
62 *
63 * Memory consideration:
64 * NetworkMonitor is owned by the caller (NetworkManager). The global network
65 * monitor factory is owned by the factory itself but needs to be released from
66 * the factory creator.
67 */
68// Generic network monitor interface. It starts and stops monitoring network
69// changes, and fires the SignalNetworksChanged event when networks change.
70class NetworkMonitorInterface {
71 public:
72 NetworkMonitorInterface();
73 virtual ~NetworkMonitorInterface();
74
75 sigslot::signal0<> SignalNetworksChanged;
76
77 virtual void Start() = 0;
78 virtual void Stop() = 0;
79
80 // Implementations should call this method on the base when networks change,
81 // and the base will fire SignalNetworksChanged on the right thread.
82 virtual void OnNetworksChanged() = 0;
honghaiza7ad7c32016-02-02 12:54:14 -080083
84 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
honghaiz023f3ef2015-10-19 09:39:32 -070085};
86
87class NetworkMonitorBase : public NetworkMonitorInterface,
88 public MessageHandler,
89 public sigslot::has_slots<> {
90 public:
91 NetworkMonitorBase();
92 ~NetworkMonitorBase() override;
93
94 void OnNetworksChanged() override;
95
96 void OnMessage(Message* msg) override;
97
honghaizcec0a082016-01-15 14:49:09 -080098 protected:
99 Thread* worker_thread() { return worker_thread_; }
100
honghaiz023f3ef2015-10-19 09:39:32 -0700101 private:
honghaizcec0a082016-01-15 14:49:09 -0800102 Thread* worker_thread_;
honghaiz023f3ef2015-10-19 09:39:32 -0700103};
104
105/*
106 * NetworkMonitorFactory creates NetworkMonitors.
107 */
108class NetworkMonitorFactory {
109 public:
110 // This is not thread-safe; it should be called once (or once per audio/video
111 // call) during the call initialization.
112 static void SetFactory(NetworkMonitorFactory* factory);
113
114 static void ReleaseFactory(NetworkMonitorFactory* factory);
115 static NetworkMonitorFactory* GetFactory();
116
117 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
118
119 virtual ~NetworkMonitorFactory();
120
121 protected:
122 NetworkMonitorFactory();
123};
124
125} // namespace rtc
126
127#endif // WEBRTC_BASE_NETWORKMONITOR_H_