Chromium Code Reviews| Index: net/base/network_change_notifier_win.cc |
| diff --git a/net/base/network_change_notifier_win.cc b/net/base/network_change_notifier_win.cc |
| index 9b2b35ec76bfbc08a1ac6008dbf9f85b80a2618b..89d2d857d4db3b630453119ae62487bc7c71ebec 100644 |
| --- a/net/base/network_change_notifier_win.cc |
| +++ b/net/base/network_change_notifier_win.cc |
| @@ -14,6 +14,7 @@ |
| #include "base/message_loop/message_loop.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "base/single_thread_task_runner.h" |
| +#include "base/task_runner_util.h" |
| #include "base/threading/thread.h" |
| #include "base/threading/thread_task_runner_handle.h" |
| #include "base/time/time.h" |
| @@ -59,6 +60,8 @@ NetworkChangeNotifierWin::NetworkChangeNotifierWin() |
| last_computed_connection_type_(RecomputeCurrentConnectionType()), |
| last_announced_offline_(last_computed_connection_type_ == |
| CONNECTION_NONE), |
| + calculate_connection_type_(base::BindRepeating( |
| + &NetworkChangeNotifierWin::RecomputeCurrentConnectionType)), |
| weak_factory_(this) { |
| memset(&addr_overlapped_, 0, sizeof addr_overlapped_); |
| addr_overlapped_.hEvent = WSACreateEvent(); |
| @@ -86,6 +89,12 @@ NetworkChangeNotifierWin::NetworkChangeCalculatorParamsWin() { |
| return params; |
| } |
| +NetworkChangeNotifier::ConnectionType |
| +NetworkChangeNotifierWin::GetCurrentConnectionType() const { |
| + base::AutoLock auto_lock(last_computed_connection_type_lock_); |
| + return last_computed_connection_type_; |
| +} |
|
pauljensen
2017/05/23 14:55:51
can we move this back down?
jkarlin
2017/05/23 15:11:06
Done.
|
| + |
| // This implementation does not return the actual connection type but merely |
| // determines if the user is "online" (in which case it returns |
| // CONNECTION_UNKNOWN) or "offline" (and then it returns CONNECTION_NONE). |
| @@ -134,10 +143,9 @@ NetworkChangeNotifierWin::NetworkChangeCalculatorParamsWin() { |
| // experiments I ran... However none of them correctly returned "offline" when |
| // executing 'ipconfig /release'. |
| // |
| +// static |
| NetworkChangeNotifier::ConnectionType |
| -NetworkChangeNotifierWin::RecomputeCurrentConnectionType() const { |
| - DCHECK(CalledOnValidThread()); |
| - |
| +NetworkChangeNotifierWin::RecomputeCurrentConnectionType() { |
| EnsureWinsockInit(); |
| // The following code was adapted from: |
| @@ -201,14 +209,7 @@ NetworkChangeNotifierWin::RecomputeCurrentConnectionType() const { |
| << "WSALookupServiceEnd() failed with: " << result; |
| // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. |
| - return found_connection ? ConnectionTypeFromInterfaces() |
| - : NetworkChangeNotifier::CONNECTION_NONE; |
| -} |
| - |
| -NetworkChangeNotifier::ConnectionType |
| -NetworkChangeNotifierWin::GetCurrentConnectionType() const { |
| - base::AutoLock auto_lock(last_computed_connection_type_lock_); |
| - return last_computed_connection_type_; |
| + return found_connection ? ConnectionTypeFromInterfaces() : CONNECTION_NONE; |
| } |
| void NetworkChangeNotifierWin::SetCurrentConnectionType( |
| @@ -224,13 +225,17 @@ void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) { |
| // Start watching for the next address change. |
| WatchForAddressChange(); |
| + DCHECK(dns_task_runner_); |
| - NotifyObservers(); |
| + base::PostTaskAndReplyWithResult( |
| + dns_task_runner_.get(), FROM_HERE, calculate_connection_type_, |
| + base::Bind(&NetworkChangeNotifierWin::NotifyObservers, |
| + weak_factory_.GetWeakPtr())); |
| } |
| -void NetworkChangeNotifierWin::NotifyObservers() { |
| +void NetworkChangeNotifierWin::NotifyObservers(ConnectionType connection_type) { |
| DCHECK(CalledOnValidThread()); |
| - SetCurrentConnectionType(RecomputeCurrentConnectionType()); |
| + SetCurrentConnectionType(connection_type); |
| NotifyObserversOfIPAddressChange(); |
| // Calling GetConnectionType() at this very moment is likely to give |
| @@ -241,6 +246,8 @@ void NetworkChangeNotifierWin::NotifyObservers() { |
| // If after one second we determine we are still offline, we will |
| // delay again. |
| offline_polls_ = 0; |
| + // TODO(jkarlin): I'm not sure that calling with 'this' is safe. Can it be |
| + // deleted? |
| timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, |
| &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); |
| } |
| @@ -274,8 +281,12 @@ void NetworkChangeNotifierWin::WatchForAddressChange() { |
| // Treat the transition from NotifyAddrChange failing to succeeding as a |
| // network change event, since network changes were not being observed in |
| // that interval. |
| - if (sequential_failures_ > 0) |
| - NotifyObservers(); |
| + if (sequential_failures_ > 0) { |
| + base::PostTaskAndReplyWithResult( |
| + dns_task_runner_.get(), FROM_HERE, calculate_connection_type_, |
| + base::Bind(&NetworkChangeNotifierWin::NotifyObservers, |
| + weak_factory_.GetWeakPtr())); |
| + } |
| if (sequential_failures_ < 2000) { |
| UMA_HISTOGRAM_COUNTS_10000("Net.NotifyAddrChangeFailures", |
| @@ -292,6 +303,9 @@ bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() { |
| if (!dns_config_service_thread_->IsRunning()) { |
| dns_config_service_thread_->StartWithOptions( |
| base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| + if (!dns_task_runner_) |
| + dns_task_runner_ = |
| + dns_config_service_thread_->message_loop()->task_runner(); |
| } |
| ResetEventIfSignaled(addr_overlapped_.hEvent); |
| @@ -305,7 +319,16 @@ bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() { |
| } |
| void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { |
| - SetCurrentConnectionType(RecomputeCurrentConnectionType()); |
| + base::PostTaskAndReplyWithResult( |
| + dns_task_runner_.get(), FROM_HERE, calculate_connection_type_, |
| + base::Bind( |
| + &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChangeImpl, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChangeImpl( |
| + ConnectionType connection_type) { |
| + SetCurrentConnectionType(connection_type); |
| bool current_offline = IsOffline(); |
| offline_polls_++; |
| // If we continue to appear offline, delay sending out the notification in |
| @@ -323,10 +346,10 @@ void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { |
| NotifyObserversOfConnectionTypeChange(); |
| double max_bandwidth_mbps = 0.0; |
| - ConnectionType connection_type = CONNECTION_NONE; |
| + ConnectionType max_connection_type = CONNECTION_NONE; |
| GetCurrentMaxBandwidthAndConnectionType(&max_bandwidth_mbps, |
| - &connection_type); |
| - NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps, connection_type); |
| + &max_connection_type); |
| + NotifyObserversOfMaxBandwidthChange(max_bandwidth_mbps, max_connection_type); |
| } |
| } // namespace net |