Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: net/base/network_change_notifier.h

Issue 9147026: API for connection type (Ethernet/WIFI/WWAN ...) in NetworkChangeNotifier. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: sync Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/observer_list_threadsafe.h" 10 #include "base/observer_list_threadsafe.h"
(...skipping 13 matching lines...) Expand all
24 // Flags which are ORed together to form |detail| in OnDNSChanged. 24 // Flags which are ORed together to form |detail| in OnDNSChanged.
25 enum { 25 enum {
26 // The DNS configuration (name servers, suffix search) has changed. 26 // The DNS configuration (name servers, suffix search) has changed.
27 CHANGE_DNS_SETTINGS = 1 << 0, 27 CHANGE_DNS_SETTINGS = 1 << 0,
28 // The HOSTS file has changed. 28 // The HOSTS file has changed.
29 CHANGE_DNS_HOSTS = 1 << 1, 29 CHANGE_DNS_HOSTS = 1 << 1,
30 // Computer name has changed. 30 // Computer name has changed.
31 CHANGE_DNS_LOCALHOST = 1 << 2, 31 CHANGE_DNS_LOCALHOST = 1 << 2,
32 }; 32 };
33 33
34 // Using the terminology of the Netwotk Information API:
35 // http://www.w3.org/TR/netinfo-api.
36 enum ConnectionType {
37 CONNECTION_NONE, // No connection.
38 CONNECTION_UNKNOWN, // A connection exists, but its type is unknown.
39 CONNECTION_2G,
40 CONNECTION_3G,
41 CONNECTION_4G,
42 CONNECTION_WIFI,
43 CONNECTION_ETHERNET
wtc 2012/05/11 01:35:05 Nit: I suggest listing these enumerators in the sa
44 };
45
34 class NET_EXPORT IPAddressObserver { 46 class NET_EXPORT IPAddressObserver {
35 public: 47 public:
36 virtual ~IPAddressObserver() {} 48 virtual ~IPAddressObserver() {}
37 49
38 // Will be called when the IP address of the primary interface changes. 50 // Will be called when the IP address of the primary interface changes.
39 // This includes when the primary interface itself changes. 51 // This includes when the primary interface itself changes.
40 virtual void OnIPAddressChanged() = 0; 52 virtual void OnIPAddressChanged() = 0;
41 53
42 protected: 54 protected:
43 IPAddressObserver() {} 55 IPAddressObserver() {}
44 56
45 private: 57 private:
46 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); 58 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver);
47 }; 59 };
48 60
49 class NET_EXPORT OnlineStateObserver { 61 class NET_EXPORT ConnectionTypeObserver {
50 public: 62 public:
51 virtual ~OnlineStateObserver() {} 63 virtual ~ConnectionTypeObserver() {}
52 64
53 // Will be called when the online state of the system may have changed. 65 // Will be called when the connection type of the system may have changed.
54 // See NetworkChangeNotifier::IsOffline() for important caveats about 66 // See NetworkChangeNotifier::GetConnectionType() for important caveats
55 // the unreliability of this signal. 67 // about the unreliability of this signal.
wtc 2012/05/11 01:35:05 Does this comment still apply? Is the "connection
56 virtual void OnOnlineStateChanged(bool online) = 0; 68 virtual void OnConnectionTypeChanged(ConnectionType type) = 0;
57 69
58 protected: 70 protected:
59 OnlineStateObserver() {} 71 ConnectionTypeObserver() {}
60 72
61 private: 73 private:
62 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); 74 DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver);
63 }; 75 };
64 76
65 class NET_EXPORT DNSObserver { 77 class NET_EXPORT DNSObserver {
66 public: 78 public:
67 virtual ~DNSObserver() {} 79 virtual ~DNSObserver() {}
68 80
69 // Will be called when the DNS settings of the system may have changed. 81 // Will be called when the DNS settings of the system may have changed.
70 // The flags set in |detail| provide the specific set of changes. 82 // The flags set in |detail| provide the specific set of changes.
71 virtual void OnDNSChanged(unsigned detail) = 0; 83 virtual void OnDNSChanged(unsigned detail) = 0;
72 84
73 protected: 85 protected:
74 DNSObserver() {} 86 DNSObserver() {}
75 87
76 private: 88 private:
77 DISALLOW_COPY_AND_ASSIGN(DNSObserver); 89 DISALLOW_COPY_AND_ASSIGN(DNSObserver);
78 }; 90 };
79 91
80 virtual ~NetworkChangeNotifier(); 92 virtual ~NetworkChangeNotifier();
81 93
82 // See the description of NetworkChangeNotifier::IsOffline(). 94 // See the description of NetworkChangeNotifier::GetConnectionType().
83 // Implementations must be thread-safe. Implementations must also be 95 // Implementations must be thread-safe. Implementations must also be
84 // cheap as this could be called (repeatedly) from the IO thread. 96 // cheap as this could be called (repeatedly) from the IO thread.
85 virtual bool IsCurrentlyOffline() const = 0; 97 virtual ConnectionType GetCurrentConnectionType() const = 0;
86 98
87 // Replaces the default class factory instance of NetworkChangeNotifier class. 99 // Replaces the default class factory instance of NetworkChangeNotifier class.
88 // The method will take over the ownership of |factory| object. 100 // The method will take over the ownership of |factory| object.
89 static void SetFactory(NetworkChangeNotifierFactory* factory); 101 static void SetFactory(NetworkChangeNotifierFactory* factory);
90 102
91 // Creates the process-wide, platform-specific NetworkChangeNotifier. The 103 // Creates the process-wide, platform-specific NetworkChangeNotifier. The
92 // caller owns the returned pointer. You may call this on any thread. You 104 // caller owns the returned pointer. You may call this on any thread. You
93 // may also avoid creating this entirely (in which case nothing will be 105 // may also avoid creating this entirely (in which case nothing will be
94 // monitored), but if you do create it, you must do so before any other 106 // monitored), but if you do create it, you must do so before any other
95 // threads try to access the API below, and it must outlive all other threads 107 // threads try to access the API below, and it must outlive all other threads
96 // which might try to use it. 108 // which might try to use it.
97 static NetworkChangeNotifier* Create(); 109 static NetworkChangeNotifier* Create();
98 110
99 // Returns true if there is currently no internet connection. 111 // A return value of |NONE| is a pretty strong indicator that the user
wtc 2012/05/11 01:35:05 |NONE| => |CONNECTION_NONE|
100 // 112 // won't be able to connect to remote sites. However, another return value is
wtc 2012/05/11 01:35:05 Nit: "another return value is inconclusive" needs
101 // A return value of |true| is a pretty strong indicator that the user 113 // inconclusive; even if some link is up, it is uncertain whether a particular
102 // won't be able to connect to remote sites. However, a return value of 114 // connection attempt to a particular remote site will be successfully.
wtc 2012/05/11 01:35:05 successfully => successful
103 // |false| is inconclusive; even if some link is up, it is uncertain 115 static ConnectionType GetConnectionType();
104 // whether a particular connection attempt to a particular remote site
105 // will be successfully.
106 static bool IsOffline();
107 116
108 // Like Create(), but for use in tests. The mock object doesn't monitor any 117 // Like Create(), but for use in tests. The mock object doesn't monitor any
109 // events, it merely rebroadcasts notifications when requested. 118 // events, it merely rebroadcasts notifications when requested.
110 static NetworkChangeNotifier* CreateMock(); 119 static NetworkChangeNotifier* CreateMock();
111 120
112 // Registers |observer| to receive notifications of network changes. The 121 // Registers |observer| to receive notifications of network changes. The
113 // thread on which this is called is the thread on which |observer| will be 122 // thread on which this is called is the thread on which |observer| will be
114 // called back with notifications. This is safe to call if Create() has not 123 // called back with notifications. This is safe to call if Create() has not
115 // been called (as long as it doesn't race the Create() call on another 124 // been called (as long as it doesn't race the Create() call on another
116 // thread), in which case it will simply do nothing. 125 // thread), in which case it will simply do nothing.
117 static void AddIPAddressObserver(IPAddressObserver* observer); 126 static void AddIPAddressObserver(IPAddressObserver* observer);
118 static void AddOnlineStateObserver(OnlineStateObserver* observer); 127 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
119 static void AddDNSObserver(DNSObserver* observer); 128 static void AddDNSObserver(DNSObserver* observer);
120 129
121 // Unregisters |observer| from receiving notifications. This must be called 130 // Unregisters |observer| from receiving notifications. This must be called
122 // on the same thread on which AddObserver() was called. Like AddObserver(), 131 // on the same thread on which AddObserver() was called. Like AddObserver(),
123 // this is safe to call if Create() has not been called (as long as it doesn't 132 // this is safe to call if Create() has not been called (as long as it doesn't
124 // race the Create() call on another thread), in which case it will simply do 133 // race the Create() call on another thread), in which case it will simply do
125 // nothing. Technically, it's also safe to call after the notifier object has 134 // nothing. Technically, it's also safe to call after the notifier object has
126 // been destroyed, if the call doesn't race the notifier's destruction, but 135 // been destroyed, if the call doesn't race the notifier's destruction, but
127 // there's no reason to use the API in this risky way, so don't do it. 136 // there's no reason to use the API in this risky way, so don't do it.
128 static void RemoveIPAddressObserver(IPAddressObserver* observer); 137 static void RemoveIPAddressObserver(IPAddressObserver* observer);
129 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); 138 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer);
130 static void RemoveDNSObserver(DNSObserver* observer); 139 static void RemoveDNSObserver(DNSObserver* observer);
131 140
132 // Allow unit tests to trigger notifications. 141 // Allow unit tests to trigger notifications.
133 static void NotifyObserversOfIPAddressChangeForTests() { 142 static void NotifyObserversOfIPAddressChangeForTests() {
134 NotifyObserversOfIPAddressChange(); 143 NotifyObserversOfIPAddressChange();
135 } 144 }
136 145
137 protected: 146 protected:
138 NetworkChangeNotifier(); 147 NetworkChangeNotifier();
139 148
140 // Broadcasts a notification to all registered observers. Note that this 149 // Broadcasts a notification to all registered observers. Note that this
141 // happens asynchronously, even for observers on the current thread, even in 150 // happens asynchronously, even for observers on the current thread, even in
142 // tests. 151 // tests.
143 static void NotifyObserversOfIPAddressChange(); 152 static void NotifyObserversOfIPAddressChange();
144 static void NotifyObserversOfOnlineStateChange(); 153 static void NotifyObserversOfConnectionTypeChange();
145 static void NotifyObserversOfDNSChange(unsigned detail); 154 static void NotifyObserversOfDNSChange(unsigned detail);
146 155
147 private: 156 private:
148 friend class NetworkChangeNotifierLinuxTest; 157 friend class NetworkChangeNotifierLinuxTest;
149 friend class NetworkChangeNotifierWinTest; 158 friend class NetworkChangeNotifierWinTest;
150 159
151 // Allows a second NetworkChangeNotifier to be created for unit testing, so 160 // Allows a second NetworkChangeNotifier to be created for unit testing, so
152 // the test suite can create a MockNetworkChangeNotifier, but platform 161 // the test suite can create a MockNetworkChangeNotifier, but platform
153 // specific NetworkChangeNotifiers can also be created for testing. To use, 162 // specific NetworkChangeNotifiers can also be created for testing. To use,
154 // create an DisableForTest object, and then create the new 163 // create an DisableForTest object, and then create the new
155 // NetworkChangeNotifier object. The NetworkChangeNotifier must be 164 // NetworkChangeNotifier object. The NetworkChangeNotifier must be
156 // destroyed before the DisableForTest object, as its destruction will restore 165 // destroyed before the DisableForTest object, as its destruction will restore
157 // the original NetworkChangeNotifier. 166 // the original NetworkChangeNotifier.
158 class NET_EXPORT_PRIVATE DisableForTest { 167 class NET_EXPORT_PRIVATE DisableForTest {
159 public: 168 public:
160 DisableForTest(); 169 DisableForTest();
161 ~DisableForTest(); 170 ~DisableForTest();
162 171
163 private: 172 private:
164 // The original NetworkChangeNotifier to be restored on destruction. 173 // The original NetworkChangeNotifier to be restored on destruction.
165 NetworkChangeNotifier* network_change_notifier_; 174 NetworkChangeNotifier* network_change_notifier_;
166 }; 175 };
167 176
168 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > 177 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> >
169 ip_address_observer_list_; 178 ip_address_observer_list_;
170 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > 179 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> >
171 online_state_observer_list_; 180 connection_type_observer_list_;
172 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > 181 const scoped_refptr<ObserverListThreadSafe<DNSObserver> >
173 resolver_state_observer_list_; 182 resolver_state_observer_list_;
174 183
175 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); 184 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier);
176 }; 185 };
177 186
178 } // namespace net 187 } // namespace net
179 188
180 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ 189 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698