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 // If |request| is a request to YouTube, enforces YouTube's Restrict mode by |
92 // setting YouTube's Safety Mode header. | 93 // setting YouTube's Restrict header. For backwards compatibility, also |
93 void ForceYouTubeSafetyMode(const net::URLRequest* request, | 94 // sets the Safety Mode header. Setting |YTRM_OFF| removes a Restrict header |
Thiemo Nagel
2016/08/12 12:12:20
I thought you're removing the safety mode header?
ljusten (tachyonic)
2016/08/16 09:24:54
Plans change, old comments stay... Reformulated th
| |
94 net::HttpRequestHeaders* headers) { | 95 // if it exists. |
95 ++g_force_youtube_safety_mode_count_for_test; | 96 void ForceYouTubeRestrict(const net::URLRequest* request, |
97 net::HttpRequestHeaders* headers, | |
98 YouTubeRestrictMode mode) { | |
99 ++g_force_youtube_restrict_count_for_test; | |
96 | 100 |
97 if (!google_util::IsYoutubeDomainUrl( | 101 if (!google_util::IsYoutubeDomainUrl( |
98 request->url(), | 102 request->url(), |
99 google_util::ALLOW_SUBDOMAIN, | 103 google_util::ALLOW_SUBDOMAIN, |
100 google_util::DISALLOW_NON_STANDARD_PORTS)) | 104 google_util::DISALLOW_NON_STANDARD_PORTS)) |
101 return; | 105 return; |
102 | 106 |
103 headers->SetHeader(kYouTubeSafetyModeHeaderName, | 107 switch (mode) { |
104 kYouTubeSafetyModeHeaderValue); | 108 case YTRM_OFF: |
109 // 'Off' wipes an existing header if it exists | |
110 headers->RemoveHeader(kYouTubeRestrictHeaderName); | |
111 return; | |
112 | |
113 case YTRM_MODERATE: | |
114 headers->SetHeader(kYouTubeRestrictHeaderName, | |
115 kYouTubeRestrictHeaderValueModerate); | |
116 break; | |
117 | |
118 case YTRM_STRICT: | |
119 headers->SetHeader(kYouTubeRestrictHeaderName, | |
120 kYouTubeRestrictHeaderValueStrict); | |
121 break; | |
122 | |
123 default: | |
124 NOTREACHED(); | |
125 } | |
105 } | 126 } |
106 | 127 |
107 int GetForceGoogleSafeSearchCountForTesting() { | 128 int GetForceGoogleSafeSearchCountForTesting() { |
108 return g_force_google_safe_search_count_for_test; | 129 return g_force_google_safe_search_count_for_test; |
109 } | 130 } |
110 | 131 |
111 int GetForceYouTubeSafetyModeCountForTesting() { | 132 int GetForceYouTubeRestrictCountForTesting() { |
112 return g_force_youtube_safety_mode_count_for_test; | 133 return g_force_youtube_restrict_count_for_test; |
113 } | 134 } |
114 | 135 |
115 void ClearForceGoogleSafeSearchCountForTesting() { | 136 void ClearForceGoogleSafeSearchCountForTesting() { |
116 g_force_google_safe_search_count_for_test = 0; | 137 g_force_google_safe_search_count_for_test = 0; |
117 } | 138 } |
118 | 139 |
119 void ClearForceYouTubeSafetyModeCountForTesting() { | 140 void ClearForceYouTubeRestrictCountForTesting() { |
120 g_force_youtube_safety_mode_count_for_test = 0; | 141 g_force_youtube_restrict_count_for_test = 0; |
121 } | 142 } |
122 | 143 |
123 } // namespace safe_search_util | 144 } // namespace safe_search_util |
OLD | NEW |