OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/core/browser/data_reduction_proxy_ping
back_client.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include "base/bind_helpers.h" |
9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
10 #include "base/optional.h" | 11 #include "base/optional.h" |
11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_page_
load_timing.h" | 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_page_
load_timing.h" |
15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" | 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" |
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.
h" | 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.
h" |
17 #include "components/data_reduction_proxy/proto/client_config.pb.h" | 19 #include "components/data_reduction_proxy/proto/client_config.pb.h" |
18 #include "components/data_use_measurement/core/data_use_user_data.h" | 20 #include "components/data_use_measurement/core/data_use_user_data.h" |
19 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
20 #include "net/nqe/effective_connection_type.h" | 22 #include "net/nqe/effective_connection_type.h" |
21 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
22 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
23 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
25 | 27 |
26 namespace data_reduction_proxy { | 28 namespace data_reduction_proxy { |
27 | 29 |
28 namespace { | 30 namespace { |
29 | 31 |
30 static const char kHistogramSucceeded[] = | 32 static const char kHistogramSucceeded[] = |
31 "DataReductionProxy.Pingback.Succeeded"; | 33 "DataReductionProxy.Pingback.Succeeded"; |
32 static const char kHistogramAttempted[] = | 34 static const char kHistogramAttempted[] = |
33 "DataReductionProxy.Pingback.Attempted"; | 35 "DataReductionProxy.Pingback.Attempted"; |
34 | 36 |
35 // Adds the relevant information to |request| for this page load based on page | 37 // Adds the relevant information to |request| for this page load based on page |
36 // timing and data reduction proxy state. | 38 // timing and data reduction proxy state. |
37 void AddDataToPageloadMetrics(const DataReductionProxyData& request_data, | 39 void AddDataToPageloadMetrics(const DataReductionProxyData& request_data, |
38 const DataReductionProxyPageLoadTiming& timing, | 40 const DataReductionProxyPageLoadTiming& timing, |
39 PageloadMetrics* request) { | 41 PageloadMetrics* request, |
| 42 bool opted_out) { |
40 request->set_session_key(request_data.session_key()); | 43 request->set_session_key(request_data.session_key()); |
41 // For the timing events, any of them could be zero. Fill the message as a | 44 // For the timing events, any of them could be zero. Fill the message as a |
42 // best effort. | 45 // best effort. |
43 request->set_allocated_first_request_time( | 46 request->set_allocated_first_request_time( |
44 protobuf_parser::CreateTimestampFromTime(timing.navigation_start) | 47 protobuf_parser::CreateTimestampFromTime(timing.navigation_start) |
45 .release()); | 48 .release()); |
46 if (request_data.request_url().is_valid()) | 49 if (request_data.request_url().is_valid()) |
47 request->set_first_request_url(request_data.request_url().spec()); | 50 request->set_first_request_url(request_data.request_url().spec()); |
48 if (timing.first_contentful_paint) { | 51 if (timing.first_contentful_paint) { |
49 request->set_allocated_time_to_first_contentful_paint( | 52 request->set_allocated_time_to_first_contentful_paint( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 92 |
90 request->set_effective_connection_type( | 93 request->set_effective_connection_type( |
91 protobuf_parser::ProtoEffectiveConnectionTypeFromEffectiveConnectionType( | 94 protobuf_parser::ProtoEffectiveConnectionTypeFromEffectiveConnectionType( |
92 request_data.effective_connection_type())); | 95 request_data.effective_connection_type())); |
93 request->set_compressed_page_size_bytes(timing.network_bytes); | 96 request->set_compressed_page_size_bytes(timing.network_bytes); |
94 request->set_original_page_size_bytes(timing.original_network_bytes); | 97 request->set_original_page_size_bytes(timing.original_network_bytes); |
95 | 98 |
96 if (request_data.page_id()) { | 99 if (request_data.page_id()) { |
97 request->set_page_id(request_data.page_id().value()); | 100 request->set_page_id(request_data.page_id().value()); |
98 } | 101 } |
| 102 |
| 103 bool was_preview_shown = false; |
| 104 if (request_data.lofi_received()) { |
| 105 request->set_previews_type(PageloadMetrics_PreviewsType_LOFI); |
| 106 was_preview_shown = true; |
| 107 } else if (request_data.lite_page_received()) { |
| 108 request->set_previews_type(PageloadMetrics_PreviewsType_LITE_PAGE); |
| 109 was_preview_shown = true; |
| 110 } else { |
| 111 request->set_previews_type(PageloadMetrics_PreviewsType_NONE); |
| 112 } |
| 113 |
| 114 if (!was_preview_shown || timing.app_background_occurred) { |
| 115 request->set_previews_opt_out(PageloadMetrics_PreviewsOptOut_UNKNOWN); |
| 116 return; |
| 117 } |
| 118 |
| 119 if (opted_out) { |
| 120 request->set_previews_opt_out(PageloadMetrics_PreviewsOptOut_OPT_OUT); |
| 121 return; |
| 122 } |
| 123 request->set_previews_opt_out(PageloadMetrics_PreviewsOptOut_NON_OPT_OUT); |
99 } | 124 } |
100 | 125 |
101 // Adds |current_time| as the metrics sent time to |request_data|, and returns | 126 // Adds |current_time| as the metrics sent time to |request_data|, and returns |
102 // the serialized request. | 127 // the serialized request. |
103 std::string AddTimeAndSerializeRequest( | 128 std::string AddTimeAndSerializeRequest( |
104 RecordPageloadMetricsRequest* request_data, | 129 RecordPageloadMetricsRequest* request_data, |
105 base::Time current_time) { | 130 base::Time current_time) { |
106 request_data->set_allocated_metrics_sent_time( | 131 request_data->set_allocated_metrics_sent_time( |
107 protobuf_parser::CreateTimestampFromTime(current_time).release()); | 132 protobuf_parser::CreateTimestampFromTime(current_time).release()); |
108 std::string serialized_request; | 133 std::string serialized_request; |
109 request_data->SerializeToString(&serialized_request); | 134 request_data->SerializeToString(&serialized_request); |
110 return serialized_request; | 135 return serialized_request; |
111 } | 136 } |
112 | 137 |
113 } // namespace | 138 } // namespace |
114 | 139 |
115 DataReductionProxyPingbackClient::DataReductionProxyPingbackClient( | 140 DataReductionProxyPingbackClient::DataReductionProxyPingbackClient( |
116 net::URLRequestContextGetter* url_request_context) | 141 net::URLRequestContextGetter* url_request_context) |
117 : url_request_context_(url_request_context), | 142 : url_request_context_(url_request_context), |
118 pingback_url_(util::AddApiKeyToUrl(params::GetPingbackURL())), | 143 pingback_url_(util::AddApiKeyToUrl(params::GetPingbackURL())), |
119 pingback_reporting_fraction_(0.0) {} | 144 pingback_reporting_fraction_(0.0) {} |
120 | 145 |
121 DataReductionProxyPingbackClient::~DataReductionProxyPingbackClient() { | 146 DataReductionProxyPingbackClient::~DataReductionProxyPingbackClient() { |
| 147 DCHECK(opt_outs_.empty()); |
122 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
123 } | 149 } |
124 | 150 |
125 void DataReductionProxyPingbackClient::OnURLFetchComplete( | 151 void DataReductionProxyPingbackClient::OnURLFetchComplete( |
126 const net::URLFetcher* source) { | 152 const net::URLFetcher* source) { |
127 DCHECK(thread_checker_.CalledOnValidThread()); | 153 DCHECK(thread_checker_.CalledOnValidThread()); |
128 DCHECK(source == current_fetcher_.get()); | 154 DCHECK(source == current_fetcher_.get()); |
129 UMA_HISTOGRAM_BOOLEAN(kHistogramSucceeded, source->GetStatus().is_success()); | 155 UMA_HISTOGRAM_BOOLEAN(kHistogramSucceeded, source->GetStatus().is_success()); |
130 current_fetcher_.reset(); | 156 current_fetcher_.reset(); |
131 if (metrics_request_.pageloads_size() > 0) { | 157 if (metrics_request_.pageloads_size() > 0) { |
132 CreateFetcherForDataAndStart(); | 158 CreateFetcherForDataAndStart(); |
133 } | 159 } |
134 } | 160 } |
135 | 161 |
136 void DataReductionProxyPingbackClient::SendPingback( | 162 void DataReductionProxyPingbackClient::SendPingback( |
137 const DataReductionProxyData& request_data, | 163 const DataReductionProxyData& request_data, |
138 const DataReductionProxyPageLoadTiming& timing) { | 164 const DataReductionProxyPageLoadTiming& timing) { |
139 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
140 bool send_pingback = ShouldSendPingback(); | 166 bool send_pingback = ShouldSendPingback(); |
141 UMA_HISTOGRAM_BOOLEAN(kHistogramAttempted, send_pingback); | 167 UMA_HISTOGRAM_BOOLEAN(kHistogramAttempted, send_pingback); |
142 if (!send_pingback) | 168 if (!send_pingback) |
143 return; | 169 return; |
| 170 |
| 171 bool opted_out = false; |
| 172 if (request_data.page_id()) { |
| 173 auto opt_out = opt_outs_.find(NavigationID(request_data.page_id().value(), |
| 174 request_data.session_key())); |
| 175 opted_out = opt_out != opt_outs_.end(); |
| 176 if (opted_out) |
| 177 opt_outs_.erase(opt_out); |
| 178 } |
| 179 |
144 PageloadMetrics* pageload_metrics = metrics_request_.add_pageloads(); | 180 PageloadMetrics* pageload_metrics = metrics_request_.add_pageloads(); |
145 AddDataToPageloadMetrics(request_data, timing, pageload_metrics); | 181 AddDataToPageloadMetrics(request_data, timing, pageload_metrics, opted_out); |
146 if (current_fetcher_.get()) | 182 if (current_fetcher_.get()) |
147 return; | 183 return; |
148 DCHECK_EQ(1, metrics_request_.pageloads_size()); | 184 DCHECK_EQ(1, metrics_request_.pageloads_size()); |
149 CreateFetcherForDataAndStart(); | 185 CreateFetcherForDataAndStart(); |
150 } | 186 } |
151 | 187 |
152 void DataReductionProxyPingbackClient::CreateFetcherForDataAndStart() { | 188 void DataReductionProxyPingbackClient::CreateFetcherForDataAndStart() { |
153 DCHECK(!current_fetcher_); | 189 DCHECK(!current_fetcher_); |
154 DCHECK_GE(metrics_request_.pageloads_size(), 1); | 190 DCHECK_GE(metrics_request_.pageloads_size(), 1); |
155 std::string serialized_request = | 191 std::string serialized_request = |
(...skipping 28 matching lines...) Expand all Loading... |
184 } | 220 } |
185 | 221 |
186 void DataReductionProxyPingbackClient::SetPingbackReportingFraction( | 222 void DataReductionProxyPingbackClient::SetPingbackReportingFraction( |
187 float pingback_reporting_fraction) { | 223 float pingback_reporting_fraction) { |
188 DCHECK(thread_checker_.CalledOnValidThread()); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
189 DCHECK_LE(0.0f, pingback_reporting_fraction); | 225 DCHECK_LE(0.0f, pingback_reporting_fraction); |
190 DCHECK_GE(1.0f, pingback_reporting_fraction); | 226 DCHECK_GE(1.0f, pingback_reporting_fraction); |
191 pingback_reporting_fraction_ = pingback_reporting_fraction; | 227 pingback_reporting_fraction_ = pingback_reporting_fraction; |
192 } | 228 } |
193 | 229 |
| 230 void DataReductionProxyPingbackClient::AddOptOut( |
| 231 const NavigationID& navigation_id) { |
| 232 opt_outs_.emplace(navigation_id); |
| 233 } |
| 234 |
| 235 void DataReductionProxyPingbackClient::ClearNavigationKeySync( |
| 236 const NavigationID& navigation_id) { |
| 237 opt_outs_.erase(navigation_id); |
| 238 } |
| 239 |
| 240 void DataReductionProxyPingbackClient::ClearNavigationKeyAsync( |
| 241 const NavigationID& navigation_id) { |
| 242 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 243 FROM_HERE, |
| 244 base::Bind(&DataReductionProxyPingbackClient::ClearNavigationKeySync, |
| 245 base::Unretained(this), navigation_id)); |
| 246 } |
| 247 |
| 248 size_t DataReductionProxyPingbackClient::OptOutsSizeForTesting() const { |
| 249 return opt_outs_.size(); |
| 250 } |
| 251 |
194 } // namespace data_reduction_proxy | 252 } // namespace data_reduction_proxy |
OLD | NEW |