OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // the unreliability of this signal. | 43 // the unreliability of this signal. |
44 virtual void OnOnlineStateChanged(bool online) = 0; | 44 virtual void OnOnlineStateChanged(bool online) = 0; |
45 | 45 |
46 protected: | 46 protected: |
47 OnlineStateObserver() {} | 47 OnlineStateObserver() {} |
48 | 48 |
49 private: | 49 private: |
50 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); | 50 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); |
51 }; | 51 }; |
52 | 52 |
| 53 class NET_EXPORT DNSObserver { |
| 54 public: |
| 55 virtual ~DNSObserver() {} |
| 56 |
| 57 // Will be called when the DNS resolver of the system may have changed. |
| 58 // This is only used on Linux currently and watches /etc/resolv.conf |
| 59 // and /etc/hosts |
| 60 virtual void OnDNSChanged() = 0; |
| 61 |
| 62 protected: |
| 63 DNSObserver() {} |
| 64 |
| 65 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
| 67 }; |
| 68 |
53 virtual ~NetworkChangeNotifier(); | 69 virtual ~NetworkChangeNotifier(); |
54 | 70 |
55 // See the description of NetworkChangeNotifier::IsOffline(). | 71 // See the description of NetworkChangeNotifier::IsOffline(). |
56 // Implementations must be thread-safe. Implementations must also be | 72 // Implementations must be thread-safe. Implementations must also be |
57 // cheap as this could be called (repeatedly) from the IO thread. | 73 // cheap as this could be called (repeatedly) from the IO thread. |
58 virtual bool IsCurrentlyOffline() const = 0; | 74 virtual bool IsCurrentlyOffline() const = 0; |
59 | 75 |
60 // Replaces the default class factory instance of NetworkChangeNotifier class. | 76 // Replaces the default class factory instance of NetworkChangeNotifier class. |
61 // The method will take over the ownership of |factory| object. | 77 // The method will take over the ownership of |factory| object. |
62 static void SetFactory(NetworkChangeNotifierFactory* factory); | 78 static void SetFactory(NetworkChangeNotifierFactory* factory); |
(...skipping 19 matching lines...) Expand all Loading... |
82 // events, it merely rebroadcasts notifications when requested. | 98 // events, it merely rebroadcasts notifications when requested. |
83 static NetworkChangeNotifier* CreateMock(); | 99 static NetworkChangeNotifier* CreateMock(); |
84 | 100 |
85 // Registers |observer| to receive notifications of network changes. The | 101 // Registers |observer| to receive notifications of network changes. The |
86 // thread on which this is called is the thread on which |observer| will be | 102 // thread on which this is called is the thread on which |observer| will be |
87 // called back with notifications. This is safe to call if Create() has not | 103 // called back with notifications. This is safe to call if Create() has not |
88 // been called (as long as it doesn't race the Create() call on another | 104 // been called (as long as it doesn't race the Create() call on another |
89 // thread), in which case it will simply do nothing. | 105 // thread), in which case it will simply do nothing. |
90 static void AddIPAddressObserver(IPAddressObserver* observer); | 106 static void AddIPAddressObserver(IPAddressObserver* observer); |
91 static void AddOnlineStateObserver(OnlineStateObserver* observer); | 107 static void AddOnlineStateObserver(OnlineStateObserver* observer); |
| 108 static void AddDNSObserver(DNSObserver* observer); |
92 | 109 |
93 // Unregisters |observer| from receiving notifications. This must be called | 110 // Unregisters |observer| from receiving notifications. This must be called |
94 // on the same thread on which AddObserver() was called. Like AddObserver(), | 111 // on the same thread on which AddObserver() was called. Like AddObserver(), |
95 // this is safe to call if Create() has not been called (as long as it doesn't | 112 // this is safe to call if Create() has not been called (as long as it doesn't |
96 // race the Create() call on another thread), in which case it will simply do | 113 // race the Create() call on another thread), in which case it will simply do |
97 // nothing. Technically, it's also safe to call after the notifier object has | 114 // nothing. Technically, it's also safe to call after the notifier object has |
98 // been destroyed, if the call doesn't race the notifier's destruction, but | 115 // been destroyed, if the call doesn't race the notifier's destruction, but |
99 // there's no reason to use the API in this risky way, so don't do it. | 116 // there's no reason to use the API in this risky way, so don't do it. |
100 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 117 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
101 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); | 118 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); |
| 119 static void RemoveDNSObserver(DNSObserver* observer); |
102 | 120 |
103 // Allow unit tests to trigger notifications. | 121 // Allow unit tests to trigger notifications. |
104 static void NotifyObserversOfIPAddressChangeForTests() { | 122 static void NotifyObserversOfIPAddressChangeForTests() { |
105 NotifyObserversOfIPAddressChange(); | 123 NotifyObserversOfIPAddressChange(); |
106 } | 124 } |
107 | 125 |
108 protected: | 126 protected: |
109 NetworkChangeNotifier(); | 127 NetworkChangeNotifier(); |
110 | 128 |
111 // Broadcasts a notification to all registered observers. Note that this | 129 // Broadcasts a notification to all registered observers. Note that this |
112 // happens asynchronously, even for observers on the current thread, even in | 130 // happens asynchronously, even for observers on the current thread, even in |
113 // tests. | 131 // tests. |
114 static void NotifyObserversOfIPAddressChange(); | 132 static void NotifyObserversOfIPAddressChange(); |
115 static void NotifyObserversOfOnlineStateChange(); | 133 static void NotifyObserversOfOnlineStateChange(); |
| 134 static void NotifyObserversOfDNSChange(); |
116 | 135 |
117 private: | 136 private: |
118 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 137 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
119 ip_address_observer_list_; | 138 ip_address_observer_list_; |
120 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > | 139 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > |
121 online_state_observer_list_; | 140 online_state_observer_list_; |
| 141 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
| 142 resolver_state_observer_list_; |
122 | 143 |
123 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 144 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
124 }; | 145 }; |
125 | 146 |
126 } // namespace net | 147 } // namespace net |
127 | 148 |
128 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 149 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |