OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 // NetworkChangeNotifier monitors the system for network changes, and notifies | 14 // NetworkChangeNotifier monitors the system for network changes, and notifies |
15 // registered observers of those events. Observers may register on any thread, | 15 // registered observers of those events. Observers may register on any thread, |
16 // and will be called back on the thread from which they registered. | 16 // and will be called back on the thread from which they registered. |
17 class NetworkChangeNotifier { | 17 class NetworkChangeNotifier { |
18 public: | 18 public: |
19 class Observer { | 19 class IPAddressObserver { |
20 public: | 20 public: |
21 virtual ~Observer() {} | 21 virtual ~IPAddressObserver() {} |
22 | 22 |
23 // Will be called when the IP address of the primary interface changes. | 23 // Will be called when the IP address of the primary interface changes. |
24 // This includes when the primary interface itself changes. | 24 // This includes when the primary interface itself changes. |
25 virtual void OnIPAddressChanged() = 0; | 25 virtual void OnIPAddressChanged() = 0; |
26 | 26 |
27 protected: | 27 protected: |
28 Observer() {} | 28 IPAddressObserver() {} |
29 | 29 |
30 private: | 30 private: |
31 DISALLOW_COPY_AND_ASSIGN(Observer); | 31 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); |
| 32 }; |
| 33 |
| 34 class OnlineStateObserver { |
| 35 public: |
| 36 virtual ~OnlineStateObserver() {} |
| 37 |
| 38 // Will be called when the online state of the system may have changed. |
| 39 // See NetworkChangeNotifier::IsOffline() for important caveats about |
| 40 // the unreliability of this signal. |
| 41 virtual void OnOnlineStateChanged(bool online) = 0; |
| 42 |
| 43 protected: |
| 44 OnlineStateObserver() {} |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); |
32 }; | 48 }; |
33 | 49 |
34 virtual ~NetworkChangeNotifier(); | 50 virtual ~NetworkChangeNotifier(); |
35 | 51 |
36 // See the description of NetworkChangeNotifier::IsOffline(). | 52 // See the description of NetworkChangeNotifier::IsOffline(). |
37 // Implementations must be thread-safe. Implementations must also be | 53 // Implementations must be thread-safe. Implementations must also be |
38 // cheap as this could be called (repeatedly) from the IO thread. | 54 // cheap as this could be called (repeatedly) from the IO thread. |
39 virtual bool IsCurrentlyOffline() const = 0; | 55 virtual bool IsCurrentlyOffline() const = 0; |
40 | 56 |
41 // Creates the process-wide, platform-specific NetworkChangeNotifier. The | 57 // Creates the process-wide, platform-specific NetworkChangeNotifier. The |
(...skipping 15 matching lines...) Expand all Loading... |
57 | 73 |
58 // Like Create(), but for use in tests. The mock object doesn't monitor any | 74 // Like Create(), but for use in tests. The mock object doesn't monitor any |
59 // events, it merely rebroadcasts notifications when requested. | 75 // events, it merely rebroadcasts notifications when requested. |
60 static NetworkChangeNotifier* CreateMock(); | 76 static NetworkChangeNotifier* CreateMock(); |
61 | 77 |
62 // Registers |observer| to receive notifications of network changes. The | 78 // Registers |observer| to receive notifications of network changes. The |
63 // thread on which this is called is the thread on which |observer| will be | 79 // thread on which this is called is the thread on which |observer| will be |
64 // called back with notifications. This is safe to call if Create() has not | 80 // called back with notifications. This is safe to call if Create() has not |
65 // been called (as long as it doesn't race the Create() call on another | 81 // been called (as long as it doesn't race the Create() call on another |
66 // thread), in which case it will simply do nothing. | 82 // thread), in which case it will simply do nothing. |
67 static void AddObserver(Observer* observer); | 83 static void AddIPAddressObserver(IPAddressObserver* observer); |
| 84 static void AddOnlineStateObserver(OnlineStateObserver* observer); |
68 | 85 |
69 // Unregisters |observer| from receiving notifications. This must be called | 86 // Unregisters |observer| from receiving notifications. This must be called |
70 // on the same thread on which AddObserver() was called. Like AddObserver(), | 87 // on the same thread on which AddObserver() was called. Like AddObserver(), |
71 // this is safe to call if Create() has not been called (as long as it doesn't | 88 // this is safe to call if Create() has not been called (as long as it doesn't |
72 // race the Create() call on another thread), in which case it will simply do | 89 // race the Create() call on another thread), in which case it will simply do |
73 // nothing. Technically, it's also safe to call after the notifier object has | 90 // nothing. Technically, it's also safe to call after the notifier object has |
74 // been destroyed, if the call doesn't race the notifier's destruction, but | 91 // been destroyed, if the call doesn't race the notifier's destruction, but |
75 // there's no reason to use the API in this risky way, so don't do it. | 92 // there's no reason to use the API in this risky way, so don't do it. |
76 static void RemoveObserver(Observer* observer); | 93 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
| 94 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); |
77 | 95 |
78 #ifdef UNIT_TEST | 96 #ifdef UNIT_TEST |
79 // Allow unit tests to trigger notifications. | 97 // Allow unit tests to trigger notifications. |
80 static void NotifyObserversOfIPAddressChangeForTests() { | 98 static void NotifyObserversOfIPAddressChangeForTests() { |
81 NotifyObserversOfIPAddressChange(); | 99 NotifyObserversOfIPAddressChange(); |
82 } | 100 } |
83 #endif | 101 #endif |
84 | 102 |
85 protected: | 103 protected: |
86 NetworkChangeNotifier(); | 104 NetworkChangeNotifier(); |
87 | 105 |
88 // Broadcasts a notification to all registered observers. Note that this | 106 // Broadcasts a notification to all registered observers. Note that this |
89 // happens asynchronously, even for observers on the current thread, even in | 107 // happens asynchronously, even for observers on the current thread, even in |
90 // tests. | 108 // tests. |
91 static void NotifyObserversOfIPAddressChange(); | 109 static void NotifyObserversOfIPAddressChange(); |
| 110 void NotifyObserversOfOnlineStateChange(); |
92 | 111 |
93 private: | 112 private: |
94 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 113 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
| 114 ip_address_observer_list_; |
| 115 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > |
| 116 online_state_observer_list_; |
95 | 117 |
96 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 118 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
97 }; | 119 }; |
98 | 120 |
99 } // namespace net | 121 } // namespace net |
100 | 122 |
101 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 123 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |