1 // Copyright (c) 2009 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.
5 #include "net/base/network_change_notifier_win.h"
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/logging.h"
14 #include "base/object_watcher.h"
18 class NetworkChangeNotifierWin::Impl
19 : public base::ObjectWatcher::Delegate
{
21 explicit Impl(NetworkChangeNotifierWin
* notifier
);
24 void WatchForAddressChange();
26 // ObjectWatcher::Delegate methods:
28 virtual void OnObjectSignaled(HANDLE object
);
31 NetworkChangeNotifierWin
* const notifier_
;
32 base::ObjectWatcher addr_watcher_
;
33 OVERLAPPED addr_overlapped_
;
35 DISALLOW_COPY_AND_ASSIGN(Impl
);
38 NetworkChangeNotifierWin::Impl::Impl(NetworkChangeNotifierWin
* notifier
)
39 : notifier_(notifier
) {
40 memset(&addr_overlapped_
, 0, sizeof(addr_overlapped_
));
41 addr_overlapped_
.hEvent
= WSACreateEvent();
44 NetworkChangeNotifierWin::Impl::~Impl() {
45 CancelIPChangeNotify(&addr_overlapped_
);
46 addr_watcher_
.StopWatching();
47 WSACloseEvent(addr_overlapped_
.hEvent
);
48 memset(&addr_overlapped_
, 0, sizeof(addr_overlapped_
));
51 void NetworkChangeNotifierWin::Impl::WatchForAddressChange() {
53 DWORD ret
= NotifyAddrChange(&handle
, &addr_overlapped_
);
54 CHECK(ret
== ERROR_IO_PENDING
);
55 addr_watcher_
.StartWatching(addr_overlapped_
.hEvent
, this);
58 void NetworkChangeNotifierWin::Impl::OnObjectSignaled(HANDLE object
) {
59 notifier_
->OnIPAddressChanged();
61 // Start watching for further address changes.
62 WatchForAddressChange();
65 NetworkChangeNotifierWin::NetworkChangeNotifierWin()
66 : impl_(new Impl(ALLOW_THIS_IN_INITIALIZER_LIST(this))) {
67 impl_
->WatchForAddressChange();
70 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() {}