| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/safe_search_util.h" | 5 #include "chrome/browser/net/safe_search_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "components/google/core/browser/google_util.h" | 19 #include "components/google/core/browser/google_util.h" |
| 20 #include "net/cookies/cookie_util.h" | 20 #include "net/cookies/cookie_util.h" |
| 21 #include "net/http/http_request_headers.h" | 21 #include "net/http/http_request_headers.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 int g_force_google_safe_search_count_for_test = 0; | 27 int g_force_google_safe_search_count_for_test = 0; |
| 28 int g_force_youtube_safety_mode_count_for_test = 0; | 28 int g_force_youtube_restrict_count_for_test = 0; |
| 29 | 29 |
| 30 const char kYouTubeSafetyModeHeaderName[] = "YouTube-Safety-Mode"; | 30 const char kYouTubeRestrictHeaderName[] = "YouTube-Restrict"; |
| 31 const char kYouTubeSafetyModeHeaderValue[] = "Active"; | 31 const char kYouTubeRestrictHeaderValueModerate[] = "Moderate"; |
| 32 const char kYouTubeRestrictHeaderValueStrict[] = "Strict"; |
| 32 | 33 |
| 33 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the | 34 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the |
| 34 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters | 35 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters |
| 35 // must be in key=value form. | 36 // must be in key=value form. |
| 36 bool HasSameParameterKey(const std::string& first_parameter, | 37 bool HasSameParameterKey(const std::string& first_parameter, |
| 37 const std::string& second_parameter) { | 38 const std::string& second_parameter) { |
| 38 DCHECK(second_parameter.find("=") != std::string::npos); | 39 DCHECK(second_parameter.find("=") != std::string::npos); |
| 39 // Prefix for "foo=bar" is "foo=". | 40 // Prefix for "foo=bar" is "foo=". |
| 40 std::string parameter_prefix = second_parameter.substr( | 41 std::string parameter_prefix = second_parameter.substr( |
| 41 0, second_parameter.find("=") + 1); | 42 0, second_parameter.find("=") + 1); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 std::string query = request->url().query(); | 82 std::string query = request->url().query(); |
| 82 std::string new_query = AddSafeSearchParameters(query); | 83 std::string new_query = AddSafeSearchParameters(query); |
| 83 if (query == new_query) | 84 if (query == new_query) |
| 84 return; | 85 return; |
| 85 | 86 |
| 86 GURL::Replacements replacements; | 87 GURL::Replacements replacements; |
| 87 replacements.SetQueryStr(new_query); | 88 replacements.SetQueryStr(new_query); |
| 88 *new_url = request->url().ReplaceComponents(replacements); | 89 *new_url = request->url().ReplaceComponents(replacements); |
| 89 } | 90 } |
| 90 | 91 |
| 91 // If |request| is a request to YouTube, enforces YouTube's Safety Mode by | 92 void ForceYouTubeRestrict(const net::URLRequest* request, |
| 92 // setting YouTube's Safety Mode header. | 93 net::HttpRequestHeaders* headers, |
| 93 void ForceYouTubeSafetyMode(const net::URLRequest* request, | 94 YouTubeRestrictMode mode) { |
| 94 net::HttpRequestHeaders* headers) { | 95 ++g_force_youtube_restrict_count_for_test; |
| 95 ++g_force_youtube_safety_mode_count_for_test; | |
| 96 | 96 |
| 97 if (!google_util::IsYoutubeDomainUrl( | 97 if (!google_util::IsYoutubeDomainUrl( |
| 98 request->url(), | 98 request->url(), |
| 99 google_util::ALLOW_SUBDOMAIN, | 99 google_util::ALLOW_SUBDOMAIN, |
| 100 google_util::DISALLOW_NON_STANDARD_PORTS)) | 100 google_util::DISALLOW_NON_STANDARD_PORTS)) |
| 101 return; | 101 return; |
| 102 | 102 |
| 103 headers->SetHeader(kYouTubeSafetyModeHeaderName, | 103 switch (mode) { |
| 104 kYouTubeSafetyModeHeaderValue); | 104 case YOUTUBE_RESTRICT_OFF: |
| 105 // 'Off' wipes an existing header if it exists |
| 106 headers->RemoveHeader(kYouTubeRestrictHeaderName); |
| 107 return; |
| 108 |
| 109 case YOUTUBE_RESTRICT_MODERATE: |
| 110 headers->SetHeader(kYouTubeRestrictHeaderName, |
| 111 kYouTubeRestrictHeaderValueModerate); |
| 112 break; |
| 113 |
| 114 case YOUTUBE_RESTRICT_STRICT: |
| 115 headers->SetHeader(kYouTubeRestrictHeaderName, |
| 116 kYouTubeRestrictHeaderValueStrict); |
| 117 break; |
| 118 } |
| 105 } | 119 } |
| 106 | 120 |
| 107 int GetForceGoogleSafeSearchCountForTesting() { | 121 int GetForceGoogleSafeSearchCountForTesting() { |
| 108 return g_force_google_safe_search_count_for_test; | 122 return g_force_google_safe_search_count_for_test; |
| 109 } | 123 } |
| 110 | 124 |
| 111 int GetForceYouTubeSafetyModeCountForTesting() { | 125 int GetForceYouTubeRestrictCountForTesting() { |
| 112 return g_force_youtube_safety_mode_count_for_test; | 126 return g_force_youtube_restrict_count_for_test; |
| 113 } | 127 } |
| 114 | 128 |
| 115 void ClearForceGoogleSafeSearchCountForTesting() { | 129 void ClearForceGoogleSafeSearchCountForTesting() { |
| 116 g_force_google_safe_search_count_for_test = 0; | 130 g_force_google_safe_search_count_for_test = 0; |
| 117 } | 131 } |
| 118 | 132 |
| 119 void ClearForceYouTubeSafetyModeCountForTesting() { | 133 void ClearForceYouTubeRestrictCountForTesting() { |
| 120 g_force_youtube_safety_mode_count_for_test = 0; | 134 g_force_youtube_restrict_count_for_test = 0; |
| 121 } | 135 } |
| 122 | 136 |
| 123 } // namespace safe_search_util | 137 } // namespace safe_search_util |
| OLD | NEW |