Chromium Code Reviews| Index: chrome/browser/net/chrome_network_delegate.cc |
| diff --git a/chrome/browser/net/chrome_network_delegate.cc b/chrome/browser/net/chrome_network_delegate.cc |
| index f34396f5f34e6b33d0ed775d1ecec5572d5511d2..444e611e53f8ab6152a582971a577521b7fc60e1 100644 |
| --- a/chrome/browser/net/chrome_network_delegate.cc |
| +++ b/chrome/browser/net/chrome_network_delegate.cc |
| @@ -4,9 +4,13 @@ |
| #include "chrome/browser/net/chrome_network_delegate.h" |
| +#include <string> |
| +#include <vector> |
| + |
| #include "base/logging.h" |
| #include "base/base_paths.h" |
| #include "base/path_service.h" |
| +#include "base/string_split.h" |
| #include "chrome/browser/api/prefs/pref_member.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/content_settings/cookie_settings.h" |
| @@ -17,6 +21,7 @@ |
| #include "chrome/browser/extensions/event_router_forwarder.h" |
| #include "chrome/browser/extensions/extension_info_map.h" |
| #include "chrome/browser/extensions/extension_process_manager.h" |
| +#include "chrome/browser/google/google_util.h" |
| #include "chrome/browser/net/load_time_stats.h" |
| #include "chrome/browser/performance_monitor/performance_monitor.h" |
| #include "chrome/browser/prefs/pref_service.h" |
| @@ -87,6 +92,77 @@ void ForwardProxyErrors(net::URLRequest* request, |
| } |
| } |
| +// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the |
| +// same key as the the |second_parameter| (e.g. foo=baz). |
| +bool HasSameParameterKey(const std::string& first_parameter, |
| + const std::string& second_parameter) { |
| + // We set the second parameter so it must have "=" in it. |
|
Pam (message me for reviews)
2012/10/31 13:50:35
I don't follow this comment. Is it making assumpti
Sergiu
2012/11/01 18:01:52
Rephrased it such that it is hopefully clearer now
|
| + DCHECK(second_parameter.find("=") != std::string::npos); |
| + // Prefix for "foo=bar" is "foo=". |
| + std::string parameter_prefix = second_parameter.substr( |
| + 0, second_parameter.find("=") + 1); |
| + return StartsWithASCII(first_parameter, parameter_prefix, false); |
| +} |
| + |
| +// Examines the query and adds the necessary parameters so that SafeSearch is |
|
battre
2012/10/31 13:16:55
nit: Examines the query parameter of a URL request
Sergiu
2012/11/01 18:01:52
Done, rephrased.
|
| +// active. |query| is the string to examine and the return value is the |
| +// |query| string modified such that SafeSearch is active. |
| +std::string AddSafeSearchParameters(const std::string& query) { |
| + std::vector<std::string> new_parameters; |
| + std::string safe_parameter = chrome::kSafeSearchSafeParameter; |
| + std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; |
|
Pam (message me for reviews)
2012/10/31 13:50:35
Any particular reason you allocate these again, as
battre
2012/10/31 14:06:08
The strings are const char[] contants. You would i
Pam (message me for reviews)
2012/11/02 14:00:35
On 2012/10/31 14:06:08, battre wrote:
|
| + |
| + std::vector<std::string> parameters; |
| + base::SplitString(query, '&', ¶meters); |
| + |
| + std::vector<std::string>::iterator it; |
| + for (it = parameters.begin(); it < parameters.end(); ++it) { |
| + if (!HasSameParameterKey(*it, safe_parameter) && |
| + !HasSameParameterKey(*it, ssui_parameter)) { |
| + new_parameters.push_back(*it); |
| + } |
| + } |
| + |
| + new_parameters.push_back(safe_parameter); |
| + new_parameters.push_back(ssui_parameter); |
| + return JoinString(new_parameters, '&'); |
| +} |
| + |
| +// Examines |request| and if it is a request to Google Web Search |
| +// it enforces that the SafeSearch query parameters are set to active. |
|
Pam (message me for reviews)
2012/10/31 13:50:35
nit: drop "it" in "it enforces", and add a comma a
Sergiu
2012/11/01 18:01:52
Rephrased.
|
| +// Sets the query part of |new_url| with the new value of the parameters. |
| +void ForceGoogleSafeSearch(net::URLRequest* request, |
| + GURL* new_url) { |
| + const bool is_search_url = google_util::IsGoogleSearchUrl( |
| + request->url().spec()); |
| + const bool is_homepage_url = google_util::IsGoogleHomePageUrl( |
| + request->url().spec()); |
|
battre
2012/10/31 13:16:55
how about moving lines 136 to 139 into google_util
Sergiu
2012/11/01 18:01:52
Done.
|
| + if (is_search_url || is_homepage_url) { |
| + std::string query = request->url().query(); |
| + std::string new_query = AddSafeSearchParameters(query); |
| + if (query == new_query) |
| + return; |
| + |
| + GURL::Replacements replacements; |
| + replacements.SetQueryStr(new_query); |
| + *new_url = request->url().ReplaceComponents(replacements); |
| + } |
| +} |
| + |
| +// Gets called when the extensions finish work on the URL. If the extensions |
| +// did not do a redirect (so |new_url| is empty) then we enforce the |
| +// SafeSearch parameters. Otherwise we will get called again after the |
| +// redirect and we enforce SafeSearch then. |
| +void ForceGoogleSafeSearchCallbackWrapper( |
| + const net::CompletionCallback& callback, |
| + net::URLRequest* request, |
| + GURL* new_url, |
| + int rv) { |
| + if (rv == net::OK && new_url->is_empty()) |
| + ForceGoogleSafeSearch(request, new_url); |
| + callback.Run(rv); |
| +} |
| + |
| enum RequestStatus { REQUEST_STARTED, REQUEST_DONE }; |
| // Notifies the ExtensionProcessManager that a request has started or stopped |
| @@ -146,6 +222,7 @@ ChromeNetworkDelegate::ChromeNetworkDelegate( |
| CookieSettings* cookie_settings, |
| BooleanPrefMember* enable_referrers, |
| BooleanPrefMember* enable_do_not_track, |
| + BooleanPrefMember* force_google_safe_search, |
| chrome_browser_net::LoadTimeStats* load_time_stats) |
| : event_router_(event_router), |
| profile_(profile), |
| @@ -153,6 +230,7 @@ ChromeNetworkDelegate::ChromeNetworkDelegate( |
| extension_info_map_(extension_info_map), |
| enable_referrers_(enable_referrers), |
| enable_do_not_track_(enable_do_not_track), |
| + force_google_safe_search_(force_google_safe_search), |
| url_blacklist_manager_(url_blacklist_manager), |
| managed_mode_url_filter_(managed_mode_url_filter), |
| load_time_stats_(load_time_stats) { |
| @@ -172,6 +250,7 @@ void ChromeNetworkDelegate::NeverThrottleRequests() { |
| void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
| BooleanPrefMember* enable_referrers, |
| BooleanPrefMember* enable_do_not_track, |
| + BooleanPrefMember* force_google_safe_search, |
| PrefService* pref_service) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| enable_referrers->Init(prefs::kEnableReferrers, pref_service, NULL); |
| @@ -180,6 +259,10 @@ void ChromeNetworkDelegate::InitializePrefsOnUIThread( |
| enable_do_not_track->Init(prefs::kEnableDoNotTrack, pref_service, NULL); |
| enable_do_not_track->MoveToThread(BrowserThread::IO); |
| } |
| + if (force_google_safe_search) { |
| + force_google_safe_search->Init(prefs::kForceSafeSearch, pref_service, NULL); |
| + force_google_safe_search->MoveToThread(BrowserThread::IO); |
| + } |
| } |
| // static |
| @@ -220,8 +303,26 @@ int ChromeNetworkDelegate::OnBeforeURLRequest( |
| request->set_referrer(std::string()); |
| if (enable_do_not_track_ && enable_do_not_track_->GetValue()) |
| request->SetExtraRequestHeaderByName(kDNTHeader, "1", true /* override */); |
| - return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( |
| - profile_, extension_info_map_.get(), request, callback, new_url); |
| + |
| + bool force_safe_search = force_google_safe_search_ && |
| + force_google_safe_search_->GetValue(); |
| + |
| + net::CompletionCallback wrapped_callback = callback; |
| + if (force_safe_search) { |
| + wrapped_callback = base::Bind(&ForceGoogleSafeSearchCallbackWrapper, |
| + callback, |
| + base::Unretained(request), |
| + base::Unretained(new_url)); |
| + } |
| + |
| + int rv = ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest( |
| + profile_, extension_info_map_.get(), request, wrapped_callback, |
| + new_url); |
| + |
| + if (force_safe_search && rv == net::OK && new_url->is_empty()) |
| + ForceGoogleSafeSearch(request, new_url); |
| + |
| + return rv; |
| } |
| int ChromeNetworkDelegate::OnBeforeSendHeaders( |