Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
bloba023228b380f86182e119946d0a44c65f1086a41
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/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_factory.h"
25 #include "chrome/browser/history/web_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/prerender/prerender_manager.h"
31 #include "chrome/browser/prerender/prerender_manager_factory.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
34 #include "chrome/browser/search_engines/template_url_service_factory.h"
35 #include "chrome/browser/sessions/session_service.h"
36 #include "chrome/browser/sessions/session_service_factory.h"
37 #include "chrome/browser/sessions/tab_restore_service.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/web_cache/browser/web_cache_manager.h"
56 #include "content/public/browser/browser_thread.h"
57 #include "content/public/browser/download_manager.h"
58 #include "content/public/browser/notification_service.h"
59 #include "content/public/browser/plugin_data_remover.h"
60 #include "content/public/browser/ssl_host_state_delegate.h"
61 #include "content/public/browser/storage_partition.h"
62 #include "content/public/browser/user_metrics.h"
63 #include "net/base/net_errors.h"
64 #include "net/cookies/cookie_store.h"
65 #include "net/http/transport_security_state.h"
66 #include "net/ssl/channel_id_service.h"
67 #include "net/ssl/channel_id_store.h"
68 #include "net/url_request/url_request_context.h"
69 #include "net/url_request/url_request_context_getter.h"
70 #include "storage/browser/quota/special_storage_policy.h"
71 #include "url/origin.h"
73 #if defined(OS_ANDROID)
74 #include "chrome/browser/precache/precache_manager_factory.h"
75 #include "components/precache/content/precache_manager.h"
76 #endif
78 #if defined(OS_CHROMEOS)
79 #include "chrome/browser/chromeos/profiles/profile_helper.h"
80 #include "chromeos/attestation/attestation_constants.h"
81 #include "chromeos/dbus/cryptohome_client.h"
82 #include "chromeos/dbus/dbus_thread_manager.h"
83 #include "components/user_manager/user.h"
84 #endif
86 #if defined(ENABLE_EXTENSIONS)
87 #include "chrome/browser/apps/ephemeral_app_service.h"
88 #include "chrome/browser/extensions/activity_log/activity_log.h"
89 #include "chrome/browser/extensions/extension_service.h"
90 #include "chrome/browser/extensions/extension_special_storage_policy.h"
91 #include "extensions/browser/extension_prefs.h"
92 #endif
94 #if defined(ENABLE_WEBRTC)
95 #include "chrome/browser/media/webrtc_log_list.h"
96 #include "chrome/browser/media/webrtc_log_util.h"
97 #endif
99 using base::UserMetricsAction;
100 using content::BrowserContext;
101 using content::BrowserThread;
102 using content::DOMStorageContext;
104 namespace {
106 using CallbackList =
107 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
109 // Contains all registered callbacks for browsing data removed notifications.
110 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
112 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
113 // the first time so that it always returns a valid object.
114 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
115 if (!g_on_browsing_data_removed_callbacks)
116 g_on_browsing_data_removed_callbacks = new CallbackList();
117 return g_on_browsing_data_removed_callbacks;
120 // A helper enum to report the deletion of cookies and/or cache. Do not reorder
121 // the entries, as this enum is passed to UMA.
122 enum CookieOrCacheDeletionChoice {
123 NEITHER_COOKIES_NOR_CACHE,
124 ONLY_COOKIES,
125 ONLY_CACHE,
126 BOTH_COOKIES_AND_CACHE,
127 MAX_CHOICE_VALUE
130 } // namespace
132 bool BrowsingDataRemover::is_removing_ = false;
134 BrowsingDataRemover::CompletionInhibitor*
135 BrowsingDataRemover::completion_inhibitor_ = nullptr;
137 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
138 // Static.
139 bool DoesOriginMatchMask(
140 int origin_type_mask,
141 const GURL& origin,
142 storage::SpecialStoragePolicy* special_storage_policy) {
143 return BrowsingDataHelper::DoesOriginMatchMask(
144 origin, origin_type_mask, special_storage_policy);
147 BrowsingDataRemover::NotificationDetails::NotificationDetails()
148 : removal_begin(base::Time()),
149 removal_mask(-1),
150 origin_type_mask(-1) {
153 BrowsingDataRemover::NotificationDetails::NotificationDetails(
154 const BrowsingDataRemover::NotificationDetails& details)
155 : removal_begin(details.removal_begin),
156 removal_mask(details.removal_mask),
157 origin_type_mask(details.origin_type_mask) {
160 BrowsingDataRemover::NotificationDetails::NotificationDetails(
161 base::Time removal_begin,
162 int removal_mask,
163 int origin_type_mask)
164 : removal_begin(removal_begin),
165 removal_mask(removal_mask),
166 origin_type_mask(origin_type_mask) {
169 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
171 // Static.
172 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
173 Profile* profile) {
174 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
177 // Static.
178 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
179 base::Time start, base::Time end) {
180 return new BrowsingDataRemover(profile, start, end);
183 // Static.
184 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
185 TimePeriod period) {
186 switch (period) {
187 case LAST_HOUR:
188 content::RecordAction(
189 UserMetricsAction("ClearBrowsingData_LastHour"));
190 break;
191 case LAST_DAY:
192 content::RecordAction(
193 UserMetricsAction("ClearBrowsingData_LastDay"));
194 break;
195 case LAST_WEEK:
196 content::RecordAction(
197 UserMetricsAction("ClearBrowsingData_LastWeek"));
198 break;
199 case FOUR_WEEKS:
200 content::RecordAction(
201 UserMetricsAction("ClearBrowsingData_LastMonth"));
202 break;
203 case EVERYTHING:
204 content::RecordAction(
205 UserMetricsAction("ClearBrowsingData_Everything"));
206 break;
208 return new BrowsingDataRemover(profile,
209 BrowsingDataRemover::CalculateBeginDeleteTime(period),
210 base::Time::Max());
213 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
214 base::Time delete_begin,
215 base::Time delete_end)
216 : profile_(profile),
217 delete_begin_(delete_begin),
218 delete_end_(delete_end),
219 main_context_getter_(profile->GetRequestContext()),
220 media_context_getter_(profile->GetMediaRequestContext()) {
221 DCHECK(profile);
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() {
231 DCHECK(AllDone());
234 // Static.
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_type_mask) {
241 RemoveImpl(remove_mask, GURL(), origin_type_mask);
244 void BrowsingDataRemover::RemoveImpl(int remove_mask,
245 const GURL& remove_url,
246 int origin_type_mask) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248 set_removing(true);
249 remove_mask_ = remove_mask;
250 origin_type_mask_ = origin_type_mask;
251 url::Origin remove_origin(remove_url);
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 || (remove_mask & REMOVE_NOCHECKS) ||
260 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
262 if (origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
263 content::RecordAction(
264 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
266 if (origin_type_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
267 content::RecordAction(
268 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
270 if (origin_type_mask_ & BrowsingDataHelper::EXTENSION) {
271 content::RecordAction(
272 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
274 // If this fires, we added a new BrowsingDataHelper::OriginTypeMask without
275 // updating the user metrics above.
276 static_assert(
277 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
278 BrowsingDataHelper::PROTECTED_WEB |
279 BrowsingDataHelper::EXTENSION),
280 "OriginTypeMask has been updated without updating user metrics");
282 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
283 history::HistoryService* history_service =
284 HistoryServiceFactory::GetForProfile(
285 profile_, ServiceAccessType::EXPLICIT_ACCESS);
286 if (history_service) {
287 std::set<GURL> restrict_urls;
288 if (!remove_url.is_empty())
289 restrict_urls.insert(remove_url);
290 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
291 waiting_for_clear_history_ = true;
293 history_service->ExpireLocalAndRemoteHistoryBetween(
294 WebHistoryServiceFactory::GetForProfile(profile_),
295 restrict_urls, delete_begin_, delete_end_,
296 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
297 base::Unretained(this)),
298 &history_task_tracker_);
300 #if defined(ENABLE_EXTENSIONS)
301 // The extension activity contains details of which websites extensions
302 // were active on. It therefore indirectly stores details of websites a
303 // user has visited so best clean from here as well.
304 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
305 #endif
308 #if defined(ENABLE_EXTENSIONS)
309 // Clear launch times as they are a form of history.
310 extensions::ExtensionPrefs* extension_prefs =
311 extensions::ExtensionPrefs::Get(profile_);
312 extension_prefs->ClearLastLaunchTimes();
313 #endif
315 // The power consumption history by origin contains details of websites
316 // that were visited.
317 power::OriginPowerMap* origin_power_map =
318 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
319 if (origin_power_map)
320 origin_power_map->ClearOriginMap();
322 // Need to clear the host cache and accumulated speculative data, as it also
323 // reveals some history: we have no mechanism to track when these items were
324 // created, so we'll clear them all. Better safe than sorry.
325 if (g_browser_process->io_thread()) {
326 waiting_for_clear_hostname_resolution_cache_ = true;
327 BrowserThread::PostTask(
328 BrowserThread::IO, FROM_HERE,
329 base::Bind(
330 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
331 base::Unretained(this),
332 g_browser_process->io_thread()));
334 if (profile_->GetNetworkPredictor()) {
335 waiting_for_clear_network_predictor_ = true;
336 BrowserThread::PostTask(
337 BrowserThread::IO, FROM_HERE,
338 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
339 base::Unretained(this),
340 profile_->GetNetworkPredictor()));
343 // As part of history deletion we also delete the auto-generated keywords.
344 TemplateURLService* keywords_model =
345 TemplateURLServiceFactory::GetForProfile(profile_);
346 if (keywords_model && !keywords_model->loaded()) {
347 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
348 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
349 base::Unretained(this)));
350 keywords_model->Load();
351 waiting_for_clear_keyword_data_ = true;
352 } else if (keywords_model) {
353 keywords_model->RemoveAutoGeneratedForOriginBetween(
354 remove_url, delete_begin_, delete_end_);
357 // The PrerenderManager keeps history of prerendered pages, so clear that.
358 // It also may have a prerendered page. If so, the page could be
359 // considered to have a small amount of historical information, so delete
360 // it, too.
361 prerender::PrerenderManager* prerender_manager =
362 prerender::PrerenderManagerFactory::GetForProfile(profile_);
363 if (prerender_manager) {
364 prerender_manager->ClearData(
365 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
366 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
369 // If the caller is removing history for all hosts, then clear ancillary
370 // historical information.
371 if (remove_url.is_empty()) {
372 // We also delete the list of recently closed tabs. Since these expire,
373 // they can't be more than a day old, so we can simply clear them all.
374 TabRestoreService* tab_service =
375 TabRestoreServiceFactory::GetForProfile(profile_);
376 if (tab_service) {
377 tab_service->ClearEntries();
378 tab_service->DeleteLastSession();
381 #if defined(ENABLE_SESSION_SERVICE)
382 // We also delete the last session when we delete the history.
383 SessionService* session_service =
384 SessionServiceFactory::GetForProfile(profile_);
385 if (session_service)
386 session_service->DeleteLastSession();
387 #endif
390 // The saved Autofill profiles and credit cards can include the origin from
391 // which these profiles and credit cards were learned. These are a form of
392 // history, so clear them as well.
393 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
394 WebDataServiceFactory::GetAutofillWebDataForProfile(
395 profile_, ServiceAccessType::EXPLICIT_ACCESS);
396 if (web_data_service.get()) {
397 waiting_for_clear_autofill_origin_urls_ = true;
398 web_data_service->RemoveOriginURLsModifiedBetween(
399 delete_begin_, delete_end_);
400 // The above calls are done on the UI thread but do their work on the DB
401 // thread. So wait for it.
402 BrowserThread::PostTaskAndReply(
403 BrowserThread::DB, FROM_HERE,
404 base::Bind(&base::DoNothing),
405 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
406 base::Unretained(this)));
408 autofill::PersonalDataManager* data_manager =
409 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
410 if (data_manager)
411 data_manager->Refresh();
414 #if defined(ENABLE_WEBRTC)
415 waiting_for_clear_webrtc_logs_ = true;
416 BrowserThread::PostTaskAndReply(
417 BrowserThread::FILE,
418 FROM_HERE,
419 base::Bind(
420 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
421 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
422 delete_begin_),
423 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
424 base::Unretained(this)));
425 #endif
427 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
428 // include origins that the user has visited, so it must be cleared.
429 if (profile_->GetSSLHostStateDelegate())
430 profile_->GetSSLHostStateDelegate()->Clear();
432 #if defined(OS_ANDROID)
433 precache::PrecacheManager* precache_manager =
434 precache::PrecacheManagerFactory::GetForBrowserContext(profile_);
435 // |precache_manager| could be nullptr if the profile is off the record.
436 if (!precache_manager) {
437 waiting_for_clear_precache_history_ = true;
438 precache_manager->ClearHistory();
439 // The above calls are done on the UI thread but do their work on the DB
440 // thread. So wait for it.
441 BrowserThread::PostTaskAndReply(
442 BrowserThread::DB, FROM_HERE,
443 base::Bind(&base::DoNothing),
444 base::Bind(&BrowsingDataRemover::OnClearedPrecacheHistory,
445 base::Unretained(this)));
447 #endif
450 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
451 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
452 content::DownloadManager* download_manager =
453 BrowserContext::GetDownloadManager(profile_);
454 if (remove_origin.unique())
455 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
456 else
457 download_manager->RemoveDownloadsByOriginAndTime(
458 remove_origin, delete_begin_, delete_end_);
459 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
460 download_manager);
461 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
464 uint32 storage_partition_remove_mask = 0;
466 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
467 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
468 // don't accidentally remove the cookies that are associated with the
469 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
470 // between UNPROTECTED_WEB and PROTECTED_WEB.
471 if (remove_mask & REMOVE_COOKIES &&
472 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
473 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
475 storage_partition_remove_mask |=
476 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
478 #if defined(SAFE_BROWSING_SERVICE)
479 // Clear the safebrowsing cookies only if time period is for "all time". It
480 // doesn't make sense to apply the time period of deleting in the last X
481 // hours/days to the safebrowsing cookies since they aren't the result of
482 // any user action.
483 if (delete_begin_ == base::Time()) {
484 SafeBrowsingService* sb_service =
485 g_browser_process->safe_browsing_service();
486 if (sb_service) {
487 net::URLRequestContextGetter* sb_context =
488 sb_service->url_request_context();
489 ++waiting_for_clear_cookies_count_;
490 BrowserThread::PostTask(
491 BrowserThread::IO, FROM_HERE,
492 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
493 base::Unretained(this), base::Unretained(sb_context)));
496 #endif
497 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
499 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
500 // flag for all credentials in the password store.
503 // Channel IDs are not separated for protected and unprotected web
504 // origins. We check the origin_type_mask_ to prevent unintended deletion.
505 if (remove_mask & REMOVE_CHANNEL_IDS &&
506 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
507 content::RecordAction(
508 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
509 // Since we are running on the UI thread don't call GetURLRequestContext().
510 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
511 if (rq_context) {
512 waiting_for_clear_channel_ids_ = true;
513 BrowserThread::PostTask(
514 BrowserThread::IO, FROM_HERE,
515 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
516 base::Unretained(this), base::Unretained(rq_context)));
520 if (remove_mask & REMOVE_LOCAL_STORAGE) {
521 storage_partition_remove_mask |=
522 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
525 if (remove_mask & REMOVE_INDEXEDDB) {
526 storage_partition_remove_mask |=
527 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
529 if (remove_mask & REMOVE_WEBSQL) {
530 storage_partition_remove_mask |=
531 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
533 if (remove_mask & REMOVE_APPCACHE) {
534 storage_partition_remove_mask |=
535 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
537 if (remove_mask & REMOVE_SERVICE_WORKERS) {
538 storage_partition_remove_mask |=
539 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
541 if (remove_mask & REMOVE_FILE_SYSTEMS) {
542 storage_partition_remove_mask |=
543 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
546 #if defined(ENABLE_PLUGINS)
547 // Plugin is data not separated for protected and unprotected web origins. We
548 // check the origin_type_mask_ to prevent unintended deletion.
549 if (remove_mask & REMOVE_PLUGIN_DATA &&
550 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
551 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
553 waiting_for_clear_plugin_data_ = true;
554 if (!plugin_data_remover_.get())
555 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
556 base::WaitableEvent* event =
557 plugin_data_remover_->StartRemoving(delete_begin_);
559 base::WaitableEventWatcher::EventCallback watcher_callback =
560 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
561 base::Unretained(this));
562 watcher_.StartWatching(event, watcher_callback);
564 #endif
566 if (remove_mask & REMOVE_SITE_USAGE_DATA || remove_mask & REMOVE_HISTORY) {
567 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
568 CONTENT_SETTINGS_TYPE_APP_BANNER);
569 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
570 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT);
573 if (remove_mask & REMOVE_PASSWORDS) {
574 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
575 password_manager::PasswordStore* password_store =
576 PasswordStoreFactory::GetForProfile(
577 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
579 if (password_store) {
580 waiting_for_clear_passwords_ = true;
581 password_store->RemoveLoginsCreatedBetween(
582 delete_begin_, delete_end_,
583 base::Bind(&BrowsingDataRemover::OnClearedPasswords,
584 base::Unretained(this)));
588 if (remove_mask & REMOVE_FORM_DATA) {
589 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
590 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
591 WebDataServiceFactory::GetAutofillWebDataForProfile(
592 profile_, ServiceAccessType::EXPLICIT_ACCESS);
594 if (web_data_service.get()) {
595 waiting_for_clear_form_ = true;
596 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
597 delete_end_);
598 web_data_service->RemoveAutofillDataModifiedBetween(
599 delete_begin_, delete_end_);
600 // The above calls are done on the UI thread but do their work on the DB
601 // thread. So wait for it.
602 BrowserThread::PostTaskAndReply(
603 BrowserThread::DB, FROM_HERE,
604 base::Bind(&base::DoNothing),
605 base::Bind(&BrowsingDataRemover::OnClearedFormData,
606 base::Unretained(this)));
608 autofill::PersonalDataManager* data_manager =
609 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
610 if (data_manager)
611 data_manager->Refresh();
615 if (remove_mask & REMOVE_CACHE) {
616 // Tell the renderers to clear their cache.
617 web_cache::WebCacheManager::GetInstance()->ClearCache();
619 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
621 waiting_for_clear_cache_ = true;
622 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
623 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
624 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
625 delete_end_)
626 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
627 base::Unretained(this)));
629 #if !defined(DISABLE_NACL)
630 waiting_for_clear_nacl_cache_ = true;
632 BrowserThread::PostTask(
633 BrowserThread::IO, FROM_HERE,
634 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
635 base::Unretained(this)));
637 waiting_for_clear_pnacl_cache_ = true;
638 BrowserThread::PostTask(
639 BrowserThread::IO, FROM_HERE,
640 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
641 base::Unretained(this), delete_begin_, delete_end_));
642 #endif
644 // The PrerenderManager may have a page actively being prerendered, which
645 // is essentially a preemptively cached page.
646 prerender::PrerenderManager* prerender_manager =
647 prerender::PrerenderManagerFactory::GetForProfile(profile_);
648 if (prerender_manager) {
649 prerender_manager->ClearData(
650 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
653 // Tell the shader disk cache to clear.
654 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
655 storage_partition_remove_mask |=
656 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
658 storage_partition_remove_mask |=
659 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
661 #if defined(ENABLE_EXTENSIONS)
662 // Clear the ephemeral apps cache. This is nullptr while testing. OTR
663 // Profile has neither apps nor an ExtensionService, so ClearCachedApps
664 // fails.
665 EphemeralAppService* ephemeral_app_service =
666 EphemeralAppService::Get(profile_);
667 if (ephemeral_app_service && !profile_->IsOffTheRecord())
668 ephemeral_app_service->ClearCachedApps();
669 #endif
672 if (remove_mask & REMOVE_WEBRTC_IDENTITY) {
673 storage_partition_remove_mask |=
674 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
677 if (storage_partition_remove_mask) {
678 waiting_for_clear_storage_partition_data_ = true;
680 content::StoragePartition* storage_partition;
681 if (storage_partition_for_testing_)
682 storage_partition = storage_partition_for_testing_;
683 else
684 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
686 uint32 quota_storage_remove_mask =
687 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
689 if (delete_begin_ == base::Time() ||
690 origin_type_mask_ &
691 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
692 // If we're deleting since the beginning of time, or we're removing
693 // protected origins, then remove persistent quota data.
694 quota_storage_remove_mask |=
695 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
698 storage_partition->ClearData(
699 storage_partition_remove_mask, quota_storage_remove_mask, remove_url,
700 base::Bind(&DoesOriginMatchMask, origin_type_mask_), delete_begin_,
701 delete_end_,
702 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
703 base::Unretained(this)));
706 #if defined(ENABLE_PLUGINS)
707 if (remove_mask & REMOVE_CONTENT_LICENSES) {
708 content::RecordAction(
709 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
711 waiting_for_clear_content_licenses_ = true;
712 if (!pepper_flash_settings_manager_.get()) {
713 pepper_flash_settings_manager_.reset(
714 new PepperFlashSettingsManager(this, profile_));
716 deauthorize_content_licenses_request_id_ =
717 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
718 #if defined(OS_CHROMEOS)
719 // On Chrome OS, also delete any content protection platform keys.
720 const user_manager::User* user =
721 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
722 if (!user) {
723 LOG(WARNING) << "Failed to find user for current profile.";
724 } else {
725 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
726 TpmAttestationDeleteKeys(
727 chromeos::attestation::KEY_USER,
728 user->email(),
729 chromeos::attestation::kContentProtectionKeyPrefix,
730 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
731 base::Unretained(this)));
732 waiting_for_clear_platform_keys_ = true;
734 #endif
736 #endif
738 // Remove omnibox zero-suggest cache results.
739 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
740 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string());
742 // Always wipe accumulated network related data (TransportSecurityState and
743 // HttpServerPropertiesManager data).
744 waiting_for_clear_networking_history_ = true;
745 profile_->ClearNetworkingHistorySince(
746 delete_begin_,
747 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
748 base::Unretained(this)));
750 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
751 domain_reliability::DomainReliabilityService* service =
752 domain_reliability::DomainReliabilityServiceFactory::
753 GetForBrowserContext(profile_);
754 if (service) {
755 domain_reliability::DomainReliabilityClearMode mode;
756 if (remove_mask & REMOVE_COOKIES)
757 mode = domain_reliability::CLEAR_CONTEXTS;
758 else
759 mode = domain_reliability::CLEAR_BEACONS;
761 waiting_for_clear_domain_reliability_monitor_ = true;
762 service->ClearBrowsingData(
763 mode,
764 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
765 base::Unretained(this)));
769 // Record the combined deletion of cookies and cache.
770 CookieOrCacheDeletionChoice choice = NEITHER_COOKIES_NOR_CACHE;
771 if (remove_mask & REMOVE_COOKIES &&
772 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
773 choice = remove_mask & REMOVE_CACHE ? BOTH_COOKIES_AND_CACHE
774 : ONLY_COOKIES;
775 } else if (remove_mask & REMOVE_CACHE) {
776 choice = ONLY_CACHE;
779 UMA_HISTOGRAM_ENUMERATION(
780 "History.ClearBrowsingData.UserDeletedCookieOrCache",
781 choice, MAX_CHOICE_VALUE);
784 void BrowsingDataRemover::AddObserver(Observer* observer) {
785 observer_list_.AddObserver(observer);
788 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
789 observer_list_.RemoveObserver(observer);
792 void BrowsingDataRemover::OnHistoryDeletionDone() {
793 waiting_for_clear_history_ = false;
794 NotifyAndDeleteIfDone();
797 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
798 content::StoragePartition* storage_partition) {
799 storage_partition_for_testing_ = storage_partition;
802 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
803 TimePeriod time_period) {
804 base::TimeDelta diff;
805 base::Time delete_begin_time = base::Time::Now();
806 switch (time_period) {
807 case LAST_HOUR:
808 diff = base::TimeDelta::FromHours(1);
809 break;
810 case LAST_DAY:
811 diff = base::TimeDelta::FromHours(24);
812 break;
813 case LAST_WEEK:
814 diff = base::TimeDelta::FromHours(7*24);
815 break;
816 case FOUR_WEEKS:
817 diff = base::TimeDelta::FromHours(4*7*24);
818 break;
819 case EVERYTHING:
820 delete_begin_time = base::Time();
821 break;
822 default:
823 NOTREACHED() << L"Missing item";
824 break;
826 return delete_begin_time - diff;
829 bool BrowsingDataRemover::AllDone() {
830 return !waiting_for_clear_autofill_origin_urls_ &&
831 !waiting_for_clear_cache_ &&
832 !waiting_for_clear_content_licenses_ &&
833 !waiting_for_clear_channel_ids_ &&
834 !waiting_for_clear_cookies_count_ &&
835 !waiting_for_clear_domain_reliability_monitor_ &&
836 !waiting_for_clear_form_ &&
837 !waiting_for_clear_history_ &&
838 !waiting_for_clear_hostname_resolution_cache_ &&
839 !waiting_for_clear_keyword_data_ &&
840 !waiting_for_clear_nacl_cache_ &&
841 !waiting_for_clear_network_predictor_ &&
842 !waiting_for_clear_networking_history_ &&
843 !waiting_for_clear_passwords_ &&
844 !waiting_for_clear_platform_keys_ &&
845 !waiting_for_clear_plugin_data_ &&
846 !waiting_for_clear_pnacl_cache_ &&
847 #if defined(OS_ANDROID)
848 !waiting_for_clear_precache_history_ &&
849 #endif
850 #if defined(ENABLE_WEBRTC)
851 !waiting_for_clear_webrtc_logs_ &&
852 #endif
853 !waiting_for_clear_storage_partition_data_;
856 void BrowsingDataRemover::OnKeywordsLoaded() {
857 // Deletes the entries from the model, and if we're not waiting on anything
858 // else notifies observers and deletes this BrowsingDataRemover.
859 TemplateURLService* model =
860 TemplateURLServiceFactory::GetForProfile(profile_);
861 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
862 waiting_for_clear_keyword_data_ = false;
863 template_url_sub_.reset();
864 NotifyAndDeleteIfDone();
867 void BrowsingDataRemover::NotifyAndDelete() {
868 set_removing(false);
870 // Notify observers.
871 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
872 origin_type_mask_);
874 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
876 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
878 // History requests aren't happy if you delete yourself from the callback.
879 // As such, we do a delete later.
880 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
883 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
884 // TODO(brettw) http://crbug.com/305259: This should also observe session
885 // clearing (what about other things such as passwords, etc.?) and wait for
886 // them to complete before continuing.
888 if (!AllDone())
889 return;
891 if (completion_inhibitor_) {
892 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
893 this,
894 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
895 base::Unretained(this)));
896 } else {
897 NotifyAndDelete();
901 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
902 DCHECK_CURRENTLY_ON(BrowserThread::UI);
903 waiting_for_clear_hostname_resolution_cache_ = false;
904 NotifyAndDeleteIfDone();
907 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
908 IOThread* io_thread) {
909 DCHECK_CURRENTLY_ON(BrowserThread::IO);
911 io_thread->ClearHostCache();
913 // Notify the UI thread that we are done.
914 BrowserThread::PostTask(
915 BrowserThread::UI,
916 FROM_HERE,
917 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
918 base::Unretained(this)));
921 void BrowsingDataRemover::OnClearedNetworkPredictor() {
922 DCHECK_CURRENTLY_ON(BrowserThread::UI);
923 waiting_for_clear_network_predictor_ = false;
924 NotifyAndDeleteIfDone();
927 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
928 chrome_browser_net::Predictor* predictor) {
929 DCHECK_CURRENTLY_ON(BrowserThread::IO);
930 DCHECK(predictor);
932 predictor->DiscardInitialNavigationHistory();
933 predictor->DiscardAllResults();
935 // Notify the UI thread that we are done.
936 BrowserThread::PostTask(
937 BrowserThread::UI,
938 FROM_HERE,
939 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
940 base::Unretained(this)));
943 void BrowsingDataRemover::OnClearedNetworkingHistory() {
944 DCHECK_CURRENTLY_ON(BrowserThread::UI);
945 waiting_for_clear_networking_history_ = false;
946 NotifyAndDeleteIfDone();
949 void BrowsingDataRemover::ClearedCache() {
950 waiting_for_clear_cache_ = false;
952 NotifyAndDeleteIfDone();
955 #if !defined(DISABLE_NACL)
956 void BrowsingDataRemover::ClearedNaClCache() {
957 // This function should be called on the UI thread.
958 DCHECK_CURRENTLY_ON(BrowserThread::UI);
960 waiting_for_clear_nacl_cache_ = false;
962 NotifyAndDeleteIfDone();
965 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
966 // This function should be called on the IO thread.
967 DCHECK_CURRENTLY_ON(BrowserThread::IO);
969 // Notify the UI thread that we are done.
970 BrowserThread::PostTask(
971 BrowserThread::UI, FROM_HERE,
972 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
973 base::Unretained(this)));
976 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
977 DCHECK_CURRENTLY_ON(BrowserThread::IO);
979 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
980 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
981 base::Unretained(this)));
984 void BrowsingDataRemover::ClearedPnaclCache() {
985 // This function should be called on the UI thread.
986 DCHECK_CURRENTLY_ON(BrowserThread::UI);
988 waiting_for_clear_pnacl_cache_ = false;
990 NotifyAndDeleteIfDone();
993 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
994 // This function should be called on the IO thread.
995 DCHECK_CURRENTLY_ON(BrowserThread::IO);
997 // Notify the UI thread that we are done.
998 BrowserThread::PostTask(
999 BrowserThread::UI, FROM_HERE,
1000 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1001 base::Unretained(this)));
1004 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1005 base::Time end) {
1006 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1008 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1009 begin, end,
1010 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1011 base::Unretained(this)));
1013 #endif
1015 void BrowsingDataRemover::OnWaitableEventSignaled(
1016 base::WaitableEvent* waitable_event) {
1017 waiting_for_clear_plugin_data_ = false;
1018 NotifyAndDeleteIfDone();
1021 #if defined(ENABLE_PLUGINS)
1022 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1023 uint32 request_id,
1024 bool /* success */) {
1025 DCHECK(waiting_for_clear_content_licenses_);
1026 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1028 waiting_for_clear_content_licenses_ = false;
1029 NotifyAndDeleteIfDone();
1031 #endif
1033 #if defined(OS_CHROMEOS)
1034 void BrowsingDataRemover::OnClearPlatformKeys(
1035 chromeos::DBusMethodCallStatus call_status,
1036 bool result) {
1037 DCHECK(waiting_for_clear_platform_keys_);
1038 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1039 LOG(ERROR) << "Failed to clear platform keys.";
1041 waiting_for_clear_platform_keys_ = false;
1042 NotifyAndDeleteIfDone();
1044 #endif
1047 void BrowsingDataRemover::OnClearedPasswords() {
1048 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1049 waiting_for_clear_passwords_ = false;
1050 NotifyAndDeleteIfDone();
1053 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1054 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1055 BrowserThread::PostTask(
1056 BrowserThread::UI, FROM_HERE,
1057 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1058 base::Unretained(this), num_deleted));
1059 return;
1062 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1063 --waiting_for_clear_cookies_count_;
1064 NotifyAndDeleteIfDone();
1067 void BrowsingDataRemover::ClearCookiesOnIOThread(
1068 net::URLRequestContextGetter* rq_context) {
1069 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1070 net::CookieStore* cookie_store = rq_context->
1071 GetURLRequestContext()->cookie_store();
1072 cookie_store->DeleteAllCreatedBetweenAsync(
1073 delete_begin_, delete_end_,
1074 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1075 base::Unretained(this)));
1078 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1079 net::URLRequestContextGetter* rq_context) {
1080 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1081 net::ChannelIDService* channel_id_service =
1082 rq_context->GetURLRequestContext()->channel_id_service();
1083 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1084 delete_begin_, delete_end_,
1085 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1086 base::Unretained(this), base::Unretained(rq_context)));
1089 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1090 net::URLRequestContextGetter* rq_context) {
1091 // Need to close open SSL connections which may be using the channel ids we
1092 // are deleting.
1093 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1094 // service/store have observers that can notify relevant things directly.
1095 rq_context->GetURLRequestContext()->ssl_config_service()->
1096 NotifySSLConfigChange();
1097 BrowserThread::PostTask(
1098 BrowserThread::UI, FROM_HERE,
1099 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1100 base::Unretained(this)));
1103 void BrowsingDataRemover::OnClearedChannelIDs() {
1104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1105 waiting_for_clear_channel_ids_ = false;
1106 NotifyAndDeleteIfDone();
1109 void BrowsingDataRemover::OnClearedFormData() {
1110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1111 waiting_for_clear_form_ = false;
1112 NotifyAndDeleteIfDone();
1115 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1116 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1117 waiting_for_clear_autofill_origin_urls_ = false;
1118 NotifyAndDeleteIfDone();
1121 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1123 waiting_for_clear_storage_partition_data_ = false;
1124 NotifyAndDeleteIfDone();
1127 #if defined(ENABLE_WEBRTC)
1128 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1130 waiting_for_clear_webrtc_logs_ = false;
1131 NotifyAndDeleteIfDone();
1133 #endif
1135 #if defined(OS_ANDROID)
1136 void BrowsingDataRemover::OnClearedPrecacheHistory() {
1137 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1138 waiting_for_clear_precache_history_ = false;
1139 NotifyAndDeleteIfDone();
1141 #endif
1143 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1144 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1145 waiting_for_clear_domain_reliability_monitor_ = false;
1146 NotifyAndDeleteIfDone();
1149 // static
1150 BrowsingDataRemover::CallbackSubscription
1151 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1152 const BrowsingDataRemover::Callback& callback) {
1153 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);