OLD | NEW |
---|---|
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/content_browser_client.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/navigation_handle.h" | 18 #include "content/public/browser/browsing_data_filter_builder.h" |
19 #include "content/public/browser/browsing_data_remover.h" | |
20 #include "content/public/browser/render_frame_host.h" | |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
20 #include "content/public/common/content_client.h" | |
21 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
22 #include "content/public/common/origin_util.h" | 23 #include "content/public/common/origin_util.h" |
24 #include "content/public/common/resource_response_info.h" | |
25 #include "net/base/load_flags.h" | |
26 #include "net/base/registry_controlled_domains/registry_controlled_domain.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 |
41 // Console logging. Adds a |text| message with |level| to |messages|. | |
42 void ConsoleLog(std::vector<ClearSiteDataThrottle::ConsoleMessage>* messages, | |
43 const GURL& url, | |
44 const std::string& text, | |
45 ConsoleMessageLevel level) { | |
46 messages->push_back({url, text, level}); | |
47 } | |
48 | |
49 bool AreExperimentalFeaturesEnabled() { | 48 bool AreExperimentalFeaturesEnabled() { |
50 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 49 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
51 switches::kEnableExperimentalWebPlatformFeatures); | 50 switches::kEnableExperimentalWebPlatformFeatures); |
52 } | 51 } |
53 | 52 |
54 // Represents the parameters as a single number to be recorded in a histogram. | 53 // 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) { | 54 int ParametersMask(bool clear_cookies, bool clear_storage, bool clear_cache) { |
56 return static_cast<int>(clear_cookies) * (1 << 0) + | 55 return static_cast<int>(clear_cookies) * (1 << 0) + |
57 static_cast<int>(clear_storage) * (1 << 1) + | 56 static_cast<int>(clear_storage) * (1 << 1) + |
58 static_cast<int>(clear_cache) * (1 << 2); | 57 static_cast<int>(clear_cache) * (1 << 2); |
59 } | 58 } |
60 | 59 |
61 } // namespace | 60 // A helper function to pass an IO thread callback to a method called on |
62 | 61 // the UI thread. |
63 // static | 62 void JumpFromUIToIOThread(const base::Closure& callback) { |
64 std::unique_ptr<NavigationThrottle> | 63 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
65 ClearSiteDataThrottle::CreateThrottleForNavigation(NavigationHandle* handle) { | 64 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 } | 65 } |
71 | 66 |
72 ClearSiteDataThrottle::ClearSiteDataThrottle( | 67 // A BrowsingDataRemover::Observer that waits for |count| |
73 NavigationHandle* navigation_handle) | 68 // OnBrowsingDataRemoverDone() callbacks, translates them into |
74 : NavigationThrottle(navigation_handle), | 69 // one base::Closure, and then destroys itself. |
75 clearing_in_progress_(false), | 70 class ClearSiteDataObserver : public BrowsingDataRemover::Observer { |
76 weak_ptr_factory_(this) {} | 71 public: |
72 explicit ClearSiteDataObserver(BrowsingDataRemover* remover, | |
73 const base::Closure& callback, | |
74 int count) | |
75 : remover_(remover), callback_(callback), count_(count) { | |
76 remover_->AddObserver(this); | |
77 } | |
77 | 78 |
78 ClearSiteDataThrottle::~ClearSiteDataThrottle() { | 79 ~ClearSiteDataObserver() override { remover_->RemoveObserver(this); } |
79 // At the end of the navigation we finally have access to the correct | 80 |
80 // RenderFrameHost. Output the cached console messages. Prefix each sequence | 81 // BrowsingDataRemover::Observer. |
81 // of messages belonging to the same URL with |kConsoleMessagePrefix|. | 82 void OnBrowsingDataRemoverDone() override { |
83 DCHECK(count_); | |
84 if (--count_) | |
85 return; | |
86 | |
87 JumpFromUIToIOThread(callback_); | |
88 delete this; | |
89 } | |
90 | |
91 private: | |
92 BrowsingDataRemover* remover_; | |
93 base::Closure callback_; | |
94 int count_; | |
95 }; | |
96 | |
97 // Finds the BrowserContext associated with the request and requests | |
98 // the actual clearing of data for |origin|. The datatypes to be deleted | |
99 // are determined by |clear_cookies|, |clear_storage|, and |clear_cache|. | |
100 // |web_contents_getter| identifies the WebContents from which the request | |
101 // originated. Must be run on the UI thread. The |callback| will be executed | |
102 // on the IO thread. | |
103 void ClearSiteDataOnUIThread( | |
104 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
105 url::Origin origin, | |
106 bool clear_cookies, | |
107 bool clear_storage, | |
108 bool clear_cache, | |
109 const base::Closure& callback) { | |
110 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
111 | |
112 WebContents* web_contents = web_contents_getter.Run(); | |
113 if (!web_contents) | |
114 return; | |
115 | |
116 BrowsingDataRemover* remover = | |
117 BrowserContext::GetBrowsingDataRemover(web_contents->GetBrowserContext()); | |
118 | |
119 // ClearSiteDataObserver deletes itself when callbacks from both removal | |
120 // tasks are received. | |
121 ClearSiteDataObserver* observer = | |
122 new ClearSiteDataObserver(remover, callback, 2 /* number of tasks */); | |
123 | |
124 // Cookies and channel IDs are scoped to | |
125 // a) eTLD+1 of |origin|'s host if |origin|'s host is a registrable domain | |
126 // or a subdomain thereof | |
127 // b) |origin|'s host exactly if it is an IP address or an internal hostname | |
128 // (e.g. "localhost" or "fileserver"). | |
129 // TODO(msramek): What about plugin data? | |
130 if (clear_cookies) { | |
131 std::string domain = GetDomainAndRegistry( | |
132 origin.host(), | |
133 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
134 | |
135 if (domain.empty()) | |
136 domain = origin.host(); // IP address or internal hostname. | |
137 | |
138 std::unique_ptr<BrowsingDataFilterBuilder> domain_filter_builder( | |
139 BrowsingDataFilterBuilder::Create( | |
140 BrowsingDataFilterBuilder::WHITELIST)); | |
141 domain_filter_builder->AddRegisterableDomain(domain); | |
142 | |
143 remover->RemoveWithFilterAndReply( | |
144 base::Time(), base::Time::Max(), | |
145 BrowsingDataRemover::DATA_TYPE_COOKIES | | |
146 BrowsingDataRemover::DATA_TYPE_CHANNEL_IDS, | |
147 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB | | |
148 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB, | |
149 std::move(domain_filter_builder), observer); | |
150 } else { | |
151 // The first removal task is a no-op. | |
152 observer->OnBrowsingDataRemoverDone(); | |
153 } | |
154 | |
155 // Delete origin-scoped data. | |
156 int remove_mask = 0; | |
157 if (clear_storage) | |
158 remove_mask |= BrowsingDataRemover::DATA_TYPE_DOM_STORAGE; | |
159 if (clear_cache) | |
160 remove_mask |= BrowsingDataRemover::DATA_TYPE_CACHE; | |
161 | |
162 if (remove_mask) { | |
163 std::unique_ptr<BrowsingDataFilterBuilder> origin_filter_builder( | |
164 BrowsingDataFilterBuilder::Create( | |
165 BrowsingDataFilterBuilder::WHITELIST)); | |
166 origin_filter_builder->AddOrigin(origin); | |
167 | |
168 remover->RemoveWithFilterAndReply( | |
169 base::Time(), base::Time::Max(), remove_mask, | |
170 BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB | | |
171 BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB, | |
172 std::move(origin_filter_builder), observer); | |
173 } else { | |
174 // The second removal task is a no-op. | |
175 observer->OnBrowsingDataRemoverDone(); | |
176 } | |
177 } | |
178 | |
179 // Outputs |messages| to the console of WebContents retrieved from | |
180 // |web_contents_getter|. Must be run on the UI thread. | |
181 void OutputMessagesOnUIThread( | |
182 const ResourceRequestInfo::WebContentsGetter& web_contents_getter, | |
183 const std::vector<ClearSiteDataThrottle::ConsoleMessagesDelegate::Message>& | |
184 messages) { | |
185 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
186 | |
187 WebContents* web_contents = web_contents_getter.Run(); | |
188 if (!web_contents) | |
189 return; | |
190 | |
191 // Prefix each sequence of messages belonging to the same URL with | |
192 // |kConsoleMessagePrefix|. | |
82 GURL last_seen_url; | 193 GURL last_seen_url; |
83 for (const ConsoleMessage& message : messages_) { | 194 for (const auto& message : messages) { |
84 if (message.url == last_seen_url) { | 195 if (message.url == last_seen_url) { |
85 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | 196 web_contents->GetMainFrame()->AddMessageToConsole(message.level, |
86 message.level, message.text); | 197 message.text); |
87 } else { | 198 } else { |
88 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | 199 web_contents->GetMainFrame()->AddMessageToConsole( |
89 message.level, | 200 message.level, |
90 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), | 201 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), |
91 message.text.c_str())); | 202 message.text.c_str())); |
92 } | 203 } |
93 | 204 |
94 last_seen_url = message.url; | 205 last_seen_url = message.url; |
95 } | 206 } |
96 } | 207 } |
97 | 208 |
98 ClearSiteDataThrottle::ThrottleCheckResult | 209 } // namespace |
99 ClearSiteDataThrottle::WillStartRequest() { | 210 |
100 current_url_ = navigation_handle()->GetURL(); | 211 //////////////////////////////////////////////////////////////////////////////// |
101 return PROCEED; | 212 // ConsoleMessagesDelegate |
213 | |
214 ClearSiteDataThrottle::ConsoleMessagesDelegate::ConsoleMessagesDelegate() {} | |
215 | |
216 ClearSiteDataThrottle::ConsoleMessagesDelegate::~ConsoleMessagesDelegate() {} | |
217 | |
218 void ClearSiteDataThrottle::ConsoleMessagesDelegate::AddMessage( | |
219 const GURL& url, | |
220 const std::string& text, | |
221 ConsoleMessageLevel level) { | |
222 messages_.push_back({url, text, level}); | |
102 } | 223 } |
103 | 224 |
104 ClearSiteDataThrottle::ThrottleCheckResult | 225 void ClearSiteDataThrottle::ConsoleMessagesDelegate::OutputMessages( |
105 ClearSiteDataThrottle::WillRedirectRequest() { | 226 const ResourceRequestInfo::WebContentsGetter& web_contents_getter) { |
106 // We are processing a redirect from url1 to url2. GetResponseHeaders() | 227 if (messages_.empty()) |
107 // contains headers from url1, but GetURL() is already equal to url2. Handle | 228 return; |
108 // the headers before updating the URL, so that |current_url_| corresponds | |
109 // to the URL that sent the headers. | |
110 HandleHeader(); | |
111 current_url_ = navigation_handle()->GetURL(); | |
112 | 229 |
113 return clearing_in_progress_ ? DEFER : PROCEED; | 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
231 BrowserThread::PostTask( | |
232 BrowserThread::UI, FROM_HERE, | |
233 base::Bind(&OutputMessagesOnUIThread, web_contents_getter, | |
234 std::move(messages_))); | |
235 | |
236 messages_.clear(); | |
114 } | 237 } |
115 | 238 |
116 ClearSiteDataThrottle::ThrottleCheckResult | 239 //////////////////////////////////////////////////////////////////////////////// |
117 ClearSiteDataThrottle::WillProcessResponse() { | 240 // ClearSiteDataThrottle |
118 HandleHeader(); | 241 |
119 return clearing_in_progress_ ? DEFER : PROCEED; | 242 // static |
243 std::unique_ptr<ResourceThrottle> | |
244 ClearSiteDataThrottle::CreateThrottleForRequest(net::URLRequest* request) { | |
245 // This is an experimental feature. | |
246 if (!AreExperimentalFeaturesEnabled()) | |
247 return std::unique_ptr<ResourceThrottle>(); | |
248 | |
249 // The throttle has no purpose if the request has no ResourceRequestInfo, | |
250 // because we won't be able to determine whose data should be deleted. | |
251 if (!ResourceRequestInfo::ForRequest(request)) | |
252 return std::unique_ptr<ResourceThrottle>(); | |
253 | |
254 return base::WrapUnique(new ClearSiteDataThrottle( | |
255 request, base::MakeUnique<ConsoleMessagesDelegate>())); | |
120 } | 256 } |
121 | 257 |
122 const char* ClearSiteDataThrottle::GetNameForLogging() { | 258 ClearSiteDataThrottle::ClearSiteDataThrottle( |
123 return "ClearSiteDataThrottle"; | 259 net::URLRequest* request, |
260 std::unique_ptr<ConsoleMessagesDelegate> delegate) | |
261 : request_(request), | |
262 delegate_(std::move(delegate)), | |
263 weak_ptr_factory_(this) { | |
264 DCHECK(request_); | |
265 DCHECK(delegate_); | |
124 } | 266 } |
125 | 267 |
126 void ClearSiteDataThrottle::HandleHeader() { | 268 ClearSiteDataThrottle::~ClearSiteDataThrottle() { |
127 NavigationHandleImpl* handle = | 269 // Output the cached console messages. We output console messages when the |
128 static_cast<NavigationHandleImpl*>(navigation_handle()); | 270 // request is finished rather than in real time, since in case of navigations |
129 const net::HttpResponseHeaders* headers = handle->GetResponseHeaders(); | 271 // swapping RenderFrameHost would cause the outputs to disappear. |
272 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | |
273 if (info) | |
274 delegate_->OutputMessages(info->GetWebContentsGetterForRequest()); | |
275 } | |
130 | 276 |
131 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) | 277 void ClearSiteDataThrottle::WillRedirectRequest( |
132 return; | 278 const net::RedirectInfo& redirect_info, |
279 bool* defer) { | |
280 *defer = HandleHeader(); | |
281 } | |
133 | 282 |
134 // Only accept the header on secure origins. | 283 void ClearSiteDataThrottle::WillProcessResponse(bool* defer) { |
135 if (!IsOriginSecure(current_url_)) { | 284 *defer = HandleHeader(); |
136 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", | 285 } |
137 CONSOLE_MESSAGE_LEVEL_ERROR); | 286 |
138 return; | 287 const char* ClearSiteDataThrottle::GetNameForLogging() const { |
288 return kNameForLogging; | |
289 } | |
290 | |
291 bool ClearSiteDataThrottle::HandleHeader() { | |
292 const net::HttpResponseHeaders* headers = GetResponseHeaders(); | |
293 | |
294 std::string header_value; | |
295 if (!headers || | |
296 !headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value)) { | |
297 return false; | |
139 } | 298 } |
140 | 299 |
141 std::string header_value; | 300 // Only accept the header on secure non-unique origins. |
142 headers->GetNormalizedHeader(kClearSiteDataHeader, &header_value); | 301 if (!IsOriginSecure(request_->url())) { |
302 delegate_->AddMessage(request_->url(), | |
303 "Not supported for insecure origins.", | |
304 CONSOLE_MESSAGE_LEVEL_ERROR); | |
305 return false; | |
306 } | |
307 | |
308 url::Origin origin(request_->url()); | |
309 if (origin.unique()) { | |
310 delegate_->AddMessage(request_->url(), "Not supported for unique origins.", | |
311 CONSOLE_MESSAGE_LEVEL_ERROR); | |
312 return false; | |
313 } | |
314 | |
315 // The LOAD_DO_NOT_SAVE_COOKIES flag prohibits the request from doing any | |
316 // modification to cookies. Clear-Site-Data applies this restriction to other | |
317 // datatypes as well. | |
318 if (request_->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES) { | |
319 delegate_->AddMessage( | |
320 request_->url(), | |
321 "The request's credentials mode prohibits modifying cookies " | |
322 "and other local data.", | |
323 CONSOLE_MESSAGE_LEVEL_ERROR); | |
324 return false; | |
325 } | |
326 | |
327 // Service workers can handle fetches of third-party resources and inject | |
328 // arbitrary headers. Ignore responses that came from a service worker, | |
329 // as supporting Clear-Site-Data would give them the power to delete data from | |
330 // any website. | |
falken
2017/05/17 07:37:00
Maybe worth linking to https://w3c.github.io/webap
msramek
2017/05/17 13:46:37
Done.
| |
331 const ServiceWorkerResponseInfo* response_info = | |
332 ServiceWorkerResponseInfo::ForRequest(request_); | |
333 if (response_info) { | |
334 ResourceResponseInfo extra_response_info; | |
335 response_info->GetExtraResponseInfo(&extra_response_info); | |
336 | |
337 if (extra_response_info.was_fetched_via_service_worker) { | |
falken
2017/05/17 07:37:00
Does the spec care if the response came from a for
msramek
2017/05/17 13:46:37
Yes, that was my understanding too - thanks for co
| |
338 delegate_->AddMessage( | |
339 request_->url(), | |
340 "Ignoring, as the response came from a service worker.", | |
341 CONSOLE_MESSAGE_LEVEL_ERROR); | |
342 return false; | |
343 } | |
344 } | |
143 | 345 |
144 bool clear_cookies; | 346 bool clear_cookies; |
145 bool clear_storage; | 347 bool clear_storage; |
146 bool clear_cache; | 348 bool clear_cache; |
147 | 349 |
148 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, | 350 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, |
149 &messages_)) { | 351 delegate_.get(), request_->url())) { |
150 return; | 352 return false; |
151 } | 353 } |
152 | 354 |
355 // If the header is valid, clear the data for this browser context and origin. | |
356 clearing_started_ = base::TimeTicks::Now(); | |
357 | |
153 // Record the call parameters. | 358 // Record the call parameters. |
154 UMA_HISTOGRAM_ENUMERATION( | 359 UMA_HISTOGRAM_ENUMERATION( |
155 "Navigation.ClearSiteData.Parameters", | 360 "Navigation.ClearSiteData.Parameters", |
156 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); | 361 ParametersMask(clear_cookies, clear_storage, clear_cache), (1 << 3)); |
157 | 362 |
158 // If the header is valid, clear the data for this browser context and origin. | 363 base::WeakPtr<ClearSiteDataThrottle> weak_ptr = |
159 BrowserContext* browser_context = | 364 weak_ptr_factory_.GetWeakPtr(); |
160 navigation_handle()->GetWebContents()->GetBrowserContext(); | |
161 url::Origin origin(current_url_); | |
162 | 365 |
163 if (origin.unique()) { | 366 // Immediately bind the weak pointer to the current thread (IO). This will |
164 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", | 367 // make a potential misuse on the UI thread DCHECK immediately rather than |
165 CONSOLE_MESSAGE_LEVEL_ERROR); | 368 // later when it's correctly used on the IO thread again. |
166 return; | 369 weak_ptr.get(); |
167 } | |
168 | 370 |
169 clearing_in_progress_ = true; | 371 ExecuteClearingTask( |
170 clearing_started_ = base::TimeTicks::Now(); | 372 origin, clear_cookies, clear_storage, clear_cache, |
171 GetContentClient()->browser()->ClearSiteData( | 373 base::Bind(&ClearSiteDataThrottle::TaskFinished, weak_ptr)); |
172 browser_context, origin, clear_cookies, clear_storage, clear_cache, | 374 |
173 base::Bind(&ClearSiteDataThrottle::TaskFinished, | 375 return true; |
174 weak_ptr_factory_.GetWeakPtr())); | |
175 } | 376 } |
176 | 377 |
378 // static | |
177 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, | 379 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, |
178 bool* clear_cookies, | 380 bool* clear_cookies, |
179 bool* clear_storage, | 381 bool* clear_storage, |
180 bool* clear_cache, | 382 bool* clear_cache, |
181 std::vector<ConsoleMessage>* messages) { | 383 ConsoleMessagesDelegate* delegate, |
384 const GURL& current_url) { | |
182 if (!base::IsStringASCII(header)) { | 385 if (!base::IsStringASCII(header)) { |
183 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", | 386 delegate->AddMessage(current_url, "Must only contain ASCII characters.", |
184 CONSOLE_MESSAGE_LEVEL_ERROR); | 387 CONSOLE_MESSAGE_LEVEL_ERROR); |
185 return false; | 388 return false; |
186 } | 389 } |
187 | 390 |
188 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); | 391 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); |
189 | 392 |
190 if (!parsed_header) { | 393 if (!parsed_header) { |
191 ConsoleLog(messages, current_url_, "Not a valid JSON.", | 394 delegate->AddMessage(current_url, "Not a valid JSON.", |
192 CONSOLE_MESSAGE_LEVEL_ERROR); | 395 CONSOLE_MESSAGE_LEVEL_ERROR); |
193 return false; | 396 return false; |
194 } | 397 } |
195 | 398 |
196 const base::DictionaryValue* dictionary = nullptr; | 399 const base::DictionaryValue* dictionary = nullptr; |
197 const base::ListValue* types = nullptr; | 400 const base::ListValue* types = nullptr; |
198 if (!parsed_header->GetAsDictionary(&dictionary) || | 401 if (!parsed_header->GetAsDictionary(&dictionary) || |
199 !dictionary->GetListWithoutPathExpansion(kTypesKey, &types)) { | 402 !dictionary->GetListWithoutPathExpansion(kTypesKey, &types)) { |
200 ConsoleLog(messages, current_url_, | 403 delegate->AddMessage(current_url, |
201 "Expecting a JSON dictionary with a 'types' field.", | 404 "Expecting a JSON dictionary with a 'types' field.", |
202 CONSOLE_MESSAGE_LEVEL_ERROR); | 405 CONSOLE_MESSAGE_LEVEL_ERROR); |
203 return false; | 406 return false; |
204 } | 407 } |
205 | 408 |
206 DCHECK(types); | 409 DCHECK(types); |
207 | 410 |
208 *clear_cookies = false; | 411 *clear_cookies = false; |
209 *clear_storage = false; | 412 *clear_storage = false; |
210 *clear_cache = false; | 413 *clear_cache = false; |
211 | 414 |
212 std::vector<std::string> type_names; | 415 std::vector<std::string> type_names; |
213 for (const base::Value& value : *types) { | 416 for (const base::Value& value : *types) { |
214 std::string type; | 417 std::string type; |
215 value.GetAsString(&type); | 418 value.GetAsString(&type); |
216 | 419 |
217 bool* datatype = nullptr; | 420 bool* datatype = nullptr; |
218 | 421 |
219 if (type == "cookies") { | 422 if (type == "cookies") { |
220 datatype = clear_cookies; | 423 datatype = clear_cookies; |
221 } else if (type == "storage") { | 424 } else if (type == "storage") { |
222 datatype = clear_storage; | 425 datatype = clear_storage; |
223 } else if (type == "cache") { | 426 } else if (type == "cache") { |
224 datatype = clear_cache; | 427 datatype = clear_cache; |
225 } else { | 428 } else { |
226 std::string serialized_type; | 429 std::string serialized_type; |
227 JSONStringValueSerializer serializer(&serialized_type); | 430 JSONStringValueSerializer serializer(&serialized_type); |
228 serializer.Serialize(value); | 431 serializer.Serialize(value); |
229 ConsoleLog( | 432 delegate->AddMessage( |
230 messages, current_url_, | 433 current_url, |
231 base::StringPrintf("Invalid type: %s.", serialized_type.c_str()), | 434 base::StringPrintf("Unrecognized type: %s.", serialized_type.c_str()), |
232 CONSOLE_MESSAGE_LEVEL_ERROR); | 435 CONSOLE_MESSAGE_LEVEL_ERROR); |
233 continue; | 436 continue; |
234 } | 437 } |
235 | 438 |
236 // Each data type should only be processed once. | 439 // Each data type should only be processed once. |
237 DCHECK(datatype); | 440 DCHECK(datatype); |
238 if (*datatype) | 441 if (*datatype) |
239 continue; | 442 continue; |
240 | 443 |
241 *datatype = true; | 444 *datatype = true; |
242 type_names.push_back(type); | 445 type_names.push_back(type); |
243 } | 446 } |
244 | 447 |
245 if (!*clear_cookies && !*clear_storage && !*clear_cache) { | 448 if (!*clear_cookies && !*clear_storage && !*clear_cache) { |
246 ConsoleLog(messages, current_url_, | 449 delegate->AddMessage(current_url, |
247 "No valid types specified in the 'types' field.", | 450 "No recognized types specified in the 'types' field.", |
248 CONSOLE_MESSAGE_LEVEL_ERROR); | 451 CONSOLE_MESSAGE_LEVEL_ERROR); |
249 return false; | 452 return false; |
250 } | 453 } |
251 | 454 |
252 // Pretty-print which types are to be cleared. | 455 // Pretty-print which types are to be cleared. |
253 std::string output; | 456 std::string output; |
254 switch (type_names.size()) { | 457 switch (type_names.size()) { |
255 case 1: | 458 case 1: |
256 output = base::StringPrintf(kClearingOneType, type_names[0].c_str()); | 459 output = base::StringPrintf(kClearingOneType, type_names[0].c_str()); |
257 break; | 460 break; |
258 case 2: | 461 case 2: |
259 output = base::StringPrintf(kClearingTwoTypes, type_names[0].c_str(), | 462 output = base::StringPrintf(kClearingTwoTypes, type_names[0].c_str(), |
260 type_names[1].c_str()); | 463 type_names[1].c_str()); |
261 break; | 464 break; |
262 case 3: | 465 case 3: |
263 output = base::StringPrintf(kClearingThreeTypes, type_names[0].c_str(), | 466 output = base::StringPrintf(kClearingThreeTypes, type_names[0].c_str(), |
264 type_names[1].c_str(), type_names[2].c_str()); | 467 type_names[1].c_str(), type_names[2].c_str()); |
265 break; | 468 break; |
266 default: | 469 default: |
267 NOTREACHED(); | 470 NOTREACHED(); |
268 } | 471 } |
269 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_INFO); | 472 delegate->AddMessage(current_url, output, CONSOLE_MESSAGE_LEVEL_INFO); |
270 | 473 |
271 return true; | 474 return true; |
272 } | 475 } |
273 | 476 |
477 const net::HttpResponseHeaders* ClearSiteDataThrottle::GetResponseHeaders() | |
478 const { | |
479 return request_->response_headers(); | |
480 } | |
481 | |
482 void ClearSiteDataThrottle::ExecuteClearingTask(const url::Origin& origin, | |
483 bool clear_cookies, | |
484 bool clear_storage, | |
485 bool clear_cache, | |
486 const base::Closure& callback) { | |
487 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
488 BrowserThread::PostTask( | |
489 BrowserThread::UI, FROM_HERE, | |
490 base::Bind(&ClearSiteDataOnUIThread, | |
491 ResourceRequestInfo::ForRequest(request_) | |
492 ->GetWebContentsGetterForRequest(), | |
493 origin, clear_cookies, clear_storage, clear_cache, callback)); | |
494 } | |
495 | |
274 void ClearSiteDataThrottle::TaskFinished() { | 496 void ClearSiteDataThrottle::TaskFinished() { |
275 DCHECK(clearing_in_progress_); | |
276 clearing_in_progress_ = false; | |
277 | |
278 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", | 497 UMA_HISTOGRAM_CUSTOM_TIMES("Navigation.ClearSiteData.Duration", |
279 base::TimeTicks::Now() - clearing_started_, | 498 base::TimeTicks::Now() - clearing_started_, |
280 base::TimeDelta::FromMilliseconds(1), | 499 base::TimeDelta::FromMilliseconds(1), |
281 base::TimeDelta::FromSeconds(1), 50); | 500 base::TimeDelta::FromSeconds(1), 50); |
282 | 501 |
283 navigation_handle()->Resume(); | 502 Resume(); |
284 } | 503 } |
285 | 504 |
286 } // namespace content | 505 } // namespace content |
OLD | NEW |