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

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: Move&use helpers from web_request_api_helpers 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/cookies/cookie_util.h"
20 #include "net/http/http_request_headers.h"
21 #include "net/url_request/url_request.h"
22 #include "url/gurl.h"
23
24 namespace {
25
26 const char kYouTubePrefCookieName[] = "PREF";
27 // YouTube pref flags are stored in bit masks of 31 bits each, called "f1",
28 // "f2" etc. The Safety Mode flag is bit 58, so bit 27 in "f2".
29 const char kYouTubePrefCookieSafetyModeFlagsEntryName[] = "f2";
30 const int kYouTubePrefCookieSafetyModeFlagsEntryValue = (1 << 27);
31
32 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the
33 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters
34 // must be in key=value form.
35 bool HasSameParameterKey(const std::string& first_parameter,
36 const std::string& second_parameter) {
37 DCHECK(second_parameter.find("=") != std::string::npos);
38 // Prefix for "foo=bar" is "foo=".
39 std::string parameter_prefix = second_parameter.substr(
40 0, second_parameter.find("=") + 1);
41 return StartsWithASCII(first_parameter, parameter_prefix, false);
42 }
43
44 // Examines the query string containing parameters and adds the necessary ones
45 // so that SafeSearch is active. |query| is the string to examine and the
46 // return value is the |query| string modified such that SafeSearch is active.
47 std::string AddSafeSearchParameters(const std::string& query) {
48 std::vector<std::string> new_parameters;
49 std::string safe_parameter = chrome::kSafeSearchSafeParameter;
50 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter;
51
52 std::vector<std::string> parameters;
53 base::SplitString(query, '&', &parameters);
54
55 std::vector<std::string>::iterator it;
56 for (it = parameters.begin(); it < parameters.end(); ++it) {
57 if (!HasSameParameterKey(*it, safe_parameter) &&
58 !HasSameParameterKey(*it, ssui_parameter)) {
59 new_parameters.push_back(*it);
60 }
61 }
62
63 new_parameters.push_back(safe_parameter);
64 new_parameters.push_back(ssui_parameter);
65 return JoinString(new_parameters, '&');
66 }
67
68 bool IsYouTubePrefCookie(const net::cookie_util::ParsedRequestCookie& cookie) {
69 return cookie.first == base::StringPiece(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 net::cookie_util::ParsedRequestCookies cookies;
114 net::cookie_util::ParseRequestCookieLine(cookie_string, &cookies);
115
116 // Find YouTube's pref cookie, or add it if it doesn't exist yet.
117 net::cookie_util::ParsedRequestCookies::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(base::StringPiece(kYouTubePrefCookieName),
121 base::StringPiece()));
122 pref_it = cookies.end() - 1;
123 }
124
125 // The pref cookie's value again consists of key/value pairs. Parse them.
erikwright (departed) 2014/07/09 15:36:23 strike "again". It's no longer relevant.
Marc Treib 2014/07/10 09:03:36 Done.
126 base::StringPairs pref_values;
127 base::SplitStringIntoKeyValuePairs(
128 pref_it->second.as_string(), '=', '&', &pref_values);
129
130 // Find the "flags" entry that contains the Safety Mode flag, or add it if it
131 // doesn't exist.
132 base::StringPairs::iterator flag_it =
133 std::find_if(pref_values.begin(), pref_values.end(),
134 IsYouTubePrefCookieSafetyModeFlagsEntry);
135 int flag_value = 0;
136 if (flag_it == pref_values.end()) {
137 pref_values.push_back(
138 std::make_pair(std::string(kYouTubePrefCookieSafetyModeFlagsEntryName),
139 std::string()));
140 flag_it = pref_values.end() - 1;
141 } else {
142 base::HexStringToInt(base::StringPiece(flag_it->second), &flag_value);
143 }
144
145 // Set the Safety Mode bit.
146 flag_value |= kYouTubePrefCookieSafetyModeFlagsEntryValue;
147
148 // Finally, put it all back together and replace the original cookie string.
149 flag_it->second = base::StringPrintf("%x", flag_value);
150 std::string pref_string = JoinStringKeyValuePairs(pref_values, '=', '&');
151 pref_it->second = base::StringPiece(pref_string);
152 cookie_string = net::cookie_util::SerializeRequestCookieLine(cookies);
153 headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie),
154 base::StringPiece(cookie_string));
155 }
156
157 } // namespace safe_search_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698