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