-// Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
-// same key as the the |second_parameter| (e.g. foo=baz). Both parameters
-// must be in key=value form.
-bool HasSameParameterKey(const std::string& first_parameter,
- const std::string& second_parameter) {
- 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 string containing parameters and adds the necessary ones
-// so that SafeSearch is 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;
-
- 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, '&');
-}
-
-// If |request| is a request to Google Web Search the function
-// enforces that the SafeSearch query parameters are set to active.
-// Sets the query part of |new_url| with the new value of the parameters.
-void ForceGoogleSafeSearch(net::URLRequest* request,
- GURL* new_url) {
- if (!google_util::IsGoogleSearchUrl(request->url()) &&
- !google_util::IsGoogleHomePageUrl(request->url()))
- return;
-
- 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);
-}
-