[Sync] Rename PSS::IsSyncEnabled to PSS::IsSyncAllowedByFlag.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blobed3f118627089cbf80251f2415b93add01d68fd6
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/prefs/pref_service.h"
16 #include "chrome/browser/autofill/personal_data_manager_factory.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/browsing_data/browsing_data_helper.h"
19 #include "chrome/browser/chrome_notification_types.h"
20 #include "chrome/browser/domain_reliability/service_factory.h"
21 #include "chrome/browser/download/download_prefs.h"
22 #include "chrome/browser/download/download_service_factory.h"
23 #include "chrome/browser/history/history_service_factory.h"
24 #include "chrome/browser/history/web_history_service_factory.h"
25 #include "chrome/browser/io_thread.h"
26 #include "chrome/browser/media/media_device_id_salt.h"
27 #include "chrome/browser/net/predictor.h"
28 #include "chrome/browser/password_manager/password_store_factory.h"
29 #include "chrome/browser/prerender/prerender_manager.h"
30 #include "chrome/browser/prerender/prerender_manager_factory.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
33 #include "chrome/browser/search_engines/template_url_service_factory.h"
34 #include "chrome/browser/sessions/session_service.h"
35 #include "chrome/browser/sessions/session_service_factory.h"
36 #include "chrome/browser/sessions/tab_restore_service.h"
37 #include "chrome/browser/sessions/tab_restore_service_factory.h"
38 #include "chrome/browser/webdata/web_data_service_factory.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h"
41 #include "components/autofill/core/browser/personal_data_manager.h"
42 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
43 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
44 #include "components/content_settings/core/browser/host_content_settings_map.h"
45 #include "components/domain_reliability/service.h"
46 #include "components/history/core/browser/history_service.h"
47 #include "components/nacl/browser/nacl_browser.h"
48 #include "components/nacl/browser/pnacl_host.h"
49 #include "components/password_manager/core/browser/password_store.h"
50 #include "components/power/origin_power_map.h"
51 #include "components/power/origin_power_map_factory.h"
52 #include "components/search_engines/template_url_service.h"
53 #include "components/web_cache/browser/web_cache_manager.h"
54 #include "content/public/browser/browser_thread.h"
55 #include "content/public/browser/download_manager.h"
56 #include "content/public/browser/notification_service.h"
57 #include "content/public/browser/plugin_data_remover.h"
58 #include "content/public/browser/ssl_host_state_delegate.h"
59 #include "content/public/browser/storage_partition.h"
60 #include "content/public/browser/user_metrics.h"
61 #include "net/base/net_errors.h"
62 #include "net/cookies/cookie_store.h"
63 #include "net/http/transport_security_state.h"
64 #include "net/ssl/channel_id_service.h"
65 #include "net/ssl/channel_id_store.h"
66 #include "net/url_request/url_request_context.h"
67 #include "net/url_request/url_request_context_getter.h"
68 #include "storage/browser/quota/special_storage_policy.h"
70 #if defined(OS_CHROMEOS)
71 #include "chrome/browser/chromeos/profiles/profile_helper.h"
72 #include "chromeos/attestation/attestation_constants.h"
73 #include "chromeos/dbus/cryptohome_client.h"
74 #include "chromeos/dbus/dbus_thread_manager.h"
75 #include "components/user_manager/user.h"
76 #endif
78 #if defined(ENABLE_EXTENSIONS)
79 #include "chrome/browser/apps/ephemeral_app_service.h"
80 #include "chrome/browser/extensions/activity_log/activity_log.h"
81 #include "chrome/browser/extensions/extension_service.h"
82 #include "chrome/browser/extensions/extension_special_storage_policy.h"
83 #include "extensions/browser/extension_prefs.h"
84 #endif
86 #if defined(ENABLE_WEBRTC)
87 #include "chrome/browser/media/webrtc_log_list.h"
88 #include "chrome/browser/media/webrtc_log_util.h"
89 #endif
91 using base::UserMetricsAction;
92 using content::BrowserContext;
93 using content::BrowserThread;
94 using content::DOMStorageContext;
96 namespace {
98 using CallbackList =
99 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
101 // Contains all registered callbacks for browsing data removed notifications.
102 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
104 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
105 // the first time so that it always returns a valid object.
106 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
107 if (!g_on_browsing_data_removed_callbacks)
108 g_on_browsing_data_removed_callbacks = new CallbackList();
109 return g_on_browsing_data_removed_callbacks;
112 } // namespace
114 bool BrowsingDataRemover::is_removing_ = false;
116 BrowsingDataRemover::CompletionInhibitor*
117 BrowsingDataRemover::completion_inhibitor_ = NULL;
119 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
120 // Static.
121 bool DoesOriginMatchMask(
122 int origin_set_mask,
123 const GURL& origin,
124 storage::SpecialStoragePolicy* special_storage_policy) {
125 return BrowsingDataHelper::DoesOriginMatchMask(
126 origin, origin_set_mask, special_storage_policy);
129 BrowsingDataRemover::NotificationDetails::NotificationDetails()
130 : removal_begin(base::Time()),
131 removal_mask(-1),
132 origin_set_mask(-1) {
135 BrowsingDataRemover::NotificationDetails::NotificationDetails(
136 const BrowsingDataRemover::NotificationDetails& details)
137 : removal_begin(details.removal_begin),
138 removal_mask(details.removal_mask),
139 origin_set_mask(details.origin_set_mask) {
142 BrowsingDataRemover::NotificationDetails::NotificationDetails(
143 base::Time removal_begin,
144 int removal_mask,
145 int origin_set_mask)
146 : removal_begin(removal_begin),
147 removal_mask(removal_mask),
148 origin_set_mask(origin_set_mask) {
151 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
153 // Static.
154 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
155 Profile* profile) {
156 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
159 // Static.
160 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
161 base::Time start, base::Time end) {
162 return new BrowsingDataRemover(profile, start, end);
165 // Static.
166 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
167 TimePeriod period) {
168 switch (period) {
169 case LAST_HOUR:
170 content::RecordAction(
171 UserMetricsAction("ClearBrowsingData_LastHour"));
172 break;
173 case LAST_DAY:
174 content::RecordAction(
175 UserMetricsAction("ClearBrowsingData_LastDay"));
176 break;
177 case LAST_WEEK:
178 content::RecordAction(
179 UserMetricsAction("ClearBrowsingData_LastWeek"));
180 break;
181 case FOUR_WEEKS:
182 content::RecordAction(
183 UserMetricsAction("ClearBrowsingData_LastMonth"));
184 break;
185 case EVERYTHING:
186 content::RecordAction(
187 UserMetricsAction("ClearBrowsingData_Everything"));
188 break;
190 return new BrowsingDataRemover(profile,
191 BrowsingDataRemover::CalculateBeginDeleteTime(period),
192 base::Time::Max());
195 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
196 base::Time delete_begin,
197 base::Time delete_end)
198 : profile_(profile),
199 delete_begin_(delete_begin),
200 delete_end_(delete_end),
201 main_context_getter_(profile->GetRequestContext()),
202 media_context_getter_(profile->GetMediaRequestContext()),
203 deauthorize_content_licenses_request_id_(0),
204 waiting_for_clear_autofill_origin_urls_(false),
205 waiting_for_clear_cache_(false),
206 waiting_for_clear_channel_ids_(false),
207 waiting_for_clear_content_licenses_(false),
208 waiting_for_clear_cookies_count_(0),
209 waiting_for_clear_domain_reliability_monitor_(false),
210 waiting_for_clear_form_(false),
211 waiting_for_clear_history_(false),
212 waiting_for_clear_hostname_resolution_cache_(false),
213 waiting_for_clear_keyword_data_(false),
214 waiting_for_clear_nacl_cache_(false),
215 waiting_for_clear_network_predictor_(false),
216 waiting_for_clear_networking_history_(false),
217 waiting_for_clear_platform_keys_(false),
218 waiting_for_clear_plugin_data_(false),
219 waiting_for_clear_pnacl_cache_(false),
220 waiting_for_clear_storage_partition_data_(false),
221 #if defined(ENABLE_WEBRTC)
222 waiting_for_clear_webrtc_logs_(false),
223 #endif
224 remove_mask_(0),
225 remove_origin_(GURL()),
226 origin_set_mask_(0),
227 storage_partition_for_testing_(NULL) {
228 DCHECK(profile);
229 // crbug.com/140910: Many places were calling this with base::Time() as
230 // delete_end, even though they should've used base::Time::Max(). Work around
231 // it here. New code should use base::Time::Max().
232 DCHECK(delete_end_ != base::Time());
233 if (delete_end_ == base::Time())
234 delete_end_ = base::Time::Max();
237 BrowsingDataRemover::~BrowsingDataRemover() {
238 DCHECK(AllDone());
241 // Static.
242 void BrowsingDataRemover::set_removing(bool is_removing) {
243 DCHECK(is_removing_ != is_removing);
244 is_removing_ = is_removing;
247 void BrowsingDataRemover::Remove(int remove_mask, int origin_set_mask) {
248 RemoveImpl(remove_mask, GURL(), origin_set_mask);
251 void BrowsingDataRemover::RemoveImpl(int remove_mask,
252 const GURL& origin,
253 int origin_set_mask) {
254 DCHECK_CURRENTLY_ON(BrowserThread::UI);
255 set_removing(true);
256 remove_mask_ = remove_mask;
257 remove_origin_ = origin;
258 origin_set_mask_ = origin_set_mask;
260 PrefService* prefs = profile_->GetPrefs();
261 bool may_delete_history = prefs->GetBoolean(
262 prefs::kAllowDeletingBrowserHistory);
264 // All the UI entry points into the BrowsingDataRemover should be disabled,
265 // but this will fire if something was missed or added.
266 DCHECK(may_delete_history ||
267 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
269 if (origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
270 content::RecordAction(
271 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
273 if (origin_set_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
274 content::RecordAction(
275 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
277 if (origin_set_mask_ & BrowsingDataHelper::EXTENSION) {
278 content::RecordAction(
279 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
281 // If this fires, we added a new BrowsingDataHelper::OriginSetMask without
282 // updating the user metrics above.
283 static_assert(
284 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
285 BrowsingDataHelper::PROTECTED_WEB |
286 BrowsingDataHelper::EXTENSION),
287 "OriginSetMask has been updated without updating user metrics");
289 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
290 history::HistoryService* history_service =
291 HistoryServiceFactory::GetForProfile(
292 profile_, ServiceAccessType::EXPLICIT_ACCESS);
293 if (history_service) {
294 std::set<GURL> restrict_urls;
295 if (!remove_origin_.is_empty())
296 restrict_urls.insert(remove_origin_);
297 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
298 waiting_for_clear_history_ = true;
300 history_service->ExpireLocalAndRemoteHistoryBetween(
301 WebHistoryServiceFactory::GetForProfile(profile_),
302 restrict_urls, delete_begin_, delete_end_,
303 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
304 base::Unretained(this)),
305 &history_task_tracker_);
307 #if defined(ENABLE_EXTENSIONS)
308 // The extension activity contains details of which websites extensions
309 // were active on. It therefore indirectly stores details of websites a
310 // user has visited so best clean from here as well.
311 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
312 #endif
315 #if defined(ENABLE_EXTENSIONS)
316 // Clear launch times as they are a form of history.
317 extensions::ExtensionPrefs* extension_prefs =
318 extensions::ExtensionPrefs::Get(profile_);
319 extension_prefs->ClearLastLaunchTimes();
320 #endif
322 // The power consumption history by origin contains details of websites
323 // that were visited.
324 power::OriginPowerMap* origin_power_map =
325 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
326 if (origin_power_map)
327 origin_power_map->ClearOriginMap();
329 // Need to clear the host cache and accumulated speculative data, as it also
330 // reveals some history: we have no mechanism to track when these items were
331 // created, so we'll clear them all. Better safe than sorry.
332 if (g_browser_process->io_thread()) {
333 waiting_for_clear_hostname_resolution_cache_ = true;
334 BrowserThread::PostTask(
335 BrowserThread::IO, FROM_HERE,
336 base::Bind(
337 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
338 base::Unretained(this),
339 g_browser_process->io_thread()));
341 if (profile_->GetNetworkPredictor()) {
342 waiting_for_clear_network_predictor_ = true;
343 BrowserThread::PostTask(
344 BrowserThread::IO, FROM_HERE,
345 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
346 base::Unretained(this),
347 profile_->GetNetworkPredictor()));
350 // As part of history deletion we also delete the auto-generated keywords.
351 TemplateURLService* keywords_model =
352 TemplateURLServiceFactory::GetForProfile(profile_);
353 if (keywords_model && !keywords_model->loaded()) {
354 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
355 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
356 base::Unretained(this)));
357 keywords_model->Load();
358 waiting_for_clear_keyword_data_ = true;
359 } else if (keywords_model) {
360 keywords_model->RemoveAutoGeneratedForOriginBetween(remove_origin_,
361 delete_begin_, delete_end_);
364 // The PrerenderManager keeps history of prerendered pages, so clear that.
365 // It also may have a prerendered page. If so, the page could be
366 // considered to have a small amount of historical information, so delete
367 // it, too.
368 prerender::PrerenderManager* prerender_manager =
369 prerender::PrerenderManagerFactory::GetForProfile(profile_);
370 if (prerender_manager) {
371 prerender_manager->ClearData(
372 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
373 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
376 // If the caller is removing history for all hosts, then clear ancillary
377 // historical information.
378 if (remove_origin_.is_empty()) {
379 // We also delete the list of recently closed tabs. Since these expire,
380 // they can't be more than a day old, so we can simply clear them all.
381 TabRestoreService* tab_service =
382 TabRestoreServiceFactory::GetForProfile(profile_);
383 if (tab_service) {
384 tab_service->ClearEntries();
385 tab_service->DeleteLastSession();
388 #if defined(ENABLE_SESSION_SERVICE)
389 // We also delete the last session when we delete the history.
390 SessionService* session_service =
391 SessionServiceFactory::GetForProfile(profile_);
392 if (session_service)
393 session_service->DeleteLastSession();
394 #endif
397 // The saved Autofill profiles and credit cards can include the origin from
398 // which these profiles and credit cards were learned. These are a form of
399 // history, so clear them as well.
400 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
401 WebDataServiceFactory::GetAutofillWebDataForProfile(
402 profile_, ServiceAccessType::EXPLICIT_ACCESS);
403 if (web_data_service.get()) {
404 waiting_for_clear_autofill_origin_urls_ = true;
405 web_data_service->RemoveOriginURLsModifiedBetween(
406 delete_begin_, delete_end_);
407 // The above calls are done on the UI thread but do their work on the DB
408 // thread. So wait for it.
409 BrowserThread::PostTaskAndReply(
410 BrowserThread::DB, FROM_HERE,
411 base::Bind(&base::DoNothing),
412 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
413 base::Unretained(this)));
415 autofill::PersonalDataManager* data_manager =
416 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
417 if (data_manager)
418 data_manager->Refresh();
421 #if defined(ENABLE_WEBRTC)
422 waiting_for_clear_webrtc_logs_ = true;
423 BrowserThread::PostTaskAndReply(
424 BrowserThread::FILE,
425 FROM_HERE,
426 base::Bind(
427 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
428 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
429 delete_begin_),
430 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
431 base::Unretained(this)));
432 #endif
434 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
435 // include origins that the user has visited, so it must be cleared.
436 if (profile_->GetSSLHostStateDelegate())
437 profile_->GetSSLHostStateDelegate()->Clear();
440 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
441 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
442 content::DownloadManager* download_manager =
443 BrowserContext::GetDownloadManager(profile_);
444 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
445 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
446 download_manager);
447 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
450 uint32 storage_partition_remove_mask = 0;
452 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
453 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
454 // don't accidentally remove the cookies that are associated with the
455 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
456 // between UNPROTECTED_WEB and PROTECTED_WEB.
457 if (remove_mask & REMOVE_COOKIES &&
458 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
459 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
461 storage_partition_remove_mask |=
462 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
464 #if defined(SAFE_BROWSING_SERVICE)
465 // Clear the safebrowsing cookies only if time period is for "all time". It
466 // doesn't make sense to apply the time period of deleting in the last X
467 // hours/days to the safebrowsing cookies since they aren't the result of
468 // any user action.
469 if (delete_begin_ == base::Time()) {
470 SafeBrowsingService* sb_service =
471 g_browser_process->safe_browsing_service();
472 if (sb_service) {
473 net::URLRequestContextGetter* sb_context =
474 sb_service->url_request_context();
475 ++waiting_for_clear_cookies_count_;
476 BrowserThread::PostTask(
477 BrowserThread::IO, FROM_HERE,
478 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
479 base::Unretained(this), base::Unretained(sb_context)));
482 #endif
483 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
485 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
486 // flag for all credentials in the password store.
489 // Channel IDs are not separated for protected and unprotected web
490 // origins. We check the origin_set_mask_ to prevent unintended deletion.
491 if (remove_mask & REMOVE_CHANNEL_IDS &&
492 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
493 content::RecordAction(
494 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
495 // Since we are running on the UI thread don't call GetURLRequestContext().
496 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
497 if (rq_context) {
498 waiting_for_clear_channel_ids_ = true;
499 BrowserThread::PostTask(
500 BrowserThread::IO, FROM_HERE,
501 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
502 base::Unretained(this), base::Unretained(rq_context)));
506 if (remove_mask & REMOVE_LOCAL_STORAGE) {
507 storage_partition_remove_mask |=
508 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
511 if (remove_mask & REMOVE_INDEXEDDB) {
512 storage_partition_remove_mask |=
513 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
515 if (remove_mask & REMOVE_WEBSQL) {
516 storage_partition_remove_mask |=
517 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
519 if (remove_mask & REMOVE_APPCACHE) {
520 storage_partition_remove_mask |=
521 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
523 if (remove_mask & REMOVE_SERVICE_WORKERS) {
524 storage_partition_remove_mask |=
525 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
527 if (remove_mask & REMOVE_FILE_SYSTEMS) {
528 storage_partition_remove_mask |=
529 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
532 #if defined(ENABLE_PLUGINS)
533 // Plugin is data not separated for protected and unprotected web origins. We
534 // check the origin_set_mask_ to prevent unintended deletion.
535 if (remove_mask & REMOVE_PLUGIN_DATA &&
536 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
537 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
539 waiting_for_clear_plugin_data_ = true;
540 if (!plugin_data_remover_.get())
541 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
542 base::WaitableEvent* event =
543 plugin_data_remover_->StartRemoving(delete_begin_);
545 base::WaitableEventWatcher::EventCallback watcher_callback =
546 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
547 base::Unretained(this));
548 watcher_.StartWatching(event, watcher_callback);
550 #endif
552 #if defined(OS_ANDROID)
553 if (remove_mask & REMOVE_APP_BANNER_DATA || remove_mask & REMOVE_HISTORY) {
554 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
555 CONTENT_SETTINGS_TYPE_APP_BANNER);
557 #endif
559 if (remove_mask & REMOVE_PASSWORDS) {
560 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
561 password_manager::PasswordStore* password_store =
562 PasswordStoreFactory::GetForProfile(
563 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
565 if (password_store)
566 password_store->RemoveLoginsCreatedBetween(delete_begin_, delete_end_);
569 if (remove_mask & REMOVE_FORM_DATA) {
570 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
571 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
572 WebDataServiceFactory::GetAutofillWebDataForProfile(
573 profile_, ServiceAccessType::EXPLICIT_ACCESS);
575 if (web_data_service.get()) {
576 waiting_for_clear_form_ = true;
577 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
578 delete_end_);
579 web_data_service->RemoveAutofillDataModifiedBetween(
580 delete_begin_, delete_end_);
581 // The above calls are done on the UI thread but do their work on the DB
582 // thread. So wait for it.
583 BrowserThread::PostTaskAndReply(
584 BrowserThread::DB, FROM_HERE,
585 base::Bind(&base::DoNothing),
586 base::Bind(&BrowsingDataRemover::OnClearedFormData,
587 base::Unretained(this)));
589 autofill::PersonalDataManager* data_manager =
590 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
591 if (data_manager)
592 data_manager->Refresh();
596 if (remove_mask & REMOVE_CACHE) {
597 // Tell the renderers to clear their cache.
598 web_cache::WebCacheManager::GetInstance()->ClearCache();
600 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
602 waiting_for_clear_cache_ = true;
603 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
604 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
605 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
606 delete_end_)
607 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
608 base::Unretained(this)));
610 #if !defined(DISABLE_NACL)
611 waiting_for_clear_nacl_cache_ = true;
613 BrowserThread::PostTask(
614 BrowserThread::IO, FROM_HERE,
615 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
616 base::Unretained(this)));
618 waiting_for_clear_pnacl_cache_ = true;
619 BrowserThread::PostTask(
620 BrowserThread::IO, FROM_HERE,
621 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
622 base::Unretained(this), delete_begin_, delete_end_));
623 #endif
625 // The PrerenderManager may have a page actively being prerendered, which
626 // is essentially a preemptively cached page.
627 prerender::PrerenderManager* prerender_manager =
628 prerender::PrerenderManagerFactory::GetForProfile(profile_);
629 if (prerender_manager) {
630 prerender_manager->ClearData(
631 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
634 // Tell the shader disk cache to clear.
635 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
636 storage_partition_remove_mask |=
637 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
639 storage_partition_remove_mask |=
640 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
642 #if defined(ENABLE_EXTENSIONS)
643 // Clear the ephemeral apps cache. This is NULL while testing. OTR Profile
644 // has neither apps nor an ExtensionService, so ClearCachedApps fails.
645 EphemeralAppService* ephemeral_app_service =
646 EphemeralAppService::Get(profile_);
647 if (ephemeral_app_service && !profile_->IsOffTheRecord())
648 ephemeral_app_service->ClearCachedApps();
649 #endif
652 if (storage_partition_remove_mask) {
653 waiting_for_clear_storage_partition_data_ = true;
655 content::StoragePartition* storage_partition;
656 if (storage_partition_for_testing_)
657 storage_partition = storage_partition_for_testing_;
658 else
659 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
661 uint32 quota_storage_remove_mask =
662 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
664 if (delete_begin_ == base::Time() ||
665 origin_set_mask_ &
666 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
667 // If we're deleting since the beginning of time, or we're removing
668 // protected origins, then remove persistent quota data.
669 quota_storage_remove_mask |=
670 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
673 storage_partition->ClearData(
674 storage_partition_remove_mask,
675 quota_storage_remove_mask,
676 remove_origin_,
677 base::Bind(&DoesOriginMatchMask, origin_set_mask_),
678 delete_begin_,
679 delete_end_,
680 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
681 base::Unretained(this)));
684 #if defined(ENABLE_PLUGINS)
685 if (remove_mask & REMOVE_CONTENT_LICENSES) {
686 content::RecordAction(
687 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
689 waiting_for_clear_content_licenses_ = true;
690 if (!pepper_flash_settings_manager_.get()) {
691 pepper_flash_settings_manager_.reset(
692 new PepperFlashSettingsManager(this, profile_));
694 deauthorize_content_licenses_request_id_ =
695 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
696 #if defined(OS_CHROMEOS)
697 // On Chrome OS, also delete any content protection platform keys.
698 const user_manager::User* user =
699 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
700 if (!user) {
701 LOG(WARNING) << "Failed to find user for current profile.";
702 } else {
703 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
704 TpmAttestationDeleteKeys(
705 chromeos::attestation::KEY_USER,
706 user->email(),
707 chromeos::attestation::kContentProtectionKeyPrefix,
708 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
709 base::Unretained(this)));
710 waiting_for_clear_platform_keys_ = true;
712 #endif
714 #endif
716 // Remove omnibox zero-suggest cache results.
717 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
718 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string());
720 // Always wipe accumulated network related data (TransportSecurityState and
721 // HttpServerPropertiesManager data).
722 waiting_for_clear_networking_history_ = true;
723 profile_->ClearNetworkingHistorySince(
724 delete_begin_,
725 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
726 base::Unretained(this)));
728 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
729 domain_reliability::DomainReliabilityService* service =
730 domain_reliability::DomainReliabilityServiceFactory::
731 GetForBrowserContext(profile_);
732 if (service) {
733 domain_reliability::DomainReliabilityClearMode mode;
734 if (remove_mask & REMOVE_COOKIES)
735 mode = domain_reliability::CLEAR_CONTEXTS;
736 else
737 mode = domain_reliability::CLEAR_BEACONS;
739 waiting_for_clear_domain_reliability_monitor_ = true;
740 service->ClearBrowsingData(
741 mode,
742 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
743 base::Unretained(this)));
748 void BrowsingDataRemover::AddObserver(Observer* observer) {
749 observer_list_.AddObserver(observer);
752 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
753 observer_list_.RemoveObserver(observer);
756 void BrowsingDataRemover::OnHistoryDeletionDone() {
757 waiting_for_clear_history_ = false;
758 NotifyAndDeleteIfDone();
761 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
762 content::StoragePartition* storage_partition) {
763 storage_partition_for_testing_ = storage_partition;
766 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
767 TimePeriod time_period) {
768 base::TimeDelta diff;
769 base::Time delete_begin_time = base::Time::Now();
770 switch (time_period) {
771 case LAST_HOUR:
772 diff = base::TimeDelta::FromHours(1);
773 break;
774 case LAST_DAY:
775 diff = base::TimeDelta::FromHours(24);
776 break;
777 case LAST_WEEK:
778 diff = base::TimeDelta::FromHours(7*24);
779 break;
780 case FOUR_WEEKS:
781 diff = base::TimeDelta::FromHours(4*7*24);
782 break;
783 case EVERYTHING:
784 delete_begin_time = base::Time();
785 break;
786 default:
787 NOTREACHED() << L"Missing item";
788 break;
790 return delete_begin_time - diff;
793 bool BrowsingDataRemover::AllDone() {
794 return !waiting_for_clear_autofill_origin_urls_ &&
795 !waiting_for_clear_cache_ &&
796 !waiting_for_clear_content_licenses_ &&
797 !waiting_for_clear_channel_ids_ &&
798 !waiting_for_clear_cookies_count_ &&
799 !waiting_for_clear_domain_reliability_monitor_ &&
800 !waiting_for_clear_form_ &&
801 !waiting_for_clear_history_ &&
802 !waiting_for_clear_hostname_resolution_cache_ &&
803 !waiting_for_clear_keyword_data_ &&
804 !waiting_for_clear_nacl_cache_ &&
805 !waiting_for_clear_network_predictor_ &&
806 !waiting_for_clear_networking_history_ &&
807 !waiting_for_clear_platform_keys_ &&
808 !waiting_for_clear_plugin_data_ &&
809 !waiting_for_clear_pnacl_cache_ &&
810 #if defined(ENABLE_WEBRTC)
811 !waiting_for_clear_webrtc_logs_ &&
812 #endif
813 !waiting_for_clear_storage_partition_data_;
816 void BrowsingDataRemover::OnKeywordsLoaded() {
817 // Deletes the entries from the model, and if we're not waiting on anything
818 // else notifies observers and deletes this BrowsingDataRemover.
819 TemplateURLService* model =
820 TemplateURLServiceFactory::GetForProfile(profile_);
821 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
822 waiting_for_clear_keyword_data_ = false;
823 template_url_sub_.reset();
824 NotifyAndDeleteIfDone();
827 void BrowsingDataRemover::NotifyAndDelete() {
828 set_removing(false);
830 // Notify observers.
831 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
832 origin_set_mask_);
834 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
836 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
838 // History requests aren't happy if you delete yourself from the callback.
839 // As such, we do a delete later.
840 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
843 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
844 // TODO(brettw) http://crbug.com/305259: This should also observe session
845 // clearing (what about other things such as passwords, etc.?) and wait for
846 // them to complete before continuing.
848 if (!AllDone())
849 return;
851 if (completion_inhibitor_) {
852 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
853 this,
854 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
855 base::Unretained(this)));
856 } else {
857 NotifyAndDelete();
861 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
862 DCHECK_CURRENTLY_ON(BrowserThread::UI);
863 waiting_for_clear_hostname_resolution_cache_ = false;
864 NotifyAndDeleteIfDone();
867 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
868 IOThread* io_thread) {
869 DCHECK_CURRENTLY_ON(BrowserThread::IO);
871 io_thread->ClearHostCache();
873 // Notify the UI thread that we are done.
874 BrowserThread::PostTask(
875 BrowserThread::UI,
876 FROM_HERE,
877 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
878 base::Unretained(this)));
881 void BrowsingDataRemover::OnClearedNetworkPredictor() {
882 DCHECK_CURRENTLY_ON(BrowserThread::UI);
883 waiting_for_clear_network_predictor_ = false;
884 NotifyAndDeleteIfDone();
887 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
888 chrome_browser_net::Predictor* predictor) {
889 DCHECK_CURRENTLY_ON(BrowserThread::IO);
890 DCHECK(predictor);
892 predictor->DiscardInitialNavigationHistory();
893 predictor->DiscardAllResults();
895 // Notify the UI thread that we are done.
896 BrowserThread::PostTask(
897 BrowserThread::UI,
898 FROM_HERE,
899 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
900 base::Unretained(this)));
903 void BrowsingDataRemover::OnClearedNetworkingHistory() {
904 DCHECK_CURRENTLY_ON(BrowserThread::UI);
905 waiting_for_clear_networking_history_ = false;
906 NotifyAndDeleteIfDone();
909 void BrowsingDataRemover::ClearedCache() {
910 waiting_for_clear_cache_ = false;
912 NotifyAndDeleteIfDone();
915 #if !defined(DISABLE_NACL)
916 void BrowsingDataRemover::ClearedNaClCache() {
917 // This function should be called on the UI thread.
918 DCHECK_CURRENTLY_ON(BrowserThread::UI);
920 waiting_for_clear_nacl_cache_ = false;
922 NotifyAndDeleteIfDone();
925 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
926 // This function should be called on the IO thread.
927 DCHECK_CURRENTLY_ON(BrowserThread::IO);
929 // Notify the UI thread that we are done.
930 BrowserThread::PostTask(
931 BrowserThread::UI, FROM_HERE,
932 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
933 base::Unretained(this)));
936 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
937 DCHECK_CURRENTLY_ON(BrowserThread::IO);
939 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
940 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
941 base::Unretained(this)));
944 void BrowsingDataRemover::ClearedPnaclCache() {
945 // This function should be called on the UI thread.
946 DCHECK_CURRENTLY_ON(BrowserThread::UI);
948 waiting_for_clear_pnacl_cache_ = false;
950 NotifyAndDeleteIfDone();
953 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
954 // This function should be called on the IO thread.
955 DCHECK_CURRENTLY_ON(BrowserThread::IO);
957 // Notify the UI thread that we are done.
958 BrowserThread::PostTask(
959 BrowserThread::UI, FROM_HERE,
960 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
961 base::Unretained(this)));
964 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
965 base::Time end) {
966 DCHECK_CURRENTLY_ON(BrowserThread::IO);
968 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
969 begin, end,
970 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
971 base::Unretained(this)));
973 #endif
975 void BrowsingDataRemover::OnWaitableEventSignaled(
976 base::WaitableEvent* waitable_event) {
977 waiting_for_clear_plugin_data_ = false;
978 NotifyAndDeleteIfDone();
981 #if defined(ENABLE_PLUGINS)
982 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
983 uint32 request_id,
984 bool /* success */) {
985 DCHECK(waiting_for_clear_content_licenses_);
986 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
988 waiting_for_clear_content_licenses_ = false;
989 NotifyAndDeleteIfDone();
991 #endif
993 #if defined(OS_CHROMEOS)
994 void BrowsingDataRemover::OnClearPlatformKeys(
995 chromeos::DBusMethodCallStatus call_status,
996 bool result) {
997 DCHECK(waiting_for_clear_platform_keys_);
998 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
999 LOG(ERROR) << "Failed to clear platform keys.";
1001 waiting_for_clear_platform_keys_ = false;
1002 NotifyAndDeleteIfDone();
1004 #endif
1006 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1007 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1008 BrowserThread::PostTask(
1009 BrowserThread::UI, FROM_HERE,
1010 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1011 base::Unretained(this), num_deleted));
1012 return;
1015 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1016 --waiting_for_clear_cookies_count_;
1017 NotifyAndDeleteIfDone();
1020 void BrowsingDataRemover::ClearCookiesOnIOThread(
1021 net::URLRequestContextGetter* rq_context) {
1022 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1023 net::CookieStore* cookie_store = rq_context->
1024 GetURLRequestContext()->cookie_store();
1025 cookie_store->DeleteAllCreatedBetweenAsync(
1026 delete_begin_, delete_end_,
1027 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1028 base::Unretained(this)));
1031 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1032 net::URLRequestContextGetter* rq_context) {
1033 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1034 net::ChannelIDService* channel_id_service =
1035 rq_context->GetURLRequestContext()->channel_id_service();
1036 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1037 delete_begin_, delete_end_,
1038 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1039 base::Unretained(this), base::Unretained(rq_context)));
1042 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1043 net::URLRequestContextGetter* rq_context) {
1044 // Need to close open SSL connections which may be using the channel ids we
1045 // are deleting.
1046 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1047 // service/store have observers that can notify relevant things directly.
1048 rq_context->GetURLRequestContext()->ssl_config_service()->
1049 NotifySSLConfigChange();
1050 BrowserThread::PostTask(
1051 BrowserThread::UI, FROM_HERE,
1052 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1053 base::Unretained(this)));
1056 void BrowsingDataRemover::OnClearedChannelIDs() {
1057 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1058 waiting_for_clear_channel_ids_ = false;
1059 NotifyAndDeleteIfDone();
1062 void BrowsingDataRemover::OnClearedFormData() {
1063 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1064 waiting_for_clear_form_ = false;
1065 NotifyAndDeleteIfDone();
1068 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1069 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1070 waiting_for_clear_autofill_origin_urls_ = false;
1071 NotifyAndDeleteIfDone();
1074 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1075 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1076 waiting_for_clear_storage_partition_data_ = false;
1077 NotifyAndDeleteIfDone();
1080 #if defined(ENABLE_WEBRTC)
1081 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1082 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1083 waiting_for_clear_webrtc_logs_ = false;
1084 NotifyAndDeleteIfDone();
1086 #endif
1088 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1089 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1090 waiting_for_clear_domain_reliability_monitor_ = false;
1091 NotifyAndDeleteIfDone();
1094 // static
1095 BrowsingDataRemover::CallbackSubscription
1096 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1097 const BrowsingDataRemover::Callback& callback) {
1098 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);