diff options
author | treib <treib@chromium.org> | 2014-11-10 09:39:22 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-10 17:39:55 +0000 |
commit | ac9766f340ceeadbe24893b93583f70b758774d8 (patch) | |
tree | bff675e36b54e3e43814cff371d2211b7b27dd14 /chrome/browser/net/safe_search_util.cc | |
parent | 11e2209b8d9fd824d10fde9cc2c006f037307f57 (diff) | |
download | chromium_src-ac9766f340ceeadbe24893b93583f70b758774d8.zip chromium_src-ac9766f340ceeadbe24893b93583f70b758774d8.tar.gz chromium_src-ac9766f340ceeadbe24893b93583f70b758774d8.tar.bz2 |
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}
Diffstat (limited to 'chrome/browser/net/safe_search_util.cc')
-rw-r--r-- | chrome/browser/net/safe_search_util.cc | 104 |
1 files changed, 5 insertions, 99 deletions
diff --git a/chrome/browser/net/safe_search_util.cc b/chrome/browser/net/safe_search_util.cc index 0e983d0..322fc2d 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<std::string, std::string>& 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 |