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