| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <winsock2.h> | 8 #include <winsock2.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "net/base/winsock_init.h" | 15 #include "net/base/winsock_init.h" |
| 16 #include "net/dns/dns_config_watcher.h" | 16 #include "net/dns/dns_config_service.h" |
| 17 | 17 |
| 18 #pragma comment(lib, "iphlpapi.lib") | 18 #pragma comment(lib, "iphlpapi.lib") |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Time between NotifyAddrChange retries, on failure. | 24 // Time between NotifyAddrChange retries, on failure. |
| 25 const int kWatchForAddressChangeRetryIntervalMs = 500; | 25 const int kWatchForAddressChangeRetryIntervalMs = 500; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 // Thread on which we can run DnsConfigWatcher, which requires AssertIOAllowed | 29 // Thread on which we can run DnsConfigService, which requires AssertIOAllowed |
| 30 // to open registry keys and to handle FilePathWatcher updates. | 30 // to open registry keys and to handle FilePathWatcher updates. |
| 31 class NetworkChangeNotifierWin::DnsWatcherThread : public base::Thread { | 31 class NetworkChangeNotifierWin::DnsConfigServiceThread : public base::Thread { |
| 32 public: | 32 public: |
| 33 DnsWatcherThread() : base::Thread("DnsWatcher") {} | 33 DnsConfigServiceThread() : base::Thread("DnsConfigService") {} |
| 34 | 34 |
| 35 virtual ~DnsWatcherThread() { | 35 virtual ~DnsConfigServiceThread() { |
| 36 Stop(); | 36 Stop(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 virtual void Init() OVERRIDE { | 39 virtual void Init() OVERRIDE { |
| 40 watcher_.Init(); | 40 service_ = DnsConfigService::CreateSystemService(); |
| 41 service_->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig)); |
| 41 } | 42 } |
| 42 | 43 |
| 43 virtual void CleanUp() OVERRIDE { | 44 virtual void CleanUp() OVERRIDE { |
| 44 watcher_.CleanUp(); | 45 service_.reset(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 internal::DnsConfigWatcher watcher_; | 49 scoped_ptr<DnsConfigService> service_; |
| 49 | 50 |
| 50 DISALLOW_COPY_AND_ASSIGN(DnsWatcherThread); | 51 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 NetworkChangeNotifierWin::NetworkChangeNotifierWin() | 54 NetworkChangeNotifierWin::NetworkChangeNotifierWin() |
| 54 : is_watching_(false), | 55 : is_watching_(false), |
| 55 sequential_failures_(0), | 56 sequential_failures_(0), |
| 56 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 57 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 57 dns_watcher_thread_(new DnsWatcherThread()) { | 58 dns_config_service_thread_(new DnsConfigServiceThread()) { |
| 58 memset(&addr_overlapped_, 0, sizeof addr_overlapped_); | 59 memset(&addr_overlapped_, 0, sizeof addr_overlapped_); |
| 59 addr_overlapped_.hEvent = WSACreateEvent(); | 60 addr_overlapped_.hEvent = WSACreateEvent(); |
| 60 dns_watcher_thread_->StartWithOptions( | 61 dns_config_service_thread_->StartWithOptions( |
| 61 base::Thread::Options(MessageLoop::TYPE_IO, 0)); | 62 base::Thread::Options(MessageLoop::TYPE_IO, 0)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { | 65 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { |
| 65 if (is_watching_) { | 66 if (is_watching_) { |
| 66 CancelIPChangeNotify(&addr_overlapped_); | 67 CancelIPChangeNotify(&addr_overlapped_); |
| 67 addr_watcher_.StopWatching(); | 68 addr_watcher_.StopWatching(); |
| 68 } | 69 } |
| 69 WSACloseEvent(addr_overlapped_.hEvent); | 70 WSACloseEvent(addr_overlapped_.hEvent); |
| 70 } | 71 } |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 268 |
| 268 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); | 269 addr_watcher_.StartWatching(addr_overlapped_.hEvent, this); |
| 269 return true; | 270 return true; |
| 270 } | 271 } |
| 271 | 272 |
| 272 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { | 273 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { |
| 273 NotifyObserversOfConnectionTypeChange(); | 274 NotifyObserversOfConnectionTypeChange(); |
| 274 } | 275 } |
| 275 | 276 |
| 276 } // namespace net | 277 } // namespace net |
| OLD | NEW |