| 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_WIN_H_ | 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
| 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "base/win/object_watcher.h" | 16 #include "base/win/object_watcher.h" |
| 17 #include "base/win/registry.h" |
| 18 #include "net/base/file_path_watcher_callback.h" |
| 17 #include "net/base/net_export.h" | 19 #include "net/base/net_export.h" |
| 18 #include "net/base/network_change_notifier.h" | 20 #include "net/base/network_change_notifier.h" |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 | 23 |
| 22 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal | 24 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal |
| 23 // notification code must be called on the thread it is created and destroyed | 25 // notification code must be called on the thread it is created and destroyed |
| 24 // on. All the NetworkChangeNotifier methods it implements are threadsafe. | 26 // on. All the NetworkChangeNotifier methods it implements are threadsafe. |
| 25 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin | 27 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin |
| 26 : public NetworkChangeNotifier, | 28 : public NetworkChangeNotifier, |
| 27 public base::win::ObjectWatcher::Delegate, | 29 public base::win::ObjectWatcher::Delegate, |
| 28 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 30 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 29 public: | 31 public: |
| 30 NetworkChangeNotifierWin(); | 32 NetworkChangeNotifierWin(); |
| 31 | 33 |
| 32 // Begins listening for a single subsequent address change. If it fails to | 34 // Begins listening for a single subsequent address change. If it fails to |
| 33 // start watching, it retries on a timer. Must be called only once, on the | 35 // start watching, it retries on a timer. Must be called only once, on the |
| 34 // thread |this| was created on. This cannot be called in the constructor, as | 36 // thread |this| was created on. This cannot be called in the constructor, as |
| 35 // WatchForAddressChangeInternal is mocked out in unit tests. | 37 // WatchForAddressChangeInternal is mocked out in unit tests. |
| 36 // TODO(mmenke): Consider making this function a part of the | 38 // TODO(mmenke): Consider making this function a part of the |
| 37 // NetworkChangeNotifier interface, so other subclasses can be | 39 // NetworkChangeNotifier interface, so other subclasses can be |
| 38 // unit tested in similar fashion, as needed. | 40 // unit tested in similar fashion, as needed. |
| 39 void WatchForAddressChange(); | 41 void WatchForAddressChange(); |
| 40 | 42 |
| 41 protected: | 43 protected: |
| 42 virtual ~NetworkChangeNotifierWin(); | 44 virtual ~NetworkChangeNotifierWin(); |
| 43 | 45 |
| 46 // Begins listening for DNS changes. |
| 47 void WatchForDNSChanges(); |
| 48 |
| 44 // For unit tests only. | 49 // For unit tests only. |
| 45 bool is_watching() { return is_watching_; } | 50 bool is_watching() { return is_watching_; } |
| 46 void set_is_watching(bool is_watching) { is_watching_ = is_watching; } | 51 void set_is_watching(bool is_watching) { is_watching_ = is_watching; } |
| 47 int sequential_failures() { return sequential_failures_; } | 52 int sequential_failures() { return sequential_failures_; } |
| 48 | 53 |
| 49 private: | 54 private: |
| 50 friend class NetworkChangeNotifierWinTest; | 55 friend class NetworkChangeNotifierWinTest; |
| 51 | 56 |
| 57 class RegistryWatcher : public base::win::ObjectWatcher::Delegate { |
| 58 public: |
| 59 explicit RegistryWatcher(NetworkChangeNotifierWin* notifier); |
| 60 ~RegistryWatcher(); |
| 61 |
| 62 bool Watch(const wchar_t* key); |
| 63 void Cancel(); |
| 64 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 65 private: |
| 66 NetworkChangeNotifierWin* notifier_; |
| 67 base::win::RegKey key_; |
| 68 base::win::ObjectWatcher watcher_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher); |
| 71 }; |
| 72 |
| 52 // NetworkChangeNotifier methods: | 73 // NetworkChangeNotifier methods: |
| 53 virtual bool IsCurrentlyOffline() const OVERRIDE; | 74 virtual bool IsCurrentlyOffline() const OVERRIDE; |
| 54 | 75 |
| 76 virtual bool IsCurrentlyWatchingDNS() const OVERRIDE; |
| 77 |
| 55 // ObjectWatcher::Delegate methods: | 78 // ObjectWatcher::Delegate methods: |
| 56 // Must only be called on the thread |this| was created on. | 79 // Must only be called on the thread |this| was created on. |
| 57 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | 80 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 58 | 81 |
| 59 // Notifies IP address change observers of a change immediately, and notifies | 82 // Notifies IP address change observers of a change immediately, and notifies |
| 60 // network state change observers on a delay. Must only be called on the | 83 // network state change observers on a delay. Must only be called on the |
| 61 // thread |this| was created on. | 84 // thread |this| was created on. |
| 62 void NotifyObservers(); | 85 void NotifyObservers(); |
| 63 | 86 |
| 64 // Forwards online state notifications to parent class. | 87 // Forwards online state notifications to parent class. |
| 65 void NotifyParentOfOnlineStateChange(); | 88 void NotifyParentOfOnlineStateChange(); |
| 66 | 89 |
| 67 // Tries to start listening for a single subsequent address change. Returns | 90 // Tries to start listening for a single subsequent address change. Returns |
| 68 // false on failure. The caller is responsible for updating |is_watching_|. | 91 // false on failure. The caller is responsible for updating |is_watching_|. |
| 69 // Virtual for unit tests. Must only be called on the thread |this| was | 92 // Virtual for unit tests. Must only be called on the thread |this| was |
| 70 // created on. | 93 // created on. |
| 71 virtual bool WatchForAddressChangeInternal(); | 94 virtual bool WatchForAddressChangeInternal(); |
| 72 | 95 |
| 96 // Called by RegistryWatcher. |watch_successful| is true iff the watch |
| 97 // continues successfully. |
| 98 void OnRegistryChanged(bool watch_successful); |
| 99 |
| 100 // Called by FilePathWatcherCallback. |
| 101 void OnDNSFileChanged(bool watch_successful); |
| 102 |
| 73 // All member variables may only be accessed on the thread |this| was created | 103 // All member variables may only be accessed on the thread |this| was created |
| 74 // on. | 104 // on. |
| 75 | 105 |
| 76 // False when not currently watching for network change events. This only | 106 // False when not currently watching for network change events. This only |
| 77 // happens on initialization and when WatchForAddressChangeInternal fails and | 107 // happens on initialization and when WatchForAddressChangeInternal fails and |
| 78 // there is a pending task to try again. Needed for safe cleanup. | 108 // there is a pending task to try again. Needed for safe cleanup. |
| 79 bool is_watching_; | 109 bool is_watching_; |
| 80 | 110 |
| 81 base::win::ObjectWatcher addr_watcher_; | 111 base::win::ObjectWatcher addr_watcher_; |
| 82 OVERLAPPED addr_overlapped_; | 112 OVERLAPPED addr_overlapped_; |
| 83 | 113 |
| 84 base::OneShotTimer<NetworkChangeNotifierWin> timer_; | 114 base::OneShotTimer<NetworkChangeNotifierWin> timer_; |
| 85 | 115 |
| 86 // Number of times WatchForAddressChange has failed in a row. | 116 // Number of times WatchForAddressChange has failed in a row. |
| 87 int sequential_failures_; | 117 int sequential_failures_; |
| 88 | 118 |
| 119 bool watching_dns_; |
| 120 // One watcher per key of interest. |
| 121 RegistryWatcher dns_policy_watcher_; |
| 122 RegistryWatcher dns_dnscache_watcher_; |
| 123 RegistryWatcher dns_tcpip_watcher_; |
| 124 |
| 125 FilePathWatcherCallback hosts_watcher_; |
| 126 |
| 89 // Used for calling WatchForAddressChange again on failure. | 127 // Used for calling WatchForAddressChange again on failure. |
| 90 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; | 128 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; |
| 91 | 129 |
| 92 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); | 130 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); |
| 93 }; | 131 }; |
| 94 | 132 |
| 95 } // namespace net | 133 } // namespace net |
| 96 | 134 |
| 97 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ | 135 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ |
| OLD | NEW |