diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-22 02:22:10 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-22 02:22:10 +0000 |
commit | 1992715a45a68c5bb09a7deda02ddf66821c3351 (patch) | |
tree | 1ecda08adac8a8f8274a03a662deec350fb799bd /net | |
parent | bd443a5415adab148b10416c6a4f8050fbe54271 (diff) | |
download | chromium_src-1992715a45a68c5bb09a7deda02ddf66821c3351.zip chromium_src-1992715a45a68c5bb09a7deda02ddf66821c3351.tar.gz chromium_src-1992715a45a68c5bb09a7deda02ddf66821c3351.tar.bz2 |
Implement NetworkChangeNotifierWin.
Uses the NotifyAddrChange() API to detect ip address changes.
BUG=http://crbug.com/26156
Review URL: http://codereview.chromium.org/551077
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36837 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/network_change_notifier_win.cc | 60 | ||||
-rw-r--r-- | net/base/network_change_notifier_win.h | 11 | ||||
-rwxr-xr-x | net/net.gyp | 5 |
3 files changed, 73 insertions, 3 deletions
diff --git a/net/base/network_change_notifier_win.cc b/net/base/network_change_notifier_win.cc index 711bd66..e52bd46 100644 --- a/net/base/network_change_notifier_win.cc +++ b/net/base/network_change_notifier_win.cc @@ -3,10 +3,68 @@ // found in the LICENSE file. #include "net/base/network_change_notifier_win.h" +#include <iphlpapi.h> +#include <windows.h> +#include <winsock2.h> +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "base/object_watcher.h" namespace net { -NetworkChangeNotifierWin::NetworkChangeNotifierWin() {} +class NetworkChangeNotifierWin::Impl + : public base::ObjectWatcher::Delegate { + public: + explicit Impl(NetworkChangeNotifierWin* notifier); + virtual ~Impl(); + + void WatchForAddressChange(); + + // ObjectWatcher::Delegate methods: + + virtual void OnObjectSignaled(HANDLE object); + + private: + NetworkChangeNotifierWin* const notifier_; + base::ObjectWatcher addr_watcher_; + OVERLAPPED addr_overlapped_; + + DISALLOW_COPY_AND_ASSIGN(Impl); +}; + +NetworkChangeNotifierWin::Impl::Impl(NetworkChangeNotifierWin* notifier) + : notifier_(notifier) { + memset(&addr_overlapped_, 0, sizeof(addr_overlapped_)); + addr_overlapped_.hEvent = WSACreateEvent(); +} + +NetworkChangeNotifierWin::Impl::~Impl() { + CancelIPChangeNotify(&addr_overlapped_); + addr_watcher_.StopWatching(); + WSACloseEvent(addr_overlapped_.hEvent); + memset(&addr_overlapped_, 0, sizeof(addr_overlapped_)); +} + +void NetworkChangeNotifierWin::Impl::WatchForAddressChange() { + HANDLE handle = NULL; + DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); + CHECK(ret == ERROR_IO_PENDING); + addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); +} + +void NetworkChangeNotifierWin::Impl::OnObjectSignaled(HANDLE object) { + notifier_->OnIPAddressChanged(); + + // Start watching for further address changes. + WatchForAddressChange(); +} + +NetworkChangeNotifierWin::NetworkChangeNotifierWin() + : impl_(new Impl(ALLOW_THIS_IN_INITIALIZER_LIST(this))) { + impl_->WatchForAddressChange(); +} + NetworkChangeNotifierWin::~NetworkChangeNotifierWin() {} } // namespace net diff --git a/net/base/network_change_notifier_win.h b/net/base/network_change_notifier_win.h index b208ad7..b11ec0d 100644 --- a/net/base/network_change_notifier_win.h +++ b/net/base/network_change_notifier_win.h @@ -6,6 +6,7 @@ #define NET_BASE_NETWORK_CHANGE_NOTIFIER_WIN_H_ #include "base/basictypes.h" +#include "base/object_watcher.h" #include "net/base/network_change_notifier_helper.h" namespace net { @@ -14,6 +15,11 @@ class NetworkChangeNotifierWin : public NetworkChangeNotifier { public: NetworkChangeNotifierWin(); + // Called by NetworkChangeNotifierWin::Impl. + void OnIPAddressChanged() { helper_.OnIPAddressChanged(); } + + // NetworkChangeNotifier methods: + virtual void AddObserver(Observer* observer) { helper_.AddObserver(observer); } @@ -23,11 +29,12 @@ class NetworkChangeNotifierWin : public NetworkChangeNotifier { } private: - virtual ~NetworkChangeNotifierWin(); + class Impl; - void OnIPAddressChanged() { helper_.OnIPAddressChanged(); } + virtual ~NetworkChangeNotifierWin(); internal::NetworkChangeNotifierHelper helper_; + scoped_ptr<Impl> impl_; DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierWin); }; diff --git a/net/net.gyp b/net/net.gyp index f16ebab..9a8824b 100755 --- a/net/net.gyp +++ b/net/net.gyp @@ -524,6 +524,11 @@ 'dependencies': [ 'tld_cleanup', ], + 'link_settings': { + 'libraries': [ + '-lIphlpapi.lib', + ], + }, }, { # else: OS != "win" 'sources!': [ |