Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blob4f41816acf8f53878debeae4744c75ea5201a23f
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/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/omnibox/browser/omnibox_pref_names.h"
50 #include "components/password_manager/core/browser/password_store.h"
51 #include "components/power/origin_power_map.h"
52 #include "components/power/origin_power_map_factory.h"
53 #include "components/search_engines/template_url_service.h"
54 #include "components/web_cache/browser/web_cache_manager.h"
55 #include "content/public/browser/browser_thread.h"
56 #include "content/public/browser/download_manager.h"
57 #include "content/public/browser/notification_service.h"
58 #include "content/public/browser/plugin_data_remover.h"
59 #include "content/public/browser/ssl_host_state_delegate.h"
60 #include "content/public/browser/storage_partition.h"
61 #include "content/public/browser/user_metrics.h"
62 #include "net/base/net_errors.h"
63 #include "net/cookies/cookie_store.h"
64 #include "net/http/transport_security_state.h"
65 #include "net/ssl/channel_id_service.h"
66 #include "net/ssl/channel_id_store.h"
67 #include "net/url_request/url_request_context.h"
68 #include "net/url_request/url_request_context_getter.h"
69 #include "storage/browser/quota/special_storage_policy.h"
71 #if defined(OS_ANDROID)
72 #include "chrome/browser/precache/precache_manager_factory.h"
73 #include "components/precache/content/precache_manager.h"
74 #endif
76 #if defined(OS_CHROMEOS)
77 #include "chrome/browser/chromeos/profiles/profile_helper.h"
78 #include "chromeos/attestation/attestation_constants.h"
79 #include "chromeos/dbus/cryptohome_client.h"
80 #include "chromeos/dbus/dbus_thread_manager.h"
81 #include "components/user_manager/user.h"
82 #endif
84 #if defined(ENABLE_EXTENSIONS)
85 #include "chrome/browser/apps/ephemeral_app_service.h"
86 #include "chrome/browser/extensions/activity_log/activity_log.h"
87 #include "chrome/browser/extensions/extension_service.h"
88 #include "chrome/browser/extensions/extension_special_storage_policy.h"
89 #include "extensions/browser/extension_prefs.h"
90 #endif
92 #if defined(ENABLE_WEBRTC)
93 #include "chrome/browser/media/webrtc_log_list.h"
94 #include "chrome/browser/media/webrtc_log_util.h"
95 #endif
97 using base::UserMetricsAction;
98 using content::BrowserContext;
99 using content::BrowserThread;
100 using content::DOMStorageContext;
102 namespace {
104 using CallbackList =
105 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
107 // Contains all registered callbacks for browsing data removed notifications.
108 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
110 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
111 // the first time so that it always returns a valid object.
112 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
113 if (!g_on_browsing_data_removed_callbacks)
114 g_on_browsing_data_removed_callbacks = new CallbackList();
115 return g_on_browsing_data_removed_callbacks;
118 } // namespace
120 bool BrowsingDataRemover::is_removing_ = false;
122 BrowsingDataRemover::CompletionInhibitor*
123 BrowsingDataRemover::completion_inhibitor_ = NULL;
125 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
126 // Static.
127 bool DoesOriginMatchMask(
128 int origin_type_mask,
129 const GURL& origin,
130 storage::SpecialStoragePolicy* special_storage_policy) {
131 return BrowsingDataHelper::DoesOriginMatchMask(
132 origin, origin_type_mask, special_storage_policy);
135 BrowsingDataRemover::NotificationDetails::NotificationDetails()
136 : removal_begin(base::Time()),
137 removal_mask(-1),
138 origin_type_mask(-1) {
141 BrowsingDataRemover::NotificationDetails::NotificationDetails(
142 const BrowsingDataRemover::NotificationDetails& details)
143 : removal_begin(details.removal_begin),
144 removal_mask(details.removal_mask),
145 origin_type_mask(details.origin_type_mask) {
148 BrowsingDataRemover::NotificationDetails::NotificationDetails(
149 base::Time removal_begin,
150 int removal_mask,
151 int origin_type_mask)
152 : removal_begin(removal_begin),
153 removal_mask(removal_mask),
154 origin_type_mask(origin_type_mask) {
157 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
159 // Static.
160 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
161 Profile* profile) {
162 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
165 // Static.
166 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
167 base::Time start, base::Time end) {
168 return new BrowsingDataRemover(profile, start, end);
171 // Static.
172 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
173 TimePeriod period) {
174 switch (period) {
175 case LAST_HOUR:
176 content::RecordAction(
177 UserMetricsAction("ClearBrowsingData_LastHour"));
178 break;
179 case LAST_DAY:
180 content::RecordAction(
181 UserMetricsAction("ClearBrowsingData_LastDay"));
182 break;
183 case LAST_WEEK:
184 content::RecordAction(
185 UserMetricsAction("ClearBrowsingData_LastWeek"));
186 break;
187 case FOUR_WEEKS:
188 content::RecordAction(
189 UserMetricsAction("ClearBrowsingData_LastMonth"));
190 break;
191 case EVERYTHING:
192 content::RecordAction(
193 UserMetricsAction("ClearBrowsingData_Everything"));
194 break;
196 return new BrowsingDataRemover(profile,
197 BrowsingDataRemover::CalculateBeginDeleteTime(period),
198 base::Time::Max());
201 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
202 base::Time delete_begin,
203 base::Time delete_end)
204 : profile_(profile),
205 delete_begin_(delete_begin),
206 delete_end_(delete_end),
207 main_context_getter_(profile->GetRequestContext()),
208 media_context_getter_(profile->GetMediaRequestContext()),
209 deauthorize_content_licenses_request_id_(0),
210 waiting_for_clear_autofill_origin_urls_(false),
211 waiting_for_clear_cache_(false),
212 waiting_for_clear_channel_ids_(false),
213 waiting_for_clear_content_licenses_(false),
214 waiting_for_clear_cookies_count_(0),
215 waiting_for_clear_domain_reliability_monitor_(false),
216 waiting_for_clear_form_(false),
217 waiting_for_clear_history_(false),
218 waiting_for_clear_hostname_resolution_cache_(false),
219 waiting_for_clear_keyword_data_(false),
220 waiting_for_clear_nacl_cache_(false),
221 waiting_for_clear_network_predictor_(false),
222 waiting_for_clear_networking_history_(false),
223 waiting_for_clear_passwords_(false),
224 waiting_for_clear_platform_keys_(false),
225 waiting_for_clear_plugin_data_(false),
226 waiting_for_clear_pnacl_cache_(false),
227 #if defined(OS_ANDROID)
228 waiting_for_clear_precache_history_(false),
229 #endif
230 waiting_for_clear_storage_partition_data_(false),
231 #if defined(ENABLE_WEBRTC)
232 waiting_for_clear_webrtc_logs_(false),
233 #endif
234 remove_mask_(0),
235 remove_origin_(GURL()),
236 origin_type_mask_(0),
237 storage_partition_for_testing_(NULL) {
238 DCHECK(profile);
239 // crbug.com/140910: Many places were calling this with base::Time() as
240 // delete_end, even though they should've used base::Time::Max(). Work around
241 // it here. New code should use base::Time::Max().
242 DCHECK(delete_end_ != base::Time());
243 if (delete_end_ == base::Time())
244 delete_end_ = base::Time::Max();
247 BrowsingDataRemover::~BrowsingDataRemover() {
248 DCHECK(AllDone());
251 // Static.
252 void BrowsingDataRemover::set_removing(bool is_removing) {
253 DCHECK(is_removing_ != is_removing);
254 is_removing_ = is_removing;
257 void BrowsingDataRemover::Remove(int remove_mask, int origin_type_mask) {
258 RemoveImpl(remove_mask, GURL(), origin_type_mask);
261 void BrowsingDataRemover::RemoveImpl(int remove_mask,
262 const GURL& origin,
263 int origin_type_mask) {
264 DCHECK_CURRENTLY_ON(BrowserThread::UI);
265 set_removing(true);
266 remove_mask_ = remove_mask;
267 remove_origin_ = origin;
268 origin_type_mask_ = origin_type_mask;
270 PrefService* prefs = profile_->GetPrefs();
271 bool may_delete_history = prefs->GetBoolean(
272 prefs::kAllowDeletingBrowserHistory);
274 // All the UI entry points into the BrowsingDataRemover should be disabled,
275 // but this will fire if something was missed or added.
276 DCHECK(may_delete_history || (remove_mask & REMOVE_NOCHECKS) ||
277 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
279 if (origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
280 content::RecordAction(
281 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
283 if (origin_type_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
284 content::RecordAction(
285 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
287 if (origin_type_mask_ & BrowsingDataHelper::EXTENSION) {
288 content::RecordAction(
289 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
291 // If this fires, we added a new BrowsingDataHelper::OriginTypeMask without
292 // updating the user metrics above.
293 static_assert(
294 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
295 BrowsingDataHelper::PROTECTED_WEB |
296 BrowsingDataHelper::EXTENSION),
297 "OriginTypeMask has been updated without updating user metrics");
299 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
300 history::HistoryService* history_service =
301 HistoryServiceFactory::GetForProfile(
302 profile_, ServiceAccessType::EXPLICIT_ACCESS);
303 if (history_service) {
304 std::set<GURL> restrict_urls;
305 if (!remove_origin_.is_empty())
306 restrict_urls.insert(remove_origin_);
307 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
308 waiting_for_clear_history_ = true;
310 history_service->ExpireLocalAndRemoteHistoryBetween(
311 WebHistoryServiceFactory::GetForProfile(profile_),
312 restrict_urls, delete_begin_, delete_end_,
313 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
314 base::Unretained(this)),
315 &history_task_tracker_);
317 #if defined(ENABLE_EXTENSIONS)
318 // The extension activity contains details of which websites extensions
319 // were active on. It therefore indirectly stores details of websites a
320 // user has visited so best clean from here as well.
321 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
322 #endif
325 #if defined(ENABLE_EXTENSIONS)
326 // Clear launch times as they are a form of history.
327 extensions::ExtensionPrefs* extension_prefs =
328 extensions::ExtensionPrefs::Get(profile_);
329 extension_prefs->ClearLastLaunchTimes();
330 #endif
332 // The power consumption history by origin contains details of websites
333 // that were visited.
334 power::OriginPowerMap* origin_power_map =
335 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
336 if (origin_power_map)
337 origin_power_map->ClearOriginMap();
339 // Need to clear the host cache and accumulated speculative data, as it also
340 // reveals some history: we have no mechanism to track when these items were
341 // created, so we'll clear them all. Better safe than sorry.
342 if (g_browser_process->io_thread()) {
343 waiting_for_clear_hostname_resolution_cache_ = true;
344 BrowserThread::PostTask(
345 BrowserThread::IO, FROM_HERE,
346 base::Bind(
347 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
348 base::Unretained(this),
349 g_browser_process->io_thread()));
351 if (profile_->GetNetworkPredictor()) {
352 waiting_for_clear_network_predictor_ = true;
353 BrowserThread::PostTask(
354 BrowserThread::IO, FROM_HERE,
355 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
356 base::Unretained(this),
357 profile_->GetNetworkPredictor()));
360 // As part of history deletion we also delete the auto-generated keywords.
361 TemplateURLService* keywords_model =
362 TemplateURLServiceFactory::GetForProfile(profile_);
363 if (keywords_model && !keywords_model->loaded()) {
364 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
365 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
366 base::Unretained(this)));
367 keywords_model->Load();
368 waiting_for_clear_keyword_data_ = true;
369 } else if (keywords_model) {
370 keywords_model->RemoveAutoGeneratedForOriginBetween(remove_origin_,
371 delete_begin_, delete_end_);
374 // The PrerenderManager keeps history of prerendered pages, so clear that.
375 // It also may have a prerendered page. If so, the page could be
376 // considered to have a small amount of historical information, so delete
377 // it, too.
378 prerender::PrerenderManager* prerender_manager =
379 prerender::PrerenderManagerFactory::GetForProfile(profile_);
380 if (prerender_manager) {
381 prerender_manager->ClearData(
382 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
383 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
386 // If the caller is removing history for all hosts, then clear ancillary
387 // historical information.
388 if (remove_origin_.is_empty()) {
389 // We also delete the list of recently closed tabs. Since these expire,
390 // they can't be more than a day old, so we can simply clear them all.
391 TabRestoreService* tab_service =
392 TabRestoreServiceFactory::GetForProfile(profile_);
393 if (tab_service) {
394 tab_service->ClearEntries();
395 tab_service->DeleteLastSession();
398 #if defined(ENABLE_SESSION_SERVICE)
399 // We also delete the last session when we delete the history.
400 SessionService* session_service =
401 SessionServiceFactory::GetForProfile(profile_);
402 if (session_service)
403 session_service->DeleteLastSession();
404 #endif
407 // The saved Autofill profiles and credit cards can include the origin from
408 // which these profiles and credit cards were learned. These are a form of
409 // history, so clear them as well.
410 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
411 WebDataServiceFactory::GetAutofillWebDataForProfile(
412 profile_, ServiceAccessType::EXPLICIT_ACCESS);
413 if (web_data_service.get()) {
414 waiting_for_clear_autofill_origin_urls_ = true;
415 web_data_service->RemoveOriginURLsModifiedBetween(
416 delete_begin_, delete_end_);
417 // The above calls are done on the UI thread but do their work on the DB
418 // thread. So wait for it.
419 BrowserThread::PostTaskAndReply(
420 BrowserThread::DB, FROM_HERE,
421 base::Bind(&base::DoNothing),
422 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
423 base::Unretained(this)));
425 autofill::PersonalDataManager* data_manager =
426 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
427 if (data_manager)
428 data_manager->Refresh();
431 #if defined(ENABLE_WEBRTC)
432 waiting_for_clear_webrtc_logs_ = true;
433 BrowserThread::PostTaskAndReply(
434 BrowserThread::FILE,
435 FROM_HERE,
436 base::Bind(
437 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
438 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
439 delete_begin_),
440 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
441 base::Unretained(this)));
442 #endif
444 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
445 // include origins that the user has visited, so it must be cleared.
446 if (profile_->GetSSLHostStateDelegate())
447 profile_->GetSSLHostStateDelegate()->Clear();
449 #if defined(OS_ANDROID)
450 precache::PrecacheManager* precache_manager =
451 precache::PrecacheManagerFactory::GetForBrowserContext(profile_);
452 // |precache_manager| could be NULL if the profile is off the record.
453 if (!precache_manager) {
454 waiting_for_clear_precache_history_ = true;
455 precache_manager->ClearHistory();
456 // The above calls are done on the UI thread but do their work on the DB
457 // thread. So wait for it.
458 BrowserThread::PostTaskAndReply(
459 BrowserThread::DB, FROM_HERE,
460 base::Bind(&base::DoNothing),
461 base::Bind(&BrowsingDataRemover::OnClearedPrecacheHistory,
462 base::Unretained(this)));
464 #endif
467 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
468 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
469 content::DownloadManager* download_manager =
470 BrowserContext::GetDownloadManager(profile_);
471 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
472 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
473 download_manager);
474 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
477 uint32 storage_partition_remove_mask = 0;
479 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
480 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
481 // don't accidentally remove the cookies that are associated with the
482 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
483 // between UNPROTECTED_WEB and PROTECTED_WEB.
484 if (remove_mask & REMOVE_COOKIES &&
485 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
486 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
488 storage_partition_remove_mask |=
489 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
491 #if defined(SAFE_BROWSING_SERVICE)
492 // Clear the safebrowsing cookies only if time period is for "all time". It
493 // doesn't make sense to apply the time period of deleting in the last X
494 // hours/days to the safebrowsing cookies since they aren't the result of
495 // any user action.
496 if (delete_begin_ == base::Time()) {
497 SafeBrowsingService* sb_service =
498 g_browser_process->safe_browsing_service();
499 if (sb_service) {
500 net::URLRequestContextGetter* sb_context =
501 sb_service->url_request_context();
502 ++waiting_for_clear_cookies_count_;
503 BrowserThread::PostTask(
504 BrowserThread::IO, FROM_HERE,
505 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
506 base::Unretained(this), base::Unretained(sb_context)));
509 #endif
510 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
512 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
513 // flag for all credentials in the password store.
516 // Channel IDs are not separated for protected and unprotected web
517 // origins. We check the origin_type_mask_ to prevent unintended deletion.
518 if (remove_mask & REMOVE_CHANNEL_IDS &&
519 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
520 content::RecordAction(
521 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
522 // Since we are running on the UI thread don't call GetURLRequestContext().
523 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
524 if (rq_context) {
525 waiting_for_clear_channel_ids_ = true;
526 BrowserThread::PostTask(
527 BrowserThread::IO, FROM_HERE,
528 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
529 base::Unretained(this), base::Unretained(rq_context)));
533 if (remove_mask & REMOVE_LOCAL_STORAGE) {
534 storage_partition_remove_mask |=
535 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
538 if (remove_mask & REMOVE_INDEXEDDB) {
539 storage_partition_remove_mask |=
540 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
542 if (remove_mask & REMOVE_WEBSQL) {
543 storage_partition_remove_mask |=
544 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
546 if (remove_mask & REMOVE_APPCACHE) {
547 storage_partition_remove_mask |=
548 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
550 if (remove_mask & REMOVE_SERVICE_WORKERS) {
551 storage_partition_remove_mask |=
552 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
554 if (remove_mask & REMOVE_FILE_SYSTEMS) {
555 storage_partition_remove_mask |=
556 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
559 #if defined(ENABLE_PLUGINS)
560 // Plugin is data not separated for protected and unprotected web origins. We
561 // check the origin_type_mask_ to prevent unintended deletion.
562 if (remove_mask & REMOVE_PLUGIN_DATA &&
563 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
564 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
566 waiting_for_clear_plugin_data_ = true;
567 if (!plugin_data_remover_.get())
568 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
569 base::WaitableEvent* event =
570 plugin_data_remover_->StartRemoving(delete_begin_);
572 base::WaitableEventWatcher::EventCallback watcher_callback =
573 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
574 base::Unretained(this));
575 watcher_.StartWatching(event, watcher_callback);
577 #endif
579 if (remove_mask & REMOVE_SITE_USAGE_DATA || remove_mask & REMOVE_HISTORY) {
580 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
581 CONTENT_SETTINGS_TYPE_APP_BANNER);
582 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
583 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT);
586 if (remove_mask & REMOVE_PASSWORDS) {
587 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
588 password_manager::PasswordStore* password_store =
589 PasswordStoreFactory::GetForProfile(
590 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
592 if (password_store) {
593 waiting_for_clear_passwords_ = true;
594 password_store->RemoveLoginsCreatedBetween(
595 delete_begin_, delete_end_,
596 base::Bind(&BrowsingDataRemover::OnClearedPasswords,
597 base::Unretained(this)));
601 if (remove_mask & REMOVE_FORM_DATA) {
602 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
603 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
604 WebDataServiceFactory::GetAutofillWebDataForProfile(
605 profile_, ServiceAccessType::EXPLICIT_ACCESS);
607 if (web_data_service.get()) {
608 waiting_for_clear_form_ = true;
609 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
610 delete_end_);
611 web_data_service->RemoveAutofillDataModifiedBetween(
612 delete_begin_, delete_end_);
613 // The above calls are done on the UI thread but do their work on the DB
614 // thread. So wait for it.
615 BrowserThread::PostTaskAndReply(
616 BrowserThread::DB, FROM_HERE,
617 base::Bind(&base::DoNothing),
618 base::Bind(&BrowsingDataRemover::OnClearedFormData,
619 base::Unretained(this)));
621 autofill::PersonalDataManager* data_manager =
622 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
623 if (data_manager)
624 data_manager->Refresh();
628 if (remove_mask & REMOVE_CACHE) {
629 // Tell the renderers to clear their cache.
630 web_cache::WebCacheManager::GetInstance()->ClearCache();
632 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
634 waiting_for_clear_cache_ = true;
635 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
636 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
637 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
638 delete_end_)
639 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
640 base::Unretained(this)));
642 #if !defined(DISABLE_NACL)
643 waiting_for_clear_nacl_cache_ = true;
645 BrowserThread::PostTask(
646 BrowserThread::IO, FROM_HERE,
647 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
648 base::Unretained(this)));
650 waiting_for_clear_pnacl_cache_ = true;
651 BrowserThread::PostTask(
652 BrowserThread::IO, FROM_HERE,
653 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
654 base::Unretained(this), delete_begin_, delete_end_));
655 #endif
657 // The PrerenderManager may have a page actively being prerendered, which
658 // is essentially a preemptively cached page.
659 prerender::PrerenderManager* prerender_manager =
660 prerender::PrerenderManagerFactory::GetForProfile(profile_);
661 if (prerender_manager) {
662 prerender_manager->ClearData(
663 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
666 // Tell the shader disk cache to clear.
667 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
668 storage_partition_remove_mask |=
669 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
671 storage_partition_remove_mask |=
672 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
674 #if defined(ENABLE_EXTENSIONS)
675 // Clear the ephemeral apps cache. This is NULL while testing. OTR Profile
676 // has neither apps nor an ExtensionService, so ClearCachedApps fails.
677 EphemeralAppService* ephemeral_app_service =
678 EphemeralAppService::Get(profile_);
679 if (ephemeral_app_service && !profile_->IsOffTheRecord())
680 ephemeral_app_service->ClearCachedApps();
681 #endif
684 if (remove_mask & REMOVE_WEBRTC_IDENTITY) {
685 storage_partition_remove_mask |=
686 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
689 if (storage_partition_remove_mask) {
690 waiting_for_clear_storage_partition_data_ = true;
692 content::StoragePartition* storage_partition;
693 if (storage_partition_for_testing_)
694 storage_partition = storage_partition_for_testing_;
695 else
696 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
698 uint32 quota_storage_remove_mask =
699 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
701 if (delete_begin_ == base::Time() ||
702 origin_type_mask_ &
703 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
704 // If we're deleting since the beginning of time, or we're removing
705 // protected origins, then remove persistent quota data.
706 quota_storage_remove_mask |=
707 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
710 storage_partition->ClearData(
711 storage_partition_remove_mask,
712 quota_storage_remove_mask,
713 remove_origin_,
714 base::Bind(&DoesOriginMatchMask, origin_type_mask_),
715 delete_begin_,
716 delete_end_,
717 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
718 base::Unretained(this)));
721 #if defined(ENABLE_PLUGINS)
722 if (remove_mask & REMOVE_CONTENT_LICENSES) {
723 content::RecordAction(
724 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
726 waiting_for_clear_content_licenses_ = true;
727 if (!pepper_flash_settings_manager_.get()) {
728 pepper_flash_settings_manager_.reset(
729 new PepperFlashSettingsManager(this, profile_));
731 deauthorize_content_licenses_request_id_ =
732 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
733 #if defined(OS_CHROMEOS)
734 // On Chrome OS, also delete any content protection platform keys.
735 const user_manager::User* user =
736 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
737 if (!user) {
738 LOG(WARNING) << "Failed to find user for current profile.";
739 } else {
740 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
741 TpmAttestationDeleteKeys(
742 chromeos::attestation::KEY_USER,
743 user->email(),
744 chromeos::attestation::kContentProtectionKeyPrefix,
745 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
746 base::Unretained(this)));
747 waiting_for_clear_platform_keys_ = true;
749 #endif
751 #endif
753 // Remove omnibox zero-suggest cache results.
754 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
755 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string());
757 // Always wipe accumulated network related data (TransportSecurityState and
758 // HttpServerPropertiesManager data).
759 waiting_for_clear_networking_history_ = true;
760 profile_->ClearNetworkingHistorySince(
761 delete_begin_,
762 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
763 base::Unretained(this)));
765 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
766 domain_reliability::DomainReliabilityService* service =
767 domain_reliability::DomainReliabilityServiceFactory::
768 GetForBrowserContext(profile_);
769 if (service) {
770 domain_reliability::DomainReliabilityClearMode mode;
771 if (remove_mask & REMOVE_COOKIES)
772 mode = domain_reliability::CLEAR_CONTEXTS;
773 else
774 mode = domain_reliability::CLEAR_BEACONS;
776 waiting_for_clear_domain_reliability_monitor_ = true;
777 service->ClearBrowsingData(
778 mode,
779 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
780 base::Unretained(this)));
785 void BrowsingDataRemover::AddObserver(Observer* observer) {
786 observer_list_.AddObserver(observer);
789 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
790 observer_list_.RemoveObserver(observer);
793 void BrowsingDataRemover::OnHistoryDeletionDone() {
794 waiting_for_clear_history_ = false;
795 NotifyAndDeleteIfDone();
798 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
799 content::StoragePartition* storage_partition) {
800 storage_partition_for_testing_ = storage_partition;
803 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
804 TimePeriod time_period) {
805 base::TimeDelta diff;
806 base::Time delete_begin_time = base::Time::Now();
807 switch (time_period) {
808 case LAST_HOUR:
809 diff = base::TimeDelta::FromHours(1);
810 break;
811 case LAST_DAY:
812 diff = base::TimeDelta::FromHours(24);
813 break;
814 case LAST_WEEK:
815 diff = base::TimeDelta::FromHours(7*24);
816 break;
817 case FOUR_WEEKS:
818 diff = base::TimeDelta::FromHours(4*7*24);
819 break;
820 case EVERYTHING:
821 delete_begin_time = base::Time();
822 break;
823 default:
824 NOTREACHED() << L"Missing item";
825 break;
827 return delete_begin_time - diff;
830 bool BrowsingDataRemover::AllDone() {
831 return !waiting_for_clear_autofill_origin_urls_ &&
832 !waiting_for_clear_cache_ &&
833 !waiting_for_clear_content_licenses_ &&
834 !waiting_for_clear_channel_ids_ &&
835 !waiting_for_clear_cookies_count_ &&
836 !waiting_for_clear_domain_reliability_monitor_ &&
837 !waiting_for_clear_form_ &&
838 !waiting_for_clear_history_ &&
839 !waiting_for_clear_hostname_resolution_cache_ &&
840 !waiting_for_clear_keyword_data_ &&
841 !waiting_for_clear_nacl_cache_ &&
842 !waiting_for_clear_network_predictor_ &&
843 !waiting_for_clear_networking_history_ &&
844 !waiting_for_clear_passwords_ &&
845 !waiting_for_clear_platform_keys_ &&
846 !waiting_for_clear_plugin_data_ &&
847 !waiting_for_clear_pnacl_cache_ &&
848 #if defined(OS_ANDROID)
849 !waiting_for_clear_precache_history_ &&
850 #endif
851 #if defined(ENABLE_WEBRTC)
852 !waiting_for_clear_webrtc_logs_ &&
853 #endif
854 !waiting_for_clear_storage_partition_data_;
857 void BrowsingDataRemover::OnKeywordsLoaded() {
858 // Deletes the entries from the model, and if we're not waiting on anything
859 // else notifies observers and deletes this BrowsingDataRemover.
860 TemplateURLService* model =
861 TemplateURLServiceFactory::GetForProfile(profile_);
862 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
863 waiting_for_clear_keyword_data_ = false;
864 template_url_sub_.reset();
865 NotifyAndDeleteIfDone();
868 void BrowsingDataRemover::NotifyAndDelete() {
869 set_removing(false);
871 // Notify observers.
872 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
873 origin_type_mask_);
875 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
877 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
879 // History requests aren't happy if you delete yourself from the callback.
880 // As such, we do a delete later.
881 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
884 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
885 // TODO(brettw) http://crbug.com/305259: This should also observe session
886 // clearing (what about other things such as passwords, etc.?) and wait for
887 // them to complete before continuing.
889 if (!AllDone())
890 return;
892 if (completion_inhibitor_) {
893 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
894 this,
895 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
896 base::Unretained(this)));
897 } else {
898 NotifyAndDelete();
902 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
903 DCHECK_CURRENTLY_ON(BrowserThread::UI);
904 waiting_for_clear_hostname_resolution_cache_ = false;
905 NotifyAndDeleteIfDone();
908 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
909 IOThread* io_thread) {
910 DCHECK_CURRENTLY_ON(BrowserThread::IO);
912 io_thread->ClearHostCache();
914 // Notify the UI thread that we are done.
915 BrowserThread::PostTask(
916 BrowserThread::UI,
917 FROM_HERE,
918 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
919 base::Unretained(this)));
922 void BrowsingDataRemover::OnClearedNetworkPredictor() {
923 DCHECK_CURRENTLY_ON(BrowserThread::UI);
924 waiting_for_clear_network_predictor_ = false;
925 NotifyAndDeleteIfDone();
928 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
929 chrome_browser_net::Predictor* predictor) {
930 DCHECK_CURRENTLY_ON(BrowserThread::IO);
931 DCHECK(predictor);
933 predictor->DiscardInitialNavigationHistory();
934 predictor->DiscardAllResults();
936 // Notify the UI thread that we are done.
937 BrowserThread::PostTask(
938 BrowserThread::UI,
939 FROM_HERE,
940 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
941 base::Unretained(this)));
944 void BrowsingDataRemover::OnClearedNetworkingHistory() {
945 DCHECK_CURRENTLY_ON(BrowserThread::UI);
946 waiting_for_clear_networking_history_ = false;
947 NotifyAndDeleteIfDone();
950 void BrowsingDataRemover::ClearedCache() {
951 waiting_for_clear_cache_ = false;
953 NotifyAndDeleteIfDone();
956 #if !defined(DISABLE_NACL)
957 void BrowsingDataRemover::ClearedNaClCache() {
958 // This function should be called on the UI thread.
959 DCHECK_CURRENTLY_ON(BrowserThread::UI);
961 waiting_for_clear_nacl_cache_ = false;
963 NotifyAndDeleteIfDone();
966 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
967 // This function should be called on the IO thread.
968 DCHECK_CURRENTLY_ON(BrowserThread::IO);
970 // Notify the UI thread that we are done.
971 BrowserThread::PostTask(
972 BrowserThread::UI, FROM_HERE,
973 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
974 base::Unretained(this)));
977 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
978 DCHECK_CURRENTLY_ON(BrowserThread::IO);
980 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
981 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
982 base::Unretained(this)));
985 void BrowsingDataRemover::ClearedPnaclCache() {
986 // This function should be called on the UI thread.
987 DCHECK_CURRENTLY_ON(BrowserThread::UI);
989 waiting_for_clear_pnacl_cache_ = false;
991 NotifyAndDeleteIfDone();
994 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
995 // This function should be called on the IO thread.
996 DCHECK_CURRENTLY_ON(BrowserThread::IO);
998 // Notify the UI thread that we are done.
999 BrowserThread::PostTask(
1000 BrowserThread::UI, FROM_HERE,
1001 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1002 base::Unretained(this)));
1005 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1006 base::Time end) {
1007 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1009 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1010 begin, end,
1011 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1012 base::Unretained(this)));
1014 #endif
1016 void BrowsingDataRemover::OnWaitableEventSignaled(
1017 base::WaitableEvent* waitable_event) {
1018 waiting_for_clear_plugin_data_ = false;
1019 NotifyAndDeleteIfDone();
1022 #if defined(ENABLE_PLUGINS)
1023 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1024 uint32 request_id,
1025 bool /* success */) {
1026 DCHECK(waiting_for_clear_content_licenses_);
1027 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1029 waiting_for_clear_content_licenses_ = false;
1030 NotifyAndDeleteIfDone();
1032 #endif
1034 #if defined(OS_CHROMEOS)
1035 void BrowsingDataRemover::OnClearPlatformKeys(
1036 chromeos::DBusMethodCallStatus call_status,
1037 bool result) {
1038 DCHECK(waiting_for_clear_platform_keys_);
1039 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1040 LOG(ERROR) << "Failed to clear platform keys.";
1042 waiting_for_clear_platform_keys_ = false;
1043 NotifyAndDeleteIfDone();
1045 #endif
1048 void BrowsingDataRemover::OnClearedPasswords() {
1049 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1050 waiting_for_clear_passwords_ = false;
1051 NotifyAndDeleteIfDone();
1054 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1055 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1056 BrowserThread::PostTask(
1057 BrowserThread::UI, FROM_HERE,
1058 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1059 base::Unretained(this), num_deleted));
1060 return;
1063 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1064 --waiting_for_clear_cookies_count_;
1065 NotifyAndDeleteIfDone();
1068 void BrowsingDataRemover::ClearCookiesOnIOThread(
1069 net::URLRequestContextGetter* rq_context) {
1070 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1071 net::CookieStore* cookie_store = rq_context->
1072 GetURLRequestContext()->cookie_store();
1073 cookie_store->DeleteAllCreatedBetweenAsync(
1074 delete_begin_, delete_end_,
1075 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1076 base::Unretained(this)));
1079 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1080 net::URLRequestContextGetter* rq_context) {
1081 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1082 net::ChannelIDService* channel_id_service =
1083 rq_context->GetURLRequestContext()->channel_id_service();
1084 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1085 delete_begin_, delete_end_,
1086 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1087 base::Unretained(this), base::Unretained(rq_context)));
1090 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1091 net::URLRequestContextGetter* rq_context) {
1092 // Need to close open SSL connections which may be using the channel ids we
1093 // are deleting.
1094 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1095 // service/store have observers that can notify relevant things directly.
1096 rq_context->GetURLRequestContext()->ssl_config_service()->
1097 NotifySSLConfigChange();
1098 BrowserThread::PostTask(
1099 BrowserThread::UI, FROM_HERE,
1100 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1101 base::Unretained(this)));
1104 void BrowsingDataRemover::OnClearedChannelIDs() {
1105 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1106 waiting_for_clear_channel_ids_ = false;
1107 NotifyAndDeleteIfDone();
1110 void BrowsingDataRemover::OnClearedFormData() {
1111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1112 waiting_for_clear_form_ = false;
1113 NotifyAndDeleteIfDone();
1116 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1117 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1118 waiting_for_clear_autofill_origin_urls_ = false;
1119 NotifyAndDeleteIfDone();
1122 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1123 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1124 waiting_for_clear_storage_partition_data_ = false;
1125 NotifyAndDeleteIfDone();
1128 #if defined(ENABLE_WEBRTC)
1129 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1130 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1131 waiting_for_clear_webrtc_logs_ = false;
1132 NotifyAndDeleteIfDone();
1134 #endif
1136 #if defined(OS_ANDROID)
1137 void BrowsingDataRemover::OnClearedPrecacheHistory() {
1138 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1139 waiting_for_clear_precache_history_ = false;
1140 NotifyAndDeleteIfDone();
1142 #endif
1144 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1145 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1146 waiting_for_clear_domain_reliability_monitor_ = false;
1147 NotifyAndDeleteIfDone();
1150 // static
1151 BrowsingDataRemover::CallbackSubscription
1152 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1153 const BrowsingDataRemover::Callback& callback) {
1154 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);