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

Side by Side Diff: content/browser/browsing_data/clear_site_data_header_observer.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: Fix URL in comments, remove accidental change. Created 4 years, 4 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_header_observer.h"
6
7 #include "base/command_line.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "content/browser/frame_host/navigation_handle_impl.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_client.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/common/origin_util.h"
21 #include "net/http/http_response_headers.h"
22 #include "url/gurl.h"
23 #include "url/origin.h"
24
25 namespace content {
26
27 namespace {
28
29 static const char* kClearSiteDataHeader = "Clear-Site-Data";
30
31 static const char* kTypesKey = "types";
32
33 // Pretty-printed log output.
34 static const char* kConsoleMessagePrefix =
35 "Clear-Site-Data header on '%s': %s";
36 static const char* kClearingOneType = "Clearing %s.";
37 static const char* kClearingTwoTypes = "Clearing %s and %s.";
38 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s.";
39
40 // Console logging. Adds a |text| message with |level| to |messages|.
41 void ConsoleLog(
42 std::vector<ClearSiteDataHeaderObserver::ConsoleMessage>* messages,
43 const GURL& url, const std::string& text, ConsoleMessageLevel level) {
44 messages->push_back({url, text, level});
45 }
46
47 bool AreExperimentalFeaturesEnabled() {
48 return base::CommandLine::ForCurrentProcess()->HasSwitch(
49 switches::kEnableExperimentalWebPlatformFeatures);
50 }
51
52 } // namespace
53
54 // static
55 std::unique_ptr<ClearSiteDataHeaderObserver>
56 ClearSiteDataHeaderObserver::CreateFor(NavigationHandle* handle) {
57 ClearSiteDataHeaderObserver* observer = AreExperimentalFeaturesEnabled()
58 ? new ClearSiteDataHeaderObserver(handle->GetWebContents())
nasko 2016/07/28 17:30:30 This looks wrong to me. You only need one observer
msramek 2016/08/01 16:03:18 Acknowledged. Please see the other comment with th
59 : nullptr;
60
61 return base::WrapUnique(observer);
62 }
63
64 ClearSiteDataHeaderObserver::~ClearSiteDataHeaderObserver() {}
65
66 void ClearSiteDataHeaderObserver::DidStartNavigation(
67 NavigationHandle* navigation_handle) {
68 current_url_ = navigation_handle->GetURL();
69 }
70
71 void ClearSiteDataHeaderObserver::DidRedirectNavigation(
72 NavigationHandle* navigation_handle) {
73 // We are processing a redirect from url1 to url2. GetResponseHeaders()
74 // contains headers from url1, but GetURL() is already equal to url2. Handle
75 // the headers before updating the URL, so that |current_url_| corresponds
76 // to the URL that sent the headers.
77 HandleHeader(navigation_handle);
78 current_url_ = navigation_handle->GetURL();
79 }
80
81 void ClearSiteDataHeaderObserver::DidFinishNavigation(
82 NavigationHandle* navigation_handle) {
83 HandleHeader(navigation_handle);
84
85 // Now that we have access to the correct RenderFrameHost,
86 // output the cached console messages. Prefix each sequence of messages
87 // belonging to the same URL with |kConsoleMessagePrefix|.
88 GURL last_seen_url;
89 for (const ConsoleMessage& message : messages_) {
90 std::string output;
nasko 2016/07/28 17:30:30 What is output used for?
msramek 2016/08/01 16:03:19 Argh :( This piece of code was added as a last-min
91 if (message.url == last_seen_url) {
nasko 2016/07/28 17:30:30 last_seen_url is never set to any meaningful URL.
msramek 2016/08/01 16:03:18 Ditto as above. Sorry for that. |last_seen_url| i
92 output = message.text;
93 } else {
94 navigation_handle->GetRenderFrameHost()->AddMessageToConsole(
95 message.level,
96 base::StringPrintf(
97 kConsoleMessagePrefix, message.url.spec().c_str(),
98 message.text.c_str()));
99 }
100 }
101 }
102
103 ClearSiteDataHeaderObserver::ClearSiteDataHeaderObserver(
nasko 2016/07/28 17:30:30 nit: I'd put the constructor next to the destructo
msramek 2016/08/01 16:03:19 Done.
104 WebContents* web_contents)
105 : WebContentsObserver(web_contents) {}
106
107 void ClearSiteDataHeaderObserver::HandleHeader(
108 NavigationHandle* navigation_handle) {
109 NavigationHandleImpl* handle =
110 static_cast<NavigationHandleImpl*>(navigation_handle);
111
112 // Some navigations don't have response headers.
113 if (!handle->GetResponseHeaders())
114 return;
115
116 // Extract the instances of the header and parse them.
117 size_t iter = 0;
118 std::string header_name;
119 std::string header_value;
120 while (handle->GetResponseHeaders()->EnumerateHeaderLines(
121 &iter, &header_name, &header_value)) {
122 if (header_name != kClearSiteDataHeader)
123 continue;
124
125 // Only accept the header on secure origins.
126 if (!IsOriginSecure(current_url_)) {
127 ConsoleLog(&messages_,
128 current_url_,
129 "Not supported for insecure origins.",
130 CONSOLE_MESSAGE_LEVEL_ERROR);
131 return;
132 }
133
134 bool clear_cookies;
135 bool clear_storage;
136 bool clear_cache;
137
138 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache,
139 &messages_)) {
140 continue;
141 }
142
143 // If the header is valid, clear the data for this browser context
144 // and origin.
145 BrowserContext* browser_context =
146 handle->GetWebContents()->GetBrowserContext();
147 url::Origin origin(current_url_);
148
149 GetContentClient()->browser()->ClearSiteData(
150 browser_context, origin, clear_cookies, clear_storage, clear_cache);
151 }
152 }
153
154 bool ClearSiteDataHeaderObserver::ParseHeader(
155 const std::string& header,
156 bool* clear_cookies, bool* clear_storage, bool* clear_cache,
nasko 2016/07/28 17:30:30 style: Each param is on a new line.
msramek 2016/08/01 16:03:19 Done.
157 std::vector<ConsoleMessage>* messages) {
158 std::unique_ptr<base::Value> parsed_header =
159 base::JSONReader::Read(header);
nasko 2016/07/28 17:30:30 Since this is untrusted data from the network, you
msramek 2016/08/01 16:03:19 I added the ASCII test for now. I'm happy to use s
160
161 if (!parsed_header) {
nasko 2016/07/28 17:30:30 I'll stop reviewing this method for now, as I thin
msramek 2016/08/01 16:03:18 I don't have a design doc at hand; I'm working wit
162 ConsoleLog(messages,
163 current_url_,
164 base::StringPrintf("%s is not a valid JSON.", header.c_str()),
165 CONSOLE_MESSAGE_LEVEL_ERROR);
166 return false;
167 }
168
169 const base::ListValue* types = nullptr;
170 if (!parsed_header->GetAsDictionary(nullptr) ||
171 !static_cast<base::DictionaryValue*>(parsed_header.get())
172 ->GetListWithoutPathExpansion(kTypesKey, &types)) {
173 ConsoleLog(messages,
174 current_url_,
175 base::StringPrintf(
176 "%s is not a JSON dictionary with a 'types' field.",
177 header.c_str()),
178 CONSOLE_MESSAGE_LEVEL_ERROR);
179 return false;
180 }
181
182 DCHECK(types);
183
184 *clear_cookies = false;
185 *clear_storage = false;
186 *clear_cache = false;
187
188 std::vector<std::string> type_names;
189 for (const std::unique_ptr<base::Value>& value : *types) {
190 std::string type;
191 value->GetAsString(&type);
192
193 bool* datatype = nullptr;
194
195 if (type == "cookies") {
196 datatype = clear_cookies;
197 } else if (type == "storage") {
198 datatype = clear_storage;
199 } else if (type == "cache") {
200 datatype = clear_cache;
201 } else {
202 std::string serialized_type;
203 JSONStringValueSerializer serializer(&serialized_type);
204 serializer.Serialize(*value);
205 ConsoleLog(messages,
206 current_url_,
207 base::StringPrintf("Invalid type: %s.",
208 serialized_type.c_str()),
209 CONSOLE_MESSAGE_LEVEL_ERROR);
210 continue;
211 }
212
213 // Each data type should only be processed once.
214 DCHECK(datatype);
215 if (*datatype)
216 continue;
217
218 *datatype = true;
219 type_names.push_back(type);
220 }
221
222 if (!*clear_cookies && !*clear_storage && !*clear_cache) {
223 ConsoleLog(messages,
224 current_url_,
225 base::StringPrintf(
226 "No valid types specified in %s.", header.c_str()),
227 CONSOLE_MESSAGE_LEVEL_ERROR);
228 return false;
229 }
230
231 // Pretty-print which types are to be cleared.
232 std::string output;
233 switch (type_names.size()) {
234 case 1:
235 output = base::StringPrintf(kClearingOneType, type_names[0].c_str());
236 break;
237 case 2:
238 output = base::StringPrintf(
239 kClearingTwoTypes, type_names[0].c_str(), type_names[1].c_str());
240 break;
241 case 3:
242 output = base::StringPrintf(
243 kClearingThreeTypes,
244 type_names[0].c_str(), type_names[1].c_str(), type_names[2].c_str());
245 break;
246 default:
247 NOTREACHED();
248 }
249 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG);
250
251 return true;
252 }
253
254 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698