blob: 35ab2b120e38b5f48057a6c580984d6837753233 [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"
15#include "webrtc/base/scoped_ptr.h"
16#include "webrtc/base/sigslot.h"
17#include "webrtc/base/thread.h"
18
19namespace rtc {
honghaizcec0a082016-01-15 14:49:09 -080020
21class IPAddress;
22
23// Error values are negative.
24enum NetworkBindingResults {
25 NETWORK_BIND_SUCCESS = 0, // No error
26 NETWORK_BIND_FAILURE = -1, // Generic error
27 NETWORK_BIND_NOT_IMPLEMENTED = -2,
28 NETWORK_BIND_ADDRESS_NOT_FOUND = -3,
29 NETWORK_BIND_NETWORK_CHANGED = -4
30};
31
honghaiza7ad7c32016-02-02 12:54:14 -080032enum AdapterType {
33 // This enum resembles the one in Chromium net::ConnectionType.
34 ADAPTER_TYPE_UNKNOWN = 0,
35 ADAPTER_TYPE_ETHERNET = 1 << 0,
36 ADAPTER_TYPE_WIFI = 1 << 1,
37 ADAPTER_TYPE_CELLULAR = 1 << 2,
38 ADAPTER_TYPE_VPN = 1 << 3,
39 ADAPTER_TYPE_LOOPBACK = 1 << 4
40};
41
honghaizcec0a082016-01-15 14:49:09 -080042class NetworkBinderInterface {
43 public:
44 // Binds a socket to the network that is attached to |address| so that all
45 // packets on the socket |socket_fd| will be sent via that network.
46 // This is needed because some operating systems (like Android) require a
47 // special bind call to put packets on a non-default network interface.
48 virtual int BindSocketToNetwork(int socket_fd, const IPAddress& address) = 0;
honghaiza0c44ea2016-03-23 16:07:48 -070049 virtual ~NetworkBinderInterface() {}
honghaizcec0a082016-01-15 14:49:09 -080050};
51
honghaiz023f3ef2015-10-19 09:39:32 -070052/*
53 * Receives network-change events via |OnNetworksChanged| and signals the
54 * networks changed event.
55 *
56 * Threading consideration:
57 * It is expected that all upstream operations (from native to Java) are
58 * performed from the worker thread. This includes creating, starting and
59 * stopping the monitor. This avoids the potential race condition when creating
60 * the singleton Java NetworkMonitor class. Downstream operations can be from
61 * any thread, but this class will forward all the downstream operations onto
62 * the worker thread.
63 *
64 * Memory consideration:
65 * NetworkMonitor is owned by the caller (NetworkManager). The global network
66 * monitor factory is owned by the factory itself but needs to be released from
67 * the factory creator.
68 */
69// Generic network monitor interface. It starts and stops monitoring network
70// changes, and fires the SignalNetworksChanged event when networks change.
71class NetworkMonitorInterface {
72 public:
73 NetworkMonitorInterface();
74 virtual ~NetworkMonitorInterface();
75
76 sigslot::signal0<> SignalNetworksChanged;
77
78 virtual void Start() = 0;
79 virtual void Stop() = 0;
80
81 // Implementations should call this method on the base when networks change,
82 // and the base will fire SignalNetworksChanged on the right thread.
83 virtual void OnNetworksChanged() = 0;
honghaiza7ad7c32016-02-02 12:54:14 -080084
85 virtual AdapterType GetAdapterType(const std::string& interface_name) = 0;
honghaiz023f3ef2015-10-19 09:39:32 -070086};
87
88class NetworkMonitorBase : public NetworkMonitorInterface,
89 public MessageHandler,
90 public sigslot::has_slots<> {
91 public:
92 NetworkMonitorBase();
93 ~NetworkMonitorBase() override;
94
95 void OnNetworksChanged() override;
96
97 void OnMessage(Message* msg) override;
98
honghaizcec0a082016-01-15 14:49:09 -080099 protected:
100 Thread* worker_thread() { return worker_thread_; }
101
honghaiz023f3ef2015-10-19 09:39:32 -0700102 private:
honghaizcec0a082016-01-15 14:49:09 -0800103 Thread* worker_thread_;
honghaiz023f3ef2015-10-19 09:39:32 -0700104};
105
106/*
107 * NetworkMonitorFactory creates NetworkMonitors.
108 */
109class NetworkMonitorFactory {
110 public:
111 // This is not thread-safe; it should be called once (or once per audio/video
112 // call) during the call initialization.
113 static void SetFactory(NetworkMonitorFactory* factory);
114
115 static void ReleaseFactory(NetworkMonitorFactory* factory);
116 static NetworkMonitorFactory* GetFactory();
117
118 virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
119
120 virtual ~NetworkMonitorFactory();
121
122 protected:
123 NetworkMonitorFactory();
124};
125
126} // namespace rtc
127
128#endif // WEBRTC_BASE_NETWORKMONITOR_H_