Chromium Code Reviews| Index: components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc |
| diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1b644ea7162cfcf4a1001ed0085dcedaf237567 |
| --- /dev/null |
| +++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.cc |
| @@ -0,0 +1,184 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h" |
|
bengr
2014/11/18 19:30:56
For tests, you'll want to test that (1) wrapping w
megjablon
2014/11/19 19:23:40
Next patch
|
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/prefs/pref_member.h" |
|
bengr
2014/11/18 19:30:56
Is this needed?
megjablon
2014/11/19 19:23:40
Removed.
|
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h" |
|
bengr
2014/11/18 19:30:56
Is this needed?
megjablon
2014/11/19 19:23:40
Done.
|
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_protocol.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h" |
|
bengr
2014/11/18 19:30:56
This can be forward declared until you move Update
megjablon
2014/11/19 19:23:41
Acknowledged.
|
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats.h" |
| +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" |
|
bengr
2014/11/18 19:30:56
Not needed.
megjablon
2014/11/19 19:23:41
Done.
|
| +#include "net/base/network_delegate.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/proxy/proxy_config.h" |
|
bengr
2014/11/18 19:30:56
This can be forward declared.
megjablon
2014/11/19 19:23:40
Done.
|
| +#include "net/proxy/proxy_info.h" |
|
bengr
2014/11/18 19:30:55
This can be forward declared.
megjablon
2014/11/19 19:23:41
Done.
|
| +#include "net/proxy/proxy_server.h" |
|
bengr
2014/11/18 19:30:56
This can be forward declared.
megjablon
2014/11/19 19:23:40
Done.
|
| +#include "net/url_request/url_request.h" |
|
bengr
2014/11/18 19:30:55
You need an include for URLRequestStatus too.
megjablon
2014/11/19 19:23:40
Done.
|
| + |
|
bengr
2014/11/18 19:30:56
You also need an include for ProxyService.
megjablon
2014/11/19 19:23:41
Done.
|
| +namespace data_reduction_proxy { |
|
bengr
2014/11/18 19:30:56
You should forward declare GURL, ProxyInfo, ProxyC
megjablon
2014/11/19 19:23:41
Done.
|
| + |
| +DataReductionProxyNetworkDelegate::DataReductionProxyNetworkDelegate( |
| + scoped_ptr<net::NetworkDelegate> network_delegate) |
| + : WrappingNetworkDelegate(network_delegate.Pass()), |
| + profile_prefs_(NULL), |
| + ui_thread_proxy_(NULL), |
| + received_content_length_(0), |
| + original_content_length_(0), |
| + data_reduction_proxy_enabled_(NULL), |
| + data_reduction_proxy_params_(NULL), |
| + data_reduction_proxy_usage_stats_(NULL), |
| + data_reduction_proxy_auth_request_handler_(NULL), |
| + data_reduction_proxy_statistics_prefs_(NULL) { |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::set_profile_prefs( |
| + PrefService* profile_prefs) { |
| + profile_prefs_ = profile_prefs; |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::set_ui_thread_proxy( |
| + scoped_refptr<base::MessageLoopProxy> ui_thread_proxy){ |
| + ui_thread_proxy_ = ui_thread_proxy; |
| +} |
| + |
| +DataReductionProxyNetworkDelegate::~DataReductionProxyNetworkDelegate() { |
| +} |
| + |
| +// static |
| +// TODO(megjablon): Use data_reduction_proxy_delayed_pref_service to read prefs. |
| +// Until updated the pref values may be up to an hour behind on desktop. |
| +base::Value* DataReductionProxyNetworkDelegate::HistoricNetworkStatsInfoToValue( |
| + PrefService* profile_prefs) { |
| + int64 total_received = profile_prefs->GetInt64( |
| + data_reduction_proxy::prefs::kHttpReceivedContentLength); |
| + int64 total_original = profile_prefs->GetInt64( |
| + data_reduction_proxy::prefs::kHttpOriginalContentLength); |
| + |
| + base::DictionaryValue* dict = new base::DictionaryValue(); |
| + // Use strings to avoid overflow. base::Value only supports 32-bit integers. |
| + dict->SetString("historic_received_content_length", |
| + base::Int64ToString(total_received)); |
| + dict->SetString("historic_original_content_length", |
| + base::Int64ToString(total_original)); |
| + return dict; |
| +} |
| + |
| +base::Value* |
| +DataReductionProxyNetworkDelegate::SessionNetworkStatsInfoToValue() const { |
| + base::DictionaryValue* dict = new base::DictionaryValue(); |
| + // Use strings to avoid overflow. base::Value only supports 32-bit integers. |
| + dict->SetString("session_received_content_length", |
| + base::Int64ToString(received_content_length_)); |
| + dict->SetString("session_original_content_length", |
| + base::Int64ToString(original_content_length_)); |
| + return dict; |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::OnResolveProxyInternal( |
| + const GURL& url, |
| + int load_flags, |
| + const net::ProxyService& proxy_service, |
| + net::ProxyInfo* result) { |
| + if (!on_resolve_proxy_handler_.is_null() && |
| + !proxy_config_getter_.is_null()) { |
| + on_resolve_proxy_handler_.Run(url, load_flags, |
| + proxy_config_getter_.Run(), |
| + proxy_service.config(), |
| + proxy_service.proxy_retry_info(), |
|
bengr
2014/11/18 19:30:55
Is this the same as the result?
megjablon
2014/11/19 19:23:41
I'm not sure what you're asking here.
|
| + data_reduction_proxy_params_, result); |
| + } |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::OnProxyFallbackInternal( |
| + const net::ProxyServer& bad_proxy, |
| + int net_error) { |
| + if (data_reduction_proxy_usage_stats_) { |
| + data_reduction_proxy_usage_stats_->OnProxyFallback( |
| + bad_proxy, net_error); |
| + } |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::OnBeforeSendProxyHeadersInternal( |
| + net::URLRequest* request, |
| + const net::ProxyInfo& proxy_info, |
| + net::HttpRequestHeaders* headers) { |
| + if (data_reduction_proxy_auth_request_handler_) { |
| + data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader( |
| + request, proxy_info.proxy_server(), headers); |
| + } |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::OnCompletedInternal( |
| + net::URLRequest* request, |
| + bool started) { |
| + if (data_reduction_proxy_usage_stats_) |
| + data_reduction_proxy_usage_stats_->OnUrlRequestCompleted(request, started); |
| + |
| + // Only record for http or https urls. |
| + bool is_http = request->url().SchemeIs("http"); |
| + bool is_https = request->url().SchemeIs("https"); |
| + |
| + if (request->status().status() == net::URLRequestStatus::SUCCESS) { |
| + // For better accuracy, we use the actual bytes read instead of the length |
| + // specified with the Content-Length header, which may be inaccurate, |
| + // or missing, as is the case with chunked encoding. |
| + int64 received_content_length = request->received_response_content_length(); |
| + |
| + if (!request->was_cached() && // Don't record cached content |
| + received_content_length && // Zero-byte responses aren't useful. |
| + (is_http || is_https)) { // Only record for HTTP or HTTPS urls. |
| + int64 original_content_length = |
| + request->response_info().headers->GetInt64HeaderValue( |
| + "x-original-content-length"); |
| + DataReductionProxyRequestType request_type = |
| + GetDataReductionProxyRequestType(request); |
| + |
| + int64 adjusted_original_content_length = |
| + GetAdjustedOriginalContentLength(request_type, |
| + original_content_length, |
| + received_content_length); |
| + AccumulateContentLength(received_content_length, |
| + adjusted_original_content_length, |
| + request_type); |
| + |
| + if (data_reduction_proxy_enabled_ && |
| + data_reduction_proxy_usage_stats_ && |
| + !proxy_config_getter_.is_null()) { |
| + data_reduction_proxy_usage_stats_->RecordBytesHistograms( |
| + request, |
| + *data_reduction_proxy_enabled_, |
| + proxy_config_getter_.Run()); |
| + } |
| + } |
| + } |
| +} |
| + |
| +void DataReductionProxyNetworkDelegate::AccumulateContentLength( |
| + int64 received_content_length, |
| + int64 original_content_length, |
| + DataReductionProxyRequestType request_type) { |
| + DCHECK_GE(received_content_length, 0); |
| + DCHECK_GE(original_content_length, 0); |
| + if (data_reduction_proxy_statistics_prefs_ && profile_prefs_) { |
| + ui_thread_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&UpdateContentLengthPrefs, |
| + received_content_length, |
| + original_content_length, |
| + profile_prefs_, |
| + request_type, |
| + data_reduction_proxy_statistics_prefs_)); |
| + |
|
bengr
2014/11/18 19:30:55
Remove blank line.
megjablon
2014/11/19 19:23:40
Done.
|
| + } |
| + received_content_length_ += received_content_length; |
| + original_content_length_ += original_content_length; |
| +} |
| + |
| +} // namespace data_reduction_proxy |