OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base_paths.h" | 11 #include "base/base_paths.h" |
12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/prefs/pref_member.h" | 16 #include "base/prefs/pref_member.h" |
17 #include "base/prefs/pref_service.h" | 17 #include "base/prefs/pref_service.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_split.h" |
19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
20 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/content_settings/cookie_settings.h" | 22 #include "chrome/browser/content_settings/cookie_settings.h" |
22 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 23 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
23 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 24 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
24 #include "chrome/browser/net/chrome_extensions_network_delegate.h" | 25 #include "chrome/browser/net/chrome_extensions_network_delegate.h" |
25 #include "chrome/browser/net/client_hints.h" | 26 #include "chrome/browser/net/client_hints.h" |
26 #include "chrome/browser/net/connect_interceptor.h" | 27 #include "chrome/browser/net/connect_interceptor.h" |
27 #include "chrome/browser/net/safe_search_util.h" | |
28 #include "chrome/browser/performance_monitor/performance_monitor.h" | 28 #include "chrome/browser/performance_monitor/performance_monitor.h" |
29 #include "chrome/browser/prerender/prerender_tracker.h" | 29 #include "chrome/browser/prerender/prerender_tracker.h" |
30 #include "chrome/browser/profiles/profile_manager.h" | 30 #include "chrome/browser/profiles/profile_manager.h" |
31 #include "chrome/browser/task_manager/task_manager.h" | 31 #include "chrome/browser/task_manager/task_manager.h" |
32 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 33 #include "chrome/common/url_constants.h" |
33 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" | 34 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" |
34 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h
" | 35 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h
" |
35 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 36 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
36 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" | 37 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" |
37 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" | 38 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" |
38 #include "components/domain_reliability/monitor.h" | 39 #include "components/domain_reliability/monitor.h" |
| 40 #include "components/google/core/browser/google_util.h" |
39 #include "content/public/browser/browser_thread.h" | 41 #include "content/public/browser/browser_thread.h" |
40 #include "content/public/browser/render_frame_host.h" | 42 #include "content/public/browser/render_frame_host.h" |
41 #include "content/public/browser/render_view_host.h" | 43 #include "content/public/browser/render_view_host.h" |
42 #include "content/public/browser/resource_request_info.h" | 44 #include "content/public/browser/resource_request_info.h" |
43 #include "extensions/common/constants.h" | 45 #include "extensions/common/constants.h" |
44 #include "net/base/host_port_pair.h" | 46 #include "net/base/host_port_pair.h" |
45 #include "net/base/net_errors.h" | 47 #include "net/base/net_errors.h" |
46 #include "net/base/net_log.h" | 48 #include "net/base/net_log.h" |
47 #include "net/cookies/canonical_cookie.h" | 49 #include "net/cookies/canonical_cookie.h" |
48 #include "net/cookies/cookie_options.h" | 50 #include "net/cookies/cookie_options.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 #endif | 86 #endif |
85 | 87 |
86 // This remains false unless the --disable-extensions-http-throttling | 88 // This remains false unless the --disable-extensions-http-throttling |
87 // flag is passed to the browser. | 89 // flag is passed to the browser. |
88 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; | 90 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
89 | 91 |
90 namespace { | 92 namespace { |
91 | 93 |
92 const char kDNTHeader[] = "DNT"; | 94 const char kDNTHeader[] = "DNT"; |
93 | 95 |
| 96 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the |
| 97 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters |
| 98 // must be in key=value form. |
| 99 bool HasSameParameterKey(const std::string& first_parameter, |
| 100 const std::string& second_parameter) { |
| 101 DCHECK(second_parameter.find("=") != std::string::npos); |
| 102 // Prefix for "foo=bar" is "foo=". |
| 103 std::string parameter_prefix = second_parameter.substr( |
| 104 0, second_parameter.find("=") + 1); |
| 105 return StartsWithASCII(first_parameter, parameter_prefix, false); |
| 106 } |
| 107 |
| 108 // Examines the query string containing parameters and adds the necessary ones |
| 109 // so that SafeSearch is active. |query| is the string to examine and the |
| 110 // return value is the |query| string modified such that SafeSearch is active. |
| 111 std::string AddSafeSearchParameters(const std::string& query) { |
| 112 std::vector<std::string> new_parameters; |
| 113 std::string safe_parameter = chrome::kSafeSearchSafeParameter; |
| 114 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; |
| 115 |
| 116 std::vector<std::string> parameters; |
| 117 base::SplitString(query, '&', ¶meters); |
| 118 |
| 119 std::vector<std::string>::iterator it; |
| 120 for (it = parameters.begin(); it < parameters.end(); ++it) { |
| 121 if (!HasSameParameterKey(*it, safe_parameter) && |
| 122 !HasSameParameterKey(*it, ssui_parameter)) { |
| 123 new_parameters.push_back(*it); |
| 124 } |
| 125 } |
| 126 |
| 127 new_parameters.push_back(safe_parameter); |
| 128 new_parameters.push_back(ssui_parameter); |
| 129 return JoinString(new_parameters, '&'); |
| 130 } |
| 131 |
| 132 // If |request| is a request to Google Web Search the function |
| 133 // enforces that the SafeSearch query parameters are set to active. |
| 134 // Sets the query part of |new_url| with the new value of the parameters. |
| 135 void ForceGoogleSafeSearch(net::URLRequest* request, |
| 136 GURL* new_url) { |
| 137 if (!google_util::IsGoogleSearchUrl(request->url()) && |
| 138 !google_util::IsGoogleHomePageUrl(request->url())) |
| 139 return; |
| 140 |
| 141 std::string query = request->url().query(); |
| 142 std::string new_query = AddSafeSearchParameters(query); |
| 143 if (query == new_query) |
| 144 return; |
| 145 |
| 146 GURL::Replacements replacements; |
| 147 replacements.SetQueryStr(new_query); |
| 148 *new_url = request->url().ReplaceComponents(replacements); |
| 149 } |
| 150 |
94 // Gets called when the extensions finish work on the URL. If the extensions | 151 // Gets called when the extensions finish work on the URL. If the extensions |
95 // did not do a redirect (so |new_url| is empty) then we enforce the | 152 // did not do a redirect (so |new_url| is empty) then we enforce the |
96 // SafeSearch parameters. Otherwise we will get called again after the | 153 // SafeSearch parameters. Otherwise we will get called again after the |
97 // redirect and we enforce SafeSearch then. | 154 // redirect and we enforce SafeSearch then. |
98 void ForceGoogleSafeSearchCallbackWrapper( | 155 void ForceGoogleSafeSearchCallbackWrapper( |
99 const net::CompletionCallback& callback, | 156 const net::CompletionCallback& callback, |
100 net::URLRequest* request, | 157 net::URLRequest* request, |
101 GURL* new_url, | 158 GURL* new_url, |
102 int rv) { | 159 int rv) { |
103 if (rv == net::OK && new_url->is_empty()) | 160 if (rv == net::OK && new_url->is_empty()) |
104 safe_search_util::ForceGoogleSafeSearch(request, new_url); | 161 ForceGoogleSafeSearch(request, new_url); |
105 callback.Run(rv); | 162 callback.Run(rv); |
106 } | 163 } |
107 | 164 |
108 void UpdateContentLengthPrefs( | 165 void UpdateContentLengthPrefs( |
109 int received_content_length, | 166 int received_content_length, |
110 int original_content_length, | 167 int original_content_length, |
111 data_reduction_proxy::DataReductionProxyRequestType request_type, | 168 data_reduction_proxy::DataReductionProxyRequestType request_type, |
112 Profile* profile) { | 169 Profile* profile) { |
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
114 DCHECK_GE(received_content_length, 0); | 171 DCHECK_GE(received_content_length, 0); |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, | 460 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, |
404 callback, | 461 callback, |
405 base::Unretained(request), | 462 base::Unretained(request), |
406 base::Unretained(new_url)); | 463 base::Unretained(new_url)); |
407 } | 464 } |
408 | 465 |
409 int rv = extensions_delegate_->OnBeforeURLRequest( | 466 int rv = extensions_delegate_->OnBeforeURLRequest( |
410 request, wrapped_callback, new_url); | 467 request, wrapped_callback, new_url); |
411 | 468 |
412 if (force_safe_search && rv == net::OK && new_url->is_empty()) | 469 if (force_safe_search && rv == net::OK && new_url->is_empty()) |
413 safe_search_util::ForceGoogleSafeSearch(request, new_url); | 470 ForceGoogleSafeSearch(request, new_url); |
414 | 471 |
415 if (connect_interceptor_) | 472 if (connect_interceptor_) |
416 connect_interceptor_->WitnessURLRequest(request); | 473 connect_interceptor_->WitnessURLRequest(request); |
417 | 474 |
418 return rv; | 475 return rv; |
419 } | 476 } |
420 | 477 |
421 void ChromeNetworkDelegate::OnResolveProxy( | 478 void ChromeNetworkDelegate::OnResolveProxy( |
422 const GURL& url, int load_flags, net::ProxyInfo* result) { | 479 const GURL& url, int load_flags, net::ProxyInfo* result) { |
423 if (!on_resolve_proxy_handler_.is_null()) { | 480 if (!on_resolve_proxy_handler_.is_null()) { |
424 on_resolve_proxy_handler_.Run(url, load_flags, | 481 on_resolve_proxy_handler_.Run(url, load_flags, |
425 data_reduction_proxy_params_, result); | 482 data_reduction_proxy_params_, result); |
426 } | 483 } |
427 } | 484 } |
428 | 485 |
429 int ChromeNetworkDelegate::OnBeforeSendHeaders( | 486 int ChromeNetworkDelegate::OnBeforeSendHeaders( |
430 net::URLRequest* request, | 487 net::URLRequest* request, |
431 const net::CompletionCallback& callback, | 488 const net::CompletionCallback& callback, |
432 net::HttpRequestHeaders* headers) { | 489 net::HttpRequestHeaders* headers) { |
433 bool force_safe_search = force_google_safe_search_ && | |
434 force_google_safe_search_->GetValue(); | |
435 if (force_safe_search) | |
436 safe_search_util::ForceYouTubeSafetyMode(request, headers); | |
437 | |
438 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest"); | 490 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest"); |
439 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers); | 491 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers); |
440 } | 492 } |
441 | 493 |
442 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders( | 494 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders( |
443 net::URLRequest* request, | 495 net::URLRequest* request, |
444 const net::ProxyInfo& proxy_info, | 496 const net::ProxyInfo& proxy_info, |
445 net::HttpRequestHeaders* headers) { | 497 net::HttpRequestHeaders* headers) { |
446 if (data_reduction_proxy_auth_request_handler_) { | 498 if (data_reduction_proxy_auth_request_handler_) { |
447 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader( | 499 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader( |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 data_reduction_proxy::DataReductionProxyRequestType request_type) { | 833 data_reduction_proxy::DataReductionProxyRequestType request_type) { |
782 DCHECK_GE(received_content_length, 0); | 834 DCHECK_GE(received_content_length, 0); |
783 DCHECK_GE(original_content_length, 0); | 835 DCHECK_GE(original_content_length, 0); |
784 StoreAccumulatedContentLength(received_content_length, | 836 StoreAccumulatedContentLength(received_content_length, |
785 original_content_length, | 837 original_content_length, |
786 request_type, | 838 request_type, |
787 reinterpret_cast<Profile*>(profile_)); | 839 reinterpret_cast<Profile*>(profile_)); |
788 received_content_length_ += received_content_length; | 840 received_content_length_ += received_content_length; |
789 original_content_length_ += original_content_length; | 841 original_content_length_ += original_content_length; |
790 } | 842 } |
OLD | NEW |