| 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/core/browser/data_reduction_proxy_netw
ork_delegate.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_netw
ork_delegate.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 // If this ever triggers, then byte counts can no longer be safely stored in | 120 // If this ever triggers, then byte counts can no longer be safely stored in |
| 121 // 64-bit ints. | 121 // 64-bit ints. |
| 122 NOTREACHED(); | 122 NOTREACHED(); |
| 123 return byte_count; | 123 return byte_count; |
| 124 } | 124 } |
| 125 return static_cast<int64_t>(scaled_byte_count); | 125 return static_cast<int64_t>(scaled_byte_count); |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Calculates the effective original content length of the |request|, accounting | 128 // Calculates the effective original content length of the |request|, accounting |
| 129 // for partial responses if necessary. | 129 // for partial responses if necessary. |
| 130 int64_t CalculateEffectiveOCL(const net::URLRequest& request) { | 130 int64_t CalculateEffectiveOCL(const net::URLRequest& request, int net_error) { |
| 131 int64_t original_content_length_from_header = | 131 int64_t original_content_length_from_header = |
| 132 request.response_headers()->GetInt64HeaderValue( | 132 request.response_headers()->GetInt64HeaderValue( |
| 133 "x-original-content-length"); | 133 "x-original-content-length"); |
| 134 | 134 |
| 135 if (original_content_length_from_header < 0) | 135 if (original_content_length_from_header < 0) |
| 136 return request.received_response_content_length(); | 136 return request.received_response_content_length(); |
| 137 if (request.status().is_success()) | 137 if (net_error == net::OK) |
| 138 return original_content_length_from_header; | 138 return original_content_length_from_header; |
| 139 | 139 |
| 140 int64_t content_length_from_header = | 140 int64_t content_length_from_header = |
| 141 request.response_headers()->GetContentLength(); | 141 request.response_headers()->GetContentLength(); |
| 142 | 142 |
| 143 if (content_length_from_header < 0) | 143 if (content_length_from_header < 0) |
| 144 return request.received_response_content_length(); | 144 return request.received_response_content_length(); |
| 145 if (content_length_from_header == 0) | 145 if (content_length_from_header == 0) |
| 146 return original_content_length_from_header; | 146 return original_content_length_from_header; |
| 147 | 147 |
| 148 return ScaleByteCountByRatio(request.received_response_content_length(), | 148 return ScaleByteCountByRatio(request.received_response_content_length(), |
| 149 original_content_length_from_header, | 149 original_content_length_from_header, |
| 150 content_length_from_header); | 150 content_length_from_header); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Given a |request| that went through the Data Reduction Proxy, this function | 153 // Given a |request| that went through the Data Reduction Proxy, this function |
| 154 // estimates how many bytes would have been received if the response had been | 154 // estimates how many bytes would have been received if the response had been |
| 155 // received directly from the origin using HTTP/1.1 with a content length of | 155 // received directly from the origin using HTTP/1.1 with a content length of |
| 156 // |adjusted_original_content_length|. | 156 // |adjusted_original_content_length|. |
| 157 int64_t EstimateOriginalReceivedBytes(const net::URLRequest& request) { | 157 int64_t EstimateOriginalReceivedBytes(const net::URLRequest& request, |
| 158 int net_error) { |
| 158 if (request.was_cached() || !request.response_headers()) | 159 if (request.was_cached() || !request.response_headers()) |
| 159 return request.GetTotalReceivedBytes(); | 160 return request.GetTotalReceivedBytes(); |
| 160 | 161 |
| 161 // TODO(sclittle): Remove headers added by Data Reduction Proxy when computing | 162 // TODO(sclittle): Remove headers added by Data Reduction Proxy when computing |
| 162 // original size. http://crbug/535701. | 163 // original size. http://crbug/535701. |
| 163 return request.response_headers()->raw_headers().size() + | 164 return request.response_headers()->raw_headers().size() + |
| 164 CalculateEffectiveOCL(request); | 165 CalculateEffectiveOCL(request, net_error); |
| 165 } | 166 } |
| 166 | 167 |
| 167 } // namespace | 168 } // namespace |
| 168 | 169 |
| 169 namespace data_reduction_proxy { | 170 namespace data_reduction_proxy { |
| 170 | 171 |
| 171 DataReductionProxyNetworkDelegate::DataReductionProxyNetworkDelegate( | 172 DataReductionProxyNetworkDelegate::DataReductionProxyNetworkDelegate( |
| 172 std::unique_ptr<net::NetworkDelegate> network_delegate, | 173 std::unique_ptr<net::NetworkDelegate> network_delegate, |
| 173 DataReductionProxyConfig* config, | 174 DataReductionProxyConfig* config, |
| 174 DataReductionProxyRequestOptions* request_options, | 175 DataReductionProxyRequestOptions* request_options, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 | 292 |
| 292 if (data_reduction_proxy_request_options_) { | 293 if (data_reduction_proxy_request_options_) { |
| 293 data_reduction_proxy_request_options_->AddRequestHeader(headers); | 294 data_reduction_proxy_request_options_->AddRequestHeader(headers); |
| 294 } | 295 } |
| 295 } | 296 } |
| 296 | 297 |
| 297 void DataReductionProxyNetworkDelegate::OnCompletedInternal( | 298 void DataReductionProxyNetworkDelegate::OnCompletedInternal( |
| 298 net::URLRequest* request, | 299 net::URLRequest* request, |
| 299 bool started) { | 300 bool started) { |
| 300 DCHECK(request); | 301 DCHECK(request); |
| 302 // TODO(maksims): remove this once OnCompletedInternal() has net_error in |
| 303 // arguments. |
| 304 int net_error = request->status().error(); |
| 305 DCHECK_NE(net::ERR_IO_PENDING, net_error); |
| 301 if (data_reduction_proxy_bypass_stats_) | 306 if (data_reduction_proxy_bypass_stats_) |
| 302 data_reduction_proxy_bypass_stats_->OnUrlRequestCompleted(request, started); | 307 data_reduction_proxy_bypass_stats_->OnUrlRequestCompleted(request, started, |
| 308 net_error); |
| 303 | 309 |
| 304 net::HttpRequestHeaders request_headers; | 310 net::HttpRequestHeaders request_headers; |
| 305 if (data_reduction_proxy_io_data_ && request->response_headers() && | 311 if (data_reduction_proxy_io_data_ && request->response_headers() && |
| 306 request->response_headers()->HasHeaderValue( | 312 request->response_headers()->HasHeaderValue( |
| 307 chrome_proxy_header(), chrome_proxy_lo_fi_directive())) { | 313 chrome_proxy_header(), chrome_proxy_lo_fi_directive())) { |
| 308 data_reduction_proxy_io_data_->lofi_ui_service()->OnLoFiReponseReceived( | 314 data_reduction_proxy_io_data_->lofi_ui_service()->OnLoFiReponseReceived( |
| 309 *request, false); | 315 *request, false); |
| 310 } else if (data_reduction_proxy_io_data_ && request->response_headers() && | 316 } else if (data_reduction_proxy_io_data_ && request->response_headers() && |
| 311 request->response_headers()->HasHeaderValue( | 317 request->response_headers()->HasHeaderValue( |
| 312 chrome_proxy_header(), | 318 chrome_proxy_header(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 333 DataReductionProxyRequestType request_type = GetDataReductionProxyRequestType( | 339 DataReductionProxyRequestType request_type = GetDataReductionProxyRequestType( |
| 334 *request, configurator_->GetProxyConfig(), *data_reduction_proxy_config_); | 340 *request, configurator_->GetProxyConfig(), *data_reduction_proxy_config_); |
| 335 | 341 |
| 336 // Determine the original content length if present. | 342 // Determine the original content length if present. |
| 337 int64_t original_content_length = | 343 int64_t original_content_length = |
| 338 request->response_headers() | 344 request->response_headers() |
| 339 ? request->response_headers()->GetInt64HeaderValue( | 345 ? request->response_headers()->GetInt64HeaderValue( |
| 340 "x-original-content-length") | 346 "x-original-content-length") |
| 341 : -1; | 347 : -1; |
| 342 | 348 |
| 343 CalculateAndRecordDataUsage(*request, request_type, original_content_length); | 349 CalculateAndRecordDataUsage(*request, request_type, original_content_length, |
| 350 net_error); |
| 344 | 351 |
| 345 RecordContentLength(*request, request_type, original_content_length); | 352 RecordContentLength(*request, request_type, original_content_length); |
| 346 } | 353 } |
| 347 | 354 |
| 348 void DataReductionProxyNetworkDelegate::CalculateAndRecordDataUsage( | 355 void DataReductionProxyNetworkDelegate::CalculateAndRecordDataUsage( |
| 349 const net::URLRequest& request, | 356 const net::URLRequest& request, |
| 350 DataReductionProxyRequestType request_type, | 357 DataReductionProxyRequestType request_type, |
| 351 int64_t original_content_length) { | 358 int64_t original_content_length, |
| 359 int net_error) { |
| 352 DCHECK_LE(-1, original_content_length); | 360 DCHECK_LE(-1, original_content_length); |
| 353 int64_t data_used = request.GetTotalReceivedBytes(); | 361 int64_t data_used = request.GetTotalReceivedBytes(); |
| 354 | 362 |
| 355 // Estimate how many bytes would have been used if the DataReductionProxy was | 363 // Estimate how many bytes would have been used if the DataReductionProxy was |
| 356 // not used, and record the data usage. | 364 // not used, and record the data usage. |
| 357 int64_t original_size = data_used; | 365 int64_t original_size = data_used; |
| 358 | 366 |
| 359 if (request_type == VIA_DATA_REDUCTION_PROXY) | 367 if (request_type == VIA_DATA_REDUCTION_PROXY) |
| 360 original_size = EstimateOriginalReceivedBytes(request); | 368 original_size = EstimateOriginalReceivedBytes(request, net_error); |
| 361 | 369 |
| 362 std::string mime_type; | 370 std::string mime_type; |
| 363 if (request.response_headers()) | 371 if (request.response_headers()) |
| 364 request.response_headers()->GetMimeType(&mime_type); | 372 request.response_headers()->GetMimeType(&mime_type); |
| 365 | 373 |
| 366 scoped_refptr<DataUseGroup> data_use_group = | 374 scoped_refptr<DataUseGroup> data_use_group = |
| 367 data_use_group_provider_ | 375 data_use_group_provider_ |
| 368 ? data_use_group_provider_->GetDataUseGroup(&request) | 376 ? data_use_group_provider_->GetDataUseGroup(&request) |
| 369 : nullptr; | 377 : nullptr; |
| 370 AccumulateDataUsage(data_used, original_size, request_type, data_use_group, | 378 AccumulateDataUsage(data_used, original_size, request_type, data_use_group, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 request.url(), | 453 request.url(), |
| 446 &data_reduction_proxy_info); | 454 &data_reduction_proxy_info); |
| 447 } | 455 } |
| 448 | 456 |
| 449 void DataReductionProxyNetworkDelegate::SetDataUseGroupProvider( | 457 void DataReductionProxyNetworkDelegate::SetDataUseGroupProvider( |
| 450 std::unique_ptr<DataUseGroupProvider> data_use_group_provider) { | 458 std::unique_ptr<DataUseGroupProvider> data_use_group_provider) { |
| 451 data_use_group_provider_.reset(data_use_group_provider.release()); | 459 data_use_group_provider_.reset(data_use_group_provider.release()); |
| 452 } | 460 } |
| 453 | 461 |
| 454 } // namespace data_reduction_proxy | 462 } // namespace data_reduction_proxy |
| OLD | NEW |