OLD | NEW |
---|---|
(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 const char kYouTubePrefCookieName[] = "PREF"; | |
26 // YouTube pref flags are stored in bit masks of 31 bits each, called "f1", | |
27 // "f2" etc. The Safety Mode flag is bit 58, so bit 27 in "f2". | |
28 const char kYouTubePrefCookieSafetyModeFlagsEntryName[] = "f2"; | |
29 const int kYouTubePrefCookieSafetyModeFlagsEntryValue = (1 << 27); | |
30 | |
31 // Returns whether a URL parameter, |first_parameter| (e.g. foo=bar), has the | |
32 // same key as the the |second_parameter| (e.g. foo=baz). Both parameters | |
33 // must be in key=value form. | |
34 bool HasSameParameterKey(const std::string& first_parameter, | |
35 const std::string& second_parameter) { | |
36 DCHECK(second_parameter.find("=") != std::string::npos); | |
37 // Prefix for "foo=bar" is "foo=". | |
38 std::string parameter_prefix = second_parameter.substr( | |
39 0, second_parameter.find("=") + 1); | |
40 return StartsWithASCII(first_parameter, parameter_prefix, false); | |
41 } | |
42 | |
43 // Examines the query string containing parameters and adds the necessary ones | |
44 // so that SafeSearch is active. |query| is the string to examine and the | |
45 // return value is the |query| string modified such that SafeSearch is active. | |
46 std::string AddSafeSearchParameters(const std::string& query) { | |
47 std::vector<std::string> new_parameters; | |
48 std::string safe_parameter = chrome::kSafeSearchSafeParameter; | |
49 std::string ssui_parameter = chrome::kSafeSearchSsuiParameter; | |
50 | |
51 std::vector<std::string> parameters; | |
52 base::SplitString(query, '&', ¶meters); | |
53 | |
54 std::vector<std::string>::iterator it; | |
55 for (it = parameters.begin(); it < parameters.end(); ++it) { | |
56 if (!HasSameParameterKey(*it, safe_parameter) && | |
57 !HasSameParameterKey(*it, ssui_parameter)) { | |
58 new_parameters.push_back(*it); | |
59 } | |
60 } | |
61 | |
62 new_parameters.push_back(safe_parameter); | |
63 new_parameters.push_back(ssui_parameter); | |
64 return JoinString(new_parameters, '&'); | |
65 } | |
66 | |
67 bool IsYouTubePrefCookie(const std::pair<std::string, std::string>& cookie) { | |
68 return cookie.first == kYouTubePrefCookieName; | |
69 } | |
70 | |
71 bool IsYouTubePrefCookieSafetyModeFlagsEntry( | |
72 const std::pair<std::string, std::string>& pref_entry) { | |
73 return pref_entry.first == kYouTubePrefCookieSafetyModeFlagsEntryName; | |
74 } | |
75 | |
76 } // namespace | |
77 | |
78 namespace safe_search_util { | |
79 | |
80 // If |request| is a request to Google Web Search the function | |
81 // enforces that the SafeSearch query parameters are set to active. | |
82 // Sets the query part of |new_url| with the new value of the parameters. | |
83 void ForceGoogleSafeSearch(const net::URLRequest* request, GURL* new_url) { | |
84 if (!google_util::IsGoogleSearchUrl(request->url()) && | |
85 !google_util::IsGoogleHomePageUrl(request->url())) | |
86 return; | |
87 | |
88 std::string query = request->url().query(); | |
89 std::string new_query = AddSafeSearchParameters(query); | |
90 if (query == new_query) | |
91 return; | |
92 | |
93 GURL::Replacements replacements; | |
94 replacements.SetQueryStr(new_query); | |
95 *new_url = request->url().ReplaceComponents(replacements); | |
96 } | |
97 | |
98 // If |request| is a request to YouTube, enforces YouTube's Safety Mode by | |
99 // adding/modifying YouTube's PrefCookie header. | |
100 void ForceYouTubeSafetyMode(const net::URLRequest* request, | |
101 net::HttpRequestHeaders* headers) { | |
102 if (!google_util::IsYoutubeDomainUrl( | |
103 request->url(), | |
104 google_util::ALLOW_SUBDOMAIN, | |
105 google_util::DISALLOW_NON_STANDARD_PORTS)) | |
106 return; | |
107 | |
108 // Get the cookie string from the headers and parse it into key/value pairs. | |
109 std::string cookie_string; | |
110 headers->GetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie), | |
erikwright (departed)
2014/07/08 14:28:26
Of course, the Chrome cookie store does not normal
| |
111 &cookie_string); | |
112 base::StringPairs cookies; | |
113 base::SplitStringIntoKeyValuePairs(cookie_string, '=', ';', &cookies); | |
114 | |
115 // Find YouTube's pref cookie, or add it if it doesn't exist yet. | |
116 base::StringPairs::iterator pref_it = | |
117 std::find_if(cookies.begin(), cookies.end(), IsYouTubePrefCookie); | |
118 if (pref_it == cookies.end()) { | |
119 cookies.push_back(std::make_pair(std::string(kYouTubePrefCookieName), | |
120 std::string())); | |
121 pref_it = cookies.end() - 1; | |
122 } | |
123 | |
124 // The pref cookie's value again consists of key/value pairs. Parse them. | |
125 base::StringPairs pref_values; | |
126 base::SplitStringIntoKeyValuePairs(pref_it->second, '=', '&', &pref_values); | |
127 | |
128 // Find the "flags" entry that contains the Safety Mode flag, or add it if it | |
129 // doesn't exist. | |
130 base::StringPairs::iterator flag_it = | |
131 std::find_if(pref_values.begin(), pref_values.end(), | |
132 IsYouTubePrefCookieSafetyModeFlagsEntry); | |
133 int flag_value = 0; | |
134 if (flag_it == pref_values.end()) { | |
135 pref_values.push_back( | |
136 std::make_pair(std::string(kYouTubePrefCookieSafetyModeFlagsEntryName), | |
137 std::string())); | |
138 flag_it = pref_values.end() - 1; | |
139 } else { | |
140 base::HexStringToInt(base::StringPiece(flag_it->second), &flag_value); | |
141 } | |
142 | |
143 // Set the Safety Mode bit. | |
144 flag_value |= kYouTubePrefCookieSafetyModeFlagsEntryValue; | |
145 | |
146 // Finally, put it all back together and replace the original cookie string. | |
147 flag_it->second = base::StringPrintf("%x", flag_value); | |
148 pref_it->second = JoinStringKeyValuePairs(pref_values, '=', '&'); | |
149 cookie_string = JoinStringKeyValuePairs(cookies, '=', ';'); | |
150 headers->SetHeader(base::StringPiece(net::HttpRequestHeaders::kCookie), | |
151 base::StringPiece(cookie_string)); | |
152 } | |
153 | |
154 } // namespace safe_search_util | |
OLD | NEW |