Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chrome/browser/net/safe_search_util.cc

Issue 354183002: Enforce SafetyMode for YouTube if prefs::kForceSafeSearch is on. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: YouTube Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/safe_search_util.h"
6
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "chrome/common/url_constants.h"
18 #include "components/google/core/browser/google_util.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/url_request/url_request.h"
21 #include "url/gurl.h"
22
23 namespace {
24
25 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
26 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters
27 // must be in key=value form.
28 bool HasSameParameterKey(const std::string& first_parameter,
29 const std::string& second_parameter) {
30 DCHECK(second_parameter.find("=") != std::string::npos);
31 // Prefix for "foo=bar" is "foo=".
32 std::string parameter_prefix = second_parameter.substr(
33 0, second_parameter.find("=") + 1);
34 return StartsWithASCII(first_parameter, parameter_prefix, false);
35 }
36
37 // Examines the query string containing parameters and adds the necessary ones
38 // so that SafeSearch is active. |query| is the string to examine and the
39 // return value is the |query| string modified such that SafeSearch is active.
40 std::string AddSafeSearchParameters(const std::string& query) {
41 std::vector<std::string> new_parameters;
42 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
43 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
44
45 std::vector<std::string> parameters;
46 base::SplitString(query, '&', &parameters);
47
48 std::vector<std::string>::iterator it;
49 for (it = parameters.begin(); it < parameters.end(); ++it) {
50 if (!HasSameParameterKey(*it, safe_parameter) &&
51 !HasSameParameterKey(*it, ssui_parameter)) {
52 new_parameters.push_back(*it);
53 }
54 }
55
56 new_parameters.push_back(safe_parameter);
57 new_parameters.push_back(ssui_parameter);
58 return JoinString(new_parameters, '&');
59 }
60
61 const char kYouTubePrefCookieName[] = "PREF";
62 // YouTube pref flags are stored in bit masks of 31 bits each, called "f1",
63 // "f2" etc. The Safety Mode flag is bit 58, so bit 27 in "f2".
64 const char kYouTubePrefCookieSafetyModeFlagsEntryName[] = "f2";
65 const int kYouTubePrefCookieSafetyModeFlagsEntryValue = (1 << 27);
66 const char kYouTubePrefCookieFlagsValueFormat[] = "%x";
battre 2014/07/08 13:43:06 I would consider to inline the format.
battre 2014/07/08 13:43:06 I think the style guide says that constants should
Marc Treib 2014/07/08 14:16:14 Done.
Marc Treib 2014/07/08 14:16:14 Done.
67
68 bool IsYouTubePrefCookie(const std::pair<std::string, std::string>& cookie) {
69 return cookie.first == kYouTubePrefCookieName;
70 }
71
72 bool IsYouTubePrefCookieSafetyModeFlagsEntry(
73 const std::pair<std::string, std::string>& pref_entry) {
74 return pref_entry.first == kYouTubePrefCookieSafetyModeFlagsEntryName;
75 }
76
77 } // namespace
78
79 namespace safe_search_util {
80
81 // If |request| is a request to Google Web Search the function
82 // enforces that the SafeSearch query parameters are set to active.
83 // Sets the query part of |new_url| with the new value of the parameters.
84 void ForceGoogleSafeSearch(const net::URLRequest* request, GURL* new_url) {
85 if (!google_util::IsGoogleSearchUrl(request->url()) &&
86 !google_util::IsGoogleHomePageUrl(request->url()))
87 return;
88
89 std::string query = request->url().query();
90 std::string new_query = AddSafeSearchParameters(query);
91 if (query == new_query)
92 return;
93
94 GURL::Replacements replacements;
95 replacements.SetQueryStr(new_query);
96 *new_url = request->url().ReplaceComponents(replacements);
97 }
98
99 // If |request| is a request to YouTube, enforces YouTube's Safety Mode by
100 // adding/modifying YouTube's PrefCookie header.
101 void ForceYouTubeSafetyMode(const net::URLRequest* request,
102 net::HttpRequestHeaders* headers) {
103 if (!google_util::IsYoutubeDomainUrl(
104 request->url(),
105 google_util::ALLOW_SUBDOMAIN,
106 google_util::DISALLOW_NON_STANDARD_PORTS))
107 return;
108
109 // Get the cookie string from the headers and parse it into key/value pairs.
110 std::string cookie_string;
111 headers->GetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie),
112 &cookie_string);
113 base::StringPairs cookies;
114 base::SplitStringIntoKeyValuePairs(cookie_string, '=', ';', &cookies);
115
116 // Find YouTube's pref cookie, or add it if it doesn't exist yet.
117 base::StringPairs::iterator pref_it =
118 std::find_if(cookies.begin(), cookies.end(), IsYouTubePrefCookie);
119 if (pref_it == cookies.end()) {
120 cookies.push_back(std::make_pair(std::string(kYouTubePrefCookieName),
121 std::string()));
122 pref_it = cookies.end() - 1;
123 }
124
125 // The pref cookie's value again consists of key/value pairs. Parse them.
126 base::StringPairs pref_values;
127 base::SplitStringIntoKeyValuePairs(pref_it->second, '=', '&', &pref_values);
128
129 // Find the "flags" entry that contains the Safety Mode flag, or add it if it
130 // doesn't exist.
131 base::StringPairs::iterator flag_it =
132 std::find_if(pref_values.begin(), pref_values.end(),
133 IsYouTubePrefCookieSafetyModeFlagsEntry);
134 int flag_value = 0;
135 if (flag_it == pref_values.end()) {
136 pref_values.push_back(
137 std::make_pair(std::string(kYouTubePrefCookieSafetyModeFlagsEntryName),
138 std::string()));
139 flag_it = pref_values.end() - 1;
140 } else {
141 base::HexStringToInt(base::StringPiece(flag_it->second), &flag_value);
142 }
143
144 // Set the Safety Mode bit.
145 flag_value |= kYouTubePrefCookieSafetyModeFlagsEntryValue;
146
147 // Finally, put it all back together and replace the original cookie string.
148 flag_it->second =
149 base::StringPrintf(kYouTubePrefCookieFlagsValueFormat, flag_value);
150 pref_it->second = JoinStringKeyValuePairs(pref_values, '=', '&');
151 cookie_string = JoinStringKeyValuePairs(cookies, '=', ';');
152 headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie),
153 base::StringPiece(cookie_string));
154 }
155
156 } // namespace safe_search_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698