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 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ 7 8 #include <windows.h> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/threading/non_thread_safe.h" 15 #include "base/timer/timer.h" 16 #include "base/win/object_watcher.h" 17 #include "net/base/net_export.h" 18 #include "net/base/network_change_notifier.h" 19 20 namespace net { 21 22 // NetworkChangeNotifierWin inherits from NonThreadSafe, as all its internal 23 // notification code must be called on the thread it is created and destroyed 24 // on. All the NetworkChangeNotifier methods it implements are threadsafe. 25 class NET_EXPORT_PRIVATE NetworkChangeNotifierWin 26 : public NetworkChangeNotifier, 27 public base::win::ObjectWatcher::Delegate, 28 NON_EXPORTED_BASE(public base::NonThreadSafe) { 29 public: 30 NetworkChangeNotifierWin(); 31 32 // 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 34 // thread |this| was created on. This cannot be called in the constructor, as 35 // WatchForAddressChangeInternal is mocked out in unit tests. 36 // TODO(mmenke): Consider making this function a part of the 37 // NetworkChangeNotifier interface, so other subclasses can be 38 // unit tested in similar fashion, as needed. 39 void WatchForAddressChange(); 40 41 protected: 42 virtual ~NetworkChangeNotifierWin(); 43 44 // For unit tests only. is_watching()45 bool is_watching() { return is_watching_; } set_is_watching(bool is_watching)46 void set_is_watching(bool is_watching) { is_watching_ = is_watching; } sequential_failures()47 int sequential_failures() { return sequential_failures_; } 48 49 private: 50 class DnsConfigServiceThread; 51 friend class NetworkChangeNotifierWinTest; 52 53 // NetworkChangeNotifier methods: 54 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; 55 56 // ObjectWatcher::Delegate methods: 57 // Must only be called on the thread |this| was created on. 58 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; 59 60 // Does the actual work to determine the current connection type. 61 // It is not thread safe, see crbug.com/324913. 62 virtual ConnectionType RecomputeCurrentConnectionType() const; 63 64 void SetCurrentConnectionType(ConnectionType connection_type); 65 66 // Notifies IP address change observers of a change immediately, and notifies 67 // network state change observers on a delay. Must only be called on the 68 // thread |this| was created on. 69 void NotifyObservers(); 70 71 // Forwards connection type notifications to parent class. 72 void NotifyParentOfConnectionTypeChange(); 73 74 // Tries to start listening for a single subsequent address change. Returns 75 // false on failure. The caller is responsible for updating |is_watching_|. 76 // Virtual for unit tests. Must only be called on the thread |this| was 77 // created on. 78 virtual bool WatchForAddressChangeInternal(); 79 80 static NetworkChangeCalculatorParams NetworkChangeCalculatorParamsWin(); 81 82 // All member variables may only be accessed on the thread |this| was created 83 // on. 84 85 // False when not currently watching for network change events. This only 86 // happens on initialization and when WatchForAddressChangeInternal fails and 87 // there is a pending task to try again. Needed for safe cleanup. 88 bool is_watching_; 89 90 base::win::ObjectWatcher addr_watcher_; 91 OVERLAPPED addr_overlapped_; 92 93 base::OneShotTimer<NetworkChangeNotifierWin> timer_; 94 95 // Number of times WatchForAddressChange has failed in a row. 96 int sequential_failures_; 97 98 // Thread on which we can run DnsConfigService. 99 scoped_ptr<DnsConfigServiceThread> dns_config_service_thread_; 100 101 mutable base::Lock last_computed_connection_type_lock_; 102 ConnectionType last_computed_connection_type_; 103 104 // Result of IsOffline() when NotifyObserversOfConnectionTypeChange() 105 // was last called. 106 bool last_announced_offline_; 107 // Number of times polled to check if still offline. 108 int offline_polls_; 109 110 // Used for calling WatchForAddressChange again on failure. 111 base::WeakPtrFactory<NetworkChangeNotifierWin> weak_factory_; 112 113 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); 114 }; 115 116 } // namespace net 117 118 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ 119