| 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 |
| 7 #include "base/callback.h" |
| 6 #include "net/proxy/proxy_retry_info.h" | 8 #include "net/proxy/proxy_retry_info.h" |
| 7 #include "net/proxy/proxy_server.h" | 9 #include "net/proxy/proxy_server.h" |
| 8 #include "net/proxy/proxy_service.h" | 10 #include "net/proxy/proxy_service.h" |
| 9 #include "net/url_request/url_request_context.h" | 11 #include "net/url_request/url_request_context.h" |
| 10 | 12 |
| 11 using base::MessageLoopProxy; | 13 using base::MessageLoopProxy; |
| 12 using net::HostPortPair; | 14 using net::HostPortPair; |
| 13 using net::ProxyServer; | 15 using net::ProxyServer; |
| 14 using net::NetworkChangeNotifier; | 16 using net::NetworkChangeNotifier; |
| 15 | 17 |
| 16 namespace data_reduction_proxy { | 18 namespace data_reduction_proxy { |
| 17 | 19 |
| 18 DataReductionProxyUsageStats::DataReductionProxyUsageStats( | 20 DataReductionProxyUsageStats::DataReductionProxyUsageStats( |
| 19 DataReductionProxyParams* params, | 21 DataReductionProxyParams* params, |
| 20 MessageLoopProxy* ui_thread_proxy, | 22 MessageLoopProxy* ui_thread_proxy, |
| 21 MessageLoopProxy* io_thread_proxy) | 23 MessageLoopProxy* io_thread_proxy) |
| 22 : data_reduction_proxy_params_(params), | 24 : data_reduction_proxy_params_(params), |
| 23 ui_thread_proxy_(ui_thread_proxy), | 25 ui_thread_proxy_(ui_thread_proxy), |
| 24 io_thread_proxy_(io_thread_proxy), | 26 io_thread_proxy_(io_thread_proxy), |
| 25 eligible_num_requests_through_proxy_(0), | 27 eligible_num_requests_through_proxy_(0), |
| 26 actual_num_requests_through_proxy_(0) { | 28 actual_num_requests_through_proxy_(0), |
| 29 unavailable_(false) { |
| 27 NetworkChangeNotifier::AddNetworkChangeObserver(this); | 30 NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 28 }; | 31 }; |
| 29 | 32 |
| 30 DataReductionProxyUsageStats::~DataReductionProxyUsageStats() { | 33 DataReductionProxyUsageStats::~DataReductionProxyUsageStats() { |
| 31 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 34 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 32 }; | 35 }; |
| 33 | 36 |
| 34 void DataReductionProxyUsageStats::OnUrlRequestCompleted( | 37 void DataReductionProxyUsageStats::OnUrlRequestCompleted( |
| 35 const net::URLRequest* request, bool started) { | 38 const net::URLRequest* request, bool started) { |
| 36 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); | 39 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); |
| 37 | 40 |
| 38 if (request->status().status() == net::URLRequestStatus::SUCCESS) { | 41 if (request->status().status() == net::URLRequestStatus::SUCCESS) { |
| 39 if (data_reduction_proxy_params_->IsDataReductionProxyEligible(request)) { | 42 if (data_reduction_proxy_params_->IsDataReductionProxyEligible(request)) { |
| 40 bool was_received_via_proxy = | 43 bool was_received_via_proxy = |
| 41 data_reduction_proxy_params_->WasDataReductionProxyUsed( | 44 data_reduction_proxy_params_->WasDataReductionProxyUsed( |
| 42 request, NULL); | 45 request, NULL); |
| 43 ui_thread_proxy_->PostTask(FROM_HERE, base::Bind( | 46 IncrementRequestCounts(was_received_via_proxy); |
| 44 &DataReductionProxyUsageStats::IncRequestCountsOnUiThread, | |
| 45 base::Unretained(this), was_received_via_proxy)); | |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 /** | |
| 51 * Determines if the data reduction proxy is currently unreachable. We keep | |
| 52 * track of count of requests which go through data reduction proxy as well as | |
| 53 * count of requests which are eligible to go through the proxy. If and only if | |
| 54 * no requests go through proxy and some requests were eligible, we conclude | |
| 55 * that the proxy is unreachable. | |
| 56 * | |
| 57 * Returns true if the data reduction proxy is unreachable. | |
| 58 */ | |
| 59 bool DataReductionProxyUsageStats::isDataReductionProxyUnreachable() { | |
| 60 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); | |
| 61 | |
| 62 return eligible_num_requests_through_proxy_ > 0 && | |
| 63 actual_num_requests_through_proxy_ == 0; | |
| 64 } | |
| 65 | |
| 66 void DataReductionProxyUsageStats::OnNetworkChanged( | 51 void DataReductionProxyUsageStats::OnNetworkChanged( |
| 67 NetworkChangeNotifier::ConnectionType type) { | 52 NetworkChangeNotifier::ConnectionType type) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 53 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 ui_thread_proxy_->PostTask(FROM_HERE, base::Bind( | 54 ClearRequestCounts(); |
| 70 &DataReductionProxyUsageStats::ClearRequestCountsOnUiThread, | |
| 71 base::Unretained(this))); | |
| 72 } | 55 } |
| 73 | 56 |
| 74 void DataReductionProxyUsageStats::IncRequestCountsOnUiThread( | 57 void DataReductionProxyUsageStats::IncrementRequestCounts( |
| 75 bool was_received_via_proxy) { | 58 bool was_received_via_proxy) { |
| 76 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); | 59 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); |
| 77 if (was_received_via_proxy) { | 60 if (was_received_via_proxy) { |
| 78 actual_num_requests_through_proxy_++; | 61 actual_num_requests_through_proxy_++; |
| 79 } | 62 } |
| 80 eligible_num_requests_through_proxy_++; | 63 eligible_num_requests_through_proxy_++; |
| 81 | 64 |
| 82 // To account for the case when the proxy works for a little while and then | 65 // To account for the case when the proxy works for a little while and then |
| 83 // gets blocked, we reset the counts occasionally. | 66 // gets blocked, we reset the counts occasionally. |
| 84 if (eligible_num_requests_through_proxy_ > 50 | 67 if (eligible_num_requests_through_proxy_ > 50 |
| 85 && actual_num_requests_through_proxy_ > 0) { | 68 && actual_num_requests_through_proxy_ > 0) { |
| 86 ClearRequestCountsOnUiThread(); | 69 ClearRequestCounts(); |
| 70 } else { |
| 71 MaybeNotifyUnavailability(); |
| 87 } | 72 } |
| 88 } | 73 } |
| 89 | 74 |
| 90 void DataReductionProxyUsageStats::ClearRequestCountsOnUiThread() { | 75 void DataReductionProxyUsageStats::ClearRequestCounts() { |
| 91 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); | 76 DCHECK(io_thread_proxy_->BelongsToCurrentThread()); |
| 92 eligible_num_requests_through_proxy_ = 0; | 77 eligible_num_requests_through_proxy_ = 0; |
| 93 actual_num_requests_through_proxy_ = 0; | 78 actual_num_requests_through_proxy_ = 0; |
| 79 MaybeNotifyUnavailability(); |
| 80 } |
| 81 |
| 82 void DataReductionProxyUsageStats::MaybeNotifyUnavailability() { |
| 83 bool prev_unavailable = unavailable_; |
| 84 unavailable_ = (eligible_num_requests_through_proxy_ > 0 && |
| 85 actual_num_requests_through_proxy_ == 0); |
| 86 if (prev_unavailable != unavailable_) { |
| 87 ui_thread_proxy_->PostTask(FROM_HERE, base::Bind( |
| 88 &DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread, |
| 89 base::Unretained(this), |
| 90 unavailable_)); |
| 91 } |
| 92 } |
| 93 |
| 94 void DataReductionProxyUsageStats::NotifyUnavailabilityOnUIThread( |
| 95 bool unavailable) { |
| 96 DCHECK(ui_thread_proxy_->BelongsToCurrentThread()); |
| 97 if (!unavailable_callback_.is_null()) |
| 98 unavailable_callback_.Run(unavailable); |
| 94 } | 99 } |
| 95 | 100 |
| 96 } // namespace data_reduction_proxy | 101 } // namespace data_reduction_proxy |
| 97 | 102 |
| 98 | 103 |
| OLD | NEW |