Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/browser/net/chrome_network_delegate.cc

Issue 388823002: Enforce SafetyMode for YouTube if prefs::kForceSafeSearch is on. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
20 #include "base/time/time.h" 19 #include "base/time/time.h"
21 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/content_settings/cookie_settings.h" 21 #include "chrome/browser/content_settings/cookie_settings.h"
23 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 22 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
24 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 23 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
25 #include "chrome/browser/net/chrome_extensions_network_delegate.h" 24 #include "chrome/browser/net/chrome_extensions_network_delegate.h"
26 #include "chrome/browser/net/client_hints.h" 25 #include "chrome/browser/net/client_hints.h"
27 #include "chrome/browser/net/connect_interceptor.h" 26 #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"
34 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ est_handler.h" 33 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ est_handler.h"
35 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h " 34 #include "components/data_reduction_proxy/browser/data_reduction_proxy_metrics.h "
36 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" 35 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
37 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol. h" 36 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol. h"
38 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta ts.h" 37 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta ts.h"
39 #include "components/domain_reliability/monitor.h" 38 #include "components/domain_reliability/monitor.h"
40 #include "components/google/core/browser/google_util.h"
41 #include "content/public/browser/browser_thread.h" 39 #include "content/public/browser/browser_thread.h"
42 #include "content/public/browser/render_frame_host.h" 40 #include "content/public/browser/render_frame_host.h"
43 #include "content/public/browser/render_view_host.h" 41 #include "content/public/browser/render_view_host.h"
44 #include "content/public/browser/resource_request_info.h" 42 #include "content/public/browser/resource_request_info.h"
45 #include "net/base/host_port_pair.h" 43 #include "net/base/host_port_pair.h"
46 #include "net/base/net_errors.h" 44 #include "net/base/net_errors.h"
47 #include "net/base/net_log.h" 45 #include "net/base/net_log.h"
48 #include "net/cookies/canonical_cookie.h" 46 #include "net/cookies/canonical_cookie.h"
49 #include "net/cookies/cookie_options.h" 47 #include "net/cookies/cookie_options.h"
50 #include "net/http/http_request_headers.h" 48 #include "net/http/http_request_headers.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 #if defined(ENABLE_EXTENSIONS) 89 #if defined(ENABLE_EXTENSIONS)
92 // This remains false unless the --disable-extensions-http-throttling 90 // This remains false unless the --disable-extensions-http-throttling
93 // flag is passed to the browser. 91 // flag is passed to the browser.
94 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; 92 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
95 #endif 93 #endif
96 94
97 namespace { 95 namespace {
98 96
99 const char kDNTHeader[] = "DNT"; 97 const char kDNTHeader[] = "DNT";
100 98
101 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
102 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters
103 // must be in key=value form.
104 bool HasSameParameterKey(const std::string& first_parameter,
105 const std::string& second_parameter) {
106 DCHECK(second_parameter.find("=") != std::string::npos);
107 // Prefix for "foo=bar" is "foo=".
108 std::string parameter_prefix = second_parameter.substr(
109 0, second_parameter.find("=") + 1);
110 return StartsWithASCII(first_parameter, parameter_prefix, false);
111 }
112
113 // Examines the query string containing parameters and adds the necessary ones
114 // so that SafeSearch is active. |query| is the string to examine and the
115 // return value is the |query| string modified such that SafeSearch is active.
116 std::string AddSafeSearchParameters(const std::string& query) {
117 std::vector<std::string> new_parameters;
118 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
119 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
120
121 std::vector<std::string> parameters;
122 base::SplitString(query, '&', &parameters);
123
124 std::vector<std::string>::iterator it;
125 for (it = parameters.begin(); it < parameters.end(); ++it) {
126 if (!HasSameParameterKey(*it, safe_parameter) &&
127 !HasSameParameterKey(*it, ssui_parameter)) {
128 new_parameters.push_back(*it);
129 }
130 }
131
132 new_parameters.push_back(safe_parameter);
133 new_parameters.push_back(ssui_parameter);
134 return JoinString(new_parameters, '&');
135 }
136
137 // If |request| is a request to Google Web Search the function
138 // enforces that the SafeSearch query parameters are set to active.
139 // Sets the query part of |new_url| with the new value of the parameters.
140 void ForceGoogleSafeSearch(net::URLRequest* request,
141 GURL* new_url) {
142 if (!google_util::IsGoogleSearchUrl(request->url()) &&
143 !google_util::IsGoogleHomePageUrl(request->url()))
144 return;
145
146 std::string query = request->url().query();
147 std::string new_query = AddSafeSearchParameters(query);
148 if (query == new_query)
149 return;
150
151 GURL::Replacements replacements;
152 replacements.SetQueryStr(new_query);
153 *new_url = request->url().ReplaceComponents(replacements);
154 }
155
156 // Gets called when the extensions finish work on the URL. If the extensions 99 // Gets called when the extensions finish work on the URL. If the extensions
157 // did not do a redirect (so |new_url| is empty) then we enforce the 100 // did not do a redirect (so |new_url| is empty) then we enforce the
158 // SafeSearch parameters. Otherwise we will get called again after the 101 // SafeSearch parameters. Otherwise we will get called again after the
159 // redirect and we enforce SafeSearch then. 102 // redirect and we enforce SafeSearch then.
160 void ForceGoogleSafeSearchCallbackWrapper( 103 void ForceGoogleSafeSearchCallbackWrapper(
161 const net::CompletionCallback& callback, 104 const net::CompletionCallback& callback,
162 net::URLRequest* request, 105 net::URLRequest* request,
163 GURL* new_url, 106 GURL* new_url,
164 int rv) { 107 int rv) {
165 if (rv == net::OK && new_url->is_empty()) 108 if (rv == net::OK && new_url->is_empty())
166 ForceGoogleSafeSearch(request, new_url); 109 safe_search_util::ForceGoogleSafeSearch(request, new_url);
167 callback.Run(rv); 110 callback.Run(rv);
168 } 111 }
169 112
170 void UpdateContentLengthPrefs( 113 void UpdateContentLengthPrefs(
171 int received_content_length, 114 int received_content_length,
172 int original_content_length, 115 int original_content_length,
173 data_reduction_proxy::DataReductionProxyRequestType request_type, 116 data_reduction_proxy::DataReductionProxyRequestType request_type,
174 Profile* profile) { 117 Profile* profile) {
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
176 DCHECK_GE(received_content_length, 0); 119 DCHECK_GE(received_content_length, 0);
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, 410 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
468 callback, 411 callback,
469 base::Unretained(request), 412 base::Unretained(request),
470 base::Unretained(new_url)); 413 base::Unretained(new_url));
471 } 414 }
472 415
473 int rv = extensions_delegate_->OnBeforeURLRequest( 416 int rv = extensions_delegate_->OnBeforeURLRequest(
474 request, wrapped_callback, new_url); 417 request, wrapped_callback, new_url);
475 418
476 if (force_safe_search && rv == net::OK && new_url->is_empty()) 419 if (force_safe_search && rv == net::OK && new_url->is_empty())
477 ForceGoogleSafeSearch(request, new_url); 420 safe_search_util::ForceGoogleSafeSearch(request, new_url);
478 421
479 if (connect_interceptor_) 422 if (connect_interceptor_)
480 connect_interceptor_->WitnessURLRequest(request); 423 connect_interceptor_->WitnessURLRequest(request);
481 424
482 return rv; 425 return rv;
483 } 426 }
484 427
485 void ChromeNetworkDelegate::OnResolveProxy( 428 void ChromeNetworkDelegate::OnResolveProxy(
486 const GURL& url, int load_flags, net::ProxyInfo* result) { 429 const GURL& url, int load_flags, net::ProxyInfo* result) {
487 if (!on_resolve_proxy_handler_.is_null()) { 430 if (!on_resolve_proxy_handler_.is_null()) {
488 on_resolve_proxy_handler_.Run(url, load_flags, 431 on_resolve_proxy_handler_.Run(url, load_flags,
489 data_reduction_proxy_params_, result); 432 data_reduction_proxy_params_, result);
490 } 433 }
491 } 434 }
492 435
493 int ChromeNetworkDelegate::OnBeforeSendHeaders( 436 int ChromeNetworkDelegate::OnBeforeSendHeaders(
494 net::URLRequest* request, 437 net::URLRequest* request,
495 const net::CompletionCallback& callback, 438 const net::CompletionCallback& callback,
496 net::HttpRequestHeaders* headers) { 439 net::HttpRequestHeaders* headers) {
440 bool force_safe_search = force_google_safe_search_ &&
441 force_google_safe_search_->GetValue();
442 if (force_safe_search)
443 safe_search_util::ForceYouTubeSafetyMode(request, headers);
444
497 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest"); 445 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
498 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers); 446 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
499 } 447 }
500 448
501 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders( 449 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders(
502 net::URLRequest* request, 450 net::URLRequest* request,
503 const net::ProxyInfo& proxy_info, 451 const net::ProxyInfo& proxy_info,
504 net::HttpRequestHeaders* headers) { 452 net::HttpRequestHeaders* headers) {
505 if (data_reduction_proxy_auth_request_handler_) { 453 if (data_reduction_proxy_auth_request_handler_) {
506 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader( 454 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader(
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 data_reduction_proxy::DataReductionProxyRequestType request_type) { 792 data_reduction_proxy::DataReductionProxyRequestType request_type) {
845 DCHECK_GE(received_content_length, 0); 793 DCHECK_GE(received_content_length, 0);
846 DCHECK_GE(original_content_length, 0); 794 DCHECK_GE(original_content_length, 0);
847 StoreAccumulatedContentLength(received_content_length, 795 StoreAccumulatedContentLength(received_content_length,
848 original_content_length, 796 original_content_length,
849 request_type, 797 request_type,
850 reinterpret_cast<Profile*>(profile_)); 798 reinterpret_cast<Profile*>(profile_));
851 received_content_length_ += received_content_length; 799 received_content_length_ += received_content_length;
852 original_content_length_ += original_content_length; 800 original_content_length_ += original_content_length;
853 } 801 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/web_request/web_request_api_helpers.cc ('k') | chrome/browser/net/safe_search_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698