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

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

Issue 354183002: Enforce SafetyMode for YouTube if prefs::kForceSafeSearch is on. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: revert string_util.h change 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 | Annotate | Revision Log
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 "extensions/common/constants.h" 43 #include "extensions/common/constants.h"
46 #include "net/base/host_port_pair.h" 44 #include "net/base/host_port_pair.h"
47 #include "net/base/net_errors.h" 45 #include "net/base/net_errors.h"
48 #include "net/base/net_log.h" 46 #include "net/base/net_log.h"
49 #include "net/cookies/canonical_cookie.h" 47 #include "net/cookies/canonical_cookie.h"
50 #include "net/cookies/cookie_options.h" 48 #include "net/cookies/cookie_options.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 #endif 84 #endif
87 85
88 // This remains false unless the --disable-extensions-http-throttling 86 // This remains false unless the --disable-extensions-http-throttling
89 // flag is passed to the browser. 87 // flag is passed to the browser.
90 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false; 88 bool ChromeNetworkDelegate::g_never_throttle_requests_ = false;
91 89
92 namespace { 90 namespace {
93 91
94 const char kDNTHeader[] = "DNT"; 92 const char kDNTHeader[] = "DNT";
95 93
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, '&', &parameters);
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
151 // Gets called when the extensions finish work on the URL. If the extensions 94 // Gets called when the extensions finish work on the URL. If the extensions
152 // did not do a redirect (so |new_url| is empty) then we enforce the 95 // did not do a redirect (so |new_url| is empty) then we enforce the
153 // SafeSearch parameters. Otherwise we will get called again after the 96 // SafeSearch parameters. Otherwise we will get called again after the
154 // redirect and we enforce SafeSearch then. 97 // redirect and we enforce SafeSearch then.
155 void ForceGoogleSafeSearchCallbackWrapper( 98 void ForceGoogleSafeSearchCallbackWrapper(
156 const net::CompletionCallback& callback, 99 const net::CompletionCallback& callback,
157 net::URLRequest* request, 100 net::URLRequest* request,
158 GURL* new_url, 101 GURL* new_url,
159 int rv) { 102 int rv) {
160 if (rv == net::OK && new_url->is_empty()) 103 if (rv == net::OK && new_url->is_empty())
161 ForceGoogleSafeSearch(request, new_url); 104 safe_search_util::ForceGoogleSafeSearch(request, new_url);
162 callback.Run(rv); 105 callback.Run(rv);
163 } 106 }
164 107
165 void UpdateContentLengthPrefs( 108 void UpdateContentLengthPrefs(
166 int received_content_length, 109 int received_content_length,
167 int original_content_length, 110 int original_content_length,
168 data_reduction_proxy::DataReductionProxyRequestType request_type, 111 data_reduction_proxy::DataReductionProxyRequestType request_type,
169 Profile* profile) { 112 Profile* profile) {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
171 DCHECK_GE(received_content_length, 0); 114 DCHECK_GE(received_content_length, 0);
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, 403 wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper,
461 callback, 404 callback,
462 base::Unretained(request), 405 base::Unretained(request),
463 base::Unretained(new_url)); 406 base::Unretained(new_url));
464 } 407 }
465 408
466 int rv = extensions_delegate_->OnBeforeURLRequest( 409 int rv = extensions_delegate_->OnBeforeURLRequest(
467 request, wrapped_callback, new_url); 410 request, wrapped_callback, new_url);
468 411
469 if (force_safe_search && rv == net::OK && new_url->is_empty()) 412 if (force_safe_search && rv == net::OK && new_url->is_empty())
470 ForceGoogleSafeSearch(request, new_url); 413 safe_search_util::ForceGoogleSafeSearch(request, new_url);
471 414
472 if (connect_interceptor_) 415 if (connect_interceptor_)
473 connect_interceptor_->WitnessURLRequest(request); 416 connect_interceptor_->WitnessURLRequest(request);
474 417
475 return rv; 418 return rv;
476 } 419 }
477 420
478 void ChromeNetworkDelegate::OnResolveProxy( 421 void ChromeNetworkDelegate::OnResolveProxy(
479 const GURL& url, int load_flags, net::ProxyInfo* result) { 422 const GURL& url, int load_flags, net::ProxyInfo* result) {
480 if (!on_resolve_proxy_handler_.is_null()) { 423 if (!on_resolve_proxy_handler_.is_null()) {
481 on_resolve_proxy_handler_.Run(url, load_flags, 424 on_resolve_proxy_handler_.Run(url, load_flags,
482 data_reduction_proxy_params_, result); 425 data_reduction_proxy_params_, result);
483 } 426 }
484 } 427 }
485 428
486 int ChromeNetworkDelegate::OnBeforeSendHeaders( 429 int ChromeNetworkDelegate::OnBeforeSendHeaders(
487 net::URLRequest* request, 430 net::URLRequest* request,
488 const net::CompletionCallback& callback, 431 const net::CompletionCallback& callback,
489 net::HttpRequestHeaders* headers) { 432 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
490 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest"); 438 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request, "SendRequest");
491 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers); 439 return extensions_delegate_->OnBeforeSendHeaders(request, callback, headers);
492 } 440 }
493 441
494 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders( 442 void ChromeNetworkDelegate::OnBeforeSendProxyHeaders(
495 net::URLRequest* request, 443 net::URLRequest* request,
496 const net::ProxyInfo& proxy_info, 444 const net::ProxyInfo& proxy_info,
497 net::HttpRequestHeaders* headers) { 445 net::HttpRequestHeaders* headers) {
498 if (data_reduction_proxy_auth_request_handler_) { 446 if (data_reduction_proxy_auth_request_handler_) {
499 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader( 447 data_reduction_proxy_auth_request_handler_->MaybeAddRequestHeader(
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 data_reduction_proxy::DataReductionProxyRequestType request_type) { 781 data_reduction_proxy::DataReductionProxyRequestType request_type) {
834 DCHECK_GE(received_content_length, 0); 782 DCHECK_GE(received_content_length, 0);
835 DCHECK_GE(original_content_length, 0); 783 DCHECK_GE(original_content_length, 0);
836 StoreAccumulatedContentLength(received_content_length, 784 StoreAccumulatedContentLength(received_content_length,
837 original_content_length, 785 original_content_length,
838 request_type, 786 request_type,
839 reinterpret_cast<Profile*>(profile_)); 787 reinterpret_cast<Profile*>(profile_));
840 received_content_length_ += received_content_length; 788 received_content_length_ += received_content_length;
841 original_content_length_ += original_content_length; 789 original_content_length_ += original_content_length;
842 } 790 }
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