Chromium Code Reviews| 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.h" | 5 #include "net/base/network_change_notifier.h" |
| 6 #include "net/base/network_change_notifier_factory.h" | 6 #include "net/base/network_change_notifier_factory.h" |
| 7 #include "base/metrics/histogram.h" | |
| 7 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "googleurl/src/gurl.h" | |
| 10 #include "net/base/net_util.h" | |
| 8 #if defined(OS_WIN) | 11 #if defined(OS_WIN) |
| 9 #include "net/base/network_change_notifier_win.h" | 12 #include "net/base/network_change_notifier_win.h" |
| 10 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) | 13 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 11 #include "net/base/network_change_notifier_linux.h" | 14 #include "net/base/network_change_notifier_linux.h" |
| 12 #elif defined(OS_MACOSX) | 15 #elif defined(OS_MACOSX) |
| 13 #include "net/base/network_change_notifier_mac.h" | 16 #include "net/base/network_change_notifier_mac.h" |
| 14 #endif | 17 #endif |
| 15 | 18 |
| 16 namespace net { | 19 namespace net { |
| 17 | 20 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 28 | 31 |
| 29 class MockNetworkChangeNotifier : public NetworkChangeNotifier { | 32 class MockNetworkChangeNotifier : public NetworkChangeNotifier { |
| 30 public: | 33 public: |
| 31 virtual ConnectionType GetCurrentConnectionType() const { | 34 virtual ConnectionType GetCurrentConnectionType() const { |
| 32 return CONNECTION_UNKNOWN; | 35 return CONNECTION_UNKNOWN; |
| 33 } | 36 } |
| 34 }; | 37 }; |
| 35 | 38 |
| 36 } // namespace | 39 } // namespace |
| 37 | 40 |
| 41 // The main observer class that records UMAs for network events. | |
| 42 class HistogramWatcher | |
| 43 : public NetworkChangeNotifier::ConnectionTypeObserver, | |
| 44 public NetworkChangeNotifier::IPAddressObserver, | |
| 45 public NetworkChangeNotifier::DNSObserver { | |
| 46 public: | |
| 47 HistogramWatcher() | |
| 48 : last_ip_address_change_(base::TimeTicks::Now()), | |
| 49 last_connection_change_(base::TimeTicks::Now()), | |
| 50 last_dns_change_(base::TimeTicks::Now()), | |
| 51 last_connection_type_(NetworkChangeNotifier::CONNECTION_UNKNOWN), | |
| 52 offline_packets_received_(0) {} | |
| 53 | |
| 54 // Registers our three Observer implementations. This is called from the | |
| 55 // network thread so that our Observer implementations are also called | |
| 56 // from the network thread. This avoids multi-threaded race conditions | |
| 57 // because the only other interface, |NotifyDataReceived| is also | |
| 58 // only called from the network thread. | |
| 59 void InitHistogramWatcher(); | |
|
szym
2012/08/08 20:41:17
You could use base::NonThreadSafe to enforce the t
pauljensen
2012/08/13 15:42:04
As we discussed this would add so much complexity
| |
| 60 | |
| 61 virtual ~HistogramWatcher() {} | |
| 62 | |
| 63 // NetworkChangeNotifier::IPAddressObserver implementation. | |
| 64 virtual void OnIPAddressChanged() OVERRIDE { | |
| 65 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.IPAddressChange", | |
| 66 SinceLast(&last_ip_address_change_)); | |
|
szym
2012/08/08 20:41:17
I'm a bit uneasy about using a function call with
pauljensen
2012/08/13 15:42:04
I don't like it either. I wish the histograms wer
| |
| 67 } | |
| 68 | |
| 69 // NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 70 virtual void OnConnectionTypeChanged( | |
| 71 NetworkChangeNotifier::ConnectionType type) OVERRIDE { | |
| 72 if (type != NetworkChangeNotifier::CONNECTION_NONE) { | |
| 73 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OnlineChange", | |
| 74 SinceLast(&last_connection_change_)); | |
| 75 | |
| 76 if (offline_packets_received_) { | |
| 77 if ((last_connection_change_ - last_offline_packet_received_) < | |
| 78 base::TimeDelta::FromSeconds(5)) { | |
| 79 // we can compare this sum with the sum of NCN.OfflineDataRecv | |
|
szym
2012/08/08 20:41:17
nit: Format like a sentence, but maybe just remove
pauljensen
2012/08/13 15:42:04
Done.
| |
| 80 UMA_HISTOGRAM_COUNTS_10000( | |
| 81 "NCN.OfflineDataRecvAny5sBeforeOnline", | |
| 82 offline_packets_received_); | |
| 83 } | |
| 84 | |
| 85 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineDataRecvUntilOnline", | |
| 86 last_connection_change_ - | |
| 87 last_offline_packet_received_); | |
| 88 } | |
| 89 } else { | |
| 90 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineChange", | |
| 91 SinceLast(&last_connection_change_)); | |
| 92 } | |
| 93 | |
| 94 offline_packets_received_ = 0; | |
| 95 last_connection_type_ = type; | |
| 96 } | |
| 97 | |
| 98 // NetworkChangeNotifier::DNSObserver implementation. | |
| 99 virtual void OnDNSChanged(unsigned detail) OVERRIDE { | |
| 100 if (detail == NetworkChangeNotifier::CHANGE_DNS_SETTINGS) { | |
| 101 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.DNSConfigChange", | |
| 102 SinceLast(&last_dns_change_)); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // Record histogram data whenever we receive a packet but think we're | |
| 107 // offline. Should only be called from the network thread. | |
| 108 void NotifyDataReceived(const GURL& source) { | |
| 109 if (last_connection_type_ != NetworkChangeNotifier::CONNECTION_NONE || | |
| 110 IsLocalhost(source.host()) || | |
| 111 !(source.SchemeIs("http") || source.SchemeIs("https"))) | |
|
szym
2012/08/08 20:41:17
nit: This might need braces.
pauljensen
2012/08/13 15:42:04
Done.
| |
| 112 return; | |
| 113 base::TimeTicks current_time = base::TimeTicks::Now(); | |
| 114 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineDataRecv", | |
| 115 current_time - last_connection_change_); | |
| 116 offline_packets_received_++; | |
| 117 last_offline_packet_received_ = current_time; | |
| 118 } | |
| 119 | |
| 120 private: | |
| 121 base::TimeTicks last_ip_address_change_; | |
| 122 base::TimeTicks last_connection_change_; | |
| 123 base::TimeTicks last_dns_change_; | |
| 124 base::TimeTicks last_offline_packet_received_; | |
| 125 NetworkChangeNotifier::ConnectionType last_connection_type_; | |
| 126 int32 offline_packets_received_; | |
| 127 | |
| 128 static base::TimeDelta SinceLast(base::TimeTicks *last_time); | |
|
szym
2012/08/08 20:41:17
Method declarations should precede field declarati
pauljensen
2012/08/13 15:42:04
Done.
| |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(HistogramWatcher); | |
| 131 }; | |
| 132 | |
| 133 base::TimeDelta HistogramWatcher::SinceLast(base::TimeTicks *last_time) { | |
|
szym
2012/08/08 20:41:17
Why is this implemented here rather than inline as
pauljensen
2012/08/13 15:42:04
Done.
| |
| 134 base::TimeTicks current_time = base::TimeTicks::Now(); | |
| 135 base::TimeDelta delta = current_time - *last_time; | |
| 136 *last_time = current_time; | |
| 137 return delta; | |
| 138 } | |
| 139 | |
| 140 void HistogramWatcher::InitHistogramWatcher() { | |
|
szym
2012/08/08 20:41:17
ditto: why implemented out-of-line?
pauljensen
2012/08/13 15:42:04
Done.
| |
| 141 NetworkChangeNotifier::AddConnectionTypeObserver(this); | |
| 142 NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 143 NetworkChangeNotifier::AddDNSObserver(this); | |
| 144 } | |
| 145 | |
| 38 NetworkChangeNotifier::~NetworkChangeNotifier() { | 146 NetworkChangeNotifier::~NetworkChangeNotifier() { |
| 39 DCHECK_EQ(this, g_network_change_notifier); | 147 DCHECK_EQ(this, g_network_change_notifier); |
| 40 g_network_change_notifier = NULL; | 148 g_network_change_notifier = NULL; |
| 41 } | 149 } |
| 42 | 150 |
| 43 // static | 151 // static |
| 44 void NetworkChangeNotifier::SetFactory( | 152 void NetworkChangeNotifier::SetFactory( |
| 45 NetworkChangeNotifierFactory* factory) { | 153 NetworkChangeNotifierFactory* factory) { |
| 46 CHECK(!g_network_change_notifier_factory); | 154 CHECK(!g_network_change_notifier_factory); |
| 47 g_network_change_notifier_factory = factory; | 155 g_network_change_notifier_factory = factory; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 77 } | 185 } |
| 78 | 186 |
| 79 // static | 187 // static |
| 80 NetworkChangeNotifier::ConnectionType | 188 NetworkChangeNotifier::ConnectionType |
| 81 NetworkChangeNotifier::GetConnectionType() { | 189 NetworkChangeNotifier::GetConnectionType() { |
| 82 return g_network_change_notifier ? | 190 return g_network_change_notifier ? |
| 83 g_network_change_notifier->GetCurrentConnectionType() : | 191 g_network_change_notifier->GetCurrentConnectionType() : |
| 84 CONNECTION_UNKNOWN; | 192 CONNECTION_UNKNOWN; |
| 85 } | 193 } |
| 86 | 194 |
| 195 // static | |
| 196 void NetworkChangeNotifier::NotifyDataReceived(const GURL& source) { | |
| 197 if (!g_network_change_notifier) | |
| 198 return; | |
| 199 g_network_change_notifier->histogram_watcher_->NotifyDataReceived(source); | |
| 200 } | |
| 201 | |
| 202 // static | |
| 203 void NetworkChangeNotifier::InitHistogramWatcher() { | |
| 204 if (!g_network_change_notifier) | |
| 205 return; | |
| 206 g_network_change_notifier->histogram_watcher_->InitHistogramWatcher(); | |
| 207 } | |
| 208 | |
| 87 #if defined(OS_LINUX) | 209 #if defined(OS_LINUX) |
| 88 // static | 210 // static |
| 89 const internal::AddressTrackerLinux* | 211 const internal::AddressTrackerLinux* |
| 90 NetworkChangeNotifier::GetAddressTracker() { | 212 NetworkChangeNotifier::GetAddressTracker() { |
| 91 return g_network_change_notifier ? | 213 return g_network_change_notifier ? |
| 92 g_network_change_notifier->GetAddressTrackerInternal() : NULL; | 214 g_network_change_notifier->GetAddressTrackerInternal() : NULL; |
| 93 } | 215 } |
| 94 #endif | 216 #endif |
| 95 | 217 |
| 96 // static | 218 // static |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)), | 277 ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)), |
| 156 connection_type_observer_list_( | 278 connection_type_observer_list_( |
| 157 new ObserverListThreadSafe<ConnectionTypeObserver>( | 279 new ObserverListThreadSafe<ConnectionTypeObserver>( |
| 158 ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)), | 280 ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)), |
| 159 resolver_state_observer_list_( | 281 resolver_state_observer_list_( |
| 160 new ObserverListThreadSafe<DNSObserver>( | 282 new ObserverListThreadSafe<DNSObserver>( |
| 161 ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)), | 283 ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)), |
| 162 watching_dns_(false) { | 284 watching_dns_(false) { |
| 163 DCHECK(!g_network_change_notifier); | 285 DCHECK(!g_network_change_notifier); |
| 164 g_network_change_notifier = this; | 286 g_network_change_notifier = this; |
| 287 histogram_watcher_.reset(new HistogramWatcher()); | |
|
szym
2012/08/08 20:41:17
nit: Put this in the initializer list.
pauljensen
2012/08/13 15:42:04
It must appear after |g_network_change_notifier =
| |
| 165 } | 288 } |
| 166 | 289 |
| 167 #if defined(OS_LINUX) | 290 #if defined(OS_LINUX) |
| 168 const internal::AddressTrackerLinux* | 291 const internal::AddressTrackerLinux* |
| 169 NetworkChangeNotifier::GetAddressTrackerInternal() const { | 292 NetworkChangeNotifier::GetAddressTrackerInternal() const { |
| 170 return NULL; | 293 return NULL; |
| 171 } | 294 } |
| 172 #endif | 295 #endif |
| 173 | 296 |
| 174 // static | 297 // static |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 DCHECK(g_network_change_notifier); | 336 DCHECK(g_network_change_notifier); |
| 214 g_network_change_notifier = NULL; | 337 g_network_change_notifier = NULL; |
| 215 } | 338 } |
| 216 | 339 |
| 217 NetworkChangeNotifier::DisableForTest::~DisableForTest() { | 340 NetworkChangeNotifier::DisableForTest::~DisableForTest() { |
| 218 DCHECK(!g_network_change_notifier); | 341 DCHECK(!g_network_change_notifier); |
| 219 g_network_change_notifier = network_change_notifier_; | 342 g_network_change_notifier = network_change_notifier_; |
| 220 } | 343 } |
| 221 | 344 |
| 222 } // namespace net | 345 } // namespace net |
| OLD | NEW |