OLD | NEW |
---|---|
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 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/observer_list_threadsafe.h" | 9 #include "base/observer_list_threadsafe.h" |
10 #include "net/base/net_export.h" | 10 #include "net/base/net_export.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 virtual void OnDNSChanged() = 0; | 79 virtual void OnDNSChanged() = 0; |
80 | 80 |
81 protected: | 81 protected: |
82 DNSObserver() {} | 82 DNSObserver() {} |
83 virtual ~DNSObserver() {} | 83 virtual ~DNSObserver() {} |
84 | 84 |
85 private: | 85 private: |
86 DISALLOW_COPY_AND_ASSIGN(DNSObserver); | 86 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
87 }; | 87 }; |
88 | 88 |
89 class NET_EXPORT NetworkChangeObserver { | |
90 public: | |
91 // OnNetworkChanged will be called when a change occurs to the host | |
92 // computer's hardware or software that affects the route network packets | |
93 // take to any network server. Some examples: | |
94 // 1. A network connection becoming available or going away. For example | |
95 // plugging or unplugging an Ethernet cable, WiFi or cellular modem | |
96 // connecting or disconnecting from a network, or a VPN tunnel being | |
97 // established or taken down. | |
98 // 2. An active network connection's IP address changes. | |
99 // 3. A change to the local IP routing tables. | |
100 // The signal shall only be produced when the change is complete. For | |
101 // example if a new network connection has become available, only give the | |
102 // signal once we think the O/S has finished establishing the connection | |
103 // (i.e. DHCP is done) to the point where the new connection is usable. | |
104 // The signal shall not be produced spuriously as it will be triggering some | |
105 // expensive operations, like socket pools closing all connections and | |
106 // sockets and then re-establishing them. | |
107 // |type| indicates the type of the active primary network connection after | |
108 // the change. Observers performing "constructive" activities like trying | |
109 // to establish a connection to a server should only do so when | |
110 // |type != CONNECTION_NONE|. Observers performing "destructive" activities | |
111 // like resetting already established server connections should only do so | |
112 // when |type == CONNECTION_NONE|. OnNetworkChanged will be called with | |
113 // CONNECTION_NONE immediately prior to being called with an online state; | |
114 // this is done to make sure that destructive actions take place prior to | |
115 // constructive actions. | |
116 virtual void OnNetworkChanged(ConnectionType type) = 0; | |
117 | |
118 protected: | |
119 NetworkChangeObserver() {} | |
120 virtual ~NetworkChangeObserver() {} | |
121 | |
122 private: | |
123 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver); | |
124 }; | |
125 | |
89 virtual ~NetworkChangeNotifier(); | 126 virtual ~NetworkChangeNotifier(); |
90 | 127 |
91 // See the description of NetworkChangeNotifier::GetConnectionType(). | 128 // See the description of NetworkChangeNotifier::GetConnectionType(). |
92 // Implementations must be thread-safe. Implementations must also be | 129 // Implementations must be thread-safe. Implementations must also be |
93 // cheap as this could be called (repeatedly) from the network thread. | 130 // cheap as this could be called (repeatedly) from the network thread. |
94 virtual ConnectionType GetCurrentConnectionType() const = 0; | 131 virtual ConnectionType GetCurrentConnectionType() const = 0; |
95 | 132 |
96 // Replaces the default class factory instance of NetworkChangeNotifier class. | 133 // Replaces the default class factory instance of NetworkChangeNotifier class. |
97 // The method will take over the ownership of |factory| object. | 134 // The method will take over the ownership of |factory| object. |
98 static void SetFactory(NetworkChangeNotifierFactory* factory); | 135 static void SetFactory(NetworkChangeNotifierFactory* factory); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 static NetworkChangeNotifier* CreateMock(); | 185 static NetworkChangeNotifier* CreateMock(); |
149 | 186 |
150 // Registers |observer| to receive notifications of network changes. The | 187 // Registers |observer| to receive notifications of network changes. The |
151 // thread on which this is called is the thread on which |observer| will be | 188 // thread on which this is called is the thread on which |observer| will be |
152 // called back with notifications. This is safe to call if Create() has not | 189 // called back with notifications. This is safe to call if Create() has not |
153 // been called (as long as it doesn't race the Create() call on another | 190 // been called (as long as it doesn't race the Create() call on another |
154 // thread), in which case it will simply do nothing. | 191 // thread), in which case it will simply do nothing. |
155 static void AddIPAddressObserver(IPAddressObserver* observer); | 192 static void AddIPAddressObserver(IPAddressObserver* observer); |
156 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); | 193 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); |
157 static void AddDNSObserver(DNSObserver* observer); | 194 static void AddDNSObserver(DNSObserver* observer); |
195 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); | |
158 | 196 |
159 // Unregisters |observer| from receiving notifications. This must be called | 197 // Unregisters |observer| from receiving notifications. This must be called |
160 // on the same thread on which AddObserver() was called. Like AddObserver(), | 198 // on the same thread on which AddObserver() was called. Like AddObserver(), |
161 // this is safe to call if Create() has not been called (as long as it doesn't | 199 // this is safe to call if Create() has not been called (as long as it doesn't |
162 // race the Create() call on another thread), in which case it will simply do | 200 // race the Create() call on another thread), in which case it will simply do |
163 // nothing. Technically, it's also safe to call after the notifier object has | 201 // nothing. Technically, it's also safe to call after the notifier object has |
164 // been destroyed, if the call doesn't race the notifier's destruction, but | 202 // been destroyed, if the call doesn't race the notifier's destruction, but |
165 // there's no reason to use the API in this risky way, so don't do it. | 203 // there's no reason to use the API in this risky way, so don't do it. |
166 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 204 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
167 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); | 205 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); |
168 static void RemoveDNSObserver(DNSObserver* observer); | 206 static void RemoveDNSObserver(DNSObserver* observer); |
207 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); | |
169 | 208 |
170 // Allow unit tests to trigger notifications. | 209 // Allow unit tests to trigger notifications. |
171 static void NotifyObserversOfIPAddressChangeForTests() { | 210 static void NotifyObserversOfIPAddressChangeForTests() { |
172 NotifyObserversOfIPAddressChange(); | 211 NotifyObserversOfIPAddressChange(); |
173 } | 212 } |
174 | 213 |
175 // Return a string equivalent to |type|. | 214 // Return a string equivalent to |type|. |
176 static const char* ConnectionTypeToString(ConnectionType type); | 215 static const char* ConnectionTypeToString(ConnectionType type); |
177 | 216 |
178 // Let the NetworkChangeNotifier know we received some data. | 217 // Let the NetworkChangeNotifier know we received some data. |
179 // This is used strictly for producing histogram data about the accuracy of | 218 // This is used strictly for producing histogram data about the accuracy of |
180 // the NetworkChangenotifier's online detection. | 219 // the NetworkChangenotifier's online detection. |
181 static void NotifyDataReceived(const GURL& source); | 220 static void NotifyDataReceived(const GURL& source); |
182 | 221 |
183 // Register the Observer callbacks for producing histogram data. This | 222 // Register the Observer callbacks for producing histogram data. This |
184 // should be called from the network thread to avoid race conditions. | 223 // should be called from the network thread to avoid race conditions. |
185 static void InitHistogramWatcher(); | 224 static void InitHistogramWatcher(); |
186 | 225 |
187 protected: | 226 protected: |
188 NetworkChangeNotifier(); | 227 // Delay arguments control how many milliseconds to delay notifications |
228 // so as to combine duplicates. They should be tuned for the particular | |
229 // child class implementation. See NetworkChangeCalculator() for more | |
230 // details. | |
231 NetworkChangeNotifier(uint32 ip_address_offline_delay_ms, | |
232 uint32 ip_address_online_delay_ms, | |
233 uint32 connection_type_offline_delay_ms, | |
234 uint32 connection_type_online_delay_ms); | |
szym
2012/11/25 07:37:37
They should be TimeDelta.
I suggest using four in
| |
189 | 235 |
190 #if defined(OS_LINUX) | 236 #if defined(OS_LINUX) |
191 // Returns the AddressTrackerLinux if present. | 237 // Returns the AddressTrackerLinux if present. |
192 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 | 238 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 |
193 virtual const internal::AddressTrackerLinux* | 239 virtual const internal::AddressTrackerLinux* |
194 GetAddressTrackerInternal() const; | 240 GetAddressTrackerInternal() const; |
195 #endif | 241 #endif |
196 | 242 |
197 // Broadcasts a notification to all registered observers. Note that this | 243 // Broadcasts a notification to all registered observers. Note that this |
198 // happens asynchronously, even for observers on the current thread, even in | 244 // happens asynchronously, even for observers on the current thread, even in |
199 // tests. | 245 // tests. |
200 static void NotifyObserversOfIPAddressChange(); | 246 static void NotifyObserversOfIPAddressChange(); |
201 static void NotifyObserversOfConnectionTypeChange(); | 247 static void NotifyObserversOfConnectionTypeChange(); |
202 static void NotifyObserversOfDNSChange(); | 248 static void NotifyObserversOfDNSChange(); |
249 static void NotifyObserversOfNetworkChange(ConnectionType type); | |
203 | 250 |
204 // Stores |config| in NetworkState and notifies observers. | 251 // Stores |config| in NetworkState and notifies observers. |
205 static void SetDnsConfig(const DnsConfig& config); | 252 static void SetDnsConfig(const DnsConfig& config); |
206 | 253 |
207 private: | 254 private: |
208 friend class HostResolverImplDnsTest; | 255 friend class HostResolverImplDnsTest; |
209 friend class NetworkChangeNotifierAndroidTest; | 256 friend class NetworkChangeNotifierAndroidTest; |
210 friend class NetworkChangeNotifierLinuxTest; | 257 friend class NetworkChangeNotifierLinuxTest; |
211 friend class NetworkChangeNotifierWinTest; | 258 friend class NetworkChangeNotifierWinTest; |
212 | 259 |
213 class NetworkState; | 260 class NetworkState; |
261 class NetworkChangeCalculator; | |
214 | 262 |
215 // Allows a second NetworkChangeNotifier to be created for unit testing, so | 263 // Allows a second NetworkChangeNotifier to be created for unit testing, so |
216 // the test suite can create a MockNetworkChangeNotifier, but platform | 264 // the test suite can create a MockNetworkChangeNotifier, but platform |
217 // specific NetworkChangeNotifiers can also be created for testing. To use, | 265 // specific NetworkChangeNotifiers can also be created for testing. To use, |
218 // create an DisableForTest object, and then create the new | 266 // create an DisableForTest object, and then create the new |
219 // NetworkChangeNotifier object. The NetworkChangeNotifier must be | 267 // NetworkChangeNotifier object. The NetworkChangeNotifier must be |
220 // destroyed before the DisableForTest object, as its destruction will restore | 268 // destroyed before the DisableForTest object, as its destruction will restore |
221 // the original NetworkChangeNotifier. | 269 // the original NetworkChangeNotifier. |
222 class NET_EXPORT_PRIVATE DisableForTest { | 270 class NET_EXPORT_PRIVATE DisableForTest { |
223 public: | 271 public: |
224 DisableForTest(); | 272 DisableForTest(); |
225 ~DisableForTest(); | 273 ~DisableForTest(); |
226 | 274 |
227 private: | 275 private: |
228 // The original NetworkChangeNotifier to be restored on destruction. | 276 // The original NetworkChangeNotifier to be restored on destruction. |
229 NetworkChangeNotifier* network_change_notifier_; | 277 NetworkChangeNotifier* network_change_notifier_; |
230 }; | 278 }; |
231 | 279 |
232 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 280 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
233 ip_address_observer_list_; | 281 ip_address_observer_list_; |
234 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > | 282 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > |
235 connection_type_observer_list_; | 283 connection_type_observer_list_; |
236 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 284 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
237 resolver_state_observer_list_; | 285 resolver_state_observer_list_; |
286 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> > | |
287 network_change_observer_list_; | |
238 | 288 |
239 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. | 289 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. |
240 scoped_ptr<NetworkState> network_state_; | 290 scoped_ptr<NetworkState> network_state_; |
241 | 291 |
242 // A little-piggy-back observer that simply logs UMA histogram data. | 292 // A little-piggy-back observer that simply logs UMA histogram data. |
243 scoped_ptr<HistogramWatcher> histogram_watcher_; | 293 scoped_ptr<HistogramWatcher> histogram_watcher_; |
244 | 294 |
295 // Computes NetworkChange signal from IPAddress and ConnectionType signals. | |
296 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; | |
297 | |
245 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 298 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
246 }; | 299 }; |
247 | 300 |
248 } // namespace net | 301 } // namespace net |
249 | 302 |
250 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 303 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |