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 StartsWithASCII(first_parameter
, parameter_prefix
, false);
44 // Examines the query string containing parameters and adds the necessary ones
45 // so that SafeSearch is active. |query| is the string to examine and the
46 // return value is the |query| string modified such that SafeSearch is active.
47 std::string
AddSafeSearchParameters(const std::string
& query
) {
48 std::vector
<std::string
> new_parameters
;
49 std::string safe_parameter
= chrome::kSafeSearchSafeParameter
;
50 std::string ssui_parameter
= chrome::kSafeSearchSsuiParameter
;
52 std::vector
<std::string
> parameters
;
53 base::SplitString(query
, '&', ¶meters
);
55 std::vector
<std::string
>::iterator it
;
56 for (it
= parameters
.begin(); it
< parameters
.end(); ++it
) {
57 if (!HasSameParameterKey(*it
, safe_parameter
) &&
58 !HasSameParameterKey(*it
, ssui_parameter
)) {
59 new_parameters
.push_back(*it
);
63 new_parameters
.push_back(safe_parameter
);
64 new_parameters
.push_back(ssui_parameter
);
65 return JoinString(new_parameters
, '&');
70 namespace safe_search_util
{
72 // If |request| is a request to Google Web Search the function
73 // enforces that the SafeSearch query parameters are set to active.
74 // Sets the query part of |new_url| with the new value of the parameters.
75 void ForceGoogleSafeSearch(const net::URLRequest
* request
, GURL
* new_url
) {
76 ++g_force_google_safe_search_count_for_test
;
78 if (!google_util::IsGoogleSearchUrl(request
->url()) &&
79 !google_util::IsGoogleHomePageUrl(request
->url()))
82 std::string query
= request
->url().query();
83 std::string new_query
= AddSafeSearchParameters(query
);
84 if (query
== new_query
)
87 GURL::Replacements replacements
;
88 replacements
.SetQueryStr(new_query
);
89 *new_url
= request
->url().ReplaceComponents(replacements
);
92 // If |request| is a request to YouTube, enforces YouTube's Safety Mode by
93 // setting YouTube's Safety Mode header.
94 void ForceYouTubeSafetyMode(const net::URLRequest
* request
,
95 net::HttpRequestHeaders
* headers
) {
96 ++g_force_youtube_safety_mode_count_for_test
;
98 if (!google_util::IsYoutubeDomainUrl(
100 google_util::ALLOW_SUBDOMAIN
,
101 google_util::DISALLOW_NON_STANDARD_PORTS
))
104 headers
->SetHeader(kYouTubeSafetyModeHeaderName
,
105 kYouTubeSafetyModeHeaderValue
);
108 int GetForceGoogleSafeSearchCountForTesting() {
109 return g_force_google_safe_search_count_for_test
;
112 int GetForceYouTubeSafetyModeCountForTesting() {
113 return g_force_youtube_safety_mode_count_for_test
;
116 void ClearForceGoogleSafeSearchCountForTesting() {
117 g_force_google_safe_search_count_for_test
= 0;
120 void ClearForceYouTubeSafetyModeCountForTesting() {
121 g_force_youtube_safety_mode_count_for_test
= 0;
124 } // namespace safe_search_util