OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "net/base/network_change_notifier_win.h" | 5 #include "net/base/network_change_notifier_win.h" |
6 | 6 |
7 #include <iphlpapi.h> | 7 #include <iphlpapi.h> |
8 #include <windows.h> | |
9 #include <winsock2.h> | 8 #include <winsock2.h> |
10 | 9 |
11 #include "base/basictypes.h" | 10 #pragma comment(lib, "iphlpapi.lib") |
12 #include "base/compiler_specific.h" | |
13 #include "base/logging.h" | |
14 #include "base/object_watcher.h" | |
15 | 11 |
16 namespace net { | 12 namespace net { |
17 | 13 |
18 class NetworkChangeNotifierWin::Impl | 14 NetworkChangeNotifierWin::NetworkChangeNotifierWin() { |
19 : public base::ObjectWatcher::Delegate { | 15 memset(&addr_overlapped_, 0, sizeof addr_overlapped_); |
20 public: | |
21 explicit Impl(NetworkChangeNotifierWin* notifier); | |
22 virtual ~Impl(); | |
23 | |
24 void WatchForAddressChange(); | |
25 | |
26 // ObjectWatcher::Delegate methods: | |
27 | |
28 virtual void OnObjectSignaled(HANDLE object); | |
29 | |
30 private: | |
31 NetworkChangeNotifierWin* const notifier_; | |
32 base::ObjectWatcher addr_watcher_; | |
33 OVERLAPPED addr_overlapped_; | |
34 | |
35 DISALLOW_COPY_AND_ASSIGN(Impl); | |
36 }; | |
37 | |
38 NetworkChangeNotifierWin::Impl::Impl(NetworkChangeNotifierWin* notifier) | |
39 : notifier_(notifier) { | |
40 memset(&addr_overlapped_, 0, sizeof(addr_overlapped_)); | |
41 addr_overlapped_.hEvent = WSACreateEvent(); | 16 addr_overlapped_.hEvent = WSACreateEvent(); |
42 } | 17 } |
43 | 18 |
44 NetworkChangeNotifierWin::Impl::~Impl() { | 19 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { |
45 CancelIPChangeNotify(&addr_overlapped_); | 20 CancelIPChangeNotify(&addr_overlapped_); |
46 addr_watcher_.StopWatching(); | 21 addr_watcher_.StopWatching(); |
47 WSACloseEvent(addr_overlapped_.hEvent); | 22 WSACloseEvent(addr_overlapped_.hEvent); |
48 memset(&addr_overlapped_, 0, sizeof(addr_overlapped_)); | |
49 } | 23 } |
50 | 24 |
51 void NetworkChangeNotifierWin::Impl::WatchForAddressChange() { | 25 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) { |
| 26 NotifyObserversOfIPAddressChange(); |
| 27 |
| 28 // Start watching for the next address change. |
| 29 WatchForAddressChange(); |
| 30 } |
| 31 |
| 32 void NetworkChangeNotifierWin::WatchForAddressChange() { |
52 HANDLE handle = NULL; | 33 HANDLE handle = NULL; |
53 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); | 34 DWORD ret = NotifyAddrChange(&handle, &addr_overlapped_); |
54 CHECK(ret == ERROR_IO_PENDING); | 35 CHECK(ret == ERROR_IO_PENDING); |
55 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); | 36 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); |
56 } | 37 } |
57 | 38 |
58 void NetworkChangeNotifierWin::Impl::OnObjectSignaled(HANDLE object) { | |
59 notifier_->OnIPAddressChanged(); | |
60 | |
61 // Start watching for further address changes. | |
62 WatchForAddressChange(); | |
63 } | |
64 | |
65 NetworkChangeNotifierWin::NetworkChangeNotifierWin() | |
66 : impl_(new Impl(ALLOW_THIS_IN_INITIALIZER_LIST(this))) { | |
67 impl_->WatchForAddressChange(); | |
68 } | |
69 void NetworkChangeNotifierWin::OnIPAddressChanged() { | |
70 DCHECK(CalledOnValidThread()); | |
71 FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged()); | |
72 } | |
73 | |
74 void NetworkChangeNotifierWin::AddObserver(Observer* observer) { | |
75 DCHECK(CalledOnValidThread()); | |
76 observers_.AddObserver(observer); | |
77 } | |
78 | |
79 void NetworkChangeNotifierWin::RemoveObserver(Observer* observer) { | |
80 DCHECK(CalledOnValidThread()); | |
81 observers_.RemoveObserver(observer); | |
82 } | |
83 | |
84 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { | |
85 DCHECK(CalledOnValidThread()); | |
86 } | |
87 | |
88 } // namespace net | 39 } // namespace net |
OLD | NEW |