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

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: WebContentsObserver Created 4 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
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.";
36
37 // Console logging. Adds a |text| message with |level| to |messages|.
38 void ConsoleLog(std::vector<ClearSiteDataThrottle::ConsoleMessage>* messages,
39 const std::string& text, ConsoleMessageLevel level) {
40 messages->push_back({text, level});
clamy 2016/07/26 16:48:13 a) Are we okay if those never get delivered? Ex: w
msramek 2016/07/27 15:02:53 a) According to the description of WebContentsObse
41 }
42
43 } // namespace
44
45 // static
46 std::unique_ptr<NavigationThrottle>
47 ClearSiteDataThrottle::CreateThrottleFor(NavigationHandle* handle) {
48 return std::unique_ptr<NavigationThrottle>(new ClearSiteDataThrottle(handle));
49 }
50
51 ClearSiteDataThrottle::~ClearSiteDataThrottle() {}
52
53 ClearSiteDataThrottle::ThrottleCheckResult
54 ClearSiteDataThrottle::WillStartRequest() {
55 current_url_ = navigation_handle()->GetURL();
56 return PROCEED;
57 }
58
59 ClearSiteDataThrottle::ThrottleCheckResult
60 ClearSiteDataThrottle::WillRedirectRequest() {
61 // We are processing a redirect from url1 to url2. GetResponseHeaders()
62 // contains headers from url1, but GetURL() is already equal to url2. Handle
63 // the headers before updating the URL, so that |current_url_| corresponds
64 // to the URL that sent the headers.
65 HandleHeader();
66 current_url_ = navigation_handle()->GetURL();
67
68 return PROCEED;
69 }
70
71 ClearSiteDataThrottle::ThrottleCheckResult
72 ClearSiteDataThrottle::WillProcessResponse() {
73 HandleHeader();
74
75 // Wait for the web contents navigation to finish before outputting console
76 // messages.
77 Observe(navigation_handle()->GetWebContents());
78
79 return PROCEED;
80 }
81
82 void ClearSiteDataThrottle::DidFinishNavigation(NavigationHandle* handle) {
83 // Now that we have access to the correct RenderFrameHost,
84 // output the cached console messages.
85 for (const ConsoleMessage& message : messages_) {
86 handle->GetRenderFrameHost()->AddMessageToConsole(
87 message.level,
88 base::StringPrintf(
89 kConsoleMessageFormat, navigation_handle()->GetURL().spec().c_str(),
90 message.text.c_str()));
91 }
92
93 Observe(nullptr);
94 }
95
96 ClearSiteDataThrottle::ClearSiteDataThrottle(NavigationHandle* handle)
97 : NavigationThrottle(handle) {}
98
99 void ClearSiteDataThrottle::HandleHeader() {
100 NavigationHandleImpl* handle =
101 static_cast<NavigationHandleImpl*>(navigation_handle());
102
103 // Some navigations don't have response headers.
104 if (!handle->GetResponseHeaders())
105 return;
106
107 // Extract the instances of the header and parse them.
108 size_t iter = 0;
109 std::string header_name;
110 std::string header_value;
111 while (handle->GetResponseHeaders()->EnumerateHeaderLines(
112 &iter, &header_name, &header_value)) {
113 if (header_name != kClearSiteDataHeader)
114 continue;
115
116 // Only accept the header on secure origins.
117 if (!IsOriginSecure(current_url_)) {
118 ConsoleLog(&messages_,
119 "Not supported for insecure origins.",
120 CONSOLE_MESSAGE_LEVEL_ERROR);
121 return;
122 }
123
124 bool clear_cookies;
125 bool clear_storage;
126 bool clear_cache;
127
128 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache,
129 &messages_)) {
130 continue;
131 }
132
133 // If the header is valid, clear the data for this browser context
134 // and origin.
135 BrowserContext* browser_context =
136 handle->GetWebContents()->GetBrowserContext();
137 url::Origin origin(current_url_);
138
139 GetContentClient()->browser()->ClearSiteData(
140 browser_context, origin, clear_cookies, clear_storage, clear_cache);
141 }
142 }
143
144 bool ClearSiteDataThrottle::ParseHeader(
145 const std::string& header,
146 bool* clear_cookies, bool* clear_storage, bool* clear_cache,
147 std::vector<ConsoleMessage>* messages) {
148 std::unique_ptr<base::Value> parsed_header =
149 base::JSONReader::Read(header);
150
151 if (!parsed_header) {
152 ConsoleLog(messages,
153 base::StringPrintf("%s is not a valid JSON.", header.c_str()),
154 CONSOLE_MESSAGE_LEVEL_ERROR);
155 return false;
156 }
157
158 const base::ListValue* types = nullptr;
159 if (!parsed_header->GetAsDictionary(nullptr) ||
160 !static_cast<base::DictionaryValue*>(parsed_header.get())
161 ->GetListWithoutPathExpansion(kTypesKey, &types)) {
162 ConsoleLog(messages,
163 base::StringPrintf(
164 "%s is not a JSON dictionary with a 'types' field.",
165 header.c_str()),
166 CONSOLE_MESSAGE_LEVEL_ERROR);
167 return false;
168 }
169
170 DCHECK(types);
171
172 *clear_cookies = false;
173 *clear_storage = false;
174 *clear_cache = false;
175
176 std::vector<std::string> type_names;
177 for (const std::unique_ptr<base::Value>& value : *types) {
178 std::string type;
179 value->GetAsString(&type);
180
181 bool* datatype = nullptr;
182
183 if (type == "cookies") {
184 datatype = clear_cookies;
185 } else if (type == "storage") {
186 datatype = clear_storage;
187 } else if (type == "cache") {
188 datatype = clear_cache;
189 } else {
190 std::string serialized_type;
191 JSONStringValueSerializer serializer(&serialized_type);
192 serializer.Serialize(*value);
193 ConsoleLog(messages,
194 base::StringPrintf("Invalid type: %s.",
195 serialized_type.c_str()),
196 CONSOLE_MESSAGE_LEVEL_ERROR);
197 continue;
198 }
199
200 // Each data type should only be processed once.
201 DCHECK(datatype);
202 if (*datatype)
203 continue;
204
205 *datatype = true;
206 type_names.push_back(type);
207 }
208
209 if (!*clear_cookies && !*clear_storage && !*clear_cache) {
210 ConsoleLog(messages,
211 base::StringPrintf(
212 "No valid types specified in %s.", header.c_str()),
213 CONSOLE_MESSAGE_LEVEL_ERROR);
214 return false;
215 }
216
217 // Pretty-print which types are to be cleared.
218 std::string output;
219 switch (type_names.size()) {
220 case 1:
221 output = base::StringPrintf(kClearingOneType, type_names[0].c_str());
222 break;
223 case 2:
224 output = base::StringPrintf(
225 kClearingTwoTypes, type_names[0].c_str(), type_names[1].c_str());
226 break;
227 case 3:
228 output = base::StringPrintf(
229 kClearingThreeTypes,
230 type_names[0].c_str(), type_names[1].c_str(), type_names[2].c_str());
231 break;
232 default:
233 NOTREACHED();
234 }
235 ConsoleLog(messages, output, CONSOLE_MESSAGE_LEVEL_LOG);
236
237 return true;
238 }
239
240 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698