[sessions]: Componentize TabRestore code
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blob4742873b0dedd6da719c96668a27ffdb0bfbad1a
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"
7 #include <map>
8 #include <set>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/prefs/pref_service.h"
17 #include "chrome/browser/autofill/personal_data_manager_factory.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/browsing_data/browsing_data_helper.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
22 #include "chrome/browser/domain_reliability/service_factory.h"
23 #include "chrome/browser/download/download_prefs.h"
24 #include "chrome/browser/download/download_service_factory.h"
25 #include "chrome/browser/history/history_service_factory.h"
26 #include "chrome/browser/history/web_history_service_factory.h"
27 #include "chrome/browser/io_thread.h"
28 #include "chrome/browser/media/media_device_id_salt.h"
29 #include "chrome/browser/net/predictor.h"
30 #include "chrome/browser/password_manager/password_store_factory.h"
31 #include "chrome/browser/prerender/prerender_manager.h"
32 #include "chrome/browser/prerender/prerender_manager_factory.h"
33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
35 #include "chrome/browser/search_engines/template_url_service_factory.h"
36 #include "chrome/browser/sessions/session_service.h"
37 #include "chrome/browser/sessions/session_service_factory.h"
38 #include "chrome/browser/sessions/tab_restore_service_factory.h"
39 #include "chrome/browser/web_data_service_factory.h"
40 #include "chrome/common/pref_names.h"
41 #include "chrome/common/url_constants.h"
42 #include "components/autofill/core/browser/personal_data_manager.h"
43 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
44 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
45 #include "components/content_settings/core/browser/host_content_settings_map.h"
46 #include "components/domain_reliability/service.h"
47 #include "components/history/core/browser/history_service.h"
48 #include "components/nacl/browser/nacl_browser.h"
49 #include "components/nacl/browser/pnacl_host.h"
50 #include "components/omnibox/browser/omnibox_pref_names.h"
51 #include "components/password_manager/core/browser/password_store.h"
52 #include "components/power/origin_power_map.h"
53 #include "components/power/origin_power_map_factory.h"
54 #include "components/search_engines/template_url_service.h"
55 #include "components/sessions/core/tab_restore_service.h"
56 #include "components/web_cache/browser/web_cache_manager.h"
57 #include "content/public/browser/browser_thread.h"
58 #include "content/public/browser/download_manager.h"
59 #include "content/public/browser/notification_service.h"
60 #include "content/public/browser/plugin_data_remover.h"
61 #include "content/public/browser/ssl_host_state_delegate.h"
62 #include "content/public/browser/storage_partition.h"
63 #include "content/public/browser/user_metrics.h"
64 #include "net/base/net_errors.h"
65 #include "net/cookies/cookie_store.h"
66 #include "net/http/transport_security_state.h"
67 #include "net/ssl/channel_id_service.h"
68 #include "net/ssl/channel_id_store.h"
69 #include "net/url_request/url_request_context.h"
70 #include "net/url_request/url_request_context_getter.h"
71 #include "storage/browser/quota/special_storage_policy.h"
72 #include "url/origin.h"
74 #if defined(OS_ANDROID)
75 #include "chrome/browser/android/webapps/webapp_registry.h"
76 #include "chrome/browser/precache/precache_manager_factory.h"
77 #include "components/precache/content/precache_manager.h"
78 #endif
80 #if defined(OS_CHROMEOS)
81 #include "chrome/browser/chromeos/profiles/profile_helper.h"
82 #include "chromeos/attestation/attestation_constants.h"
83 #include "chromeos/dbus/cryptohome_client.h"
84 #include "chromeos/dbus/dbus_thread_manager.h"
85 #include "components/user_manager/user.h"
86 #endif
88 #if defined(ENABLE_EXTENSIONS)
89 #include "chrome/browser/apps/ephemeral_app_service.h"
90 #include "chrome/browser/extensions/activity_log/activity_log.h"
91 #include "chrome/browser/extensions/extension_service.h"
92 #include "chrome/browser/extensions/extension_special_storage_policy.h"
93 #include "extensions/browser/extension_prefs.h"
94 #endif
96 #if defined(ENABLE_WEBRTC)
97 #include "chrome/browser/media/webrtc_log_list.h"
98 #include "chrome/browser/media/webrtc_log_util.h"
99 #endif
101 using base::UserMetricsAction;
102 using content::BrowserContext;
103 using content::BrowserThread;
104 using content::DOMStorageContext;
106 namespace {
108 using CallbackList =
109 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
111 // Contains all registered callbacks for browsing data removed notifications.
112 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
114 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
115 // the first time so that it always returns a valid object.
116 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
117 if (!g_on_browsing_data_removed_callbacks)
118 g_on_browsing_data_removed_callbacks = new CallbackList();
119 return g_on_browsing_data_removed_callbacks;
122 } // namespace
124 bool BrowsingDataRemover::is_removing_ = false;
126 BrowsingDataRemover::CompletionInhibitor*
127 BrowsingDataRemover::completion_inhibitor_ = nullptr;
129 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
130 // Static.
131 bool DoesOriginMatchMask(
132 int origin_type_mask,
133 const GURL& origin,
134 storage::SpecialStoragePolicy* special_storage_policy) {
135 return BrowsingDataHelper::DoesOriginMatchMask(
136 origin, origin_type_mask, special_storage_policy);
139 BrowsingDataRemover::NotificationDetails::NotificationDetails()
140 : removal_begin(base::Time()),
141 removal_mask(-1),
142 origin_type_mask(-1) {
145 BrowsingDataRemover::NotificationDetails::NotificationDetails(
146 const BrowsingDataRemover::NotificationDetails& details)
147 : removal_begin(details.removal_begin),
148 removal_mask(details.removal_mask),
149 origin_type_mask(details.origin_type_mask) {
152 BrowsingDataRemover::NotificationDetails::NotificationDetails(
153 base::Time removal_begin,
154 int removal_mask,
155 int origin_type_mask)
156 : removal_begin(removal_begin),
157 removal_mask(removal_mask),
158 origin_type_mask(origin_type_mask) {
161 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
163 // Static.
164 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
165 Profile* profile) {
166 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
169 // Static.
170 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
171 base::Time start, base::Time end) {
172 return new BrowsingDataRemover(profile, start, end);
175 // Static.
176 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
177 TimePeriod period) {
178 switch (period) {
179 case LAST_HOUR:
180 content::RecordAction(
181 UserMetricsAction("ClearBrowsingData_LastHour"));
182 break;
183 case LAST_DAY:
184 content::RecordAction(
185 UserMetricsAction("ClearBrowsingData_LastDay"));
186 break;
187 case LAST_WEEK:
188 content::RecordAction(
189 UserMetricsAction("ClearBrowsingData_LastWeek"));
190 break;
191 case FOUR_WEEKS:
192 content::RecordAction(
193 UserMetricsAction("ClearBrowsingData_LastMonth"));
194 break;
195 case EVERYTHING:
196 content::RecordAction(
197 UserMetricsAction("ClearBrowsingData_Everything"));
198 break;
200 return new BrowsingDataRemover(profile,
201 BrowsingDataRemover::CalculateBeginDeleteTime(period),
202 base::Time::Max());
205 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
206 base::Time delete_begin,
207 base::Time delete_end)
208 : profile_(profile),
209 delete_begin_(delete_begin),
210 delete_end_(delete_end),
211 main_context_getter_(profile->GetRequestContext()),
212 media_context_getter_(profile->GetMediaRequestContext()) {
213 DCHECK(profile);
214 // crbug.com/140910: Many places were calling this with base::Time() as
215 // delete_end, even though they should've used base::Time::Max(). Work around
216 // it here. New code should use base::Time::Max().
217 DCHECK(delete_end_ != base::Time());
218 if (delete_end_ == base::Time())
219 delete_end_ = base::Time::Max();
222 BrowsingDataRemover::~BrowsingDataRemover() {
223 DCHECK(AllDone());
226 // Static.
227 void BrowsingDataRemover::set_removing(bool is_removing) {
228 DCHECK(is_removing_ != is_removing);
229 is_removing_ = is_removing;
232 void BrowsingDataRemover::Remove(int remove_mask, int origin_type_mask) {
233 RemoveImpl(remove_mask, GURL(), origin_type_mask);
236 void BrowsingDataRemover::RemoveImpl(int remove_mask,
237 const GURL& remove_url,
238 int origin_type_mask) {
239 DCHECK_CURRENTLY_ON(BrowserThread::UI);
240 set_removing(true);
241 remove_mask_ = remove_mask;
242 origin_type_mask_ = origin_type_mask;
243 url::Origin remove_origin(remove_url);
245 PrefService* prefs = profile_->GetPrefs();
246 bool may_delete_history = prefs->GetBoolean(
247 prefs::kAllowDeletingBrowserHistory);
249 // All the UI entry points into the BrowsingDataRemover should be disabled,
250 // but this will fire if something was missed or added.
251 DCHECK(may_delete_history || (remove_mask & REMOVE_NOCHECKS) ||
252 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
254 if (origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
255 content::RecordAction(
256 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
258 if (origin_type_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
259 content::RecordAction(
260 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
262 if (origin_type_mask_ & BrowsingDataHelper::EXTENSION) {
263 content::RecordAction(
264 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
266 // If this fires, we added a new BrowsingDataHelper::OriginTypeMask without
267 // updating the user metrics above.
268 static_assert(
269 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
270 BrowsingDataHelper::PROTECTED_WEB |
271 BrowsingDataHelper::EXTENSION),
272 "OriginTypeMask has been updated without updating user metrics");
274 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
275 history::HistoryService* history_service =
276 HistoryServiceFactory::GetForProfile(
277 profile_, ServiceAccessType::EXPLICIT_ACCESS);
278 if (history_service) {
279 std::set<GURL> restrict_urls;
280 if (!remove_url.is_empty())
281 restrict_urls.insert(remove_url);
282 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
283 waiting_for_clear_history_ = true;
285 history_service->ExpireLocalAndRemoteHistoryBetween(
286 WebHistoryServiceFactory::GetForProfile(profile_),
287 restrict_urls, delete_begin_, delete_end_,
288 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
289 base::Unretained(this)),
290 &history_task_tracker_);
292 #if defined(ENABLE_EXTENSIONS)
293 // The extension activity contains details of which websites extensions
294 // were active on. It therefore indirectly stores details of websites a
295 // user has visited so best clean from here as well.
296 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
297 #endif
300 #if defined(ENABLE_EXTENSIONS)
301 // Clear launch times as they are a form of history.
302 extensions::ExtensionPrefs* extension_prefs =
303 extensions::ExtensionPrefs::Get(profile_);
304 extension_prefs->ClearLastLaunchTimes();
305 #endif
307 // The power consumption history by origin contains details of websites
308 // that were visited.
309 power::OriginPowerMap* origin_power_map =
310 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
311 if (origin_power_map)
312 origin_power_map->ClearOriginMap();
314 // Need to clear the host cache and accumulated speculative data, as it also
315 // reveals some history: we have no mechanism to track when these items were
316 // created, so we'll clear them all. Better safe than sorry.
317 if (g_browser_process->io_thread()) {
318 waiting_for_clear_hostname_resolution_cache_ = true;
319 BrowserThread::PostTask(
320 BrowserThread::IO, FROM_HERE,
321 base::Bind(
322 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
323 base::Unretained(this),
324 g_browser_process->io_thread()));
326 if (profile_->GetNetworkPredictor()) {
327 waiting_for_clear_network_predictor_ = true;
328 BrowserThread::PostTask(
329 BrowserThread::IO, FROM_HERE,
330 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
331 base::Unretained(this),
332 profile_->GetNetworkPredictor()));
335 // As part of history deletion we also delete the auto-generated keywords.
336 TemplateURLService* keywords_model =
337 TemplateURLServiceFactory::GetForProfile(profile_);
338 if (keywords_model && !keywords_model->loaded()) {
339 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
340 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
341 base::Unretained(this)));
342 keywords_model->Load();
343 waiting_for_clear_keyword_data_ = true;
344 } else if (keywords_model) {
345 keywords_model->RemoveAutoGeneratedForOriginBetween(
346 remove_url, delete_begin_, delete_end_);
349 // The PrerenderManager keeps history of prerendered pages, so clear that.
350 // It also may have a prerendered page. If so, the page could be
351 // considered to have a small amount of historical information, so delete
352 // it, too.
353 prerender::PrerenderManager* prerender_manager =
354 prerender::PrerenderManagerFactory::GetForProfile(profile_);
355 if (prerender_manager) {
356 prerender_manager->ClearData(
357 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
358 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
361 // If the caller is removing history for all hosts, then clear ancillary
362 // historical information.
363 if (remove_url.is_empty()) {
364 // We also delete the list of recently closed tabs. Since these expire,
365 // they can't be more than a day old, so we can simply clear them all.
366 TabRestoreService* tab_service =
367 TabRestoreServiceFactory::GetForProfile(profile_);
368 if (tab_service) {
369 tab_service->ClearEntries();
370 tab_service->DeleteLastSession();
373 #if defined(ENABLE_SESSION_SERVICE)
374 // We also delete the last session when we delete the history.
375 SessionService* session_service =
376 SessionServiceFactory::GetForProfile(profile_);
377 if (session_service)
378 session_service->DeleteLastSession();
379 #endif
382 // The saved Autofill profiles and credit cards can include the origin from
383 // which these profiles and credit cards were learned. These are a form of
384 // history, so clear them as well.
385 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
386 WebDataServiceFactory::GetAutofillWebDataForProfile(
387 profile_, ServiceAccessType::EXPLICIT_ACCESS);
388 if (web_data_service.get()) {
389 waiting_for_clear_autofill_origin_urls_ = true;
390 web_data_service->RemoveOriginURLsModifiedBetween(
391 delete_begin_, delete_end_);
392 // The above calls are done on the UI thread but do their work on the DB
393 // thread. So wait for it.
394 BrowserThread::PostTaskAndReply(
395 BrowserThread::DB, FROM_HERE,
396 base::Bind(&base::DoNothing),
397 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
398 base::Unretained(this)));
400 autofill::PersonalDataManager* data_manager =
401 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
402 if (data_manager)
403 data_manager->Refresh();
406 #if defined(ENABLE_WEBRTC)
407 waiting_for_clear_webrtc_logs_ = true;
408 BrowserThread::PostTaskAndReply(
409 BrowserThread::FILE,
410 FROM_HERE,
411 base::Bind(
412 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
413 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
414 delete_begin_),
415 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
416 base::Unretained(this)));
417 #endif
419 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
420 // include origins that the user has visited, so it must be cleared.
421 if (profile_->GetSSLHostStateDelegate())
422 profile_->GetSSLHostStateDelegate()->Clear();
424 #if defined(OS_ANDROID)
425 precache::PrecacheManager* precache_manager =
426 precache::PrecacheManagerFactory::GetForBrowserContext(profile_);
427 // |precache_manager| could be nullptr if the profile is off the record.
428 if (!precache_manager) {
429 waiting_for_clear_precache_history_ = true;
430 precache_manager->ClearHistory();
431 // The above calls are done on the UI thread but do their work on the DB
432 // thread. So wait for it.
433 BrowserThread::PostTaskAndReply(
434 BrowserThread::DB, FROM_HERE,
435 base::Bind(&base::DoNothing),
436 base::Bind(&BrowsingDataRemover::OnClearedPrecacheHistory,
437 base::Unretained(this)));
439 #endif
442 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
443 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
444 content::DownloadManager* download_manager =
445 BrowserContext::GetDownloadManager(profile_);
446 if (remove_origin.unique())
447 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
448 else
449 download_manager->RemoveDownloadsByOriginAndTime(
450 remove_origin, delete_begin_, delete_end_);
451 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
452 download_manager);
453 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
456 uint32 storage_partition_remove_mask = 0;
458 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
459 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
460 // don't accidentally remove the cookies that are associated with the
461 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
462 // between UNPROTECTED_WEB and PROTECTED_WEB.
463 if (remove_mask & REMOVE_COOKIES &&
464 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
465 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
467 storage_partition_remove_mask |=
468 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
470 #if defined(SAFE_BROWSING_SERVICE)
471 // Clear the safebrowsing cookies only if time period is for "all time". It
472 // doesn't make sense to apply the time period of deleting in the last X
473 // hours/days to the safebrowsing cookies since they aren't the result of
474 // any user action.
475 if (delete_begin_ == base::Time()) {
476 SafeBrowsingService* sb_service =
477 g_browser_process->safe_browsing_service();
478 if (sb_service) {
479 net::URLRequestContextGetter* sb_context =
480 sb_service->url_request_context();
481 ++waiting_for_clear_cookies_count_;
482 BrowserThread::PostTask(
483 BrowserThread::IO, FROM_HERE,
484 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
485 base::Unretained(this), base::Unretained(sb_context)));
488 #endif
489 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
491 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
492 // flag for all credentials in the password store.
495 // Channel IDs are not separated for protected and unprotected web
496 // origins. We check the origin_type_mask_ to prevent unintended deletion.
497 if (remove_mask & REMOVE_CHANNEL_IDS &&
498 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
499 content::RecordAction(
500 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
501 // Since we are running on the UI thread don't call GetURLRequestContext().
502 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
503 if (rq_context) {
504 waiting_for_clear_channel_ids_ = true;
505 BrowserThread::PostTask(
506 BrowserThread::IO, FROM_HERE,
507 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
508 base::Unretained(this), base::Unretained(rq_context)));
512 if (remove_mask & REMOVE_LOCAL_STORAGE) {
513 storage_partition_remove_mask |=
514 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
517 if (remove_mask & REMOVE_INDEXEDDB) {
518 storage_partition_remove_mask |=
519 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
521 if (remove_mask & REMOVE_WEBSQL) {
522 storage_partition_remove_mask |=
523 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
525 if (remove_mask & REMOVE_APPCACHE) {
526 storage_partition_remove_mask |=
527 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
529 if (remove_mask & REMOVE_SERVICE_WORKERS) {
530 storage_partition_remove_mask |=
531 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
533 if (remove_mask & REMOVE_CACHE_STORAGE) {
534 storage_partition_remove_mask |=
535 content::StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE;
537 if (remove_mask & REMOVE_FILE_SYSTEMS) {
538 storage_partition_remove_mask |=
539 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
542 #if defined(ENABLE_PLUGINS)
543 // Plugin is data not separated for protected and unprotected web origins. We
544 // check the origin_type_mask_ to prevent unintended deletion.
545 if (remove_mask & REMOVE_PLUGIN_DATA &&
546 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
547 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
549 waiting_for_clear_plugin_data_ = true;
550 if (!plugin_data_remover_.get())
551 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
552 base::WaitableEvent* event =
553 plugin_data_remover_->StartRemoving(delete_begin_);
555 base::WaitableEventWatcher::EventCallback watcher_callback =
556 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
557 base::Unretained(this));
558 watcher_.StartWatching(event, watcher_callback);
560 #endif
562 if (remove_mask & REMOVE_SITE_USAGE_DATA || remove_mask & REMOVE_HISTORY) {
563 HostContentSettingsMapFactory::GetForProfile(profile_)
564 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_APP_BANNER);
565 HostContentSettingsMapFactory::GetForProfile(profile_)
566 ->ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT);
569 if (remove_mask & REMOVE_PASSWORDS) {
570 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
571 password_manager::PasswordStore* password_store =
572 PasswordStoreFactory::GetForProfile(
573 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
575 if (password_store) {
576 waiting_for_clear_passwords_ = true;
577 password_store->RemoveLoginsCreatedBetween(
578 delete_begin_, delete_end_,
579 base::Bind(&BrowsingDataRemover::OnClearedPasswords,
580 base::Unretained(this)));
584 if (remove_mask & REMOVE_FORM_DATA) {
585 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
586 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
587 WebDataServiceFactory::GetAutofillWebDataForProfile(
588 profile_, ServiceAccessType::EXPLICIT_ACCESS);
590 if (web_data_service.get()) {
591 waiting_for_clear_form_ = true;
592 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
593 delete_end_);
594 web_data_service->RemoveAutofillDataModifiedBetween(
595 delete_begin_, delete_end_);
596 // The above calls are done on the UI thread but do their work on the DB
597 // thread. So wait for it.
598 BrowserThread::PostTaskAndReply(
599 BrowserThread::DB, FROM_HERE,
600 base::Bind(&base::DoNothing),
601 base::Bind(&BrowsingDataRemover::OnClearedFormData,
602 base::Unretained(this)));
604 autofill::PersonalDataManager* data_manager =
605 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
606 if (data_manager)
607 data_manager->Refresh();
611 if (remove_mask & REMOVE_CACHE) {
612 // Tell the renderers to clear their cache.
613 web_cache::WebCacheManager::GetInstance()->ClearCache();
615 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
617 waiting_for_clear_cache_ = true;
618 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
619 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
620 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
621 delete_end_)
622 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
623 base::Unretained(this)));
625 #if !defined(DISABLE_NACL)
626 waiting_for_clear_nacl_cache_ = true;
628 BrowserThread::PostTask(
629 BrowserThread::IO, FROM_HERE,
630 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
631 base::Unretained(this)));
633 waiting_for_clear_pnacl_cache_ = true;
634 BrowserThread::PostTask(
635 BrowserThread::IO, FROM_HERE,
636 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
637 base::Unretained(this), delete_begin_, delete_end_));
638 #endif
640 // The PrerenderManager may have a page actively being prerendered, which
641 // is essentially a preemptively cached page.
642 prerender::PrerenderManager* prerender_manager =
643 prerender::PrerenderManagerFactory::GetForProfile(profile_);
644 if (prerender_manager) {
645 prerender_manager->ClearData(
646 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
649 // Tell the shader disk cache to clear.
650 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
651 storage_partition_remove_mask |=
652 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
654 storage_partition_remove_mask |=
655 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
657 #if defined(ENABLE_EXTENSIONS)
658 // Clear the ephemeral apps cache. This is nullptr while testing. OTR
659 // Profile has neither apps nor an ExtensionService, so ClearCachedApps
660 // fails.
661 EphemeralAppService* ephemeral_app_service =
662 EphemeralAppService::Get(profile_);
663 if (ephemeral_app_service && !profile_->IsOffTheRecord())
664 ephemeral_app_service->ClearCachedApps();
665 #endif
668 if (remove_mask & REMOVE_WEBRTC_IDENTITY) {
669 storage_partition_remove_mask |=
670 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
673 if (storage_partition_remove_mask) {
674 waiting_for_clear_storage_partition_data_ = true;
676 content::StoragePartition* storage_partition;
677 if (storage_partition_for_testing_)
678 storage_partition = storage_partition_for_testing_;
679 else
680 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
682 uint32 quota_storage_remove_mask =
683 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
685 if (delete_begin_ == base::Time() ||
686 origin_type_mask_ &
687 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
688 // If we're deleting since the beginning of time, or we're removing
689 // protected origins, then remove persistent quota data.
690 quota_storage_remove_mask |=
691 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
694 storage_partition->ClearData(
695 storage_partition_remove_mask, quota_storage_remove_mask, remove_url,
696 base::Bind(&DoesOriginMatchMask, origin_type_mask_), delete_begin_,
697 delete_end_,
698 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
699 base::Unretained(this)));
702 #if defined(ENABLE_PLUGINS)
703 if (remove_mask & REMOVE_CONTENT_LICENSES) {
704 content::RecordAction(
705 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
707 waiting_for_clear_content_licenses_ = true;
708 if (!pepper_flash_settings_manager_.get()) {
709 pepper_flash_settings_manager_.reset(
710 new PepperFlashSettingsManager(this, profile_));
712 deauthorize_content_licenses_request_id_ =
713 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
714 #if defined(OS_CHROMEOS)
715 // On Chrome OS, also delete any content protection platform keys.
716 const user_manager::User* user =
717 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
718 if (!user) {
719 LOG(WARNING) << "Failed to find user for current profile.";
720 } else {
721 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
722 TpmAttestationDeleteKeys(
723 chromeos::attestation::KEY_USER,
724 user->email(),
725 chromeos::attestation::kContentProtectionKeyPrefix,
726 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
727 base::Unretained(this)));
728 waiting_for_clear_platform_keys_ = true;
730 #endif
732 #endif
734 // Remove omnibox zero-suggest cache results.
735 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
736 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string());
738 // Always wipe accumulated network related data (TransportSecurityState and
739 // HttpServerPropertiesManager data).
740 waiting_for_clear_networking_history_ = true;
741 profile_->ClearNetworkingHistorySince(
742 delete_begin_,
743 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
744 base::Unretained(this)));
746 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
747 domain_reliability::DomainReliabilityService* service =
748 domain_reliability::DomainReliabilityServiceFactory::
749 GetForBrowserContext(profile_);
750 if (service) {
751 domain_reliability::DomainReliabilityClearMode mode;
752 if (remove_mask & REMOVE_COOKIES)
753 mode = domain_reliability::CLEAR_CONTEXTS;
754 else
755 mode = domain_reliability::CLEAR_BEACONS;
757 waiting_for_clear_domain_reliability_monitor_ = true;
758 service->ClearBrowsingData(
759 mode,
760 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
761 base::Unretained(this)));
765 #if defined(OS_ANDROID)
766 if (remove_mask & REMOVE_WEBAPP_DATA) {
767 waiting_for_clear_webapp_data_ = true;
768 WebappRegistry::UnregisterWebapps(
769 base::Bind(&BrowsingDataRemover::OnClearedWebappData,
770 base::Unretained(this)));
772 #endif
774 // Record the combined deletion of cookies and cache.
775 CookieOrCacheDeletionChoice choice = NEITHER_COOKIES_NOR_CACHE;
776 if (remove_mask & REMOVE_COOKIES &&
777 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
778 choice = remove_mask & REMOVE_CACHE ? BOTH_COOKIES_AND_CACHE
779 : ONLY_COOKIES;
780 } else if (remove_mask & REMOVE_CACHE) {
781 choice = ONLY_CACHE;
784 UMA_HISTOGRAM_ENUMERATION(
785 "History.ClearBrowsingData.UserDeletedCookieOrCache",
786 choice, MAX_CHOICE_VALUE);
789 void BrowsingDataRemover::AddObserver(Observer* observer) {
790 observer_list_.AddObserver(observer);
793 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
794 observer_list_.RemoveObserver(observer);
797 void BrowsingDataRemover::OnHistoryDeletionDone() {
798 waiting_for_clear_history_ = false;
799 NotifyAndDeleteIfDone();
802 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
803 content::StoragePartition* storage_partition) {
804 storage_partition_for_testing_ = storage_partition;
807 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
808 TimePeriod time_period) {
809 base::TimeDelta diff;
810 base::Time delete_begin_time = base::Time::Now();
811 switch (time_period) {
812 case LAST_HOUR:
813 diff = base::TimeDelta::FromHours(1);
814 break;
815 case LAST_DAY:
816 diff = base::TimeDelta::FromHours(24);
817 break;
818 case LAST_WEEK:
819 diff = base::TimeDelta::FromHours(7*24);
820 break;
821 case FOUR_WEEKS:
822 diff = base::TimeDelta::FromHours(4*7*24);
823 break;
824 case EVERYTHING:
825 delete_begin_time = base::Time();
826 break;
827 default:
828 NOTREACHED() << L"Missing item";
829 break;
831 return delete_begin_time - diff;
834 bool BrowsingDataRemover::AllDone() {
835 return !waiting_for_clear_autofill_origin_urls_ &&
836 !waiting_for_clear_cache_ &&
837 !waiting_for_clear_content_licenses_ &&
838 !waiting_for_clear_channel_ids_ &&
839 !waiting_for_clear_cookies_count_ &&
840 !waiting_for_clear_domain_reliability_monitor_ &&
841 !waiting_for_clear_form_ &&
842 !waiting_for_clear_history_ &&
843 !waiting_for_clear_hostname_resolution_cache_ &&
844 !waiting_for_clear_keyword_data_ &&
845 !waiting_for_clear_nacl_cache_ &&
846 !waiting_for_clear_network_predictor_ &&
847 !waiting_for_clear_networking_history_ &&
848 !waiting_for_clear_passwords_ &&
849 !waiting_for_clear_platform_keys_ &&
850 !waiting_for_clear_plugin_data_ &&
851 !waiting_for_clear_pnacl_cache_ &&
852 #if defined(OS_ANDROID)
853 !waiting_for_clear_precache_history_ &&
854 !waiting_for_clear_webapp_data_ &&
855 #endif
856 #if defined(ENABLE_WEBRTC)
857 !waiting_for_clear_webrtc_logs_ &&
858 #endif
859 !waiting_for_clear_storage_partition_data_;
862 void BrowsingDataRemover::OnKeywordsLoaded() {
863 // Deletes the entries from the model, and if we're not waiting on anything
864 // else notifies observers and deletes this BrowsingDataRemover.
865 TemplateURLService* model =
866 TemplateURLServiceFactory::GetForProfile(profile_);
867 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
868 waiting_for_clear_keyword_data_ = false;
869 template_url_sub_.reset();
870 NotifyAndDeleteIfDone();
873 void BrowsingDataRemover::NotifyAndDelete() {
874 set_removing(false);
876 // Notify observers.
877 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
878 origin_type_mask_);
880 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
882 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
884 // History requests aren't happy if you delete yourself from the callback.
885 // As such, we do a delete later.
886 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
889 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
890 // TODO(brettw) http://crbug.com/305259: This should also observe session
891 // clearing (what about other things such as passwords, etc.?) and wait for
892 // them to complete before continuing.
894 if (!AllDone())
895 return;
897 if (completion_inhibitor_) {
898 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
899 this,
900 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
901 base::Unretained(this)));
902 } else {
903 NotifyAndDelete();
907 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
908 DCHECK_CURRENTLY_ON(BrowserThread::UI);
909 waiting_for_clear_hostname_resolution_cache_ = false;
910 NotifyAndDeleteIfDone();
913 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
914 IOThread* io_thread) {
915 DCHECK_CURRENTLY_ON(BrowserThread::IO);
917 io_thread->ClearHostCache();
919 // Notify the UI thread that we are done.
920 BrowserThread::PostTask(
921 BrowserThread::UI,
922 FROM_HERE,
923 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
924 base::Unretained(this)));
927 void BrowsingDataRemover::OnClearedNetworkPredictor() {
928 DCHECK_CURRENTLY_ON(BrowserThread::UI);
929 waiting_for_clear_network_predictor_ = false;
930 NotifyAndDeleteIfDone();
933 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
934 chrome_browser_net::Predictor* predictor) {
935 DCHECK_CURRENTLY_ON(BrowserThread::IO);
936 DCHECK(predictor);
938 predictor->DiscardInitialNavigationHistory();
939 predictor->DiscardAllResults();
941 // Notify the UI thread that we are done.
942 BrowserThread::PostTask(
943 BrowserThread::UI,
944 FROM_HERE,
945 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
946 base::Unretained(this)));
949 void BrowsingDataRemover::OnClearedNetworkingHistory() {
950 DCHECK_CURRENTLY_ON(BrowserThread::UI);
951 waiting_for_clear_networking_history_ = false;
952 NotifyAndDeleteIfDone();
955 void BrowsingDataRemover::ClearedCache() {
956 waiting_for_clear_cache_ = false;
958 NotifyAndDeleteIfDone();
961 #if !defined(DISABLE_NACL)
962 void BrowsingDataRemover::ClearedNaClCache() {
963 // This function should be called on the UI thread.
964 DCHECK_CURRENTLY_ON(BrowserThread::UI);
966 waiting_for_clear_nacl_cache_ = false;
968 NotifyAndDeleteIfDone();
971 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
972 // This function should be called on the IO thread.
973 DCHECK_CURRENTLY_ON(BrowserThread::IO);
975 // Notify the UI thread that we are done.
976 BrowserThread::PostTask(
977 BrowserThread::UI, FROM_HERE,
978 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
979 base::Unretained(this)));
982 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
983 DCHECK_CURRENTLY_ON(BrowserThread::IO);
985 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
986 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
987 base::Unretained(this)));
990 void BrowsingDataRemover::ClearedPnaclCache() {
991 // This function should be called on the UI thread.
992 DCHECK_CURRENTLY_ON(BrowserThread::UI);
994 waiting_for_clear_pnacl_cache_ = false;
996 NotifyAndDeleteIfDone();
999 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
1000 // This function should be called on the IO thread.
1001 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1003 // Notify the UI thread that we are done.
1004 BrowserThread::PostTask(
1005 BrowserThread::UI, FROM_HERE,
1006 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1007 base::Unretained(this)));
1010 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1011 base::Time end) {
1012 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1014 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1015 begin, end,
1016 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1017 base::Unretained(this)));
1019 #endif
1021 void BrowsingDataRemover::OnWaitableEventSignaled(
1022 base::WaitableEvent* waitable_event) {
1023 waiting_for_clear_plugin_data_ = false;
1024 NotifyAndDeleteIfDone();
1027 #if defined(ENABLE_PLUGINS)
1028 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1029 uint32 request_id,
1030 bool /* success */) {
1031 DCHECK(waiting_for_clear_content_licenses_);
1032 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1034 waiting_for_clear_content_licenses_ = false;
1035 NotifyAndDeleteIfDone();
1037 #endif
1039 #if defined(OS_CHROMEOS)
1040 void BrowsingDataRemover::OnClearPlatformKeys(
1041 chromeos::DBusMethodCallStatus call_status,
1042 bool result) {
1043 DCHECK(waiting_for_clear_platform_keys_);
1044 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1045 LOG(ERROR) << "Failed to clear platform keys.";
1047 waiting_for_clear_platform_keys_ = false;
1048 NotifyAndDeleteIfDone();
1050 #endif
1053 void BrowsingDataRemover::OnClearedPasswords() {
1054 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1055 waiting_for_clear_passwords_ = false;
1056 NotifyAndDeleteIfDone();
1059 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1060 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1061 BrowserThread::PostTask(
1062 BrowserThread::UI, FROM_HERE,
1063 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1064 base::Unretained(this), num_deleted));
1065 return;
1068 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1069 --waiting_for_clear_cookies_count_;
1070 NotifyAndDeleteIfDone();
1073 void BrowsingDataRemover::ClearCookiesOnIOThread(
1074 net::URLRequestContextGetter* rq_context) {
1075 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1076 net::CookieStore* cookie_store = rq_context->
1077 GetURLRequestContext()->cookie_store();
1078 cookie_store->DeleteAllCreatedBetweenAsync(
1079 delete_begin_, delete_end_,
1080 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1081 base::Unretained(this)));
1084 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1085 net::URLRequestContextGetter* rq_context) {
1086 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1087 net::ChannelIDService* channel_id_service =
1088 rq_context->GetURLRequestContext()->channel_id_service();
1089 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1090 delete_begin_, delete_end_,
1091 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1092 base::Unretained(this), base::Unretained(rq_context)));
1095 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1096 net::URLRequestContextGetter* rq_context) {
1097 // Need to close open SSL connections which may be using the channel ids we
1098 // are deleting.
1099 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1100 // service/store have observers that can notify relevant things directly.
1101 rq_context->GetURLRequestContext()->ssl_config_service()->
1102 NotifySSLConfigChange();
1103 BrowserThread::PostTask(
1104 BrowserThread::UI, FROM_HERE,
1105 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1106 base::Unretained(this)));
1109 void BrowsingDataRemover::OnClearedChannelIDs() {
1110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1111 waiting_for_clear_channel_ids_ = false;
1112 NotifyAndDeleteIfDone();
1115 void BrowsingDataRemover::OnClearedFormData() {
1116 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1117 waiting_for_clear_form_ = false;
1118 NotifyAndDeleteIfDone();
1121 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1123 waiting_for_clear_autofill_origin_urls_ = false;
1124 NotifyAndDeleteIfDone();
1127 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1128 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1129 waiting_for_clear_storage_partition_data_ = false;
1130 NotifyAndDeleteIfDone();
1133 #if defined(ENABLE_WEBRTC)
1134 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1135 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1136 waiting_for_clear_webrtc_logs_ = false;
1137 NotifyAndDeleteIfDone();
1139 #endif
1141 #if defined(OS_ANDROID)
1142 void BrowsingDataRemover::OnClearedPrecacheHistory() {
1143 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1144 waiting_for_clear_precache_history_ = false;
1145 NotifyAndDeleteIfDone();
1148 void BrowsingDataRemover::OnClearedWebappData() {
1149 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1150 waiting_for_clear_webapp_data_ = false;
1151 NotifyAndDeleteIfDone();
1153 #endif
1155 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1156 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1157 waiting_for_clear_domain_reliability_monitor_ = false;
1158 NotifyAndDeleteIfDone();
1161 // static
1162 BrowsingDataRemover::CallbackSubscription
1163 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1164 const BrowsingDataRemover::Callback& callback) {
1165 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);