ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blobae8f01e409b8f273725a592fca8d9a5570f8c292
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_CACHE_STORAGE) {
542 storage_partition_remove_mask |=
543 content::StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE;
545 if (remove_mask & REMOVE_FILE_SYSTEMS) {
546 storage_partition_remove_mask |=
547 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
550 #if defined(ENABLE_PLUGINS)
551 // Plugin is data not separated for protected and unprotected web origins. We
552 // check the origin_type_mask_ to prevent unintended deletion.
553 if (remove_mask & REMOVE_PLUGIN_DATA &&
554 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
555 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
557 waiting_for_clear_plugin_data_ = true;
558 if (!plugin_data_remover_.get())
559 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
560 base::WaitableEvent* event =
561 plugin_data_remover_->StartRemoving(delete_begin_);
563 base::WaitableEventWatcher::EventCallback watcher_callback =
564 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
565 base::Unretained(this));
566 watcher_.StartWatching(event, watcher_callback);
568 #endif
570 if (remove_mask & REMOVE_SITE_USAGE_DATA || remove_mask & REMOVE_HISTORY) {
571 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
572 CONTENT_SETTINGS_TYPE_APP_BANNER);
573 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
574 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT);
577 if (remove_mask & REMOVE_PASSWORDS) {
578 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
579 password_manager::PasswordStore* password_store =
580 PasswordStoreFactory::GetForProfile(
581 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
583 if (password_store) {
584 waiting_for_clear_passwords_ = true;
585 password_store->RemoveLoginsCreatedBetween(
586 delete_begin_, delete_end_,
587 base::Bind(&BrowsingDataRemover::OnClearedPasswords,
588 base::Unretained(this)));
592 if (remove_mask & REMOVE_FORM_DATA) {
593 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
594 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
595 WebDataServiceFactory::GetAutofillWebDataForProfile(
596 profile_, ServiceAccessType::EXPLICIT_ACCESS);
598 if (web_data_service.get()) {
599 waiting_for_clear_form_ = true;
600 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
601 delete_end_);
602 web_data_service->RemoveAutofillDataModifiedBetween(
603 delete_begin_, delete_end_);
604 // The above calls are done on the UI thread but do their work on the DB
605 // thread. So wait for it.
606 BrowserThread::PostTaskAndReply(
607 BrowserThread::DB, FROM_HERE,
608 base::Bind(&base::DoNothing),
609 base::Bind(&BrowsingDataRemover::OnClearedFormData,
610 base::Unretained(this)));
612 autofill::PersonalDataManager* data_manager =
613 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
614 if (data_manager)
615 data_manager->Refresh();
619 if (remove_mask & REMOVE_CACHE) {
620 // Tell the renderers to clear their cache.
621 web_cache::WebCacheManager::GetInstance()->ClearCache();
623 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
625 waiting_for_clear_cache_ = true;
626 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
627 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
628 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
629 delete_end_)
630 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
631 base::Unretained(this)));
633 #if !defined(DISABLE_NACL)
634 waiting_for_clear_nacl_cache_ = true;
636 BrowserThread::PostTask(
637 BrowserThread::IO, FROM_HERE,
638 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
639 base::Unretained(this)));
641 waiting_for_clear_pnacl_cache_ = true;
642 BrowserThread::PostTask(
643 BrowserThread::IO, FROM_HERE,
644 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
645 base::Unretained(this), delete_begin_, delete_end_));
646 #endif
648 // The PrerenderManager may have a page actively being prerendered, which
649 // is essentially a preemptively cached page.
650 prerender::PrerenderManager* prerender_manager =
651 prerender::PrerenderManagerFactory::GetForProfile(profile_);
652 if (prerender_manager) {
653 prerender_manager->ClearData(
654 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
657 // Tell the shader disk cache to clear.
658 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
659 storage_partition_remove_mask |=
660 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
662 storage_partition_remove_mask |=
663 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
665 #if defined(ENABLE_EXTENSIONS)
666 // Clear the ephemeral apps cache. This is nullptr while testing. OTR
667 // Profile has neither apps nor an ExtensionService, so ClearCachedApps
668 // fails.
669 EphemeralAppService* ephemeral_app_service =
670 EphemeralAppService::Get(profile_);
671 if (ephemeral_app_service && !profile_->IsOffTheRecord())
672 ephemeral_app_service->ClearCachedApps();
673 #endif
676 if (remove_mask & REMOVE_WEBRTC_IDENTITY) {
677 storage_partition_remove_mask |=
678 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
681 if (storage_partition_remove_mask) {
682 waiting_for_clear_storage_partition_data_ = true;
684 content::StoragePartition* storage_partition;
685 if (storage_partition_for_testing_)
686 storage_partition = storage_partition_for_testing_;
687 else
688 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
690 uint32 quota_storage_remove_mask =
691 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
693 if (delete_begin_ == base::Time() ||
694 origin_type_mask_ &
695 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
696 // If we're deleting since the beginning of time, or we're removing
697 // protected origins, then remove persistent quota data.
698 quota_storage_remove_mask |=
699 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
702 storage_partition->ClearData(
703 storage_partition_remove_mask, quota_storage_remove_mask, remove_url,
704 base::Bind(&DoesOriginMatchMask, origin_type_mask_), delete_begin_,
705 delete_end_,
706 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
707 base::Unretained(this)));
710 #if defined(ENABLE_PLUGINS)
711 if (remove_mask & REMOVE_CONTENT_LICENSES) {
712 content::RecordAction(
713 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
715 waiting_for_clear_content_licenses_ = true;
716 if (!pepper_flash_settings_manager_.get()) {
717 pepper_flash_settings_manager_.reset(
718 new PepperFlashSettingsManager(this, profile_));
720 deauthorize_content_licenses_request_id_ =
721 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
722 #if defined(OS_CHROMEOS)
723 // On Chrome OS, also delete any content protection platform keys.
724 const user_manager::User* user =
725 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
726 if (!user) {
727 LOG(WARNING) << "Failed to find user for current profile.";
728 } else {
729 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
730 TpmAttestationDeleteKeys(
731 chromeos::attestation::KEY_USER,
732 user->email(),
733 chromeos::attestation::kContentProtectionKeyPrefix,
734 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
735 base::Unretained(this)));
736 waiting_for_clear_platform_keys_ = true;
738 #endif
740 #endif
742 // Remove omnibox zero-suggest cache results.
743 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
744 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string());
746 // Always wipe accumulated network related data (TransportSecurityState and
747 // HttpServerPropertiesManager data).
748 waiting_for_clear_networking_history_ = true;
749 profile_->ClearNetworkingHistorySince(
750 delete_begin_,
751 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
752 base::Unretained(this)));
754 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
755 domain_reliability::DomainReliabilityService* service =
756 domain_reliability::DomainReliabilityServiceFactory::
757 GetForBrowserContext(profile_);
758 if (service) {
759 domain_reliability::DomainReliabilityClearMode mode;
760 if (remove_mask & REMOVE_COOKIES)
761 mode = domain_reliability::CLEAR_CONTEXTS;
762 else
763 mode = domain_reliability::CLEAR_BEACONS;
765 waiting_for_clear_domain_reliability_monitor_ = true;
766 service->ClearBrowsingData(
767 mode,
768 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
769 base::Unretained(this)));
773 // Record the combined deletion of cookies and cache.
774 CookieOrCacheDeletionChoice choice = NEITHER_COOKIES_NOR_CACHE;
775 if (remove_mask & REMOVE_COOKIES &&
776 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
777 choice = remove_mask & REMOVE_CACHE ? BOTH_COOKIES_AND_CACHE
778 : ONLY_COOKIES;
779 } else if (remove_mask & REMOVE_CACHE) {
780 choice = ONLY_CACHE;
783 UMA_HISTOGRAM_ENUMERATION(
784 "History.ClearBrowsingData.UserDeletedCookieOrCache",
785 choice, MAX_CHOICE_VALUE);
788 void BrowsingDataRemover::AddObserver(Observer* observer) {
789 observer_list_.AddObserver(observer);
792 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
793 observer_list_.RemoveObserver(observer);
796 void BrowsingDataRemover::OnHistoryDeletionDone() {
797 waiting_for_clear_history_ = false;
798 NotifyAndDeleteIfDone();
801 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
802 content::StoragePartition* storage_partition) {
803 storage_partition_for_testing_ = storage_partition;
806 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
807 TimePeriod time_period) {
808 base::TimeDelta diff;
809 base::Time delete_begin_time = base::Time::Now();
810 switch (time_period) {
811 case LAST_HOUR:
812 diff = base::TimeDelta::FromHours(1);
813 break;
814 case LAST_DAY:
815 diff = base::TimeDelta::FromHours(24);
816 break;
817 case LAST_WEEK:
818 diff = base::TimeDelta::FromHours(7*24);
819 break;
820 case FOUR_WEEKS:
821 diff = base::TimeDelta::FromHours(4*7*24);
822 break;
823 case EVERYTHING:
824 delete_begin_time = base::Time();
825 break;
826 default:
827 NOTREACHED() << L"Missing item";
828 break;
830 return delete_begin_time - diff;
833 bool BrowsingDataRemover::AllDone() {
834 return !waiting_for_clear_autofill_origin_urls_ &&
835 !waiting_for_clear_cache_ &&
836 !waiting_for_clear_content_licenses_ &&
837 !waiting_for_clear_channel_ids_ &&
838 !waiting_for_clear_cookies_count_ &&
839 !waiting_for_clear_domain_reliability_monitor_ &&
840 !waiting_for_clear_form_ &&
841 !waiting_for_clear_history_ &&
842 !waiting_for_clear_hostname_resolution_cache_ &&
843 !waiting_for_clear_keyword_data_ &&
844 !waiting_for_clear_nacl_cache_ &&
845 !waiting_for_clear_network_predictor_ &&
846 !waiting_for_clear_networking_history_ &&
847 !waiting_for_clear_passwords_ &&
848 !waiting_for_clear_platform_keys_ &&
849 !waiting_for_clear_plugin_data_ &&
850 !waiting_for_clear_pnacl_cache_ &&
851 #if defined(OS_ANDROID)
852 !waiting_for_clear_precache_history_ &&
853 #endif
854 #if defined(ENABLE_WEBRTC)
855 !waiting_for_clear_webrtc_logs_ &&
856 #endif
857 !waiting_for_clear_storage_partition_data_;
860 void BrowsingDataRemover::OnKeywordsLoaded() {
861 // Deletes the entries from the model, and if we're not waiting on anything
862 // else notifies observers and deletes this BrowsingDataRemover.
863 TemplateURLService* model =
864 TemplateURLServiceFactory::GetForProfile(profile_);
865 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
866 waiting_for_clear_keyword_data_ = false;
867 template_url_sub_.reset();
868 NotifyAndDeleteIfDone();
871 void BrowsingDataRemover::NotifyAndDelete() {
872 set_removing(false);
874 // Notify observers.
875 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
876 origin_type_mask_);
878 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
880 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
882 // History requests aren't happy if you delete yourself from the callback.
883 // As such, we do a delete later.
884 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
887 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
888 // TODO(brettw) http://crbug.com/305259: This should also observe session
889 // clearing (what about other things such as passwords, etc.?) and wait for
890 // them to complete before continuing.
892 if (!AllDone())
893 return;
895 if (completion_inhibitor_) {
896 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
897 this,
898 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
899 base::Unretained(this)));
900 } else {
901 NotifyAndDelete();
905 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
906 DCHECK_CURRENTLY_ON(BrowserThread::UI);
907 waiting_for_clear_hostname_resolution_cache_ = false;
908 NotifyAndDeleteIfDone();
911 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
912 IOThread* io_thread) {
913 DCHECK_CURRENTLY_ON(BrowserThread::IO);
915 io_thread->ClearHostCache();
917 // Notify the UI thread that we are done.
918 BrowserThread::PostTask(
919 BrowserThread::UI,
920 FROM_HERE,
921 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
922 base::Unretained(this)));
925 void BrowsingDataRemover::OnClearedNetworkPredictor() {
926 DCHECK_CURRENTLY_ON(BrowserThread::UI);
927 waiting_for_clear_network_predictor_ = false;
928 NotifyAndDeleteIfDone();
931 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
932 chrome_browser_net::Predictor* predictor) {
933 DCHECK_CURRENTLY_ON(BrowserThread::IO);
934 DCHECK(predictor);
936 predictor->DiscardInitialNavigationHistory();
937 predictor->DiscardAllResults();
939 // Notify the UI thread that we are done.
940 BrowserThread::PostTask(
941 BrowserThread::UI,
942 FROM_HERE,
943 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
944 base::Unretained(this)));
947 void BrowsingDataRemover::OnClearedNetworkingHistory() {
948 DCHECK_CURRENTLY_ON(BrowserThread::UI);
949 waiting_for_clear_networking_history_ = false;
950 NotifyAndDeleteIfDone();
953 void BrowsingDataRemover::ClearedCache() {
954 waiting_for_clear_cache_ = false;
956 NotifyAndDeleteIfDone();
959 #if !defined(DISABLE_NACL)
960 void BrowsingDataRemover::ClearedNaClCache() {
961 // This function should be called on the UI thread.
962 DCHECK_CURRENTLY_ON(BrowserThread::UI);
964 waiting_for_clear_nacl_cache_ = false;
966 NotifyAndDeleteIfDone();
969 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
970 // This function should be called on the IO thread.
971 DCHECK_CURRENTLY_ON(BrowserThread::IO);
973 // Notify the UI thread that we are done.
974 BrowserThread::PostTask(
975 BrowserThread::UI, FROM_HERE,
976 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
977 base::Unretained(this)));
980 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
981 DCHECK_CURRENTLY_ON(BrowserThread::IO);
983 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
984 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
985 base::Unretained(this)));
988 void BrowsingDataRemover::ClearedPnaclCache() {
989 // This function should be called on the UI thread.
990 DCHECK_CURRENTLY_ON(BrowserThread::UI);
992 waiting_for_clear_pnacl_cache_ = false;
994 NotifyAndDeleteIfDone();
997 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
998 // This function should be called on the IO thread.
999 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1001 // Notify the UI thread that we are done.
1002 BrowserThread::PostTask(
1003 BrowserThread::UI, FROM_HERE,
1004 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1005 base::Unretained(this)));
1008 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1009 base::Time end) {
1010 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1012 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1013 begin, end,
1014 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1015 base::Unretained(this)));
1017 #endif
1019 void BrowsingDataRemover::OnWaitableEventSignaled(
1020 base::WaitableEvent* waitable_event) {
1021 waiting_for_clear_plugin_data_ = false;
1022 NotifyAndDeleteIfDone();
1025 #if defined(ENABLE_PLUGINS)
1026 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1027 uint32 request_id,
1028 bool /* success */) {
1029 DCHECK(waiting_for_clear_content_licenses_);
1030 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1032 waiting_for_clear_content_licenses_ = false;
1033 NotifyAndDeleteIfDone();
1035 #endif
1037 #if defined(OS_CHROMEOS)
1038 void BrowsingDataRemover::OnClearPlatformKeys(
1039 chromeos::DBusMethodCallStatus call_status,
1040 bool result) {
1041 DCHECK(waiting_for_clear_platform_keys_);
1042 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1043 LOG(ERROR) << "Failed to clear platform keys.";
1045 waiting_for_clear_platform_keys_ = false;
1046 NotifyAndDeleteIfDone();
1048 #endif
1051 void BrowsingDataRemover::OnClearedPasswords() {
1052 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1053 waiting_for_clear_passwords_ = false;
1054 NotifyAndDeleteIfDone();
1057 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1058 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1059 BrowserThread::PostTask(
1060 BrowserThread::UI, FROM_HERE,
1061 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1062 base::Unretained(this), num_deleted));
1063 return;
1066 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1067 --waiting_for_clear_cookies_count_;
1068 NotifyAndDeleteIfDone();
1071 void BrowsingDataRemover::ClearCookiesOnIOThread(
1072 net::URLRequestContextGetter* rq_context) {
1073 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1074 net::CookieStore* cookie_store = rq_context->
1075 GetURLRequestContext()->cookie_store();
1076 cookie_store->DeleteAllCreatedBetweenAsync(
1077 delete_begin_, delete_end_,
1078 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1079 base::Unretained(this)));
1082 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1083 net::URLRequestContextGetter* rq_context) {
1084 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1085 net::ChannelIDService* channel_id_service =
1086 rq_context->GetURLRequestContext()->channel_id_service();
1087 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1088 delete_begin_, delete_end_,
1089 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1090 base::Unretained(this), base::Unretained(rq_context)));
1093 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1094 net::URLRequestContextGetter* rq_context) {
1095 // Need to close open SSL connections which may be using the channel ids we
1096 // are deleting.
1097 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1098 // service/store have observers that can notify relevant things directly.
1099 rq_context->GetURLRequestContext()->ssl_config_service()->
1100 NotifySSLConfigChange();
1101 BrowserThread::PostTask(
1102 BrowserThread::UI, FROM_HERE,
1103 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1104 base::Unretained(this)));
1107 void BrowsingDataRemover::OnClearedChannelIDs() {
1108 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1109 waiting_for_clear_channel_ids_ = false;
1110 NotifyAndDeleteIfDone();
1113 void BrowsingDataRemover::OnClearedFormData() {
1114 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1115 waiting_for_clear_form_ = false;
1116 NotifyAndDeleteIfDone();
1119 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1120 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1121 waiting_for_clear_autofill_origin_urls_ = false;
1122 NotifyAndDeleteIfDone();
1125 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1127 waiting_for_clear_storage_partition_data_ = false;
1128 NotifyAndDeleteIfDone();
1131 #if defined(ENABLE_WEBRTC)
1132 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1133 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1134 waiting_for_clear_webrtc_logs_ = false;
1135 NotifyAndDeleteIfDone();
1137 #endif
1139 #if defined(OS_ANDROID)
1140 void BrowsingDataRemover::OnClearedPrecacheHistory() {
1141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1142 waiting_for_clear_precache_history_ = false;
1143 NotifyAndDeleteIfDone();
1145 #endif
1147 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1148 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1149 waiting_for_clear_domain_reliability_monitor_ = false;
1150 NotifyAndDeleteIfDone();
1153 // static
1154 BrowsingDataRemover::CallbackSubscription
1155 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1156 const BrowsingDataRemover::Callback& callback) {
1157 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);