From ac9766f340ceeadbe24893b93583f70b758774d8 Mon Sep 17 00:00:00 2001 From: treib Date: Mon, 10 Nov 2014 09:39:22 -0800 Subject: [PATCH] Set "YouTube-Safety-Mode:Active" header instead of modifying YouTube's Pref cookie. This is a cleanup of https://codereview.chromium.org/354183002, possible now that YouTube supports a special HTTP header to enable Safety Mode. BUG=430028 Review URL: https://codereview.chromium.org/712453004 Cr-Commit-Position: refs/heads/master@{#303487} --- chrome/browser/net/safe_search_util.cc | 104 ++---------------------- chrome/browser/net/safe_search_util.h | 2 +- chrome/browser/net/safe_search_util_unittest.cc | 60 +++++--------- 3 files changed, 25 insertions(+), 141 deletions(-) diff --git a/chrome/browser/net/safe_search_util.cc b/chrome/browser/net/safe_search_util.cc index 0e983d0434c5..322fc2d022ed 100644 --- a/chrome/browser/net/safe_search_util.cc +++ b/chrome/browser/net/safe_search_util.cc @@ -23,11 +23,8 @@ namespace { -const char kYouTubePrefCookieName[] = "PREF"; -// YouTube pref flags are stored in bit masks of 31 bits each, called "f1", -// "f2" etc. The Safety Mode flag is bit 58, so bit 27 in "f2". -const char kYouTubePrefCookieSafetyModeFlagsEntryName[] = "f2"; -const int kYouTubePrefCookieSafetyModeFlagsEntryValue = (1 << 27); +const char kYouTubeSafetyModeHeaderName[] = "YouTube-Safety-Mode"; +const char kYouTubeSafetyModeHeaderValue[] = "Active"; // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the // same key as the the |second_parameter| (e.g. foo=baz). Both parameters @@ -65,41 +62,6 @@ std::string AddSafeSearchParameters(const std::string& query) { return JoinString(new_parameters, '&'); } -bool IsYouTubePrefCookie(const net::cookie_util::ParsedRequestCookie& cookie) { - return cookie.first == base::StringPiece(kYouTubePrefCookieName); -} - -bool IsYouTubePrefCookieSafetyModeFlagsEntry( - const std::pair& pref_entry) { - return pref_entry.first == kYouTubePrefCookieSafetyModeFlagsEntryName; -} - -std::string JoinStringKeyValuePair( - const base::StringPairs::value_type& key_value, - char delimiter) { - return key_value.first + delimiter + key_value.second; -} - -// Does the opposite of base::SplitStringIntoKeyValuePairs() from -// base/strings/string_util.h. -std::string JoinStringKeyValuePairs(const base::StringPairs& pairs, - char key_value_delimiter, - char key_value_pair_delimiter) { - if (pairs.empty()) - return std::string(); - - base::StringPairs::const_iterator it = pairs.begin(); - std::string result = JoinStringKeyValuePair(*it, key_value_delimiter); - ++it; - - for (; it != pairs.end(); ++it) { - result += key_value_pair_delimiter; - result += JoinStringKeyValuePair(*it, key_value_delimiter); - } - - return result; -} - } // namespace namespace safe_search_util { @@ -123,7 +85,7 @@ void ForceGoogleSafeSearch(const net::URLRequest* request, GURL* new_url) { } // If |request| is a request to YouTube, enforces YouTube's Safety Mode by -// adding/modifying YouTube's PrefCookie header. +// setting YouTube's Safety Mode header. void ForceYouTubeSafetyMode(const net::URLRequest* request, net::HttpRequestHeaders* headers) { if (!google_util::IsYoutubeDomainUrl( @@ -132,64 +94,8 @@ void ForceYouTubeSafetyMode(const net::URLRequest* request, google_util::DISALLOW_NON_STANDARD_PORTS)) return; - // Get the cookie string from the headers and parse it into key/value pairs. - std::string cookie_string; - headers->GetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie), - &cookie_string); - net::cookie_util::ParsedRequestCookies cookies; - net::cookie_util::ParseRequestCookieLine(cookie_string, &cookies); - - // Find YouTube's pref cookie, or add it if it doesn't exist yet. - net::cookie_util::ParsedRequestCookies::iterator pref_it = - std::find_if(cookies.begin(), cookies.end(), IsYouTubePrefCookie); - if (pref_it == cookies.end()) { - cookies.push_back(std::make_pair(base::StringPiece(kYouTubePrefCookieName), - base::StringPiece())); - pref_it = cookies.end() - 1; - } - - // The pref cookie's value may be quoted. If so, remove the quotes. - std::string pref_string = pref_it->second.as_string(); - bool pref_string_quoted = false; - if (pref_string.size() >= 2 && - pref_string[0] == '\"' && - pref_string[pref_string.size() - 1] == '\"') { - pref_string_quoted = true; - pref_string = pref_string.substr(1, pref_string.length() - 2); - } - - // The pref cookie's value consists of key/value pairs. Parse them. - base::StringPairs pref_values; - base::SplitStringIntoKeyValuePairs(pref_string, '=', '&', &pref_values); - - // Find the "flags" entry that contains the Safety Mode flag, or add it if it - // doesn't exist. - base::StringPairs::iterator flag_it = - std::find_if(pref_values.begin(), pref_values.end(), - IsYouTubePrefCookieSafetyModeFlagsEntry); - int flag_value = 0; - if (flag_it == pref_values.end()) { - pref_values.push_back( - std::make_pair(std::string(kYouTubePrefCookieSafetyModeFlagsEntryName), - std::string())); - flag_it = pref_values.end() - 1; - } else { - base::HexStringToInt(base::StringPiece(flag_it->second), &flag_value); - } - - // Set the Safety Mode bit. - flag_value |= kYouTubePrefCookieSafetyModeFlagsEntryValue; - - // Finally, put it all back together and replace the original cookie string. - flag_it->second = base::StringPrintf("%x", flag_value); - pref_string = JoinStringKeyValuePairs(pref_values, '=', '&'); - if (pref_string_quoted) { - pref_string = '\"' + pref_string + '\"'; - } - pref_it->second = base::StringPiece(pref_string); - cookie_string = net::cookie_util::SerializeRequestCookieLine(cookies); - headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie), - base::StringPiece(cookie_string)); + headers->SetHeader(kYouTubeSafetyModeHeaderName, + kYouTubeSafetyModeHeaderValue); } } // namespace safe_search_util diff --git a/chrome/browser/net/safe_search_util.h b/chrome/browser/net/safe_search_util.h index 8b5e0e9c9b5a..81211a9626f1 100644 --- a/chrome/browser/net/safe_search_util.h +++ b/chrome/browser/net/safe_search_util.h @@ -20,7 +20,7 @@ namespace safe_search_util { void ForceGoogleSafeSearch(const net::URLRequest* request, GURL* new_url); // If |request| is a request to YouTube, enforces YouTube's Safety Mode by -// adding/modifying YouTube's PrefCookie header. +// setting YouTube's Safety Mode header. void ForceYouTubeSafetyMode(const net::URLRequest* request, net::HttpRequestHeaders* headers); diff --git a/chrome/browser/net/safe_search_util_unittest.cc b/chrome/browser/net/safe_search_util_unittest.cc index 4a1872b39d3a..fbf2989c6a7e 100644 --- a/chrome/browser/net/safe_search_util_unittest.cc +++ b/chrome/browser/net/safe_search_util_unittest.cc @@ -30,24 +30,6 @@ class SafeSearchUtilTest : public ::testing::Test { NULL); } - static void SetCookie(net::HttpRequestHeaders* headers, - const std::string& value) { - headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie), - base::StringPiece(value)); - } - - static void CheckHeaders(net::URLRequest* request, - const std::string& header_string_original, - const std::string& header_string_expected) { - net::HttpRequestHeaders headers; - SetCookie(&headers, header_string_original); - safe_search_util::ForceYouTubeSafetyMode(request, &headers); - - net::HttpRequestHeaders headers_expected; - SetCookie(&headers_expected, header_string_expected); - EXPECT_EQ(headers_expected.ToString(), headers.ToString()); - } - base::MessageLoop message_loop_; net::TestURLRequestContext context_; }; @@ -56,35 +38,31 @@ class SafeSearchUtilTest : public ::testing::Test { // ChromeNetworkDelegateSafeSearchTest (in chrome_network_delegate_unittest.cc), // so we won't test it again here. -TEST_F(SafeSearchUtilTest, CreateYoutubePrefCookie) { +TEST_F(SafeSearchUtilTest, SetYoutubeHeader) { scoped_ptr request = CreateYoutubeRequest(); - CheckHeaders(request.get(), - "OtherCookie=value", - "OtherCookie=value; PREF=f2=8000000"); + net::HttpRequestHeaders headers; + safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers); + std::string value; + EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value)); + EXPECT_EQ("Active", value); } -TEST_F(SafeSearchUtilTest, ModifyYoutubePrefCookie) { +TEST_F(SafeSearchUtilTest, OverrideYoutubeHeader) { scoped_ptr request = CreateYoutubeRequest(); - CheckHeaders(request.get(), - "PREF=f1=123; OtherCookie=value", - "PREF=f1=123&f2=8000000; OtherCookie=value"); - CheckHeaders(request.get(), - "PREF=", - "PREF=f2=8000000"); - CheckHeaders(request.get(), - "PREF=\"\"", - "PREF=\"f2=8000000\""); - CheckHeaders(request.get(), - "PREF=f1=123&f2=4321&foo=bar", - "PREF=f1=123&f2=8004321&foo=bar"); - CheckHeaders(request.get(), - "PREF=\"f1=1&f2=4321\"; OtherCookie=value", - "PREF=\"f1=1&f2=8004321\"; OtherCookie=value"); + net::HttpRequestHeaders headers; + headers.SetHeader("Youtube-Safety-Mode", "Off"); + safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers); + std::string value; + EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value)); + EXPECT_EQ("Active", value); } TEST_F(SafeSearchUtilTest, DoesntTouchNonYoutubeURL) { scoped_ptr request = CreateNonYoutubeRequest(); - CheckHeaders(request.get(), - "PREF=f2=0", - "PREF=f2=0"); + net::HttpRequestHeaders headers; + headers.SetHeader("Youtube-Safety-Mode", "Off"); + safe_search_util::ForceYouTubeSafetyMode(request.get(), &headers); + std::string value; + EXPECT_TRUE(headers.GetHeader("Youtube-Safety-Mode", &value)); + EXPECT_EQ("Off", value); } -- 2.11.4.GIT