Chromium Code Reviews| Index: chrome/browser/metrics/metrics_network_observer.cc |
| diff --git a/chrome/browser/metrics/metrics_network_observer.cc b/chrome/browser/metrics/metrics_network_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9e8ef75eca9ceee08d36e98307cb9823b29bb170 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/metrics_network_observer.cc |
| @@ -0,0 +1,68 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/metrics/metrics_network_observer.h" |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| + |
| +void ReadWifiPhyMode(net::WifiPhyMode* phy_mode) { |
| + *phy_mode = net::GetWifiPhyMode(); |
| +} |
| + |
| +} // namespace |
| + |
| +MetricsNetworkObserver::MetricsNetworkObserver() |
| + : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| + connection_type_is_ambiguous_(false), |
| + wifi_phy_mode_is_ambiguous_(false) { |
| + net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| + Reset(); |
| +} |
| + |
| +MetricsNetworkObserver::~MetricsNetworkObserver() { |
| + net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| +} |
| + |
| +void MetricsNetworkObserver::Reset() { |
| + connection_type_is_ambiguous_ = false; |
| + connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); |
| + wifi_phy_mode_is_ambiguous_ = false; |
| + wifi_phy_mode_ = net::WIFI_PHY_MODE_UNKNOWN; |
| + ProbeWifiPhyMode(); |
|
Ilya Sherman
2013/02/13 01:54:33
Should you invalidate existing WeakPtrs as part of
szym
2013/02/13 05:09:34
On second thought, I realized there's no need to P
|
| +} |
| + |
| +void MetricsNetworkObserver::OnConnectionTypeChanged( |
| + net::NetworkChangeNotifier::ConnectionType type) { |
| + if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
| + return; |
| + if (type != connection_type_ && |
| + connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| + connection_type_is_ambiguous_ = true; |
| + } |
| + connection_type_ = type; |
| + |
| + ProbeWifiPhyMode(); |
| +} |
| + |
| +void MetricsNetworkObserver::ProbeWifiPhyMode() { |
| + weak_ptr_factory_.InvalidateWeakPtrs(); |
|
Ilya Sherman
2013/02/13 01:54:33
Are you sure you want to invalidate any existing W
szym
2013/02/13 05:09:34
I don't think it matters in practice, since GetWif
|
| + net::WifiPhyMode* phy_mode = new net::WifiPhyMode(); |
| + content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| + FROM_HERE, |
| + base::Bind(&ReadWifiPhyMode, base::Unretained(phy_mode)), |
| + base::Bind(&MetricsNetworkObserver::OnWifiPhyModeResult, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Owned(phy_mode))); |
| +} |
| + |
| +void MetricsNetworkObserver::OnWifiPhyModeResult(net::WifiPhyMode* mode) { |
| + if (wifi_phy_mode_ != net::WIFI_PHY_MODE_UNKNOWN && *mode != wifi_phy_mode_) |
| + wifi_phy_mode_is_ambiguous_ = true; |
| + wifi_phy_mode_ = *mode; |
| +} |
| + |