1 // Copyright (c) 2012 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.
5 #include "chrome/browser/browsing_data/browsing_data_remover.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
15 #include "base/prefs/pref_service.h"
16 #include "chrome/browser/autofill/personal_data_manager_factory.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/browsing_data/browsing_data_helper.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/content_settings/host_content_settings_map.h"
21 #include "chrome/browser/domain_reliability/service_factory.h"
22 #include "chrome/browser/download/download_prefs.h"
23 #include "chrome/browser/download/download_service_factory.h"
24 #include "chrome/browser/history/history_service.h"
25 #include "chrome/browser/history/history_service_factory.h"
26 #include "chrome/browser/io_thread.h"
27 #include "chrome/browser/media/media_device_id_salt.h"
28 #include "chrome/browser/net/predictor.h"
29 #include "chrome/browser/password_manager/password_store_factory.h"
30 #include "chrome/browser/predictors/logged_in_predictor_table.h"
31 #include "chrome/browser/predictors/predictor_database.h"
32 #include "chrome/browser/predictors/predictor_database_factory.h"
33 #include "chrome/browser/prerender/prerender_manager.h"
34 #include "chrome/browser/prerender/prerender_manager_factory.h"
35 #include "chrome/browser/profiles/profile.h"
36 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
37 #include "chrome/browser/search_engines/template_url_service_factory.h"
38 #include "chrome/browser/sessions/session_service.h"
39 #include "chrome/browser/sessions/session_service_factory.h"
40 #include "chrome/browser/sessions/tab_restore_service.h"
41 #include "chrome/browser/sessions/tab_restore_service_factory.h"
42 #include "chrome/browser/webdata/web_data_service_factory.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/common/url_constants.h"
45 #include "components/autofill/core/browser/personal_data_manager.h"
46 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
47 #include "components/domain_reliability/service.h"
48 #include "components/nacl/browser/nacl_browser.h"
49 #include "components/nacl/browser/pnacl_host.h"
50 #include "components/password_manager/core/browser/password_store.h"
51 #include "components/power/origin_power_map.h"
52 #include "components/power/origin_power_map_factory.h"
53 #include "components/search_engines/template_url_service.h"
54 #include "components/web_cache/browser/web_cache_manager.h"
55 #include "content/public/browser/browser_thread.h"
56 #include "content/public/browser/dom_storage_context.h"
57 #include "content/public/browser/download_manager.h"
58 #include "content/public/browser/local_storage_usage_info.h"
59 #include "content/public/browser/notification_service.h"
60 #include "content/public/browser/plugin_data_remover.h"
61 #include "content/public/browser/session_storage_usage_info.h"
62 #include "content/public/browser/ssl_host_state_delegate.h"
63 #include "content/public/browser/storage_partition.h"
64 #include "content/public/browser/user_metrics.h"
65 #include "net/base/net_errors.h"
66 #include "net/base/sdch_manager.h"
67 #include "net/cookies/cookie_store.h"
68 #include "net/disk_cache/disk_cache.h"
69 #include "net/http/http_cache.h"
70 #include "net/http/transport_security_state.h"
71 #include "net/ssl/channel_id_service.h"
72 #include "net/ssl/channel_id_store.h"
73 #include "net/url_request/url_request_context.h"
74 #include "net/url_request/url_request_context_getter.h"
75 #include "storage/browser/quota/quota_manager.h"
76 #include "storage/browser/quota/special_storage_policy.h"
77 #include "storage/common/quota/quota_types.h"
79 #if defined(OS_CHROMEOS)
80 #include "chrome/browser/chromeos/profiles/profile_helper.h"
81 #include "chromeos/attestation/attestation_constants.h"
82 #include "chromeos/dbus/cryptohome_client.h"
83 #include "chromeos/dbus/dbus_thread_manager.h"
84 #include "components/user_manager/user.h"
87 #if defined(ENABLE_EXTENSIONS)
88 #include "chrome/browser/apps/ephemeral_app_service.h"
89 #include "chrome/browser/extensions/activity_log/activity_log.h"
90 #include "chrome/browser/extensions/extension_service.h"
91 #include "chrome/browser/extensions/extension_special_storage_policy.h"
94 #if defined(ENABLE_WEBRTC)
95 #include "chrome/browser/media/webrtc_log_list.h"
96 #include "chrome/browser/media/webrtc_log_util.h"
99 using base::UserMetricsAction
;
100 using content::BrowserContext
;
101 using content::BrowserThread
;
102 using content::DOMStorageContext
;
104 bool BrowsingDataRemover::is_removing_
= false;
106 BrowsingDataRemover::CompletionInhibitor
*
107 BrowsingDataRemover::completion_inhibitor_
= NULL
;
109 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
111 bool DoesOriginMatchMask(
114 storage::SpecialStoragePolicy
* special_storage_policy
) {
115 return BrowsingDataHelper::DoesOriginMatchMask(
116 origin
, origin_set_mask
, special_storage_policy
);
119 BrowsingDataRemover::NotificationDetails::NotificationDetails()
120 : removal_begin(base::Time()),
122 origin_set_mask(-1) {
125 BrowsingDataRemover::NotificationDetails::NotificationDetails(
126 const BrowsingDataRemover::NotificationDetails
& details
)
127 : removal_begin(details
.removal_begin
),
128 removal_mask(details
.removal_mask
),
129 origin_set_mask(details
.origin_set_mask
) {
132 BrowsingDataRemover::NotificationDetails::NotificationDetails(
133 base::Time removal_begin
,
136 : removal_begin(removal_begin
),
137 removal_mask(removal_mask
),
138 origin_set_mask(origin_set_mask
) {
141 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
144 BrowsingDataRemover
* BrowsingDataRemover::CreateForUnboundedRange(
146 return new BrowsingDataRemover(profile
, base::Time(), base::Time::Max());
150 BrowsingDataRemover
* BrowsingDataRemover::CreateForRange(Profile
* profile
,
151 base::Time start
, base::Time end
) {
152 return new BrowsingDataRemover(profile
, start
, end
);
156 BrowsingDataRemover
* BrowsingDataRemover::CreateForPeriod(Profile
* profile
,
160 content::RecordAction(
161 UserMetricsAction("ClearBrowsingData_LastHour"));
164 content::RecordAction(
165 UserMetricsAction("ClearBrowsingData_LastDay"));
168 content::RecordAction(
169 UserMetricsAction("ClearBrowsingData_LastWeek"));
172 content::RecordAction(
173 UserMetricsAction("ClearBrowsingData_LastMonth"));
176 content::RecordAction(
177 UserMetricsAction("ClearBrowsingData_Everything"));
180 return new BrowsingDataRemover(profile
,
181 BrowsingDataRemover::CalculateBeginDeleteTime(period
),
185 BrowsingDataRemover::BrowsingDataRemover(Profile
* profile
,
186 base::Time delete_begin
,
187 base::Time delete_end
)
189 delete_begin_(delete_begin
),
190 delete_end_(delete_end
),
191 next_cache_state_(STATE_NONE
),
193 main_context_getter_(profile
->GetRequestContext()),
194 media_context_getter_(profile
->GetMediaRequestContext()),
195 deauthorize_content_licenses_request_id_(0),
196 waiting_for_clear_autofill_origin_urls_(false),
197 waiting_for_clear_cache_(false),
198 waiting_for_clear_channel_ids_(false),
199 waiting_for_clear_content_licenses_(false),
200 waiting_for_clear_cookies_count_(0),
201 waiting_for_clear_domain_reliability_monitor_(false),
202 waiting_for_clear_form_(false),
203 waiting_for_clear_history_(false),
204 waiting_for_clear_hostname_resolution_cache_(false),
205 waiting_for_clear_keyword_data_(false),
206 waiting_for_clear_logged_in_predictor_(false),
207 waiting_for_clear_nacl_cache_(false),
208 waiting_for_clear_network_predictor_(false),
209 waiting_for_clear_networking_history_(false),
210 waiting_for_clear_platform_keys_(false),
211 waiting_for_clear_plugin_data_(false),
212 waiting_for_clear_pnacl_cache_(false),
213 waiting_for_clear_storage_partition_data_(false),
214 #if defined(ENABLE_WEBRTC)
215 waiting_for_clear_webrtc_logs_(false),
218 remove_origin_(GURL()),
220 storage_partition_for_testing_(NULL
) {
222 // crbug.com/140910: Many places were calling this with base::Time() as
223 // delete_end, even though they should've used base::Time::Max(). Work around
224 // it here. New code should use base::Time::Max().
225 DCHECK(delete_end_
!= base::Time());
226 if (delete_end_
== base::Time())
227 delete_end_
= base::Time::Max();
230 BrowsingDataRemover::~BrowsingDataRemover() {
235 void BrowsingDataRemover::set_removing(bool is_removing
) {
236 DCHECK(is_removing_
!= is_removing
);
237 is_removing_
= is_removing
;
240 void BrowsingDataRemover::Remove(int remove_mask
, int origin_set_mask
) {
241 RemoveImpl(remove_mask
, GURL(), origin_set_mask
);
244 void BrowsingDataRemover::RemoveImpl(int remove_mask
,
246 int origin_set_mask
) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
249 remove_mask_
= remove_mask
;
250 remove_origin_
= origin
;
251 origin_set_mask_
= origin_set_mask
;
253 PrefService
* prefs
= profile_
->GetPrefs();
254 bool may_delete_history
= prefs
->GetBoolean(
255 prefs::kAllowDeletingBrowserHistory
);
257 // All the UI entry points into the BrowsingDataRemover should be disabled,
258 // but this will fire if something was missed or added.
259 DCHECK(may_delete_history
||
260 (!(remove_mask
& REMOVE_HISTORY
) && !(remove_mask
& REMOVE_DOWNLOADS
)));
262 if (origin_set_mask_
& BrowsingDataHelper::UNPROTECTED_WEB
) {
263 content::RecordAction(
264 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
266 if (origin_set_mask_
& BrowsingDataHelper::PROTECTED_WEB
) {
267 content::RecordAction(
268 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
270 if (origin_set_mask_
& BrowsingDataHelper::EXTENSION
) {
271 content::RecordAction(
272 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
274 // If this fires, we added a new BrowsingDataHelper::OriginSetMask without
275 // updating the user metrics above.
277 BrowsingDataHelper::ALL
== (BrowsingDataHelper::UNPROTECTED_WEB
|
278 BrowsingDataHelper::PROTECTED_WEB
|
279 BrowsingDataHelper::EXTENSION
),
280 forgotten_to_add_origin_mask_type
);
282 if ((remove_mask
& REMOVE_HISTORY
) && may_delete_history
) {
283 HistoryService
* history_service
= HistoryServiceFactory::GetForProfile(
284 profile_
, Profile::EXPLICIT_ACCESS
);
285 if (history_service
) {
286 std::set
<GURL
> restrict_urls
;
287 if (!remove_origin_
.is_empty())
288 restrict_urls
.insert(remove_origin_
);
289 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
290 waiting_for_clear_history_
= true;
292 history_service
->ExpireLocalAndRemoteHistoryBetween(
293 restrict_urls
, delete_begin_
, delete_end_
,
294 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone
,
295 base::Unretained(this)),
296 &history_task_tracker_
);
298 #if defined(ENABLE_EXTENSIONS)
299 // The extension activity contains details of which websites extensions
300 // were active on. It therefore indirectly stores details of websites a
301 // user has visited so best clean from here as well.
302 extensions::ActivityLog::GetInstance(profile_
)->RemoveURLs(restrict_urls
);
306 // The power consumption history by origin contains details of websites
307 // that were visited.
308 power::OriginPowerMap
* origin_power_map
=
309 power::OriginPowerMapFactory::GetForBrowserContext(profile_
);
310 if (origin_power_map
)
311 origin_power_map
->ClearOriginMap();
313 // Need to clear the host cache and accumulated speculative data, as it also
314 // reveals some history: we have no mechanism to track when these items were
315 // created, so we'll clear them all. Better safe than sorry.
316 if (g_browser_process
->io_thread()) {
317 waiting_for_clear_hostname_resolution_cache_
= true;
318 BrowserThread::PostTask(
319 BrowserThread::IO
, FROM_HERE
,
321 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread
,
322 base::Unretained(this),
323 g_browser_process
->io_thread()));
325 if (profile_
->GetNetworkPredictor()) {
326 waiting_for_clear_network_predictor_
= true;
327 BrowserThread::PostTask(
328 BrowserThread::IO
, FROM_HERE
,
329 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread
,
330 base::Unretained(this),
331 profile_
->GetNetworkPredictor()));
334 // As part of history deletion we also delete the auto-generated keywords.
335 TemplateURLService
* keywords_model
=
336 TemplateURLServiceFactory::GetForProfile(profile_
);
337 if (keywords_model
&& !keywords_model
->loaded()) {
338 template_url_sub_
= keywords_model
->RegisterOnLoadedCallback(
339 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded
,
340 base::Unretained(this)));
341 keywords_model
->Load();
342 waiting_for_clear_keyword_data_
= true;
343 } else if (keywords_model
) {
344 keywords_model
->RemoveAutoGeneratedForOriginBetween(remove_origin_
,
345 delete_begin_
, delete_end_
);
348 // The PrerenderManager keeps history of prerendered pages, so clear that.
349 // It also may have a prerendered page. If so, the page could be
350 // considered to have a small amount of historical information, so delete
352 prerender::PrerenderManager
* prerender_manager
=
353 prerender::PrerenderManagerFactory::GetForProfile(profile_
);
354 if (prerender_manager
) {
355 prerender_manager
->ClearData(
356 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS
|
357 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY
);
360 // If the caller is removing history for all hosts, then clear ancillary
361 // historical information.
362 if (remove_origin_
.is_empty()) {
363 // We also delete the list of recently closed tabs. Since these expire,
364 // they can't be more than a day old, so we can simply clear them all.
365 TabRestoreService
* tab_service
=
366 TabRestoreServiceFactory::GetForProfile(profile_
);
368 tab_service
->ClearEntries();
369 tab_service
->DeleteLastSession();
372 #if defined(ENABLE_SESSION_SERVICE)
373 // We also delete the last session when we delete the history.
374 SessionService
* session_service
=
375 SessionServiceFactory::GetForProfile(profile_
);
377 session_service
->DeleteLastSession();
381 // The saved Autofill profiles and credit cards can include the origin from
382 // which these profiles and credit cards were learned. These are a form of
383 // history, so clear them as well.
384 scoped_refptr
<autofill::AutofillWebDataService
> web_data_service
=
385 WebDataServiceFactory::GetAutofillWebDataForProfile(
386 profile_
, Profile::EXPLICIT_ACCESS
);
387 if (web_data_service
.get()) {
388 waiting_for_clear_autofill_origin_urls_
= true;
389 web_data_service
->RemoveOriginURLsModifiedBetween(
390 delete_begin_
, delete_end_
);
391 // The above calls are done on the UI thread but do their work on the DB
392 // thread. So wait for it.
393 BrowserThread::PostTaskAndReply(
394 BrowserThread::DB
, FROM_HERE
,
395 base::Bind(&base::DoNothing
),
396 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs
,
397 base::Unretained(this)));
399 autofill::PersonalDataManager
* data_manager
=
400 autofill::PersonalDataManagerFactory::GetForProfile(profile_
);
402 data_manager
->Refresh();
405 #if defined(ENABLE_WEBRTC)
406 waiting_for_clear_webrtc_logs_
= true;
407 BrowserThread::PostTaskAndReply(
411 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles
,
412 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_
->GetPath()),
414 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs
,
415 base::Unretained(this)));
418 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
419 // include origins that the user has visited, so it must be cleared.
420 if (profile_
->GetSSLHostStateDelegate())
421 profile_
->GetSSLHostStateDelegate()->Clear();
424 if ((remove_mask
& REMOVE_DOWNLOADS
) && may_delete_history
) {
425 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
426 content::DownloadManager
* download_manager
=
427 BrowserContext::GetDownloadManager(profile_
);
428 download_manager
->RemoveDownloadsBetween(delete_begin_
, delete_end_
);
429 DownloadPrefs
* download_prefs
= DownloadPrefs::FromDownloadManager(
431 download_prefs
->SetSaveFilePath(download_prefs
->DownloadPath());
434 uint32 storage_partition_remove_mask
= 0;
436 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
437 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
438 // don't accidentally remove the cookies that are associated with the
439 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
440 // between UNPROTECTED_WEB and PROTECTED_WEB.
441 if (remove_mask
& REMOVE_COOKIES
&&
442 origin_set_mask_
& BrowsingDataHelper::UNPROTECTED_WEB
) {
443 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
445 storage_partition_remove_mask
|=
446 content::StoragePartition::REMOVE_DATA_MASK_COOKIES
;
448 // Also delete the LoggedIn Predictor, which tries to keep track of which
449 // sites a user is logged into.
450 ClearLoggedInPredictor();
452 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
453 // Clear the safebrowsing cookies only if time period is for "all time". It
454 // doesn't make sense to apply the time period of deleting in the last X
455 // hours/days to the safebrowsing cookies since they aren't the result of
457 if (delete_begin_
== base::Time()) {
458 SafeBrowsingService
* sb_service
=
459 g_browser_process
->safe_browsing_service();
461 net::URLRequestContextGetter
* sb_context
=
462 sb_service
->url_request_context();
463 ++waiting_for_clear_cookies_count_
;
464 BrowserThread::PostTask(
465 BrowserThread::IO
, FROM_HERE
,
466 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread
,
467 base::Unretained(this), base::Unretained(sb_context
)));
471 MediaDeviceIDSalt::Reset(profile_
->GetPrefs());
474 // Channel IDs are not separated for protected and unprotected web
475 // origins. We check the origin_set_mask_ to prevent unintended deletion.
476 if (remove_mask
& REMOVE_CHANNEL_IDS
&&
477 origin_set_mask_
& BrowsingDataHelper::UNPROTECTED_WEB
) {
478 content::RecordAction(
479 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
480 // Since we are running on the UI thread don't call GetURLRequestContext().
481 net::URLRequestContextGetter
* rq_context
= profile_
->GetRequestContext();
483 waiting_for_clear_channel_ids_
= true;
484 BrowserThread::PostTask(
485 BrowserThread::IO
, FROM_HERE
,
486 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread
,
487 base::Unretained(this), base::Unretained(rq_context
)));
491 if (remove_mask
& REMOVE_LOCAL_STORAGE
) {
492 storage_partition_remove_mask
|=
493 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE
;
496 if (remove_mask
& REMOVE_INDEXEDDB
) {
497 storage_partition_remove_mask
|=
498 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
;
500 if (remove_mask
& REMOVE_WEBSQL
) {
501 storage_partition_remove_mask
|=
502 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL
;
504 if (remove_mask
& REMOVE_APPCACHE
) {
505 storage_partition_remove_mask
|=
506 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE
;
508 if (remove_mask
& REMOVE_SERVICE_WORKERS
) {
509 storage_partition_remove_mask
|=
510 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS
;
512 if (remove_mask
& REMOVE_FILE_SYSTEMS
) {
513 storage_partition_remove_mask
|=
514 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
;
517 #if defined(ENABLE_PLUGINS)
518 // Plugin is data not separated for protected and unprotected web origins. We
519 // check the origin_set_mask_ to prevent unintended deletion.
520 if (remove_mask
& REMOVE_PLUGIN_DATA
&&
521 origin_set_mask_
& BrowsingDataHelper::UNPROTECTED_WEB
) {
522 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
524 waiting_for_clear_plugin_data_
= true;
525 if (!plugin_data_remover_
.get())
526 plugin_data_remover_
.reset(content::PluginDataRemover::Create(profile_
));
527 base::WaitableEvent
* event
=
528 plugin_data_remover_
->StartRemoving(delete_begin_
);
530 base::WaitableEventWatcher::EventCallback watcher_callback
=
531 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled
,
532 base::Unretained(this));
533 watcher_
.StartWatching(event
, watcher_callback
);
537 #if defined(OS_ANDROID)
538 if (remove_mask
& REMOVE_APP_BANNER_DATA
) {
539 profile_
->GetHostContentSettingsMap()->ClearSettingsForOneType(
540 CONTENT_SETTINGS_TYPE_APP_BANNER
);
544 if (remove_mask
& REMOVE_PASSWORDS
) {
545 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
546 password_manager::PasswordStore
* password_store
=
547 PasswordStoreFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
)
551 password_store
->RemoveLoginsCreatedBetween(delete_begin_
, delete_end_
);
554 if (remove_mask
& REMOVE_FORM_DATA
) {
555 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
556 scoped_refptr
<autofill::AutofillWebDataService
> web_data_service
=
557 WebDataServiceFactory::GetAutofillWebDataForProfile(
558 profile_
, Profile::EXPLICIT_ACCESS
);
560 if (web_data_service
.get()) {
561 waiting_for_clear_form_
= true;
562 web_data_service
->RemoveFormElementsAddedBetween(delete_begin_
,
564 web_data_service
->RemoveAutofillDataModifiedBetween(
565 delete_begin_
, delete_end_
);
566 // The above calls are done on the UI thread but do their work on the DB
567 // thread. So wait for it.
568 BrowserThread::PostTaskAndReply(
569 BrowserThread::DB
, FROM_HERE
,
570 base::Bind(&base::DoNothing
),
571 base::Bind(&BrowsingDataRemover::OnClearedFormData
,
572 base::Unretained(this)));
574 autofill::PersonalDataManager
* data_manager
=
575 autofill::PersonalDataManagerFactory::GetForProfile(profile_
);
577 data_manager
->Refresh();
581 if (remove_mask
& REMOVE_CACHE
) {
582 // Tell the renderers to clear their cache.
583 web_cache::WebCacheManager::GetInstance()->ClearCache();
585 // Invoke DoClearCache on the IO thread.
586 waiting_for_clear_cache_
= true;
587 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
589 BrowserThread::PostTask(
590 BrowserThread::IO
, FROM_HERE
,
591 base::Bind(&BrowsingDataRemover::ClearCacheOnIOThread
,
592 base::Unretained(this)));
594 #if !defined(DISABLE_NACL)
595 waiting_for_clear_nacl_cache_
= true;
597 BrowserThread::PostTask(
598 BrowserThread::IO
, FROM_HERE
,
599 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread
,
600 base::Unretained(this)));
602 waiting_for_clear_pnacl_cache_
= true;
603 BrowserThread::PostTask(
604 BrowserThread::IO
, FROM_HERE
,
605 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread
,
606 base::Unretained(this), delete_begin_
, delete_end_
));
609 // The PrerenderManager may have a page actively being prerendered, which
610 // is essentially a preemptively cached page.
611 prerender::PrerenderManager
* prerender_manager
=
612 prerender::PrerenderManagerFactory::GetForProfile(profile_
);
613 if (prerender_manager
) {
614 prerender_manager
->ClearData(
615 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS
);
618 // Tell the shader disk cache to clear.
619 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
620 storage_partition_remove_mask
|=
621 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE
;
623 storage_partition_remove_mask
|=
624 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY
;
626 #if defined(ENABLE_EXTENSIONS)
627 // Clear the ephemeral apps cache.
628 EphemeralAppService::Get(profile_
)->ClearCachedApps();
632 if (storage_partition_remove_mask
) {
633 waiting_for_clear_storage_partition_data_
= true;
635 content::StoragePartition
* storage_partition
;
636 if (storage_partition_for_testing_
)
637 storage_partition
= storage_partition_for_testing_
;
639 storage_partition
= BrowserContext::GetDefaultStoragePartition(profile_
);
641 uint32 quota_storage_remove_mask
=
642 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
;
644 if (delete_begin_
== base::Time() ||
646 (BrowsingDataHelper::PROTECTED_WEB
| BrowsingDataHelper::EXTENSION
)) {
647 // If we're deleting since the beginning of time, or we're removing
648 // protected origins, then remove persistent quota data.
649 quota_storage_remove_mask
|=
650 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
;
653 storage_partition
->ClearData(
654 storage_partition_remove_mask
,
655 quota_storage_remove_mask
,
657 base::Bind(&DoesOriginMatchMask
, origin_set_mask_
),
660 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData
,
661 base::Unretained(this)));
664 #if defined(ENABLE_PLUGINS)
665 if (remove_mask
& REMOVE_CONTENT_LICENSES
) {
666 content::RecordAction(
667 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
669 waiting_for_clear_content_licenses_
= true;
670 if (!pepper_flash_settings_manager_
.get()) {
671 pepper_flash_settings_manager_
.reset(
672 new PepperFlashSettingsManager(this, profile_
));
674 deauthorize_content_licenses_request_id_
=
675 pepper_flash_settings_manager_
->DeauthorizeContentLicenses(prefs
);
676 #if defined(OS_CHROMEOS)
677 // On Chrome OS, also delete any content protection platform keys.
678 user_manager::User
* user
=
679 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_
);
681 LOG(WARNING
) << "Failed to find user for current profile.";
683 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
684 TpmAttestationDeleteKeys(
685 chromeos::attestation::KEY_USER
,
687 chromeos::attestation::kContentProtectionKeyPrefix
,
688 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys
,
689 base::Unretained(this)));
690 waiting_for_clear_platform_keys_
= true;
696 // Remove omnibox zero-suggest cache results.
697 if ((remove_mask
& (REMOVE_CACHE
| REMOVE_COOKIES
)))
698 prefs
->SetString(prefs::kZeroSuggestCachedResults
, std::string());
700 // Always wipe accumulated network related data (TransportSecurityState and
701 // HttpServerPropertiesManager data).
702 waiting_for_clear_networking_history_
= true;
703 profile_
->ClearNetworkingHistorySince(
705 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory
,
706 base::Unretained(this)));
708 if (remove_mask
& (REMOVE_COOKIES
| REMOVE_HISTORY
)) {
709 domain_reliability::DomainReliabilityService
* service
=
710 domain_reliability::DomainReliabilityServiceFactory::
711 GetForBrowserContext(profile_
);
713 domain_reliability::DomainReliabilityClearMode mode
;
714 if (remove_mask
& REMOVE_COOKIES
)
715 mode
= domain_reliability::CLEAR_CONTEXTS
;
717 mode
= domain_reliability::CLEAR_BEACONS
;
719 waiting_for_clear_domain_reliability_monitor_
= true;
720 service
->ClearBrowsingData(
722 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor
,
723 base::Unretained(this)));
728 void BrowsingDataRemover::AddObserver(Observer
* observer
) {
729 observer_list_
.AddObserver(observer
);
732 void BrowsingDataRemover::RemoveObserver(Observer
* observer
) {
733 observer_list_
.RemoveObserver(observer
);
736 void BrowsingDataRemover::OnHistoryDeletionDone() {
737 waiting_for_clear_history_
= false;
738 NotifyAndDeleteIfDone();
741 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
742 content::StoragePartition
* storage_partition
) {
743 storage_partition_for_testing_
= storage_partition
;
746 base::Time
BrowsingDataRemover::CalculateBeginDeleteTime(
747 TimePeriod time_period
) {
748 base::TimeDelta diff
;
749 base::Time delete_begin_time
= base::Time::Now();
750 switch (time_period
) {
752 diff
= base::TimeDelta::FromHours(1);
755 diff
= base::TimeDelta::FromHours(24);
758 diff
= base::TimeDelta::FromHours(7*24);
761 diff
= base::TimeDelta::FromHours(4*7*24);
764 delete_begin_time
= base::Time();
767 NOTREACHED() << L
"Missing item";
770 return delete_begin_time
- diff
;
773 bool BrowsingDataRemover::AllDone() {
774 return !waiting_for_clear_autofill_origin_urls_
&&
775 !waiting_for_clear_cache_
&&
776 !waiting_for_clear_content_licenses_
&&
777 !waiting_for_clear_channel_ids_
&&
778 !waiting_for_clear_cookies_count_
&&
779 !waiting_for_clear_domain_reliability_monitor_
&&
780 !waiting_for_clear_form_
&&
781 !waiting_for_clear_history_
&&
782 !waiting_for_clear_hostname_resolution_cache_
&&
783 !waiting_for_clear_keyword_data_
&&
784 !waiting_for_clear_logged_in_predictor_
&&
785 !waiting_for_clear_nacl_cache_
&&
786 !waiting_for_clear_network_predictor_
&&
787 !waiting_for_clear_networking_history_
&&
788 !waiting_for_clear_platform_keys_
&&
789 !waiting_for_clear_plugin_data_
&&
790 !waiting_for_clear_pnacl_cache_
&&
791 #if defined(ENABLE_WEBRTC)
792 !waiting_for_clear_webrtc_logs_
&&
794 !waiting_for_clear_storage_partition_data_
;
797 void BrowsingDataRemover::OnKeywordsLoaded() {
798 // Deletes the entries from the model, and if we're not waiting on anything
799 // else notifies observers and deletes this BrowsingDataRemover.
800 TemplateURLService
* model
=
801 TemplateURLServiceFactory::GetForProfile(profile_
);
802 model
->RemoveAutoGeneratedBetween(delete_begin_
, delete_end_
);
803 waiting_for_clear_keyword_data_
= false;
804 template_url_sub_
.reset();
805 NotifyAndDeleteIfDone();
808 void BrowsingDataRemover::NotifyAndDelete() {
811 // Send global notification, then notify any explicit observers.
812 BrowsingDataRemover::NotificationDetails
details(delete_begin_
, remove_mask_
,
814 content::NotificationService::current()->Notify(
815 chrome::NOTIFICATION_BROWSING_DATA_REMOVED
,
816 content::Source
<Profile
>(profile_
),
817 content::Details
<BrowsingDataRemover::NotificationDetails
>(&details
));
819 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnBrowsingDataRemoverDone());
821 // History requests aren't happy if you delete yourself from the callback.
822 // As such, we do a delete later.
823 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
826 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
827 // TODO(brettw) http://crbug.com/305259: This should also observe session
828 // clearing (what about other things such as passwords, etc.?) and wait for
829 // them to complete before continuing.
834 if (completion_inhibitor_
) {
835 completion_inhibitor_
->OnBrowsingDataRemoverWouldComplete(
837 base::Bind(&BrowsingDataRemover::NotifyAndDelete
,
838 base::Unretained(this)));
844 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
845 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
846 waiting_for_clear_hostname_resolution_cache_
= false;
847 NotifyAndDeleteIfDone();
850 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
851 IOThread
* io_thread
) {
852 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
854 io_thread
->ClearHostCache();
856 // Notify the UI thread that we are done.
857 BrowserThread::PostTask(
860 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache
,
861 base::Unretained(this)));
864 void BrowsingDataRemover::OnClearedLoggedInPredictor() {
865 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
866 DCHECK(waiting_for_clear_logged_in_predictor_
);
867 waiting_for_clear_logged_in_predictor_
= false;
868 NotifyAndDeleteIfDone();
871 void BrowsingDataRemover::ClearLoggedInPredictor() {
872 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
873 DCHECK(!waiting_for_clear_logged_in_predictor_
);
875 predictors::PredictorDatabase
* predictor_db
=
876 predictors::PredictorDatabaseFactory::GetForProfile(profile_
);
880 predictors::LoggedInPredictorTable
* logged_in_table
=
881 predictor_db
->logged_in_table().get();
882 if (!logged_in_table
)
885 waiting_for_clear_logged_in_predictor_
= true;
887 BrowserThread::PostTaskAndReply(
890 base::Bind(&predictors::LoggedInPredictorTable::DeleteAllCreatedBetween
,
894 base::Bind(&BrowsingDataRemover::OnClearedLoggedInPredictor
,
895 base::Unretained(this)));
898 void BrowsingDataRemover::OnClearedNetworkPredictor() {
899 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
900 waiting_for_clear_network_predictor_
= false;
901 NotifyAndDeleteIfDone();
904 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
905 chrome_browser_net::Predictor
* predictor
) {
906 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
909 predictor
->DiscardInitialNavigationHistory();
910 predictor
->DiscardAllResults();
912 // Notify the UI thread that we are done.
913 BrowserThread::PostTask(
916 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor
,
917 base::Unretained(this)));
920 void BrowsingDataRemover::OnClearedNetworkingHistory() {
921 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
922 waiting_for_clear_networking_history_
= false;
923 NotifyAndDeleteIfDone();
926 void BrowsingDataRemover::ClearedCache() {
927 waiting_for_clear_cache_
= false;
929 NotifyAndDeleteIfDone();
932 void BrowsingDataRemover::ClearCacheOnIOThread() {
933 // This function should be called on the IO thread.
934 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
935 DCHECK_EQ(STATE_NONE
, next_cache_state_
);
936 DCHECK(main_context_getter_
.get());
937 DCHECK(media_context_getter_
.get());
939 next_cache_state_
= STATE_CREATE_MAIN
;
940 DoClearCache(net::OK
);
943 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN -->
944 // STATE_DELETE_MAIN --> STATE_CREATE_MEDIA --> STATE_DELETE_MEDIA -->
945 // STATE_DONE, and any errors are ignored.
946 void BrowsingDataRemover::DoClearCache(int rv
) {
947 DCHECK_NE(STATE_NONE
, next_cache_state_
);
949 while (rv
!= net::ERR_IO_PENDING
&& next_cache_state_
!= STATE_NONE
) {
950 switch (next_cache_state_
) {
951 case STATE_CREATE_MAIN
:
952 case STATE_CREATE_MEDIA
: {
953 // Get a pointer to the cache.
954 net::URLRequestContextGetter
* getter
=
955 (next_cache_state_
== STATE_CREATE_MAIN
)
956 ? main_context_getter_
.get()
957 : media_context_getter_
.get();
958 net::HttpCache
* http_cache
=
959 getter
->GetURLRequestContext()->http_transaction_factory()->
962 next_cache_state_
= (next_cache_state_
== STATE_CREATE_MAIN
) ?
963 STATE_DELETE_MAIN
: STATE_DELETE_MEDIA
;
965 // Clear QUIC server information from memory and the disk cache.
966 http_cache
->GetSession()->quic_stream_factory()->
967 ClearCachedStatesInCryptoConfig();
969 // Clear SDCH dictionary state.
970 net::SdchManager
* sdch_manager
=
971 getter
->GetURLRequestContext()->sdch_manager();
972 // The test is probably overkill, since chrome should always have an
973 // SdchManager. But in general the URLRequestContext is *not*
974 // guaranteed to have an SdchManager, so checking is wise.
976 sdch_manager
->ClearData();
978 rv
= http_cache
->GetBackend(
979 &cache_
, base::Bind(&BrowsingDataRemover::DoClearCache
,
980 base::Unretained(this)));
983 case STATE_DELETE_MAIN
:
984 case STATE_DELETE_MEDIA
: {
985 next_cache_state_
= (next_cache_state_
== STATE_DELETE_MAIN
) ?
986 STATE_CREATE_MEDIA
: STATE_DONE
;
988 // |cache_| can be null if it cannot be initialized.
990 if (delete_begin_
.is_null()) {
991 rv
= cache_
->DoomAllEntries(
992 base::Bind(&BrowsingDataRemover::DoClearCache
,
993 base::Unretained(this)));
995 rv
= cache_
->DoomEntriesBetween(
996 delete_begin_
, delete_end_
,
997 base::Bind(&BrowsingDataRemover::DoClearCache
,
998 base::Unretained(this)));
1006 next_cache_state_
= STATE_NONE
;
1008 // Notify the UI thread that we are done.
1009 BrowserThread::PostTask(
1010 BrowserThread::UI
, FROM_HERE
,
1011 base::Bind(&BrowsingDataRemover::ClearedCache
,
1012 base::Unretained(this)));
1016 NOTREACHED() << "bad state";
1017 next_cache_state_
= STATE_NONE
; // Stop looping.
1024 #if !defined(DISABLE_NACL)
1025 void BrowsingDataRemover::ClearedNaClCache() {
1026 // This function should be called on the UI thread.
1027 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1029 waiting_for_clear_nacl_cache_
= false;
1031 NotifyAndDeleteIfDone();
1034 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
1035 // This function should be called on the IO thread.
1036 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1038 // Notify the UI thread that we are done.
1039 BrowserThread::PostTask(
1040 BrowserThread::UI
, FROM_HERE
,
1041 base::Bind(&BrowsingDataRemover::ClearedNaClCache
,
1042 base::Unretained(this)));
1045 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
1046 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1048 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
1049 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread
,
1050 base::Unretained(this)));
1053 void BrowsingDataRemover::ClearedPnaclCache() {
1054 // This function should be called on the UI thread.
1055 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1057 waiting_for_clear_pnacl_cache_
= false;
1059 NotifyAndDeleteIfDone();
1062 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
1063 // This function should be called on the IO thread.
1064 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1066 // Notify the UI thread that we are done.
1067 BrowserThread::PostTask(
1068 BrowserThread::UI
, FROM_HERE
,
1069 base::Bind(&BrowsingDataRemover::ClearedPnaclCache
,
1070 base::Unretained(this)));
1073 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin
,
1075 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1077 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1079 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread
,
1080 base::Unretained(this)));
1084 void BrowsingDataRemover::OnWaitableEventSignaled(
1085 base::WaitableEvent
* waitable_event
) {
1086 waiting_for_clear_plugin_data_
= false;
1087 NotifyAndDeleteIfDone();
1090 #if defined(ENABLE_PLUGINS)
1091 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1093 bool /* success */) {
1094 DCHECK(waiting_for_clear_content_licenses_
);
1095 DCHECK_EQ(request_id
, deauthorize_content_licenses_request_id_
);
1097 waiting_for_clear_content_licenses_
= false;
1098 NotifyAndDeleteIfDone();
1102 #if defined(OS_CHROMEOS)
1103 void BrowsingDataRemover::OnClearPlatformKeys(
1104 chromeos::DBusMethodCallStatus call_status
,
1106 DCHECK(waiting_for_clear_platform_keys_
);
1107 if (call_status
!= chromeos::DBUS_METHOD_CALL_SUCCESS
|| !result
) {
1108 LOG(ERROR
) << "Failed to clear platform keys.";
1110 waiting_for_clear_platform_keys_
= false;
1111 NotifyAndDeleteIfDone();
1115 void BrowsingDataRemover::OnClearedCookies(int num_deleted
) {
1116 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
1117 BrowserThread::PostTask(
1118 BrowserThread::UI
, FROM_HERE
,
1119 base::Bind(&BrowsingDataRemover::OnClearedCookies
,
1120 base::Unretained(this), num_deleted
));
1124 DCHECK_GT(waiting_for_clear_cookies_count_
, 0);
1125 --waiting_for_clear_cookies_count_
;
1126 NotifyAndDeleteIfDone();
1129 void BrowsingDataRemover::ClearCookiesOnIOThread(
1130 net::URLRequestContextGetter
* rq_context
) {
1131 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1132 net::CookieStore
* cookie_store
= rq_context
->
1133 GetURLRequestContext()->cookie_store();
1134 cookie_store
->DeleteAllCreatedBetweenAsync(
1135 delete_begin_
, delete_end_
,
1136 base::Bind(&BrowsingDataRemover::OnClearedCookies
,
1137 base::Unretained(this)));
1140 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1141 net::URLRequestContextGetter
* rq_context
) {
1142 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
1143 net::ChannelIDService
* channel_id_service
=
1144 rq_context
->GetURLRequestContext()->channel_id_service();
1145 channel_id_service
->GetChannelIDStore()->DeleteAllCreatedBetween(
1146 delete_begin_
, delete_end_
,
1147 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread
,
1148 base::Unretained(this), base::Unretained(rq_context
)));
1151 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1152 net::URLRequestContextGetter
* rq_context
) {
1153 // Need to close open SSL connections which may be using the channel ids we
1155 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1156 // service/store have observers that can notify relevant things directly.
1157 rq_context
->GetURLRequestContext()->ssl_config_service()->
1158 NotifySSLConfigChange();
1159 BrowserThread::PostTask(
1160 BrowserThread::UI
, FROM_HERE
,
1161 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs
,
1162 base::Unretained(this)));
1165 void BrowsingDataRemover::OnClearedChannelIDs() {
1166 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1167 waiting_for_clear_channel_ids_
= false;
1168 NotifyAndDeleteIfDone();
1171 void BrowsingDataRemover::OnClearedFormData() {
1172 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1173 waiting_for_clear_form_
= false;
1174 NotifyAndDeleteIfDone();
1177 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1178 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1179 waiting_for_clear_autofill_origin_urls_
= false;
1180 NotifyAndDeleteIfDone();
1183 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1184 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1185 waiting_for_clear_storage_partition_data_
= false;
1186 NotifyAndDeleteIfDone();
1189 #if defined(ENABLE_WEBRTC)
1190 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1191 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1192 waiting_for_clear_webrtc_logs_
= false;
1193 NotifyAndDeleteIfDone();
1197 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1198 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
1199 waiting_for_clear_domain_reliability_monitor_
= false;
1200 NotifyAndDeleteIfDone();