| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 using net::HostPortPair; | 21 using net::HostPortPair; |
| 22 using net::ProxyServer; | 22 using net::ProxyServer; |
| 23 using net::ProxyService; | 23 using net::ProxyService; |
| 24 using net::NetworkChangeNotifier; | 24 using net::NetworkChangeNotifier; |
| 25 using net::URLRequest; | 25 using net::URLRequest; |
| 26 | 26 |
| 27 namespace data_reduction_proxy { | 27 namespace data_reduction_proxy { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 const int kMinFailedRequestsWhenUnavailable = 1; |
| 32 const int kMaxSuccessfulRequestsWhenUnavailable = 0; |
| 33 const int kMaxFailedRequestsBeforeReset = 3; |
| 34 |
| 31 // Records a net error code that resulted in bypassing the data reduction | 35 // Records a net error code that resulted in bypassing the data reduction |
| 32 // proxy (|is_primary| is true) or the data reduction proxy fallback. | 36 // proxy (|is_primary| is true) or the data reduction proxy fallback. |
| 33 void RecordDataReductionProxyBypassOnNetworkError( | 37 void RecordDataReductionProxyBypassOnNetworkError( |
| 34 bool is_primary, | 38 bool is_primary, |
| 35 const ProxyServer& proxy_server, | 39 const ProxyServer& proxy_server, |
| 36 int net_error) { | 40 int net_error) { |
| 37 if (is_primary) { | 41 if (is_primary) { |
| 38 UMA_HISTOGRAM_SPARSE_SLOWLY( | 42 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 39 "DataReductionProxy.BypassOnNetworkErrorPrimary", | 43 "DataReductionProxy.BypassOnNetworkErrorPrimary", |
| 40 std::abs(net_error)); | 44 std::abs(net_error)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 76 } |
| 73 } | 77 } |
| 74 | 78 |
| 75 DataReductionProxyUsageStats::DataReductionProxyUsageStats( | 79 DataReductionProxyUsageStats::DataReductionProxyUsageStats( |
| 76 DataReductionProxyParams* params, | 80 DataReductionProxyParams* params, |
| 77 MessageLoopProxy* ui_thread_proxy) | 81 MessageLoopProxy* ui_thread_proxy) |
| 78 : data_reduction_proxy_params_(params), | 82 : data_reduction_proxy_params_(params), |
| 79 last_bypass_type_(BYPASS_EVENT_TYPE_MAX), | 83 last_bypass_type_(BYPASS_EVENT_TYPE_MAX), |
| 80 triggering_request_(true), | 84 triggering_request_(true), |
| 81 ui_thread_proxy_(ui_thread_proxy), | 85 ui_thread_proxy_(ui_thread_proxy), |
| 82 eligible_num_requests_through_proxy_(0), | 86 successful_requests_through_proxy_count_(0), |
| 83 actual_num_requests_through_proxy_(0), | 87 proxy_net_errors_count_(0), |
| 84 unavailable_(false) { | 88 unavailable_(false) { |
| 89 DCHECK(params); |
| 90 |
| 85 NetworkChangeNotifier::AddNetworkChangeObserver(this); | 91 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 86 }; | 92 }; |
| 87 | 93 |
| 88 DataReductionProxyUsageStats::~DataReductionProxyUsageStats() { | 94 DataReductionProxyUsageStats::~DataReductionProxyUsageStats() { |
| 89 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 95 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 90 }; | 96 }; |
| 91 | 97 |
| 92 void DataReductionProxyUsageStats::OnUrlRequestCompleted( | 98 void DataReductionProxyUsageStats::OnUrlRequestCompleted( |
| 93 const net::URLRequest* request, bool started) { | 99 const net::URLRequest* request, bool started) { |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 95 | 101 |
| 96 if (request->status().status() == net::URLRequestStatus::SUCCESS) { | 102 if (request->status().status() == net::URLRequestStatus::SUCCESS && |
| 97 if (data_reduction_proxy_params_->IsDataReductionProxyEligible(request)) { | 103 data_reduction_proxy_params_->WasDataReductionProxyUsed(request, NULL)) { |
| 98 bool was_received_via_proxy = | 104 successful_requests_through_proxy_count_++; |
| 99 data_reduction_proxy_params_->WasDataReductionProxyUsed( | 105 NotifyUnavailabilityIfChanged(); |
| 100 request, NULL); | |
| 101 IncrementRequestCounts(was_received_via_proxy); | |
| 102 } | |
| 103 } | 106 } |
| 104 } | 107 } |
| 105 | 108 |
| 106 void DataReductionProxyUsageStats::OnNetworkChanged( | 109 void DataReductionProxyUsageStats::OnNetworkChanged( |
| 107 NetworkChangeNotifier::ConnectionType type) { | 110 NetworkChangeNotifier::ConnectionType type) { |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
| 109 ClearRequestCounts(); | 112 ClearRequestCounts(); |
| 110 } | 113 } |
| 111 | 114 |
| 112 void DataReductionProxyUsageStats::IncrementRequestCounts( | |
| 113 bool was_received_via_proxy) { | |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 115 if (was_received_via_proxy) { | |
| 116 actual_num_requests_through_proxy_++; | |
| 117 } | |
| 118 eligible_num_requests_through_proxy_++; | |
| 119 | |
| 120 // To account for the case when the proxy works for a little while and then | |
| 121 // gets blocked, we reset the counts occasionally. | |
| 122 if (eligible_num_requests_through_proxy_ > 50 | |
| 123 && actual_num_requests_through_proxy_ > 0) { | |
| 124 ClearRequestCounts(); | |
| 125 } else { | |
| 126 MaybeNotifyUnavailability(); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void DataReductionProxyUsageStats::ClearRequestCounts() { | 115 void DataReductionProxyUsageStats::ClearRequestCounts() { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 eligible_num_requests_through_proxy_ = 0; | 117 successful_requests_through_proxy_count_ = 0; |
| 133 actual_num_requests_through_proxy_ = 0; | 118 proxy_net_errors_count_ = 0; |
| 134 MaybeNotifyUnavailability(); | |
| 135 } | 119 } |
| 136 | 120 |
| 137 void DataReductionProxyUsageStats::MaybeNotifyUnavailability() { | 121 void DataReductionProxyUsageStats::NotifyUnavailabilityIfChanged() { |
| 138 bool prev_unavailable = unavailable_; | 122 bool prev_unavailable = unavailable_; |
| 139 unavailable_ = (eligible_num_requests_through_proxy_ > 0 && | 123 unavailable_ = |
| 140 actual_num_requests_through_proxy_ == 0); | 124 (proxy_net_errors_count_ >= kMinFailedRequestsWhenUnavailable && |
| 125 successful_requests_through_proxy_count_ <= |
| 126 kMaxSuccessfulRequestsWhenUnavailable); |
| 141 if (prev_unavailable != unavailable_) { | 127 if (prev_unavailable != unavailable_) { |
| 142 ui_thread_proxy_->PostTask(FROM_HERE, base::Bind( | 128 ui_thread_proxy_->PostTask(FROM_HERE, base::Bind( |
| 143 &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread, | 129 &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread, |
| 144 base::Unretained(this), | 130 base::Unretained(this), |
| 145 unavailable_)); | 131 unavailable_)); |
| 146 } | 132 } |
| 147 } | 133 } |
| 148 | 134 |
| 149 void DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread( | 135 void DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread( |
| 150 bool unavailable) { | 136 bool unavailable) { |
| 151 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); | 137 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); |
| 152 if (!unavailable_callback_.is_null()) | 138 if (!unavailable_callback_.is_null()) |
| 153 unavailable_callback_.Run(unavailable); | 139 unavailable_callback_.Run(unavailable); |
| 154 } | 140 } |
| 155 | 141 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 } | 219 } |
| 234 | 220 |
| 235 if (data_reduction_proxy_params_->AreDataReductionProxiesBypassed(request, | 221 if (data_reduction_proxy_params_->AreDataReductionProxiesBypassed(request, |
| 236 NULL)) { | 222 NULL)) { |
| 237 RecordBypassedBytes(last_bypass_type_, | 223 RecordBypassedBytes(last_bypass_type_, |
| 238 DataReductionProxyUsageStats::NETWORK_ERROR, | 224 DataReductionProxyUsageStats::NETWORK_ERROR, |
| 239 content_length); | 225 content_length); |
| 240 } | 226 } |
| 241 } | 227 } |
| 242 | 228 |
| 243 void DataReductionProxyUsageStats::RecordBypassEventHistograms( | 229 void DataReductionProxyUsageStats::OnProxyFallback( |
| 244 const net::ProxyServer& bypassed_proxy, | 230 const net::ProxyServer& bypassed_proxy, |
| 245 int net_error) const { | 231 int net_error) { |
| 246 DataReductionProxyTypeInfo data_reduction_proxy_info; | 232 DataReductionProxyTypeInfo data_reduction_proxy_info; |
| 247 if (bypassed_proxy.is_valid() && !bypassed_proxy.is_direct() && | 233 if (bypassed_proxy.is_valid() && !bypassed_proxy.is_direct() && |
| 248 data_reduction_proxy_params_->IsDataReductionProxy( | 234 data_reduction_proxy_params_->IsDataReductionProxy( |
| 249 bypassed_proxy.host_port_pair(), &data_reduction_proxy_info)) { | 235 bypassed_proxy.host_port_pair(), &data_reduction_proxy_info)) { |
| 250 if (data_reduction_proxy_info.is_ssl) | 236 if (data_reduction_proxy_info.is_ssl) |
| 251 return; | 237 return; |
| 238 |
| 239 proxy_net_errors_count_++; |
| 240 |
| 241 // To account for the case when the proxy is reachable for sometime, and |
| 242 // then gets blocked, we reset counts when number of errors exceed |
| 243 // the threshold. |
| 244 if (proxy_net_errors_count_ >= kMaxFailedRequestsBeforeReset && |
| 245 successful_requests_through_proxy_count_ > |
| 246 kMaxSuccessfulRequestsWhenUnavailable) { |
| 247 ClearRequestCounts(); |
| 248 } else { |
| 249 NotifyUnavailabilityIfChanged(); |
| 250 } |
| 251 |
| 252 if (!data_reduction_proxy_info.is_fallback) { | 252 if (!data_reduction_proxy_info.is_fallback) { |
| 253 RecordDataReductionProxyBypassInfo( | 253 RecordDataReductionProxyBypassInfo( |
| 254 true, false, bypassed_proxy, BYPASS_EVENT_TYPE_NETWORK_ERROR); | 254 true, false, bypassed_proxy, BYPASS_EVENT_TYPE_NETWORK_ERROR); |
| 255 RecordDataReductionProxyBypassOnNetworkError( | 255 RecordDataReductionProxyBypassOnNetworkError( |
| 256 true, bypassed_proxy, net_error); | 256 true, bypassed_proxy, net_error); |
| 257 } else { | 257 } else { |
| 258 RecordDataReductionProxyBypassInfo( | 258 RecordDataReductionProxyBypassInfo( |
| 259 false, false, bypassed_proxy, BYPASS_EVENT_TYPE_NETWORK_ERROR); | 259 false, false, bypassed_proxy, BYPASS_EVENT_TYPE_NETWORK_ERROR); |
| 260 RecordDataReductionProxyBypassOnNetworkError( | 260 RecordDataReductionProxyBypassOnNetworkError( |
| 261 false, bypassed_proxy, net_error); | 261 false, bypassed_proxy, net_error); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 default: | 374 default: |
| 375 break; | 375 break; |
| 376 } | 376 } |
| 377 break; | 377 break; |
| 378 } | 378 } |
| 379 } | 379 } |
| 380 | 380 |
| 381 } // namespace data_reduction_proxy | 381 } // namespace data_reduction_proxy |
| 382 | 382 |
| 383 | 383 |
| OLD | NEW |