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

Side by Side Diff: content/browser/browsing_data/clear_site_data_throttle.cc

Issue 2025683003: First experimental implementation of the Clear-Site-Data header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment, file URLs Created 4 years, 6 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
OLDNEW
(Empty)
1 // Copyright 2016 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 "content/browser/browsing_data/clear_site_data_throttle.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/values.h"
11 #include "content/browser/frame_host/navigation_handle_impl.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/navigation_handle.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_client.h"
17 #include "content/public/common/origin_util.h"
18 #include "net/http/http_response_headers.h"
19 #include "url/gurl.h"
20 #include "url/origin.h"
21
22 namespace content {
23
24 namespace {
25
26 static const char* kClearSiteDataHeader = "Clear-Site-Data";
27
28 static const char* kTypesKey = "types";
29
30 // Pretty-printed log output.
31 static const char* kConsoleMessageFormat =
32 "Clear-Site-Data header on '%s': %s";
33 static const char* kClearingOneType = "Clearing %s.";
34 static const char* kClearingTwoTypes = "Clearing %s and %s.";
35 static const char* kClearingThreeTypes = "Clearing %s, %s and %s.";
Mike West 2016/06/20 07:57:37 Nit: Oxford comma, plz.
msramek 2016/07/15 16:47:39 Done.
36
37 } // namespace
38
39 // static
40 std::unique_ptr<NavigationThrottle>
41 ClearSiteDataThrottle::CreateThrottleFor(NavigationHandle* handle) {
42 return std::unique_ptr<NavigationThrottle>(new ClearSiteDataThrottle(handle));
43 }
44
45 ClearSiteDataThrottle::~ClearSiteDataThrottle() {}
46
47 ClearSiteDataThrottle::ThrottleCheckResult
48 ClearSiteDataThrottle::WillStartRequest() {
49 current_url_ = navigation_handle()->GetURL();
50 return PROCEED;
51 }
52
53 ClearSiteDataThrottle::ThrottleCheckResult
54 ClearSiteDataThrottle::WillRedirectRequest() {
55 // We are processing a redirect from url1 to url2. GetResponseHeaders()
56 // contains headers from url1, but GetURL() is already equal to url2. Handle
57 // the headers before updating the URL, so that |current_url_| corresponds
58 // to the URL that sent the headers.
59 HandleHeader();
60 current_url_ = navigation_handle()->GetURL();
61
62 return PROCEED;
63 }
64
65 ClearSiteDataThrottle::ThrottleCheckResult
66 ClearSiteDataThrottle::WillProcessResponse() {
67 HandleHeader();
68
69 // Now that RenderFrameHost is ready, output the console messages.
70 for (const ConsoleMessage& message : messages_) {
71 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole(
72 message.level,
73 base::StringPrintf(
74 kConsoleMessageFormat, navigation_handle()->GetURL().spec().c_str(),
75 message.text.c_str()));
76 }
77
78 return PROCEED;
79 }
80
81 ClearSiteDataThrottle::ClearSiteDataThrottle(NavigationHandle* handle)
82 : NavigationThrottle(handle) {}
83
84 void ClearSiteDataThrottle::HandleHeader() {
85 NavigationHandleImpl* handle =
86 static_cast<NavigationHandleImpl*>(navigation_handle());
87
88 // Ignore file URL navigations, as those have no response headers.
89 if (current_url_.SchemeIsFile())
90 return;
91
92 // Only accept the header on secure origins.
93 if (!IsOriginSecure(current_url_)) {
94 ConsoleLog(&messages_,
95 "Not supported for insecure origins.",
96 CONSOLE_MESSAGE_LEVEL_ERROR);
97 return;
98 }
99
100 // Extract the instances of the header and parse them.
101 size_t iter = 0;
102 std::string header_name;
103 std::string header_value;
104 while (handle->GetResponseHeaders()->EnumerateHeaderLines(
105 &iter, &header_name, &header_value)) {
106 if (header_name != kClearSiteDataHeader)
107 continue;
108
109 bool clear_cookies;
110 bool clear_storage;
111 bool clear_cache;
112
113 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache,
114 &messages_)) {
115 continue;
116 }
117
118 // If the header is valid, clear the data for this browser context
119 // and origin.
120 BrowserContext* browser_context =
121 handle->GetWebContents()->GetBrowserContext();
122 url::Origin origin(current_url_);
123
124 GetContentClient()->browser()->ClearSiteData(
125 browser_context, origin, clear_cookies, clear_storage, clear_cache);
126 }
127 }
128
129 bool ClearSiteDataThrottle::ParseHeader(
130 const std::string& header,
131 bool* clear_cookies, bool* clear_storage, bool* clear_cache,
132 std::vector<ConsoleMessage>* messages) {
133 std::unique_ptr<base::Value> parsed_header =
134 base::JSONReader::Read(header);
135
136 if (!parsed_header) {
137 ConsoleLog(messages,
138 base::StringPrintf("%s is not a valid JSON.", header.c_str()),
139 CONSOLE_MESSAGE_LEVEL_ERROR);
140 return false;
141 }
142
143 if (!parsed_header->GetAsDictionary(nullptr)) {
144 ConsoleLog(messages,
145 base::StringPrintf("%s is not a dictionary.", header.c_str()),
146 CONSOLE_MESSAGE_LEVEL_ERROR);
147 return false;
148 }
Mike West 2016/06/20 07:57:38 It doesn't seem worthwhile to distinguish between
msramek 2016/07/15 16:47:39 Done. But I would still call it a JSON dictionary
149
150 const base::ListValue* types;
151 if (!static_cast<base::DictionaryValue*>(parsed_header.get())
152 ->GetListWithoutPathExpansion(kTypesKey, &types)) {
153 ConsoleLog(messages,
154 base::StringPrintf(
155 "No 'types' field present in %s.", header.c_str()),
156 CONSOLE_MESSAGE_LEVEL_ERROR);
157 return false;
158 }
159
160 *clear_cookies = false;
161 *clear_storage = false;
162 *clear_cache = false;
163
164 std::vector<std::string> type_names;
165 for (const std::unique_ptr<base::Value>& value : *types) {
166 std::string type;
167 value->GetAsString(&type);
168
169 bool* datatype = nullptr;
170
171 if (type == "cookies") {
172 datatype = clear_cookies;
173 } else if (type == "storage") {
174 datatype = clear_storage;
175 } else if (type == "cache") {
176 datatype = clear_cache;
177 } else {
178 std::string serialized_type;
179 JSONStringValueSerializer serializer(&serialized_type);
180 serializer.Serialize(*value);
Mike West 2016/06/20 07:57:38 Nit: It doesn't look like you use |serialized_type
msramek 2016/07/15 16:47:39 Done. Thanks for catching. If |value| is e.g. anot
181 ConsoleLog(messages,
182 base::StringPrintf("Invalid type: '%s'.", type.c_str()),
183 CONSOLE_MESSAGE_LEVEL_ERROR);
184 continue;
185 }
186
187 // Each data type should only be
Mike West 2016/06/20 07:57:37 Nit: Should only be... ?
msramek 2016/07/15 16:47:39 Correct. Every data type should exist, and nothing
188 DCHECK(datatype != nullptr);
Mike West 2016/06/20 07:57:37 Nit: `DCHECK(datatype)`
msramek 2016/07/15 16:47:39 Done.
189 if (*datatype)
190 continue;
191
192 *datatype = true;
193 type_names.push_back(type);
194 }
195
196 if (!*clear_cookies && !*clear_storage && !*clear_cache) {
197 ConsoleLog(messages,
198 base::StringPrintf(
199 "No valid types specified in %s.", header.c_str()),
200 CONSOLE_MESSAGE_LEVEL_ERROR);
201 return false;
202 }
203
204 // Pretty-print which types are to be cleared.
205 std::string output;
206 switch (type_names.size()) {
207 case 1:
208 output = base::StringPrintf(kClearingOneType, type_names[0].c_str());
209 break;
210 case 2:
211 output = base::StringPrintf(
212 kClearingTwoTypes, type_names[0].c_str(), type_names[1].c_str());
213 break;
214 case 3:
215 output = base::StringPrintf(
216 kClearingThreeTypes,
217 type_names[0].c_str(), type_names[1].c_str(), type_names[2].c_str());
218 break;
219 default:
220 NOTREACHED();
221 }
222 ConsoleLog(messages, output, CONSOLE_MESSAGE_LEVEL_LOG);
Mike West 2016/06/20 07:57:37 Nit: Would you mind pulling this out into a static
msramek 2016/07/15 16:47:39 Done. That also requires ConsoleMessage to be publ
223
224 return true;
225 }
226
227 void ClearSiteDataThrottle::ConsoleLog(
228 std::vector<ConsoleMessage>* messages,
229 const std::string& text, ConsoleMessageLevel level) {
230 messages->push_back({text, level});
231 }
232
233 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698