Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blob079b41a32547e314532a0d10a06be1673d50ad68
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/predictors/logged_in_predictor_table.h"
30 #include "chrome/browser/predictors/predictor_database.h"
31 #include "chrome/browser/predictors/predictor_database_factory.h"
32 #include "chrome/browser/prerender/prerender_manager.h"
33 #include "chrome/browser/prerender/prerender_manager_factory.h"
34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
36 #include "chrome/browser/search_engines/template_url_service_factory.h"
37 #include "chrome/browser/sessions/session_service.h"
38 #include "chrome/browser/sessions/session_service_factory.h"
39 #include "chrome/browser/sessions/tab_restore_service.h"
40 #include "chrome/browser/sessions/tab_restore_service_factory.h"
41 #include "chrome/browser/webdata/web_data_service_factory.h"
42 #include "chrome/common/pref_names.h"
43 #include "chrome/common/url_constants.h"
44 #include "components/autofill/core/browser/personal_data_manager.h"
45 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
46 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
47 #include "components/content_settings/core/browser/host_content_settings_map.h"
48 #include "components/domain_reliability/service.h"
49 #include "components/history/core/browser/history_service.h"
50 #include "components/nacl/browser/nacl_browser.h"
51 #include "components/nacl/browser/pnacl_host.h"
52 #include "components/password_manager/core/browser/password_store.h"
53 #include "components/power/origin_power_map.h"
54 #include "components/power/origin_power_map_factory.h"
55 #include "components/search_engines/template_url_service.h"
56 #include "components/web_cache/browser/web_cache_manager.h"
57 #include "content/public/browser/browser_thread.h"
58 #include "content/public/browser/download_manager.h"
59 #include "content/public/browser/notification_service.h"
60 #include "content/public/browser/plugin_data_remover.h"
61 #include "content/public/browser/ssl_host_state_delegate.h"
62 #include "content/public/browser/storage_partition.h"
63 #include "content/public/browser/user_metrics.h"
64 #include "net/base/net_errors.h"
65 #include "net/cookies/cookie_store.h"
66 #include "net/http/transport_security_state.h"
67 #include "net/ssl/channel_id_service.h"
68 #include "net/ssl/channel_id_store.h"
69 #include "net/url_request/url_request_context.h"
70 #include "net/url_request/url_request_context_getter.h"
71 #include "storage/browser/quota/special_storage_policy.h"
73 #if defined(OS_CHROMEOS)
74 #include "chrome/browser/chromeos/profiles/profile_helper.h"
75 #include "chromeos/attestation/attestation_constants.h"
76 #include "chromeos/dbus/cryptohome_client.h"
77 #include "chromeos/dbus/dbus_thread_manager.h"
78 #include "components/user_manager/user.h"
79 #endif
81 #if defined(ENABLE_EXTENSIONS)
82 #include "chrome/browser/apps/ephemeral_app_service.h"
83 #include "chrome/browser/extensions/activity_log/activity_log.h"
84 #include "chrome/browser/extensions/extension_service.h"
85 #include "chrome/browser/extensions/extension_special_storage_policy.h"
86 #include "extensions/browser/extension_prefs.h"
87 #endif
89 #if defined(ENABLE_WEBRTC)
90 #include "chrome/browser/media/webrtc_log_list.h"
91 #include "chrome/browser/media/webrtc_log_util.h"
92 #endif
94 using base::UserMetricsAction;
95 using content::BrowserContext;
96 using content::BrowserThread;
97 using content::DOMStorageContext;
99 namespace {
101 using CallbackList =
102 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
104 // Contains all registered callbacks for browsing data removed notifications.
105 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
107 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
108 // the first time so that it always returns a valid object.
109 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
110 if (!g_on_browsing_data_removed_callbacks)
111 g_on_browsing_data_removed_callbacks = new CallbackList();
112 return g_on_browsing_data_removed_callbacks;
115 } // namespace
117 bool BrowsingDataRemover::is_removing_ = false;
119 BrowsingDataRemover::CompletionInhibitor*
120 BrowsingDataRemover::completion_inhibitor_ = NULL;
122 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
123 // Static.
124 bool DoesOriginMatchMask(
125 int origin_set_mask,
126 const GURL& origin,
127 storage::SpecialStoragePolicy* special_storage_policy) {
128 return BrowsingDataHelper::DoesOriginMatchMask(
129 origin, origin_set_mask, special_storage_policy);
132 BrowsingDataRemover::NotificationDetails::NotificationDetails()
133 : removal_begin(base::Time()),
134 removal_mask(-1),
135 origin_set_mask(-1) {
138 BrowsingDataRemover::NotificationDetails::NotificationDetails(
139 const BrowsingDataRemover::NotificationDetails& details)
140 : removal_begin(details.removal_begin),
141 removal_mask(details.removal_mask),
142 origin_set_mask(details.origin_set_mask) {
145 BrowsingDataRemover::NotificationDetails::NotificationDetails(
146 base::Time removal_begin,
147 int removal_mask,
148 int origin_set_mask)
149 : removal_begin(removal_begin),
150 removal_mask(removal_mask),
151 origin_set_mask(origin_set_mask) {
154 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
156 // Static.
157 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
158 Profile* profile) {
159 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
162 // Static.
163 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
164 base::Time start, base::Time end) {
165 return new BrowsingDataRemover(profile, start, end);
168 // Static.
169 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
170 TimePeriod period) {
171 switch (period) {
172 case LAST_HOUR:
173 content::RecordAction(
174 UserMetricsAction("ClearBrowsingData_LastHour"));
175 break;
176 case LAST_DAY:
177 content::RecordAction(
178 UserMetricsAction("ClearBrowsingData_LastDay"));
179 break;
180 case LAST_WEEK:
181 content::RecordAction(
182 UserMetricsAction("ClearBrowsingData_LastWeek"));
183 break;
184 case FOUR_WEEKS:
185 content::RecordAction(
186 UserMetricsAction("ClearBrowsingData_LastMonth"));
187 break;
188 case EVERYTHING:
189 content::RecordAction(
190 UserMetricsAction("ClearBrowsingData_Everything"));
191 break;
193 return new BrowsingDataRemover(profile,
194 BrowsingDataRemover::CalculateBeginDeleteTime(period),
195 base::Time::Max());
198 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
199 base::Time delete_begin,
200 base::Time delete_end)
201 : profile_(profile),
202 delete_begin_(delete_begin),
203 delete_end_(delete_end),
204 main_context_getter_(profile->GetRequestContext()),
205 media_context_getter_(profile->GetMediaRequestContext()),
206 deauthorize_content_licenses_request_id_(0),
207 waiting_for_clear_autofill_origin_urls_(false),
208 waiting_for_clear_cache_(false),
209 waiting_for_clear_channel_ids_(false),
210 waiting_for_clear_content_licenses_(false),
211 waiting_for_clear_cookies_count_(0),
212 waiting_for_clear_domain_reliability_monitor_(false),
213 waiting_for_clear_form_(false),
214 waiting_for_clear_history_(false),
215 waiting_for_clear_hostname_resolution_cache_(false),
216 waiting_for_clear_keyword_data_(false),
217 waiting_for_clear_logged_in_predictor_(false),
218 waiting_for_clear_nacl_cache_(false),
219 waiting_for_clear_network_predictor_(false),
220 waiting_for_clear_networking_history_(false),
221 waiting_for_clear_platform_keys_(false),
222 waiting_for_clear_plugin_data_(false),
223 waiting_for_clear_pnacl_cache_(false),
224 waiting_for_clear_storage_partition_data_(false),
225 #if defined(ENABLE_WEBRTC)
226 waiting_for_clear_webrtc_logs_(false),
227 #endif
228 remove_mask_(0),
229 remove_origin_(GURL()),
230 origin_set_mask_(0),
231 storage_partition_for_testing_(NULL) {
232 DCHECK(profile);
233 // crbug.com/140910: Many places were calling this with base::Time() as
234 // delete_end, even though they should've used base::Time::Max(). Work around
235 // it here. New code should use base::Time::Max().
236 DCHECK(delete_end_ != base::Time());
237 if (delete_end_ == base::Time())
238 delete_end_ = base::Time::Max();
241 BrowsingDataRemover::~BrowsingDataRemover() {
242 DCHECK(AllDone());
245 // Static.
246 void BrowsingDataRemover::set_removing(bool is_removing) {
247 DCHECK(is_removing_ != is_removing);
248 is_removing_ = is_removing;
251 void BrowsingDataRemover::Remove(int remove_mask, int origin_set_mask) {
252 RemoveImpl(remove_mask, GURL(), origin_set_mask);
255 void BrowsingDataRemover::RemoveImpl(int remove_mask,
256 const GURL& origin,
257 int origin_set_mask) {
258 DCHECK_CURRENTLY_ON(BrowserThread::UI);
259 set_removing(true);
260 remove_mask_ = remove_mask;
261 remove_origin_ = origin;
262 origin_set_mask_ = origin_set_mask;
264 PrefService* prefs = profile_->GetPrefs();
265 bool may_delete_history = prefs->GetBoolean(
266 prefs::kAllowDeletingBrowserHistory);
268 // All the UI entry points into the BrowsingDataRemover should be disabled,
269 // but this will fire if something was missed or added.
270 DCHECK(may_delete_history ||
271 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
273 if (origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
274 content::RecordAction(
275 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
277 if (origin_set_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
278 content::RecordAction(
279 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
281 if (origin_set_mask_ & BrowsingDataHelper::EXTENSION) {
282 content::RecordAction(
283 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
285 // If this fires, we added a new BrowsingDataHelper::OriginSetMask without
286 // updating the user metrics above.
287 static_assert(
288 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
289 BrowsingDataHelper::PROTECTED_WEB |
290 BrowsingDataHelper::EXTENSION),
291 "OriginSetMask has been updated without updating user metrics");
293 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
294 history::HistoryService* history_service =
295 HistoryServiceFactory::GetForProfile(
296 profile_, ServiceAccessType::EXPLICIT_ACCESS);
297 if (history_service) {
298 std::set<GURL> restrict_urls;
299 if (!remove_origin_.is_empty())
300 restrict_urls.insert(remove_origin_);
301 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
302 waiting_for_clear_history_ = true;
304 history_service->ExpireLocalAndRemoteHistoryBetween(
305 WebHistoryServiceFactory::GetForProfile(profile_),
306 restrict_urls, delete_begin_, delete_end_,
307 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
308 base::Unretained(this)),
309 &history_task_tracker_);
311 #if defined(ENABLE_EXTENSIONS)
312 // The extension activity contains details of which websites extensions
313 // were active on. It therefore indirectly stores details of websites a
314 // user has visited so best clean from here as well.
315 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
316 #endif
319 #if defined(ENABLE_EXTENSIONS)
320 // Clear launch times as they are a form of history.
321 extensions::ExtensionPrefs* extension_prefs =
322 extensions::ExtensionPrefs::Get(profile_);
323 extension_prefs->ClearLastLaunchTimes();
324 #endif
326 // The power consumption history by origin contains details of websites
327 // that were visited.
328 power::OriginPowerMap* origin_power_map =
329 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
330 if (origin_power_map)
331 origin_power_map->ClearOriginMap();
333 // Need to clear the host cache and accumulated speculative data, as it also
334 // reveals some history: we have no mechanism to track when these items were
335 // created, so we'll clear them all. Better safe than sorry.
336 if (g_browser_process->io_thread()) {
337 waiting_for_clear_hostname_resolution_cache_ = true;
338 BrowserThread::PostTask(
339 BrowserThread::IO, FROM_HERE,
340 base::Bind(
341 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
342 base::Unretained(this),
343 g_browser_process->io_thread()));
345 if (profile_->GetNetworkPredictor()) {
346 waiting_for_clear_network_predictor_ = true;
347 BrowserThread::PostTask(
348 BrowserThread::IO, FROM_HERE,
349 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
350 base::Unretained(this),
351 profile_->GetNetworkPredictor()));
354 // As part of history deletion we also delete the auto-generated keywords.
355 TemplateURLService* keywords_model =
356 TemplateURLServiceFactory::GetForProfile(profile_);
357 if (keywords_model && !keywords_model->loaded()) {
358 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
359 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
360 base::Unretained(this)));
361 keywords_model->Load();
362 waiting_for_clear_keyword_data_ = true;
363 } else if (keywords_model) {
364 keywords_model->RemoveAutoGeneratedForOriginBetween(remove_origin_,
365 delete_begin_, delete_end_);
368 // The PrerenderManager keeps history of prerendered pages, so clear that.
369 // It also may have a prerendered page. If so, the page could be
370 // considered to have a small amount of historical information, so delete
371 // it, too.
372 prerender::PrerenderManager* prerender_manager =
373 prerender::PrerenderManagerFactory::GetForProfile(profile_);
374 if (prerender_manager) {
375 prerender_manager->ClearData(
376 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
377 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
380 // If the caller is removing history for all hosts, then clear ancillary
381 // historical information.
382 if (remove_origin_.is_empty()) {
383 // We also delete the list of recently closed tabs. Since these expire,
384 // they can't be more than a day old, so we can simply clear them all.
385 TabRestoreService* tab_service =
386 TabRestoreServiceFactory::GetForProfile(profile_);
387 if (tab_service) {
388 tab_service->ClearEntries();
389 tab_service->DeleteLastSession();
392 #if defined(ENABLE_SESSION_SERVICE)
393 // We also delete the last session when we delete the history.
394 SessionService* session_service =
395 SessionServiceFactory::GetForProfile(profile_);
396 if (session_service)
397 session_service->DeleteLastSession();
398 #endif
401 // The saved Autofill profiles and credit cards can include the origin from
402 // which these profiles and credit cards were learned. These are a form of
403 // history, so clear them as well.
404 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
405 WebDataServiceFactory::GetAutofillWebDataForProfile(
406 profile_, ServiceAccessType::EXPLICIT_ACCESS);
407 if (web_data_service.get()) {
408 waiting_for_clear_autofill_origin_urls_ = true;
409 web_data_service->RemoveOriginURLsModifiedBetween(
410 delete_begin_, delete_end_);
411 // The above calls are done on the UI thread but do their work on the DB
412 // thread. So wait for it.
413 BrowserThread::PostTaskAndReply(
414 BrowserThread::DB, FROM_HERE,
415 base::Bind(&base::DoNothing),
416 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
417 base::Unretained(this)));
419 autofill::PersonalDataManager* data_manager =
420 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
421 if (data_manager)
422 data_manager->Refresh();
425 #if defined(ENABLE_WEBRTC)
426 waiting_for_clear_webrtc_logs_ = true;
427 BrowserThread::PostTaskAndReply(
428 BrowserThread::FILE,
429 FROM_HERE,
430 base::Bind(
431 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
432 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
433 delete_begin_),
434 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
435 base::Unretained(this)));
436 #endif
438 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
439 // include origins that the user has visited, so it must be cleared.
440 if (profile_->GetSSLHostStateDelegate())
441 profile_->GetSSLHostStateDelegate()->Clear();
444 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
445 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
446 content::DownloadManager* download_manager =
447 BrowserContext::GetDownloadManager(profile_);
448 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
449 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
450 download_manager);
451 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
454 uint32 storage_partition_remove_mask = 0;
456 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
457 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
458 // don't accidentally remove the cookies that are associated with the
459 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
460 // between UNPROTECTED_WEB and PROTECTED_WEB.
461 if (remove_mask & REMOVE_COOKIES &&
462 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
463 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
465 storage_partition_remove_mask |=
466 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
468 // Also delete the LoggedIn Predictor, which tries to keep track of which
469 // sites a user is logged into.
470 ClearLoggedInPredictor();
472 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
473 // Clear the safebrowsing cookies only if time period is for "all time". It
474 // doesn't make sense to apply the time period of deleting in the last X
475 // hours/days to the safebrowsing cookies since they aren't the result of
476 // any user action.
477 if (delete_begin_ == base::Time()) {
478 SafeBrowsingService* sb_service =
479 g_browser_process->safe_browsing_service();
480 if (sb_service) {
481 net::URLRequestContextGetter* sb_context =
482 sb_service->url_request_context();
483 ++waiting_for_clear_cookies_count_;
484 BrowserThread::PostTask(
485 BrowserThread::IO, FROM_HERE,
486 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
487 base::Unretained(this), base::Unretained(sb_context)));
490 #endif
491 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
493 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
494 // flag for all credentials in the password store.
497 // Channel IDs are not separated for protected and unprotected web
498 // origins. We check the origin_set_mask_ to prevent unintended deletion.
499 if (remove_mask & REMOVE_CHANNEL_IDS &&
500 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
501 content::RecordAction(
502 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
503 // Since we are running on the UI thread don't call GetURLRequestContext().
504 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
505 if (rq_context) {
506 waiting_for_clear_channel_ids_ = true;
507 BrowserThread::PostTask(
508 BrowserThread::IO, FROM_HERE,
509 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
510 base::Unretained(this), base::Unretained(rq_context)));
514 if (remove_mask & REMOVE_LOCAL_STORAGE) {
515 storage_partition_remove_mask |=
516 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
519 if (remove_mask & REMOVE_INDEXEDDB) {
520 storage_partition_remove_mask |=
521 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
523 if (remove_mask & REMOVE_WEBSQL) {
524 storage_partition_remove_mask |=
525 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
527 if (remove_mask & REMOVE_APPCACHE) {
528 storage_partition_remove_mask |=
529 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
531 if (remove_mask & REMOVE_SERVICE_WORKERS) {
532 storage_partition_remove_mask |=
533 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
535 if (remove_mask & REMOVE_FILE_SYSTEMS) {
536 storage_partition_remove_mask |=
537 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
540 #if defined(ENABLE_PLUGINS)
541 // Plugin is data not separated for protected and unprotected web origins. We
542 // check the origin_set_mask_ to prevent unintended deletion.
543 if (remove_mask & REMOVE_PLUGIN_DATA &&
544 origin_set_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
545 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
547 waiting_for_clear_plugin_data_ = true;
548 if (!plugin_data_remover_.get())
549 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
550 base::WaitableEvent* event =
551 plugin_data_remover_->StartRemoving(delete_begin_);
553 base::WaitableEventWatcher::EventCallback watcher_callback =
554 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
555 base::Unretained(this));
556 watcher_.StartWatching(event, watcher_callback);
558 #endif
560 #if defined(OS_ANDROID)
561 if (remove_mask & REMOVE_APP_BANNER_DATA || remove_mask & REMOVE_HISTORY) {
562 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
563 CONTENT_SETTINGS_TYPE_APP_BANNER);
565 #endif
567 if (remove_mask & REMOVE_PASSWORDS) {
568 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
569 password_manager::PasswordStore* password_store =
570 PasswordStoreFactory::GetForProfile(
571 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
573 if (password_store)
574 password_store->RemoveLoginsCreatedBetween(delete_begin_, delete_end_);
577 if (remove_mask & REMOVE_FORM_DATA) {
578 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
579 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
580 WebDataServiceFactory::GetAutofillWebDataForProfile(
581 profile_, ServiceAccessType::EXPLICIT_ACCESS);
583 if (web_data_service.get()) {
584 waiting_for_clear_form_ = true;
585 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
586 delete_end_);
587 web_data_service->RemoveAutofillDataModifiedBetween(
588 delete_begin_, delete_end_);
589 // The above calls are done on the UI thread but do their work on the DB
590 // thread. So wait for it.
591 BrowserThread::PostTaskAndReply(
592 BrowserThread::DB, FROM_HERE,
593 base::Bind(&base::DoNothing),
594 base::Bind(&BrowsingDataRemover::OnClearedFormData,
595 base::Unretained(this)));
597 autofill::PersonalDataManager* data_manager =
598 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
599 if (data_manager)
600 data_manager->Refresh();
604 if (remove_mask & REMOVE_CACHE) {
605 // Tell the renderers to clear their cache.
606 web_cache::WebCacheManager::GetInstance()->ClearCache();
608 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
610 waiting_for_clear_cache_ = true;
611 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
612 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
613 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
614 delete_end_)
615 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
616 base::Unretained(this)));
618 #if !defined(DISABLE_NACL)
619 waiting_for_clear_nacl_cache_ = true;
621 BrowserThread::PostTask(
622 BrowserThread::IO, FROM_HERE,
623 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
624 base::Unretained(this)));
626 waiting_for_clear_pnacl_cache_ = true;
627 BrowserThread::PostTask(
628 BrowserThread::IO, FROM_HERE,
629 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
630 base::Unretained(this), delete_begin_, delete_end_));
631 #endif
633 // The PrerenderManager may have a page actively being prerendered, which
634 // is essentially a preemptively cached page.
635 prerender::PrerenderManager* prerender_manager =
636 prerender::PrerenderManagerFactory::GetForProfile(profile_);
637 if (prerender_manager) {
638 prerender_manager->ClearData(
639 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
642 // Tell the shader disk cache to clear.
643 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
644 storage_partition_remove_mask |=
645 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
647 storage_partition_remove_mask |=
648 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
650 #if defined(ENABLE_EXTENSIONS)
651 // Clear the ephemeral apps cache. This is NULL while testing. OTR Profile
652 // has neither apps nor an ExtensionService, so ClearCachedApps fails.
653 EphemeralAppService* ephemeral_app_service =
654 EphemeralAppService::Get(profile_);
655 if (ephemeral_app_service && !profile_->IsOffTheRecord())
656 ephemeral_app_service->ClearCachedApps();
657 #endif
660 if (storage_partition_remove_mask) {
661 waiting_for_clear_storage_partition_data_ = true;
663 content::StoragePartition* storage_partition;
664 if (storage_partition_for_testing_)
665 storage_partition = storage_partition_for_testing_;
666 else
667 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
669 uint32 quota_storage_remove_mask =
670 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
672 if (delete_begin_ == base::Time() ||
673 origin_set_mask_ &
674 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
675 // If we're deleting since the beginning of time, or we're removing
676 // protected origins, then remove persistent quota data.
677 quota_storage_remove_mask |=
678 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
681 storage_partition->ClearData(
682 storage_partition_remove_mask,
683 quota_storage_remove_mask,
684 remove_origin_,
685 base::Bind(&DoesOriginMatchMask, origin_set_mask_),
686 delete_begin_,
687 delete_end_,
688 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
689 base::Unretained(this)));
692 #if defined(ENABLE_PLUGINS)
693 if (remove_mask & REMOVE_CONTENT_LICENSES) {
694 content::RecordAction(
695 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
697 waiting_for_clear_content_licenses_ = true;
698 if (!pepper_flash_settings_manager_.get()) {
699 pepper_flash_settings_manager_.reset(
700 new PepperFlashSettingsManager(this, profile_));
702 deauthorize_content_licenses_request_id_ =
703 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
704 #if defined(OS_CHROMEOS)
705 // On Chrome OS, also delete any content protection platform keys.
706 const user_manager::User* user =
707 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
708 if (!user) {
709 LOG(WARNING) << "Failed to find user for current profile.";
710 } else {
711 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
712 TpmAttestationDeleteKeys(
713 chromeos::attestation::KEY_USER,
714 user->email(),
715 chromeos::attestation::kContentProtectionKeyPrefix,
716 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
717 base::Unretained(this)));
718 waiting_for_clear_platform_keys_ = true;
720 #endif
722 #endif
724 // Remove omnibox zero-suggest cache results.
725 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
726 prefs->SetString(prefs::kZeroSuggestCachedResults, std::string());
728 // Always wipe accumulated network related data (TransportSecurityState and
729 // HttpServerPropertiesManager data).
730 waiting_for_clear_networking_history_ = true;
731 profile_->ClearNetworkingHistorySince(
732 delete_begin_,
733 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
734 base::Unretained(this)));
736 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
737 domain_reliability::DomainReliabilityService* service =
738 domain_reliability::DomainReliabilityServiceFactory::
739 GetForBrowserContext(profile_);
740 if (service) {
741 domain_reliability::DomainReliabilityClearMode mode;
742 if (remove_mask & REMOVE_COOKIES)
743 mode = domain_reliability::CLEAR_CONTEXTS;
744 else
745 mode = domain_reliability::CLEAR_BEACONS;
747 waiting_for_clear_domain_reliability_monitor_ = true;
748 service->ClearBrowsingData(
749 mode,
750 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
751 base::Unretained(this)));
756 void BrowsingDataRemover::AddObserver(Observer* observer) {
757 observer_list_.AddObserver(observer);
760 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
761 observer_list_.RemoveObserver(observer);
764 void BrowsingDataRemover::OnHistoryDeletionDone() {
765 waiting_for_clear_history_ = false;
766 NotifyAndDeleteIfDone();
769 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
770 content::StoragePartition* storage_partition) {
771 storage_partition_for_testing_ = storage_partition;
774 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
775 TimePeriod time_period) {
776 base::TimeDelta diff;
777 base::Time delete_begin_time = base::Time::Now();
778 switch (time_period) {
779 case LAST_HOUR:
780 diff = base::TimeDelta::FromHours(1);
781 break;
782 case LAST_DAY:
783 diff = base::TimeDelta::FromHours(24);
784 break;
785 case LAST_WEEK:
786 diff = base::TimeDelta::FromHours(7*24);
787 break;
788 case FOUR_WEEKS:
789 diff = base::TimeDelta::FromHours(4*7*24);
790 break;
791 case EVERYTHING:
792 delete_begin_time = base::Time();
793 break;
794 default:
795 NOTREACHED() << L"Missing item";
796 break;
798 return delete_begin_time - diff;
801 bool BrowsingDataRemover::AllDone() {
802 return !waiting_for_clear_autofill_origin_urls_ &&
803 !waiting_for_clear_cache_ &&
804 !waiting_for_clear_content_licenses_ &&
805 !waiting_for_clear_channel_ids_ &&
806 !waiting_for_clear_cookies_count_ &&
807 !waiting_for_clear_domain_reliability_monitor_ &&
808 !waiting_for_clear_form_ &&
809 !waiting_for_clear_history_ &&
810 !waiting_for_clear_hostname_resolution_cache_ &&
811 !waiting_for_clear_keyword_data_ &&
812 !waiting_for_clear_logged_in_predictor_ &&
813 !waiting_for_clear_nacl_cache_ &&
814 !waiting_for_clear_network_predictor_ &&
815 !waiting_for_clear_networking_history_ &&
816 !waiting_for_clear_platform_keys_ &&
817 !waiting_for_clear_plugin_data_ &&
818 !waiting_for_clear_pnacl_cache_ &&
819 #if defined(ENABLE_WEBRTC)
820 !waiting_for_clear_webrtc_logs_ &&
821 #endif
822 !waiting_for_clear_storage_partition_data_;
825 void BrowsingDataRemover::OnKeywordsLoaded() {
826 // Deletes the entries from the model, and if we're not waiting on anything
827 // else notifies observers and deletes this BrowsingDataRemover.
828 TemplateURLService* model =
829 TemplateURLServiceFactory::GetForProfile(profile_);
830 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
831 waiting_for_clear_keyword_data_ = false;
832 template_url_sub_.reset();
833 NotifyAndDeleteIfDone();
836 void BrowsingDataRemover::NotifyAndDelete() {
837 set_removing(false);
839 // Notify observers.
840 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
841 origin_set_mask_);
843 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
845 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
847 // History requests aren't happy if you delete yourself from the callback.
848 // As such, we do a delete later.
849 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
852 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
853 // TODO(brettw) http://crbug.com/305259: This should also observe session
854 // clearing (what about other things such as passwords, etc.?) and wait for
855 // them to complete before continuing.
857 if (!AllDone())
858 return;
860 if (completion_inhibitor_) {
861 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
862 this,
863 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
864 base::Unretained(this)));
865 } else {
866 NotifyAndDelete();
870 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
871 DCHECK_CURRENTLY_ON(BrowserThread::UI);
872 waiting_for_clear_hostname_resolution_cache_ = false;
873 NotifyAndDeleteIfDone();
876 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
877 IOThread* io_thread) {
878 DCHECK_CURRENTLY_ON(BrowserThread::IO);
880 io_thread->ClearHostCache();
882 // Notify the UI thread that we are done.
883 BrowserThread::PostTask(
884 BrowserThread::UI,
885 FROM_HERE,
886 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
887 base::Unretained(this)));
890 void BrowsingDataRemover::OnClearedLoggedInPredictor() {
891 DCHECK_CURRENTLY_ON(BrowserThread::UI);
892 DCHECK(waiting_for_clear_logged_in_predictor_);
893 waiting_for_clear_logged_in_predictor_ = false;
894 NotifyAndDeleteIfDone();
897 void BrowsingDataRemover::ClearLoggedInPredictor() {
898 DCHECK_CURRENTLY_ON(BrowserThread::UI);
899 DCHECK(!waiting_for_clear_logged_in_predictor_);
901 predictors::PredictorDatabase* predictor_db =
902 predictors::PredictorDatabaseFactory::GetForProfile(profile_);
903 if (!predictor_db)
904 return;
906 predictors::LoggedInPredictorTable* logged_in_table =
907 predictor_db->logged_in_table().get();
908 if (!logged_in_table)
909 return;
911 waiting_for_clear_logged_in_predictor_ = true;
913 BrowserThread::PostTaskAndReply(
914 BrowserThread::DB,
915 FROM_HERE,
916 base::Bind(&predictors::LoggedInPredictorTable::DeleteAllCreatedBetween,
917 logged_in_table,
918 delete_begin_,
919 delete_end_),
920 base::Bind(&BrowsingDataRemover::OnClearedLoggedInPredictor,
921 base::Unretained(this)));
924 void BrowsingDataRemover::OnClearedNetworkPredictor() {
925 DCHECK_CURRENTLY_ON(BrowserThread::UI);
926 waiting_for_clear_network_predictor_ = false;
927 NotifyAndDeleteIfDone();
930 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
931 chrome_browser_net::Predictor* predictor) {
932 DCHECK_CURRENTLY_ON(BrowserThread::IO);
933 DCHECK(predictor);
935 predictor->DiscardInitialNavigationHistory();
936 predictor->DiscardAllResults();
938 // Notify the UI thread that we are done.
939 BrowserThread::PostTask(
940 BrowserThread::UI,
941 FROM_HERE,
942 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
943 base::Unretained(this)));
946 void BrowsingDataRemover::OnClearedNetworkingHistory() {
947 DCHECK_CURRENTLY_ON(BrowserThread::UI);
948 waiting_for_clear_networking_history_ = false;
949 NotifyAndDeleteIfDone();
952 void BrowsingDataRemover::ClearedCache() {
953 waiting_for_clear_cache_ = false;
955 NotifyAndDeleteIfDone();
958 #if !defined(DISABLE_NACL)
959 void BrowsingDataRemover::ClearedNaClCache() {
960 // This function should be called on the UI thread.
961 DCHECK_CURRENTLY_ON(BrowserThread::UI);
963 waiting_for_clear_nacl_cache_ = false;
965 NotifyAndDeleteIfDone();
968 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
969 // This function should be called on the IO thread.
970 DCHECK_CURRENTLY_ON(BrowserThread::IO);
972 // Notify the UI thread that we are done.
973 BrowserThread::PostTask(
974 BrowserThread::UI, FROM_HERE,
975 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
976 base::Unretained(this)));
979 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
980 DCHECK_CURRENTLY_ON(BrowserThread::IO);
982 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
983 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
984 base::Unretained(this)));
987 void BrowsingDataRemover::ClearedPnaclCache() {
988 // This function should be called on the UI thread.
989 DCHECK_CURRENTLY_ON(BrowserThread::UI);
991 waiting_for_clear_pnacl_cache_ = false;
993 NotifyAndDeleteIfDone();
996 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
997 // This function should be called on the IO thread.
998 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1000 // Notify the UI thread that we are done.
1001 BrowserThread::PostTask(
1002 BrowserThread::UI, FROM_HERE,
1003 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1004 base::Unretained(this)));
1007 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1008 base::Time end) {
1009 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1011 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1012 begin, end,
1013 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1014 base::Unretained(this)));
1016 #endif
1018 void BrowsingDataRemover::OnWaitableEventSignaled(
1019 base::WaitableEvent* waitable_event) {
1020 waiting_for_clear_plugin_data_ = false;
1021 NotifyAndDeleteIfDone();
1024 #if defined(ENABLE_PLUGINS)
1025 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1026 uint32 request_id,
1027 bool /* success */) {
1028 DCHECK(waiting_for_clear_content_licenses_);
1029 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1031 waiting_for_clear_content_licenses_ = false;
1032 NotifyAndDeleteIfDone();
1034 #endif
1036 #if defined(OS_CHROMEOS)
1037 void BrowsingDataRemover::OnClearPlatformKeys(
1038 chromeos::DBusMethodCallStatus call_status,
1039 bool result) {
1040 DCHECK(waiting_for_clear_platform_keys_);
1041 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1042 LOG(ERROR) << "Failed to clear platform keys.";
1044 waiting_for_clear_platform_keys_ = false;
1045 NotifyAndDeleteIfDone();
1047 #endif
1049 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1050 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1051 BrowserThread::PostTask(
1052 BrowserThread::UI, FROM_HERE,
1053 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1054 base::Unretained(this), num_deleted));
1055 return;
1058 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1059 --waiting_for_clear_cookies_count_;
1060 NotifyAndDeleteIfDone();
1063 void BrowsingDataRemover::ClearCookiesOnIOThread(
1064 net::URLRequestContextGetter* rq_context) {
1065 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1066 net::CookieStore* cookie_store = rq_context->
1067 GetURLRequestContext()->cookie_store();
1068 cookie_store->DeleteAllCreatedBetweenAsync(
1069 delete_begin_, delete_end_,
1070 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1071 base::Unretained(this)));
1074 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1075 net::URLRequestContextGetter* rq_context) {
1076 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1077 net::ChannelIDService* channel_id_service =
1078 rq_context->GetURLRequestContext()->channel_id_service();
1079 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1080 delete_begin_, delete_end_,
1081 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1082 base::Unretained(this), base::Unretained(rq_context)));
1085 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1086 net::URLRequestContextGetter* rq_context) {
1087 // Need to close open SSL connections which may be using the channel ids we
1088 // are deleting.
1089 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1090 // service/store have observers that can notify relevant things directly.
1091 rq_context->GetURLRequestContext()->ssl_config_service()->
1092 NotifySSLConfigChange();
1093 BrowserThread::PostTask(
1094 BrowserThread::UI, FROM_HERE,
1095 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1096 base::Unretained(this)));
1099 void BrowsingDataRemover::OnClearedChannelIDs() {
1100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1101 waiting_for_clear_channel_ids_ = false;
1102 NotifyAndDeleteIfDone();
1105 void BrowsingDataRemover::OnClearedFormData() {
1106 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1107 waiting_for_clear_form_ = false;
1108 NotifyAndDeleteIfDone();
1111 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1112 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1113 waiting_for_clear_autofill_origin_urls_ = false;
1114 NotifyAndDeleteIfDone();
1117 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1118 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1119 waiting_for_clear_storage_partition_data_ = false;
1120 NotifyAndDeleteIfDone();
1123 #if defined(ENABLE_WEBRTC)
1124 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1126 waiting_for_clear_webrtc_logs_ = false;
1127 NotifyAndDeleteIfDone();
1129 #endif
1131 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1132 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1133 waiting_for_clear_domain_reliability_monitor_ = false;
1134 NotifyAndDeleteIfDone();
1137 // static
1138 BrowsingDataRemover::CallbackSubscription
1139 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1140 const BrowsingDataRemover::Callback& callback) {
1141 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);