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

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

Issue 2368923003: Support the Clear-Site-Data header on resource requests (Closed)
Patch Set: Addressed comments. Created 4 years, 2 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/browsing_data/clear_site_data_throttle.h" 5 #include "content/browser/browsing_data/clear_site_data_throttle.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "content/browser/frame_host/navigation_handle_impl.h" 15 #include "content/browser/service_worker/service_worker_response_info.h"
16 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/content_browser_client.h" 18 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/browser/navigation_handle.h" 19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/resource_controller.h"
21 #include "content/public/browser/storage_partition.h"
19 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/content_client.h" 23 #include "content/public/common/content_client.h"
21 #include "content/public/common/content_switches.h" 24 #include "content/public/common/content_switches.h"
22 #include "content/public/common/origin_util.h" 25 #include "content/public/common/origin_util.h"
26 #include "net/base/load_flags.h"
23 #include "net/http/http_response_headers.h" 27 #include "net/http/http_response_headers.h"
28 #include "net/url_request/redirect_info.h"
24 #include "url/gurl.h" 29 #include "url/gurl.h"
25 #include "url/origin.h" 30 #include "url/origin.h"
26 31
27 namespace content { 32 namespace content {
28 33
29 namespace { 34 namespace {
30 35
36 static const char* kNameForLogging = "ClearSiteDataThrottle";
37
31 static const char* kClearSiteDataHeader = "Clear-Site-Data"; 38 static const char* kClearSiteDataHeader = "Clear-Site-Data";
32 39
33 static const char* kTypesKey = "types"; 40 static const char* kTypesKey = "types";
34 41
35 // Pretty-printed log output. 42 // Pretty-printed log output.
36 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; 43 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s";
37 static const char* kClearingOneType = "Clearing %s."; 44 static const char* kClearingOneType = "Clearing %s.";
38 static const char* kClearingTwoTypes = "Clearing %s and %s."; 45 static const char* kClearingTwoTypes = "Clearing %s and %s.";
39 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; 46 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s.";
40 47
(...skipping 10 matching lines...) Expand all
51 switches::kEnableExperimentalWebPlatformFeatures); 58 switches::kEnableExperimentalWebPlatformFeatures);
52 } 59 }
53 60
54 // Represents the parameters as a single number to be recorded in a histogram. 61 // Represents the parameters as a single number to be recorded in a histogram.
55 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { 62 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) {
56 return static_cast<int>(clear_cookies) * (1 << 0) + 63 return static_cast<int>(clear_cookies) * (1 << 0) +
57 static_cast<int>(clear_storage) * (1 << 1) + 64 static_cast<int>(clear_storage) * (1 << 1) +
58 static_cast<int>(clear_cache) * (1 << 2); 65 static_cast<int>(clear_cache) * (1 << 2);
59 } 66 }
60 67
61 } // namespace 68 // A helper function to pass an IO thread callback to a method called on
62 69 // the UI thread.
63 // static 70 void JumpFromUIToIOThread(const base::Closure& callback) {
64 std::unique_ptr<NavigationThrottle> 71 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 ClearSiteDataThrottle::CreateThrottleForNavigation(NavigationHandle* handle) { 72 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback);
66 if (AreExperimentalFeaturesEnabled())
67 return base::WrapUnique(new ClearSiteDataThrottle(handle));
68
69 return std::unique_ptr<NavigationThrottle>();
70 } 73 }
71 74
72 ClearSiteDataThrottle::ClearSiteDataThrottle( 75 // Finds the BrowserContext associated with the request and requests
73 NavigationHandle* navigation_handle) 76 // the actual clearing of data for |origin|. The datatypes to be deleted
74 : NavigationThrottle(navigation_handle), 77 // are determined by |clear_cookies|, |clear_storage|, and |clear_cache|.
75 clearing_in_progress_(false), 78 // |web_contents_getter| identifies the WebContents from which the request
76 weak_ptr_factory_(this) {} 79 // originated. Must be run on the UI thread. The |callback| will be executed
80 // on the IO thread.
81 void ClearSiteDataOnUIThread(
82 const ResourceRequestInfo::WebContentsGetter& web_contents_getter,
83 url::Origin origin,
84 bool clear_cookies,
85 bool clear_storage,
86 bool clear_cache,
87 const base::Closure& callback) {
88 DCHECK_CURRENTLY_ON(BrowserThread::UI);
77 89
78 ClearSiteDataThrottle::~ClearSiteDataThrottle() { 90 WebContents* web_contents = web_contents_getter.Run();
79 // At the end of the navigation we finally have access to the correct 91 if (!web_contents)
80 // RenderFrameHost. Output the cached console messages. Prefix each sequence 92 return;
81 // of messages belonging to the same URL with |kConsoleMessagePrefix|. 93
94 BrowserContext* browser_context = web_contents->GetBrowserContext();
95
96 GetContentClient()->browser()->ClearSiteData(
97 browser_context, origin, clear_cookies, clear_storage, clear_cache,
98 base::Bind(&JumpFromUIToIOThread, callback));
99 }
100
101 // Outputs |messages| to the console of WebContents retrieved from
102 // |web_contents_getter|. Must be run on the UI thread.
103 void OutputConsoleMessagesOnUIThread(
104 const ResourceRequestInfo::WebContentsGetter& web_contents_getter,
105 const std::vector<ClearSiteDataThrottle::ConsoleMessage>& messages) {
106 DCHECK_CURRENTLY_ON(BrowserThread::UI);
107
108 WebContents* web_contents = web_contents_getter.Run();
109 if (!web_contents)
110 return;
111
82 GURL last_seen_url; 112 GURL last_seen_url;
83 for (const ConsoleMessage& message : messages_) { 113 for (const ClearSiteDataThrottle::ConsoleMessage& message : messages) {
84 if (message.url == last_seen_url) { 114 if (message.url == last_seen_url) {
85 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( 115 web_contents->GetMainFrame()->AddMessageToConsole(message.level,
86 message.level, message.text); 116 message.text);
87 } else { 117 } else {
88 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( 118 web_contents->GetMainFrame()->AddMessageToConsole(
89 message.level, 119 message.level,
90 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), 120 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(),
91 message.text.c_str())); 121 message.text.c_str()));
92 } 122 }
93 123
94 last_seen_url = message.url; 124 last_seen_url = message.url;
95 } 125 }
96 } 126 }
97 127
98 ClearSiteDataThrottle::ThrottleCheckResult 128 } // namespace
99 ClearSiteDataThrottle::WillStartRequest() { 129
100 current_url_ = navigation_handle()->GetURL(); 130 // static
101 return PROCEED; 131 std::unique_ptr<ResourceThrottle>
132 ClearSiteDataThrottle::CreateThrottleForRequest(net::URLRequest* request) {
133 // This is an experimental feature.
134 if (!AreExperimentalFeaturesEnabled())
135 return std::unique_ptr<ResourceThrottle>();
136
137 // The LOAD_DO_NOT_SAVE_COOKIES flag prohibits the request from doing any
138 // modification to cookies.
139 std::unique_ptr<base::Value> value = request->GetStateAsValue();
140 const base::DictionaryValue* state;
141 DCHECK(value->GetAsDictionary(&state));
142 int load_flags = 0;
143 bool success =
144 state->GetIntegerWithoutPathExpansion("load_flags", &load_flags);
145 DCHECK(success);
146 if (load_flags & net::LOAD_DO_NOT_SAVE_COOKIES)
147 return std::unique_ptr<ResourceThrottle>();
148
149 // The throttle has no purpose if the request has no ResourceRequestInfo,
150 // because we won't be able to determine whose data should be deleted.
151 if (!ResourceRequestInfo::ForRequest(request))
152 return std::unique_ptr<ResourceThrottle>();
153
154 return base::WrapUnique(new ClearSiteDataThrottle(request));
102 } 155 }
103 156
104 ClearSiteDataThrottle::ThrottleCheckResult 157 ClearSiteDataThrottle::ClearSiteDataThrottle(net::URLRequest* request)
105 ClearSiteDataThrottle::WillRedirectRequest() { 158 : request_(request),
159 clearing_in_progress_(false),
160 weak_ptr_factory_(this) {}
161
162 ClearSiteDataThrottle::~ClearSiteDataThrottle() {
163 // Output the cached console messages. Prefix each sequence of messages
164 // belonging to the same URL with |kConsoleMessagePrefix|. We output console
165 // messages when the request is finished rather than in real time, since in
166 // case of navigations swapping RenderFrameHost would cause the outputs to
167 // disappear.
168 if (messages_.empty())
169 return;
170
171 DCHECK_CURRENTLY_ON(BrowserThread::IO);
172 BrowserThread::PostTask(
173 BrowserThread::UI, FROM_HERE,
174 base::Bind(&OutputConsoleMessagesOnUIThread,
175 ResourceRequestInfo::ForRequest(request_)
176 ->GetWebContentsGetterForRequest(),
177 std::move(messages_)));
178 }
179
180 void ClearSiteDataThrottle::WillStartRequest(bool* defer) {
181 current_url_ = request_->original_url();
182 *defer = false;
183 }
184
185 void ClearSiteDataThrottle::WillRedirectRequest(
186 const net::RedirectInfo& redirect_info,
187 bool* defer) {
106 // We are processing a redirect from url1 to url2. GetResponseHeaders() 188 // We are processing a redirect from url1 to url2. GetResponseHeaders()
107 // contains headers from url1, but GetURL() is already equal to url2. Handle 189 // contains headers from url1, but GetURL() is already equal to url2. Handle
108 // the headers before updating the URL, so that |current_url_| corresponds 190 // the headers before updating the URL, so that |current_url_| corresponds
109 // to the URL that sent the headers. 191 // to the URL that sent the headers.
110 HandleHeader(); 192 HandleHeader();
111 current_url_ = navigation_handle()->GetURL(); 193 current_url_ = redirect_info.new_url;
112 194 *defer = clearing_in_progress_;
113 return clearing_in_progress_ ? DEFER : PROCEED;
114 } 195 }
115 196
116 ClearSiteDataThrottle::ThrottleCheckResult 197 void ClearSiteDataThrottle::WillProcessResponse(bool* defer) {
117 ClearSiteDataThrottle::WillProcessResponse() {
118 HandleHeader(); 198 HandleHeader();
119 return clearing_in_progress_ ? DEFER : PROCEED; 199 *defer = clearing_in_progress_;
200 }
201
202 const char* ClearSiteDataThrottle::GetNameForLogging() const {
203 return kNameForLogging;
120 } 204 }
121 205
122 void ClearSiteDataThrottle::HandleHeader() { 206 void ClearSiteDataThrottle::HandleHeader() {
123 NavigationHandleImpl* handle = 207 const net::HttpResponseHeaders* headers = request_->response_headers();
124 static_cast<NavigationHandleImpl*>(navigation_handle());
125 const net::HttpResponseHeaders* headers = handle->GetResponseHeaders();
126 208
127 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) 209 if (!headers || !headers->HasHeader(kClearSiteDataHeader))
128 return; 210 return;
129 211
130 // Only accept the header on secure origins. 212 // Only accept the header on secure origins.
131 if (!IsOriginSecure(current_url_)) { 213 if (!IsOriginSecure(current_url_)) {
132 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", 214 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.",
133 CONSOLE_MESSAGE_LEVEL_ERROR); 215 CONSOLE_MESSAGE_LEVEL_ERROR);
134 return; 216 return;
135 } 217 }
136 218
219 // Service workers can handle fetches of third-party resources and inject
220 // arbitrary headers. Supporting Clear-Site-Data would give them the power
221 // to delete data from any website.
222 if (ServiceWorkerResponseInfo::ForRequest(request_)) {
223 ConsoleLog(&messages_, current_url_,
224 "Ignoring, as the response came from a service worker.",
225 CONSOLE_MESSAGE_LEVEL_ERROR);
226 return;
227 }
228
137 std::string header_value; 229 std::string header_value;
138 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value); 230 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value);
139 231
140 bool clear_cookies; 232 bool clear_cookies;
141 bool clear_storage; 233 bool clear_storage;
142 bool clear_cache; 234 bool clear_cache;
143 235
144 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, 236 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache,
145 &messages_)) { 237 &messages_)) {
146 return; 238 return;
147 } 239 }
148 240
149 // Record the call parameters. 241 // Record the call parameters.
150 UMA_HISTOGRAM_ENUMERATION( 242 UMA_HISTOGRAM_ENUMERATION(
151 "Navigation.ClearSiteData.Parameters", 243 "Navigation.ClearSiteData.Parameters",
152 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); 244 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3));
153 245
154 // If the header is valid, clear the data for this browser context and origin. 246 // If the header is valid, clear the data for this browser context and origin.
155 BrowserContext* browser_context =
156 navigation_handle()->GetWebContents()->GetBrowserContext();
157 url::Origin origin(current_url_); 247 url::Origin origin(current_url_);
158
159 if (origin.unique()) { 248 if (origin.unique()) {
160 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", 249 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.",
161 CONSOLE_MESSAGE_LEVEL_ERROR); 250 CONSOLE_MESSAGE_LEVEL_ERROR);
162 return; 251 return;
163 } 252 }
164 253
165 clearing_in_progress_ = true; 254 clearing_in_progress_ = true;
166 clearing_started_ = base::TimeTicks::Now(); 255 clearing_started_ = base::TimeTicks::Now();
167 GetContentClient()->browser()->ClearSiteData( 256
168 browser_context, origin, clear_cookies, clear_storage, clear_cache, 257 DCHECK_CURRENTLY_ON(BrowserThread::IO);
169 base::Bind(&ClearSiteDataThrottle::TaskFinished, 258 BrowserThread::PostTask(
170 weak_ptr_factory_.GetWeakPtr())); 259 BrowserThread::UI, FROM_HERE,
260 base::Bind(&ClearSiteDataOnUIThread,
261 ResourceRequestInfo::ForRequest(request_)
262 ->GetWebContentsGetterForRequest(),
263 origin, clear_cookies, clear_storage, clear_cache,
264 base::Bind(&ClearSiteDataThrottle::TaskFinished,
265 weak_ptr_factory_.GetWeakPtr())));
171 } 266 }
172 267
173 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, 268 bool ClearSiteDataThrottle::ParseHeader(const std::string& header,
174 bool* clear_cookies, 269 bool* clear_cookies,
175 bool* clear_storage, 270 bool* clear_storage,
176 bool* clear_cache, 271 bool* clear_cache,
177 std::vector<ConsoleMessage>* messages) { 272 std::vector<ConsoleMessage>* messages) {
178 if (!base::IsStringASCII(header)) { 273 if (!base::IsStringASCII(header)) {
179 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", 274 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.",
180 CONSOLE_MESSAGE_LEVEL_ERROR); 275 CONSOLE_MESSAGE_LEVEL_ERROR);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 break; 356 break;
262 default: 357 default:
263 NOTREACHED(); 358 NOTREACHED();
264 } 359 }
265 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); 360 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG);
266 361
267 return true; 362 return true;
268 } 363 }
269 364
270 void ClearSiteDataThrottle::TaskFinished() { 365 void ClearSiteDataThrottle::TaskFinished() {
366 DCHECK_CURRENTLY_ON(BrowserThread::IO);
271 DCHECK(clearing_in_progress_); 367 DCHECK(clearing_in_progress_);
272 clearing_in_progress_ = false; 368 clearing_in_progress_ = false;
273 369
274 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", 370 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration",
275 base::TimeTicks::Now() - clearing_started_, 371 base::TimeTicks::Now() - clearing_started_,
276 base::TimeDelta::FromMilliseconds(1), 372 base::TimeDelta::FromMilliseconds(1),
277 base::TimeDelta::FromSeconds(1), 50); 373 base::TimeDelta::FromSeconds(1), 50);
278 374
279 navigation_handle()->Resume(); 375 controller()->Resume();
280 } 376 }
281 377
282 } // namespace content 378 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698