| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 int g_force_google_safe_search_count_for_test = 0; | 26 int g_force_google_safe_search_count_for_test = 0; |
| 27 int g_force_youtube_restrict_count_for_test = 0; | 27 int g_force_youtube_restrict_count_for_test = 0; |
| 28 | 28 |
| 29 const char kYouTubeRestrictHeaderName[] = "YouTube-Restrict"; | 29 const char kYouTubeRestrictHeaderName[] = "YouTube-Restrict"; |
| 30 const char kYouTubeRestrictHeaderValueModerate[] = "Moderate"; | 30 const char kYouTubeRestrictHeaderValueModerate[] = "Moderate"; |
| 31 const char kYouTubeRestrictHeaderValueStrict[] = "Strict"; | 31 const char kYouTubeRestrictHeaderValueStrict[] = "Strict"; |
| 32 | 32 |
| 33 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the | 33 // 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 | 34 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters |
| 35 // must be in key=value form. | 35 // must be in key=value form. |
| 36 bool HasSameParameterKey(const std::string& first_parameter, | 36 bool HasSameParameterKey(base::StringPiece first_parameter, |
| 37 const std::string& second_parameter) { | 37 base::StringPiece second_parameter) { |
| 38 DCHECK(second_parameter.find("=") != std::string::npos); | 38 DCHECK(second_parameter.find("=") != std::string::npos); |
| 39 // Prefix for "foo=bar" is "foo=". | 39 // Prefix for "foo=bar" is "foo=". |
| 40 std::string parameter_prefix = second_parameter.substr( | 40 base::StringPiece parameter_prefix = |
| 41 0, second_parameter.find("=") + 1); | 41 second_parameter.substr(0, second_parameter.find("=") + 1); |
| 42 return base::StartsWith(first_parameter, parameter_prefix, | 42 return base::StartsWith(first_parameter, parameter_prefix, |
| 43 base::CompareCase::INSENSITIVE_ASCII); | 43 base::CompareCase::INSENSITIVE_ASCII); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Examines the query string containing parameters and adds the necessary ones | 46 // Examines the query string containing parameters and adds the necessary ones |
| 47 // so that SafeSearch is active. |query| is the string to examine and the | 47 // so that SafeSearch is active. |query| is the string to examine and the |
| 48 // return value is the |query| string modified such that SafeSearch is active. | 48 // return value is the |query| string modified such that SafeSearch is active. |
| 49 std::string AddSafeSearchParameters(const std::string& query) { | 49 std::string AddSafeSearchParameters(const std::string& query) { |
| 50 std::vector<std::string> new_parameters; | 50 std::vector<base::StringPiece> new_parameters; |
| 51 std::string safe_parameter = chrome::kSafeSearchSafeParameter; | 51 std::string safe_parameter = chrome::kSafeSearchSafeParameter; |
| 52 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; | 52 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; |
| 53 | 53 |
| 54 for (const std::string& param : base::SplitString( | 54 for (const base::StringPiece& param : base::SplitStringPiece( |
| 55 query, "&", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 55 query, "&", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 56 if (!HasSameParameterKey(param, safe_parameter) && | 56 if (!HasSameParameterKey(param, safe_parameter) && |
| 57 !HasSameParameterKey(param, ssui_parameter)) { | 57 !HasSameParameterKey(param, ssui_parameter)) { |
| 58 new_parameters.push_back(param); | 58 new_parameters.push_back(param); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 new_parameters.push_back(safe_parameter); | 62 new_parameters.push_back(safe_parameter); |
| 63 new_parameters.push_back(ssui_parameter); | 63 new_parameters.push_back(ssui_parameter); |
| 64 return base::JoinString(new_parameters, "&"); | 64 return base::JoinString(new_parameters, "&"); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 | 127 |
| 128 void ClearForceGoogleSafeSearchCountForTesting() { | 128 void ClearForceGoogleSafeSearchCountForTesting() { |
| 129 g_force_google_safe_search_count_for_test = 0; | 129 g_force_google_safe_search_count_for_test = 0; |
| 130 } | 130 } |
| 131 | 131 |
| 132 void ClearForceYouTubeRestrictCountForTesting() { | 132 void ClearForceYouTubeRestrictCountForTesting() { |
| 133 g_force_youtube_restrict_count_for_test = 0; | 133 g_force_youtube_restrict_count_for_test = 0; |
| 134 } | 134 } |
| 135 | 135 |
| 136 } // namespace safe_search_util | 136 } // namespace safe_search_util |
| OLD | NEW |