1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/safe_search_util.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "chrome/common/url_constants.h"
18 #include "components/google/core/browser/google_util.h"
19 #include "net/cookies/cookie_util.h"
20 #include "net/http/http_request_headers.h"
21 #include "net/url_request/url_request.h"
26 int g_force_google_safe_search_count_for_test
= 0;
27 int g_force_youtube_safety_mode_count_for_test
= 0;
29 const char kYouTubeSafetyModeHeaderName
[] = "YouTube-Safety-Mode";
30 const char kYouTubeSafetyModeHeaderValue
[] = "Active";
32 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
33 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters
34 // must be in key=value form.
35 bool HasSameParameterKey(const std::string
& first_parameter
,
36 const std::string
& second_parameter
) {
37 DCHECK(second_parameter
.find("=") != std::string::npos
);
38 // Prefix for "foo=bar" is "foo=".
39 std::string parameter_prefix
= second_parameter
.substr(
40 0, second_parameter
.find("=") + 1);
41 return base::StartsWith(first_parameter
, parameter_prefix
,
42 base::CompareCase::INSENSITIVE_ASCII
);
45 // Examines the query string containing parameters and adds the necessary ones
46 // so that SafeSearch is active. |query| is the string to examine and the
47 // return value is the |query| string modified such that SafeSearch is active.
48 std::string
AddSafeSearchParameters(const std::string
& query
) {
49 std::vector
<std::string
> new_parameters
;
50 std::string safe_parameter
= chrome::kSafeSearchSafeParameter
;
51 std::string ssui_parameter
= chrome::kSafeSearchSsuiParameter
;
53 for (const std::string
& param
: base::SplitString(
54 query
, "&", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
)) {
55 if (!HasSameParameterKey(param
, safe_parameter
) &&
56 !HasSameParameterKey(param
, ssui_parameter
)) {
57 new_parameters
.push_back(param
);
61 new_parameters
.push_back(safe_parameter
);
62 new_parameters
.push_back(ssui_parameter
);
63 return base::JoinString(new_parameters
, "&");
68 namespace safe_search_util
{
70 // If |request| is a request to Google Web Search the function
71 // enforces that the SafeSearch query parameters are set to active.
72 // Sets the query part of |new_url| with the new value of the parameters.
73 void ForceGoogleSafeSearch(const net::URLRequest
* request
, GURL
* new_url
) {
74 ++g_force_google_safe_search_count_for_test
;
76 if (!google_util::IsGoogleSearchUrl(request
->url()) &&
77 !google_util::IsGoogleHomePageUrl(request
->url()))
80 std::string query
= request
->url().query();
81 std::string new_query
= AddSafeSearchParameters(query
);
82 if (query
== new_query
)
85 GURL::Replacements replacements
;
86 replacements
.SetQueryStr(new_query
);
87 *new_url
= request
->url().ReplaceComponents(replacements
);
90 // If |request| is a request to YouTube, enforces YouTube's Safety Mode by
91 // setting YouTube's Safety Mode header.
92 void ForceYouTubeSafetyMode(const net::URLRequest
* request
,
93 net::HttpRequestHeaders
* headers
) {
94 ++g_force_youtube_safety_mode_count_for_test
;
96 if (!google_util::IsYoutubeDomainUrl(
98 google_util::ALLOW_SUBDOMAIN
,
99 google_util::DISALLOW_NON_STANDARD_PORTS
))
102 headers
->SetHeader(kYouTubeSafetyModeHeaderName
,
103 kYouTubeSafetyModeHeaderValue
);
106 int GetForceGoogleSafeSearchCountForTesting() {
107 return g_force_google_safe_search_count_for_test
;
110 int GetForceYouTubeSafetyModeCountForTesting() {
111 return g_force_youtube_safety_mode_count_for_test
;
114 void ClearForceGoogleSafeSearchCountForTesting() {
115 g_force_google_safe_search_count_for_test
= 0;
118 void ClearForceYouTubeSafetyModeCountForTesting() {
119 g_force_youtube_safety_mode_count_for_test
= 0;
122 } // namespace safe_search_util