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() { |
| 60 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 61 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 62 NetworkChangeNotifier::AddDNSObserver(this); |
| 63 } |
| 64 |
| 65 virtual ~HistogramWatcher() {} |
| 66 |
| 67 // NetworkChangeNotifier::IPAddressObserver implementation. |
| 68 virtual void OnIPAddressChanged() OVERRIDE { |
| 69 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.IPAddressChange", |
| 70 SinceLast(&last_ip_address_change_)); |
| 71 } |
| 72 |
| 73 // NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 74 virtual void OnConnectionTypeChanged( |
| 75 NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 76 if (type != NetworkChangeNotifier::CONNECTION_NONE) { |
| 77 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OnlineChange", |
| 78 SinceLast(&last_connection_change_)); |
| 79 |
| 80 if (offline_packets_received_) { |
| 81 if ((last_connection_change_ - last_offline_packet_received_) < |
| 82 base::TimeDelta::FromSeconds(5)) { |
| 83 // We can compare this sum with the sum of NCN.OfflineDataRecv. |
| 84 UMA_HISTOGRAM_COUNTS_10000( |
| 85 "NCN.OfflineDataRecvAny5sBeforeOnline", |
| 86 offline_packets_received_); |
| 87 } |
| 88 |
| 89 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineDataRecvUntilOnline", |
| 90 last_connection_change_ - |
| 91 last_offline_packet_received_); |
| 92 } |
| 93 } else { |
| 94 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineChange", |
| 95 SinceLast(&last_connection_change_)); |
| 96 } |
| 97 |
| 98 offline_packets_received_ = 0; |
| 99 last_connection_type_ = type; |
| 100 } |
| 101 |
| 102 // NetworkChangeNotifier::DNSObserver implementation. |
| 103 virtual void OnDNSChanged(unsigned detail) OVERRIDE { |
| 104 if (detail == NetworkChangeNotifier::CHANGE_DNS_SETTINGS) { |
| 105 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.DNSConfigChange", |
| 106 SinceLast(&last_dns_change_)); |
| 107 } |
| 108 } |
| 109 |
| 110 // Record histogram data whenever we receive a packet but think we're |
| 111 // offline. Should only be called from the network thread. |
| 112 void NotifyDataReceived(const GURL& source) { |
| 113 if (last_connection_type_ != NetworkChangeNotifier::CONNECTION_NONE || |
| 114 IsLocalhost(source.host()) || |
| 115 !(source.SchemeIs("http") || source.SchemeIs("https"))) { |
| 116 return; |
| 117 } |
| 118 |
| 119 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 120 UMA_HISTOGRAM_MEDIUM_TIMES("NCN.OfflineDataRecv", |
| 121 current_time - last_connection_change_); |
| 122 offline_packets_received_++; |
| 123 last_offline_packet_received_ = current_time; |
| 124 } |
| 125 |
| 126 private: |
| 127 static base::TimeDelta SinceLast(base::TimeTicks *last_time) { |
| 128 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 129 base::TimeDelta delta = current_time - *last_time; |
| 130 *last_time = current_time; |
| 131 return delta; |
| 132 } |
| 133 |
| 134 base::TimeTicks last_ip_address_change_; |
| 135 base::TimeTicks last_connection_change_; |
| 136 base::TimeTicks last_dns_change_; |
| 137 base::TimeTicks last_offline_packet_received_; |
| 138 NetworkChangeNotifier::ConnectionType last_connection_type_; |
| 139 int32 offline_packets_received_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(HistogramWatcher); |
| 142 }; |
| 143 |
38 NetworkChangeNotifier::~NetworkChangeNotifier() { | 144 NetworkChangeNotifier::~NetworkChangeNotifier() { |
39 DCHECK_EQ(this, g_network_change_notifier); | 145 DCHECK_EQ(this, g_network_change_notifier); |
40 g_network_change_notifier = NULL; | 146 g_network_change_notifier = NULL; |
41 } | 147 } |
42 | 148 |
43 // static | 149 // static |
44 void NetworkChangeNotifier::SetFactory( | 150 void NetworkChangeNotifier::SetFactory( |
45 NetworkChangeNotifierFactory* factory) { | 151 NetworkChangeNotifierFactory* factory) { |
46 CHECK(!g_network_change_notifier_factory); | 152 CHECK(!g_network_change_notifier_factory); |
47 g_network_change_notifier_factory = factory; | 153 g_network_change_notifier_factory = factory; |
(...skipping 29 matching lines...) Expand all Loading... |
77 } | 183 } |
78 | 184 |
79 // static | 185 // static |
80 NetworkChangeNotifier::ConnectionType | 186 NetworkChangeNotifier::ConnectionType |
81 NetworkChangeNotifier::GetConnectionType() { | 187 NetworkChangeNotifier::GetConnectionType() { |
82 return g_network_change_notifier ? | 188 return g_network_change_notifier ? |
83 g_network_change_notifier->GetCurrentConnectionType() : | 189 g_network_change_notifier->GetCurrentConnectionType() : |
84 CONNECTION_UNKNOWN; | 190 CONNECTION_UNKNOWN; |
85 } | 191 } |
86 | 192 |
| 193 // static |
| 194 void NetworkChangeNotifier::NotifyDataReceived(const GURL& source) { |
| 195 if (!g_network_change_notifier) |
| 196 return; |
| 197 g_network_change_notifier->histogram_watcher_->NotifyDataReceived(source); |
| 198 } |
| 199 |
| 200 // static |
| 201 void NetworkChangeNotifier::InitHistogramWatcher() { |
| 202 if (!g_network_change_notifier) |
| 203 return; |
| 204 g_network_change_notifier->histogram_watcher_->InitHistogramWatcher(); |
| 205 } |
| 206 |
87 #if defined(OS_LINUX) | 207 #if defined(OS_LINUX) |
88 // static | 208 // static |
89 const internal::AddressTrackerLinux* | 209 const internal::AddressTrackerLinux* |
90 NetworkChangeNotifier::GetAddressTracker() { | 210 NetworkChangeNotifier::GetAddressTracker() { |
91 return g_network_change_notifier ? | 211 return g_network_change_notifier ? |
92 g_network_change_notifier->GetAddressTrackerInternal() : NULL; | 212 g_network_change_notifier->GetAddressTrackerInternal() : NULL; |
93 } | 213 } |
94 #endif | 214 #endif |
95 | 215 |
96 // static | 216 // static |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)), | 275 ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)), |
156 connection_type_observer_list_( | 276 connection_type_observer_list_( |
157 new ObserverListThreadSafe<ConnectionTypeObserver>( | 277 new ObserverListThreadSafe<ConnectionTypeObserver>( |
158 ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)), | 278 ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)), |
159 resolver_state_observer_list_( | 279 resolver_state_observer_list_( |
160 new ObserverListThreadSafe<DNSObserver>( | 280 new ObserverListThreadSafe<DNSObserver>( |
161 ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)), | 281 ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)), |
162 watching_dns_(false) { | 282 watching_dns_(false) { |
163 DCHECK(!g_network_change_notifier); | 283 DCHECK(!g_network_change_notifier); |
164 g_network_change_notifier = this; | 284 g_network_change_notifier = this; |
| 285 histogram_watcher_.reset(new HistogramWatcher()); |
165 } | 286 } |
166 | 287 |
167 #if defined(OS_LINUX) | 288 #if defined(OS_LINUX) |
168 const internal::AddressTrackerLinux* | 289 const internal::AddressTrackerLinux* |
169 NetworkChangeNotifier::GetAddressTrackerInternal() const { | 290 NetworkChangeNotifier::GetAddressTrackerInternal() const { |
170 return NULL; | 291 return NULL; |
171 } | 292 } |
172 #endif | 293 #endif |
173 | 294 |
174 // static | 295 // static |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 DCHECK(g_network_change_notifier); | 334 DCHECK(g_network_change_notifier); |
214 g_network_change_notifier = NULL; | 335 g_network_change_notifier = NULL; |
215 } | 336 } |
216 | 337 |
217 NetworkChangeNotifier::DisableForTest::~DisableForTest() { | 338 NetworkChangeNotifier::DisableForTest::~DisableForTest() { |
218 DCHECK(!g_network_change_notifier); | 339 DCHECK(!g_network_change_notifier); |
219 g_network_change_notifier = network_change_notifier_; | 340 g_network_change_notifier = network_change_notifier_; |
220 } | 341 } |
221 | 342 |
222 } // namespace net | 343 } // namespace net |
OLD | NEW |