Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.cc
blob16ffc0d0457519e74ea1bfde5b551563ca6a83b5
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/browsing_data/browsing_data_remover.h"
7 #include <map>
8 #include <set>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram_macros.h"
16 #include "base/prefs/pref_service.h"
17 #include "chrome/browser/autofill/personal_data_manager_factory.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/browsing_data/browsing_data_helper.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/domain_reliability/service_factory.h"
22 #include "chrome/browser/download/download_prefs.h"
23 #include "chrome/browser/download/download_service_factory.h"
24 #include "chrome/browser/history/history_service_factory.h"
25 #include "chrome/browser/history/web_history_service_factory.h"
26 #include "chrome/browser/io_thread.h"
27 #include "chrome/browser/media/media_device_id_salt.h"
28 #include "chrome/browser/net/predictor.h"
29 #include "chrome/browser/password_manager/password_store_factory.h"
30 #include "chrome/browser/prerender/prerender_manager.h"
31 #include "chrome/browser/prerender/prerender_manager_factory.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
34 #include "chrome/browser/search_engines/template_url_service_factory.h"
35 #include "chrome/browser/sessions/session_service.h"
36 #include "chrome/browser/sessions/session_service_factory.h"
37 #include "chrome/browser/sessions/tab_restore_service.h"
38 #include "chrome/browser/sessions/tab_restore_service_factory.h"
39 #include "chrome/browser/web_data_service_factory.h"
40 #include "chrome/common/pref_names.h"
41 #include "chrome/common/url_constants.h"
42 #include "components/autofill/core/browser/personal_data_manager.h"
43 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
44 #include "components/browsing_data/storage_partition_http_cache_data_remover.h"
45 #include "components/content_settings/core/browser/host_content_settings_map.h"
46 #include "components/domain_reliability/service.h"
47 #include "components/history/core/browser/history_service.h"
48 #include "components/nacl/browser/nacl_browser.h"
49 #include "components/nacl/browser/pnacl_host.h"
50 #include "components/omnibox/browser/omnibox_pref_names.h"
51 #include "components/password_manager/core/browser/password_store.h"
52 #include "components/power/origin_power_map.h"
53 #include "components/power/origin_power_map_factory.h"
54 #include "components/search_engines/template_url_service.h"
55 #include "components/web_cache/browser/web_cache_manager.h"
56 #include "content/public/browser/browser_thread.h"
57 #include "content/public/browser/download_manager.h"
58 #include "content/public/browser/notification_service.h"
59 #include "content/public/browser/plugin_data_remover.h"
60 #include "content/public/browser/ssl_host_state_delegate.h"
61 #include "content/public/browser/storage_partition.h"
62 #include "content/public/browser/user_metrics.h"
63 #include "net/base/net_errors.h"
64 #include "net/cookies/cookie_store.h"
65 #include "net/http/transport_security_state.h"
66 #include "net/ssl/channel_id_service.h"
67 #include "net/ssl/channel_id_store.h"
68 #include "net/url_request/url_request_context.h"
69 #include "net/url_request/url_request_context_getter.h"
70 #include "storage/browser/quota/special_storage_policy.h"
71 #include "url/origin.h"
73 #if defined(OS_ANDROID)
74 #include "chrome/browser/android/webapps/webapp_registry.h"
75 #include "chrome/browser/precache/precache_manager_factory.h"
76 #include "components/precache/content/precache_manager.h"
77 #endif
79 #if defined(OS_CHROMEOS)
80 #include "chrome/browser/chromeos/profiles/profile_helper.h"
81 #include "chromeos/attestation/attestation_constants.h"
82 #include "chromeos/dbus/cryptohome_client.h"
83 #include "chromeos/dbus/dbus_thread_manager.h"
84 #include "components/user_manager/user.h"
85 #endif
87 #if defined(ENABLE_EXTENSIONS)
88 #include "chrome/browser/apps/ephemeral_app_service.h"
89 #include "chrome/browser/extensions/activity_log/activity_log.h"
90 #include "chrome/browser/extensions/extension_service.h"
91 #include "chrome/browser/extensions/extension_special_storage_policy.h"
92 #include "extensions/browser/extension_prefs.h"
93 #endif
95 #if defined(ENABLE_WEBRTC)
96 #include "chrome/browser/media/webrtc_log_list.h"
97 #include "chrome/browser/media/webrtc_log_util.h"
98 #endif
100 using base::UserMetricsAction;
101 using content::BrowserContext;
102 using content::BrowserThread;
103 using content::DOMStorageContext;
105 namespace {
107 using CallbackList =
108 base::CallbackList<void(const BrowsingDataRemover::NotificationDetails&)>;
110 // Contains all registered callbacks for browsing data removed notifications.
111 CallbackList* g_on_browsing_data_removed_callbacks = nullptr;
113 // Accessor for |*g_on_browsing_data_removed_callbacks|. Creates a new object
114 // the first time so that it always returns a valid object.
115 CallbackList* GetOnBrowsingDataRemovedCallbacks() {
116 if (!g_on_browsing_data_removed_callbacks)
117 g_on_browsing_data_removed_callbacks = new CallbackList();
118 return g_on_browsing_data_removed_callbacks;
121 } // namespace
123 bool BrowsingDataRemover::is_removing_ = false;
125 BrowsingDataRemover::CompletionInhibitor*
126 BrowsingDataRemover::completion_inhibitor_ = nullptr;
128 // Helper to create callback for BrowsingDataRemover::DoesOriginMatchMask.
129 // Static.
130 bool DoesOriginMatchMask(
131 int origin_type_mask,
132 const GURL& origin,
133 storage::SpecialStoragePolicy* special_storage_policy) {
134 return BrowsingDataHelper::DoesOriginMatchMask(
135 origin, origin_type_mask, special_storage_policy);
138 BrowsingDataRemover::NotificationDetails::NotificationDetails()
139 : removal_begin(base::Time()),
140 removal_mask(-1),
141 origin_type_mask(-1) {
144 BrowsingDataRemover::NotificationDetails::NotificationDetails(
145 const BrowsingDataRemover::NotificationDetails& details)
146 : removal_begin(details.removal_begin),
147 removal_mask(details.removal_mask),
148 origin_type_mask(details.origin_type_mask) {
151 BrowsingDataRemover::NotificationDetails::NotificationDetails(
152 base::Time removal_begin,
153 int removal_mask,
154 int origin_type_mask)
155 : removal_begin(removal_begin),
156 removal_mask(removal_mask),
157 origin_type_mask(origin_type_mask) {
160 BrowsingDataRemover::NotificationDetails::~NotificationDetails() {}
162 // Static.
163 BrowsingDataRemover* BrowsingDataRemover::CreateForUnboundedRange(
164 Profile* profile) {
165 return new BrowsingDataRemover(profile, base::Time(), base::Time::Max());
168 // Static.
169 BrowsingDataRemover* BrowsingDataRemover::CreateForRange(Profile* profile,
170 base::Time start, base::Time end) {
171 return new BrowsingDataRemover(profile, start, end);
174 // Static.
175 BrowsingDataRemover* BrowsingDataRemover::CreateForPeriod(Profile* profile,
176 TimePeriod period) {
177 switch (period) {
178 case LAST_HOUR:
179 content::RecordAction(
180 UserMetricsAction("ClearBrowsingData_LastHour"));
181 break;
182 case LAST_DAY:
183 content::RecordAction(
184 UserMetricsAction("ClearBrowsingData_LastDay"));
185 break;
186 case LAST_WEEK:
187 content::RecordAction(
188 UserMetricsAction("ClearBrowsingData_LastWeek"));
189 break;
190 case FOUR_WEEKS:
191 content::RecordAction(
192 UserMetricsAction("ClearBrowsingData_LastMonth"));
193 break;
194 case EVERYTHING:
195 content::RecordAction(
196 UserMetricsAction("ClearBrowsingData_Everything"));
197 break;
199 return new BrowsingDataRemover(profile,
200 BrowsingDataRemover::CalculateBeginDeleteTime(period),
201 base::Time::Max());
204 BrowsingDataRemover::BrowsingDataRemover(Profile* profile,
205 base::Time delete_begin,
206 base::Time delete_end)
207 : profile_(profile),
208 delete_begin_(delete_begin),
209 delete_end_(delete_end),
210 main_context_getter_(profile->GetRequestContext()),
211 media_context_getter_(profile->GetMediaRequestContext()) {
212 DCHECK(profile);
213 // crbug.com/140910: Many places were calling this with base::Time() as
214 // delete_end, even though they should've used base::Time::Max(). Work around
215 // it here. New code should use base::Time::Max().
216 DCHECK(delete_end_ != base::Time());
217 if (delete_end_ == base::Time())
218 delete_end_ = base::Time::Max();
221 BrowsingDataRemover::~BrowsingDataRemover() {
222 DCHECK(AllDone());
225 // Static.
226 void BrowsingDataRemover::set_removing(bool is_removing) {
227 DCHECK(is_removing_ != is_removing);
228 is_removing_ = is_removing;
231 void BrowsingDataRemover::Remove(int remove_mask, int origin_type_mask) {
232 RemoveImpl(remove_mask, GURL(), origin_type_mask);
235 void BrowsingDataRemover::RemoveImpl(int remove_mask,
236 const GURL& remove_url,
237 int origin_type_mask) {
238 DCHECK_CURRENTLY_ON(BrowserThread::UI);
239 set_removing(true);
240 remove_mask_ = remove_mask;
241 origin_type_mask_ = origin_type_mask;
242 url::Origin remove_origin(remove_url);
244 PrefService* prefs = profile_->GetPrefs();
245 bool may_delete_history = prefs->GetBoolean(
246 prefs::kAllowDeletingBrowserHistory);
248 // All the UI entry points into the BrowsingDataRemover should be disabled,
249 // but this will fire if something was missed or added.
250 DCHECK(may_delete_history || (remove_mask & REMOVE_NOCHECKS) ||
251 (!(remove_mask & REMOVE_HISTORY) && !(remove_mask & REMOVE_DOWNLOADS)));
253 if (origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
254 content::RecordAction(
255 UserMetricsAction("ClearBrowsingData_MaskContainsUnprotectedWeb"));
257 if (origin_type_mask_ & BrowsingDataHelper::PROTECTED_WEB) {
258 content::RecordAction(
259 UserMetricsAction("ClearBrowsingData_MaskContainsProtectedWeb"));
261 if (origin_type_mask_ & BrowsingDataHelper::EXTENSION) {
262 content::RecordAction(
263 UserMetricsAction("ClearBrowsingData_MaskContainsExtension"));
265 // If this fires, we added a new BrowsingDataHelper::OriginTypeMask without
266 // updating the user metrics above.
267 static_assert(
268 BrowsingDataHelper::ALL == (BrowsingDataHelper::UNPROTECTED_WEB |
269 BrowsingDataHelper::PROTECTED_WEB |
270 BrowsingDataHelper::EXTENSION),
271 "OriginTypeMask has been updated without updating user metrics");
273 if ((remove_mask & REMOVE_HISTORY) && may_delete_history) {
274 history::HistoryService* history_service =
275 HistoryServiceFactory::GetForProfile(
276 profile_, ServiceAccessType::EXPLICIT_ACCESS);
277 if (history_service) {
278 std::set<GURL> restrict_urls;
279 if (!remove_url.is_empty())
280 restrict_urls.insert(remove_url);
281 content::RecordAction(UserMetricsAction("ClearBrowsingData_History"));
282 waiting_for_clear_history_ = true;
284 history_service->ExpireLocalAndRemoteHistoryBetween(
285 WebHistoryServiceFactory::GetForProfile(profile_),
286 restrict_urls, delete_begin_, delete_end_,
287 base::Bind(&BrowsingDataRemover::OnHistoryDeletionDone,
288 base::Unretained(this)),
289 &history_task_tracker_);
291 #if defined(ENABLE_EXTENSIONS)
292 // The extension activity contains details of which websites extensions
293 // were active on. It therefore indirectly stores details of websites a
294 // user has visited so best clean from here as well.
295 extensions::ActivityLog::GetInstance(profile_)->RemoveURLs(restrict_urls);
296 #endif
299 #if defined(ENABLE_EXTENSIONS)
300 // Clear launch times as they are a form of history.
301 extensions::ExtensionPrefs* extension_prefs =
302 extensions::ExtensionPrefs::Get(profile_);
303 extension_prefs->ClearLastLaunchTimes();
304 #endif
306 // The power consumption history by origin contains details of websites
307 // that were visited.
308 power::OriginPowerMap* origin_power_map =
309 power::OriginPowerMapFactory::GetForBrowserContext(profile_);
310 if (origin_power_map)
311 origin_power_map->ClearOriginMap();
313 // Need to clear the host cache and accumulated speculative data, as it also
314 // reveals some history: we have no mechanism to track when these items were
315 // created, so we'll clear them all. Better safe than sorry.
316 if (g_browser_process->io_thread()) {
317 waiting_for_clear_hostname_resolution_cache_ = true;
318 BrowserThread::PostTask(
319 BrowserThread::IO, FROM_HERE,
320 base::Bind(
321 &BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread,
322 base::Unretained(this),
323 g_browser_process->io_thread()));
325 if (profile_->GetNetworkPredictor()) {
326 waiting_for_clear_network_predictor_ = true;
327 BrowserThread::PostTask(
328 BrowserThread::IO, FROM_HERE,
329 base::Bind(&BrowsingDataRemover::ClearNetworkPredictorOnIOThread,
330 base::Unretained(this),
331 profile_->GetNetworkPredictor()));
334 // As part of history deletion we also delete the auto-generated keywords.
335 TemplateURLService* keywords_model =
336 TemplateURLServiceFactory::GetForProfile(profile_);
337 if (keywords_model && !keywords_model->loaded()) {
338 template_url_sub_ = keywords_model->RegisterOnLoadedCallback(
339 base::Bind(&BrowsingDataRemover::OnKeywordsLoaded,
340 base::Unretained(this)));
341 keywords_model->Load();
342 waiting_for_clear_keyword_data_ = true;
343 } else if (keywords_model) {
344 keywords_model->RemoveAutoGeneratedForOriginBetween(
345 remove_url, delete_begin_, delete_end_);
348 // The PrerenderManager keeps history of prerendered pages, so clear that.
349 // It also may have a prerendered page. If so, the page could be
350 // considered to have a small amount of historical information, so delete
351 // it, too.
352 prerender::PrerenderManager* prerender_manager =
353 prerender::PrerenderManagerFactory::GetForProfile(profile_);
354 if (prerender_manager) {
355 prerender_manager->ClearData(
356 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS |
357 prerender::PrerenderManager::CLEAR_PRERENDER_HISTORY);
360 // If the caller is removing history for all hosts, then clear ancillary
361 // historical information.
362 if (remove_url.is_empty()) {
363 // We also delete the list of recently closed tabs. Since these expire,
364 // they can't be more than a day old, so we can simply clear them all.
365 TabRestoreService* tab_service =
366 TabRestoreServiceFactory::GetForProfile(profile_);
367 if (tab_service) {
368 tab_service->ClearEntries();
369 tab_service->DeleteLastSession();
372 #if defined(ENABLE_SESSION_SERVICE)
373 // We also delete the last session when we delete the history.
374 SessionService* session_service =
375 SessionServiceFactory::GetForProfile(profile_);
376 if (session_service)
377 session_service->DeleteLastSession();
378 #endif
381 // The saved Autofill profiles and credit cards can include the origin from
382 // which these profiles and credit cards were learned. These are a form of
383 // history, so clear them as well.
384 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
385 WebDataServiceFactory::GetAutofillWebDataForProfile(
386 profile_, ServiceAccessType::EXPLICIT_ACCESS);
387 if (web_data_service.get()) {
388 waiting_for_clear_autofill_origin_urls_ = true;
389 web_data_service->RemoveOriginURLsModifiedBetween(
390 delete_begin_, delete_end_);
391 // The above calls are done on the UI thread but do their work on the DB
392 // thread. So wait for it.
393 BrowserThread::PostTaskAndReply(
394 BrowserThread::DB, FROM_HERE,
395 base::Bind(&base::DoNothing),
396 base::Bind(&BrowsingDataRemover::OnClearedAutofillOriginURLs,
397 base::Unretained(this)));
399 autofill::PersonalDataManager* data_manager =
400 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
401 if (data_manager)
402 data_manager->Refresh();
405 #if defined(ENABLE_WEBRTC)
406 waiting_for_clear_webrtc_logs_ = true;
407 BrowserThread::PostTaskAndReply(
408 BrowserThread::FILE,
409 FROM_HERE,
410 base::Bind(
411 &WebRtcLogUtil::DeleteOldAndRecentWebRtcLogFiles,
412 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()),
413 delete_begin_),
414 base::Bind(&BrowsingDataRemover::OnClearedWebRtcLogs,
415 base::Unretained(this)));
416 #endif
418 // The SSL Host State that tracks SSL interstitial "proceed" decisions may
419 // include origins that the user has visited, so it must be cleared.
420 if (profile_->GetSSLHostStateDelegate())
421 profile_->GetSSLHostStateDelegate()->Clear();
423 #if defined(OS_ANDROID)
424 precache::PrecacheManager* precache_manager =
425 precache::PrecacheManagerFactory::GetForBrowserContext(profile_);
426 // |precache_manager| could be nullptr if the profile is off the record.
427 if (!precache_manager) {
428 waiting_for_clear_precache_history_ = true;
429 precache_manager->ClearHistory();
430 // The above calls are done on the UI thread but do their work on the DB
431 // thread. So wait for it.
432 BrowserThread::PostTaskAndReply(
433 BrowserThread::DB, FROM_HERE,
434 base::Bind(&base::DoNothing),
435 base::Bind(&BrowsingDataRemover::OnClearedPrecacheHistory,
436 base::Unretained(this)));
438 #endif
441 if ((remove_mask & REMOVE_DOWNLOADS) && may_delete_history) {
442 content::RecordAction(UserMetricsAction("ClearBrowsingData_Downloads"));
443 content::DownloadManager* download_manager =
444 BrowserContext::GetDownloadManager(profile_);
445 if (remove_origin.unique())
446 download_manager->RemoveDownloadsBetween(delete_begin_, delete_end_);
447 else
448 download_manager->RemoveDownloadsByOriginAndTime(
449 remove_origin, delete_begin_, delete_end_);
450 DownloadPrefs* download_prefs = DownloadPrefs::FromDownloadManager(
451 download_manager);
452 download_prefs->SetSaveFilePath(download_prefs->DownloadPath());
455 uint32 storage_partition_remove_mask = 0;
457 // We ignore the REMOVE_COOKIES request if UNPROTECTED_WEB is not set,
458 // so that callers who request REMOVE_SITE_DATA with PROTECTED_WEB
459 // don't accidentally remove the cookies that are associated with the
460 // UNPROTECTED_WEB origin. This is necessary because cookies are not separated
461 // between UNPROTECTED_WEB and PROTECTED_WEB.
462 if (remove_mask & REMOVE_COOKIES &&
463 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
464 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cookies"));
466 storage_partition_remove_mask |=
467 content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
469 #if defined(SAFE_BROWSING_SERVICE)
470 // Clear the safebrowsing cookies only if time period is for "all time". It
471 // doesn't make sense to apply the time period of deleting in the last X
472 // hours/days to the safebrowsing cookies since they aren't the result of
473 // any user action.
474 if (delete_begin_ == base::Time()) {
475 SafeBrowsingService* sb_service =
476 g_browser_process->safe_browsing_service();
477 if (sb_service) {
478 net::URLRequestContextGetter* sb_context =
479 sb_service->url_request_context();
480 ++waiting_for_clear_cookies_count_;
481 BrowserThread::PostTask(
482 BrowserThread::IO, FROM_HERE,
483 base::Bind(&BrowsingDataRemover::ClearCookiesOnIOThread,
484 base::Unretained(this), base::Unretained(sb_context)));
487 #endif
488 MediaDeviceIDSalt::Reset(profile_->GetPrefs());
490 // TODO(mkwst): If we're not removing passwords, then clear the 'zero-click'
491 // flag for all credentials in the password store.
494 // Channel IDs are not separated for protected and unprotected web
495 // origins. We check the origin_type_mask_ to prevent unintended deletion.
496 if (remove_mask & REMOVE_CHANNEL_IDS &&
497 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
498 content::RecordAction(
499 UserMetricsAction("ClearBrowsingData_ChannelIDs"));
500 // Since we are running on the UI thread don't call GetURLRequestContext().
501 net::URLRequestContextGetter* rq_context = profile_->GetRequestContext();
502 if (rq_context) {
503 waiting_for_clear_channel_ids_ = true;
504 BrowserThread::PostTask(
505 BrowserThread::IO, FROM_HERE,
506 base::Bind(&BrowsingDataRemover::ClearChannelIDsOnIOThread,
507 base::Unretained(this), base::Unretained(rq_context)));
511 if (remove_mask & REMOVE_LOCAL_STORAGE) {
512 storage_partition_remove_mask |=
513 content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
516 if (remove_mask & REMOVE_INDEXEDDB) {
517 storage_partition_remove_mask |=
518 content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
520 if (remove_mask & REMOVE_WEBSQL) {
521 storage_partition_remove_mask |=
522 content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
524 if (remove_mask & REMOVE_APPCACHE) {
525 storage_partition_remove_mask |=
526 content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
528 if (remove_mask & REMOVE_SERVICE_WORKERS) {
529 storage_partition_remove_mask |=
530 content::StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
532 if (remove_mask & REMOVE_CACHE_STORAGE) {
533 storage_partition_remove_mask |=
534 content::StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE;
536 if (remove_mask & REMOVE_FILE_SYSTEMS) {
537 storage_partition_remove_mask |=
538 content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
541 #if defined(ENABLE_PLUGINS)
542 // Plugin is data not separated for protected and unprotected web origins. We
543 // check the origin_type_mask_ to prevent unintended deletion.
544 if (remove_mask & REMOVE_PLUGIN_DATA &&
545 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
546 content::RecordAction(UserMetricsAction("ClearBrowsingData_LSOData"));
548 waiting_for_clear_plugin_data_ = true;
549 if (!plugin_data_remover_.get())
550 plugin_data_remover_.reset(content::PluginDataRemover::Create(profile_));
551 base::WaitableEvent* event =
552 plugin_data_remover_->StartRemoving(delete_begin_);
554 base::WaitableEventWatcher::EventCallback watcher_callback =
555 base::Bind(&BrowsingDataRemover::OnWaitableEventSignaled,
556 base::Unretained(this));
557 watcher_.StartWatching(event, watcher_callback);
559 #endif
561 if (remove_mask & REMOVE_SITE_USAGE_DATA || remove_mask & REMOVE_HISTORY) {
562 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
563 CONTENT_SETTINGS_TYPE_APP_BANNER);
564 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType(
565 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT);
568 if (remove_mask & REMOVE_PASSWORDS) {
569 content::RecordAction(UserMetricsAction("ClearBrowsingData_Passwords"));
570 password_manager::PasswordStore* password_store =
571 PasswordStoreFactory::GetForProfile(
572 profile_, ServiceAccessType::EXPLICIT_ACCESS).get();
574 if (password_store) {
575 waiting_for_clear_passwords_ = true;
576 password_store->RemoveLoginsCreatedBetween(
577 delete_begin_, delete_end_,
578 base::Bind(&BrowsingDataRemover::OnClearedPasswords,
579 base::Unretained(this)));
583 if (remove_mask & REMOVE_FORM_DATA) {
584 content::RecordAction(UserMetricsAction("ClearBrowsingData_Autofill"));
585 scoped_refptr<autofill::AutofillWebDataService> web_data_service =
586 WebDataServiceFactory::GetAutofillWebDataForProfile(
587 profile_, ServiceAccessType::EXPLICIT_ACCESS);
589 if (web_data_service.get()) {
590 waiting_for_clear_form_ = true;
591 web_data_service->RemoveFormElementsAddedBetween(delete_begin_,
592 delete_end_);
593 web_data_service->RemoveAutofillDataModifiedBetween(
594 delete_begin_, delete_end_);
595 // The above calls are done on the UI thread but do their work on the DB
596 // thread. So wait for it.
597 BrowserThread::PostTaskAndReply(
598 BrowserThread::DB, FROM_HERE,
599 base::Bind(&base::DoNothing),
600 base::Bind(&BrowsingDataRemover::OnClearedFormData,
601 base::Unretained(this)));
603 autofill::PersonalDataManager* data_manager =
604 autofill::PersonalDataManagerFactory::GetForProfile(profile_);
605 if (data_manager)
606 data_manager->Refresh();
610 if (remove_mask & REMOVE_CACHE) {
611 // Tell the renderers to clear their cache.
612 web_cache::WebCacheManager::GetInstance()->ClearCache();
614 content::RecordAction(UserMetricsAction("ClearBrowsingData_Cache"));
616 waiting_for_clear_cache_ = true;
617 // StoragePartitionHttpCacheDataRemover deletes itself when it is done.
618 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
619 BrowserContext::GetDefaultStoragePartition(profile_), delete_begin_,
620 delete_end_)
621 ->Remove(base::Bind(&BrowsingDataRemover::ClearedCache,
622 base::Unretained(this)));
624 #if !defined(DISABLE_NACL)
625 waiting_for_clear_nacl_cache_ = true;
627 BrowserThread::PostTask(
628 BrowserThread::IO, FROM_HERE,
629 base::Bind(&BrowsingDataRemover::ClearNaClCacheOnIOThread,
630 base::Unretained(this)));
632 waiting_for_clear_pnacl_cache_ = true;
633 BrowserThread::PostTask(
634 BrowserThread::IO, FROM_HERE,
635 base::Bind(&BrowsingDataRemover::ClearPnaclCacheOnIOThread,
636 base::Unretained(this), delete_begin_, delete_end_));
637 #endif
639 // The PrerenderManager may have a page actively being prerendered, which
640 // is essentially a preemptively cached page.
641 prerender::PrerenderManager* prerender_manager =
642 prerender::PrerenderManagerFactory::GetForProfile(profile_);
643 if (prerender_manager) {
644 prerender_manager->ClearData(
645 prerender::PrerenderManager::CLEAR_PRERENDER_CONTENTS);
648 // Tell the shader disk cache to clear.
649 content::RecordAction(UserMetricsAction("ClearBrowsingData_ShaderCache"));
650 storage_partition_remove_mask |=
651 content::StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
653 storage_partition_remove_mask |=
654 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
656 #if defined(ENABLE_EXTENSIONS)
657 // Clear the ephemeral apps cache. This is nullptr while testing. OTR
658 // Profile has neither apps nor an ExtensionService, so ClearCachedApps
659 // fails.
660 EphemeralAppService* ephemeral_app_service =
661 EphemeralAppService::Get(profile_);
662 if (ephemeral_app_service && !profile_->IsOffTheRecord())
663 ephemeral_app_service->ClearCachedApps();
664 #endif
667 if (remove_mask & REMOVE_WEBRTC_IDENTITY) {
668 storage_partition_remove_mask |=
669 content::StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
672 if (storage_partition_remove_mask) {
673 waiting_for_clear_storage_partition_data_ = true;
675 content::StoragePartition* storage_partition;
676 if (storage_partition_for_testing_)
677 storage_partition = storage_partition_for_testing_;
678 else
679 storage_partition = BrowserContext::GetDefaultStoragePartition(profile_);
681 uint32 quota_storage_remove_mask =
682 ~content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
684 if (delete_begin_ == base::Time() ||
685 origin_type_mask_ &
686 (BrowsingDataHelper::PROTECTED_WEB | BrowsingDataHelper::EXTENSION)) {
687 // If we're deleting since the beginning of time, or we're removing
688 // protected origins, then remove persistent quota data.
689 quota_storage_remove_mask |=
690 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
693 storage_partition->ClearData(
694 storage_partition_remove_mask, quota_storage_remove_mask, remove_url,
695 base::Bind(&DoesOriginMatchMask, origin_type_mask_), delete_begin_,
696 delete_end_,
697 base::Bind(&BrowsingDataRemover::OnClearedStoragePartitionData,
698 base::Unretained(this)));
701 #if defined(ENABLE_PLUGINS)
702 if (remove_mask & REMOVE_CONTENT_LICENSES) {
703 content::RecordAction(
704 UserMetricsAction("ClearBrowsingData_ContentLicenses"));
706 waiting_for_clear_content_licenses_ = true;
707 if (!pepper_flash_settings_manager_.get()) {
708 pepper_flash_settings_manager_.reset(
709 new PepperFlashSettingsManager(this, profile_));
711 deauthorize_content_licenses_request_id_ =
712 pepper_flash_settings_manager_->DeauthorizeContentLicenses(prefs);
713 #if defined(OS_CHROMEOS)
714 // On Chrome OS, also delete any content protection platform keys.
715 const user_manager::User* user =
716 chromeos::ProfileHelper::Get()->GetUserByProfile(profile_);
717 if (!user) {
718 LOG(WARNING) << "Failed to find user for current profile.";
719 } else {
720 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->
721 TpmAttestationDeleteKeys(
722 chromeos::attestation::KEY_USER,
723 user->email(),
724 chromeos::attestation::kContentProtectionKeyPrefix,
725 base::Bind(&BrowsingDataRemover::OnClearPlatformKeys,
726 base::Unretained(this)));
727 waiting_for_clear_platform_keys_ = true;
729 #endif
731 #endif
733 // Remove omnibox zero-suggest cache results.
734 if ((remove_mask & (REMOVE_CACHE | REMOVE_COOKIES)))
735 prefs->SetString(omnibox::kZeroSuggestCachedResults, std::string());
737 // Always wipe accumulated network related data (TransportSecurityState and
738 // HttpServerPropertiesManager data).
739 waiting_for_clear_networking_history_ = true;
740 profile_->ClearNetworkingHistorySince(
741 delete_begin_,
742 base::Bind(&BrowsingDataRemover::OnClearedNetworkingHistory,
743 base::Unretained(this)));
745 if (remove_mask & (REMOVE_COOKIES | REMOVE_HISTORY)) {
746 domain_reliability::DomainReliabilityService* service =
747 domain_reliability::DomainReliabilityServiceFactory::
748 GetForBrowserContext(profile_);
749 if (service) {
750 domain_reliability::DomainReliabilityClearMode mode;
751 if (remove_mask & REMOVE_COOKIES)
752 mode = domain_reliability::CLEAR_CONTEXTS;
753 else
754 mode = domain_reliability::CLEAR_BEACONS;
756 waiting_for_clear_domain_reliability_monitor_ = true;
757 service->ClearBrowsingData(
758 mode,
759 base::Bind(&BrowsingDataRemover::OnClearedDomainReliabilityMonitor,
760 base::Unretained(this)));
764 #if defined(OS_ANDROID)
765 if (remove_mask & REMOVE_WEBAPP_DATA) {
766 waiting_for_clear_webapp_data_ = true;
767 WebappRegistry::UnregisterWebapps(
768 base::Bind(&BrowsingDataRemover::OnClearedWebappData,
769 base::Unretained(this)));
771 #endif
773 // Record the combined deletion of cookies and cache.
774 CookieOrCacheDeletionChoice choice = NEITHER_COOKIES_NOR_CACHE;
775 if (remove_mask & REMOVE_COOKIES &&
776 origin_type_mask_ & BrowsingDataHelper::UNPROTECTED_WEB) {
777 choice = remove_mask & REMOVE_CACHE ? BOTH_COOKIES_AND_CACHE
778 : ONLY_COOKIES;
779 } else if (remove_mask & REMOVE_CACHE) {
780 choice = ONLY_CACHE;
783 UMA_HISTOGRAM_ENUMERATION(
784 "History.ClearBrowsingData.UserDeletedCookieOrCache",
785 choice, MAX_CHOICE_VALUE);
788 void BrowsingDataRemover::AddObserver(Observer* observer) {
789 observer_list_.AddObserver(observer);
792 void BrowsingDataRemover::RemoveObserver(Observer* observer) {
793 observer_list_.RemoveObserver(observer);
796 void BrowsingDataRemover::OnHistoryDeletionDone() {
797 waiting_for_clear_history_ = false;
798 NotifyAndDeleteIfDone();
801 void BrowsingDataRemover::OverrideStoragePartitionForTesting(
802 content::StoragePartition* storage_partition) {
803 storage_partition_for_testing_ = storage_partition;
806 base::Time BrowsingDataRemover::CalculateBeginDeleteTime(
807 TimePeriod time_period) {
808 base::TimeDelta diff;
809 base::Time delete_begin_time = base::Time::Now();
810 switch (time_period) {
811 case LAST_HOUR:
812 diff = base::TimeDelta::FromHours(1);
813 break;
814 case LAST_DAY:
815 diff = base::TimeDelta::FromHours(24);
816 break;
817 case LAST_WEEK:
818 diff = base::TimeDelta::FromHours(7*24);
819 break;
820 case FOUR_WEEKS:
821 diff = base::TimeDelta::FromHours(4*7*24);
822 break;
823 case EVERYTHING:
824 delete_begin_time = base::Time();
825 break;
826 default:
827 NOTREACHED() << L"Missing item";
828 break;
830 return delete_begin_time - diff;
833 bool BrowsingDataRemover::AllDone() {
834 return !waiting_for_clear_autofill_origin_urls_ &&
835 !waiting_for_clear_cache_ &&
836 !waiting_for_clear_content_licenses_ &&
837 !waiting_for_clear_channel_ids_ &&
838 !waiting_for_clear_cookies_count_ &&
839 !waiting_for_clear_domain_reliability_monitor_ &&
840 !waiting_for_clear_form_ &&
841 !waiting_for_clear_history_ &&
842 !waiting_for_clear_hostname_resolution_cache_ &&
843 !waiting_for_clear_keyword_data_ &&
844 !waiting_for_clear_nacl_cache_ &&
845 !waiting_for_clear_network_predictor_ &&
846 !waiting_for_clear_networking_history_ &&
847 !waiting_for_clear_passwords_ &&
848 !waiting_for_clear_platform_keys_ &&
849 !waiting_for_clear_plugin_data_ &&
850 !waiting_for_clear_pnacl_cache_ &&
851 #if defined(OS_ANDROID)
852 !waiting_for_clear_precache_history_ &&
853 !waiting_for_clear_webapp_data_ &&
854 #endif
855 #if defined(ENABLE_WEBRTC)
856 !waiting_for_clear_webrtc_logs_ &&
857 #endif
858 !waiting_for_clear_storage_partition_data_;
861 void BrowsingDataRemover::OnKeywordsLoaded() {
862 // Deletes the entries from the model, and if we're not waiting on anything
863 // else notifies observers and deletes this BrowsingDataRemover.
864 TemplateURLService* model =
865 TemplateURLServiceFactory::GetForProfile(profile_);
866 model->RemoveAutoGeneratedBetween(delete_begin_, delete_end_);
867 waiting_for_clear_keyword_data_ = false;
868 template_url_sub_.reset();
869 NotifyAndDeleteIfDone();
872 void BrowsingDataRemover::NotifyAndDelete() {
873 set_removing(false);
875 // Notify observers.
876 BrowsingDataRemover::NotificationDetails details(delete_begin_, remove_mask_,
877 origin_type_mask_);
879 GetOnBrowsingDataRemovedCallbacks()->Notify(details);
881 FOR_EACH_OBSERVER(Observer, observer_list_, OnBrowsingDataRemoverDone());
883 // History requests aren't happy if you delete yourself from the callback.
884 // As such, we do a delete later.
885 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
888 void BrowsingDataRemover::NotifyAndDeleteIfDone() {
889 // TODO(brettw) http://crbug.com/305259: This should also observe session
890 // clearing (what about other things such as passwords, etc.?) and wait for
891 // them to complete before continuing.
893 if (!AllDone())
894 return;
896 if (completion_inhibitor_) {
897 completion_inhibitor_->OnBrowsingDataRemoverWouldComplete(
898 this,
899 base::Bind(&BrowsingDataRemover::NotifyAndDelete,
900 base::Unretained(this)));
901 } else {
902 NotifyAndDelete();
906 void BrowsingDataRemover::OnClearedHostnameResolutionCache() {
907 DCHECK_CURRENTLY_ON(BrowserThread::UI);
908 waiting_for_clear_hostname_resolution_cache_ = false;
909 NotifyAndDeleteIfDone();
912 void BrowsingDataRemover::ClearHostnameResolutionCacheOnIOThread(
913 IOThread* io_thread) {
914 DCHECK_CURRENTLY_ON(BrowserThread::IO);
916 io_thread->ClearHostCache();
918 // Notify the UI thread that we are done.
919 BrowserThread::PostTask(
920 BrowserThread::UI,
921 FROM_HERE,
922 base::Bind(&BrowsingDataRemover::OnClearedHostnameResolutionCache,
923 base::Unretained(this)));
926 void BrowsingDataRemover::OnClearedNetworkPredictor() {
927 DCHECK_CURRENTLY_ON(BrowserThread::UI);
928 waiting_for_clear_network_predictor_ = false;
929 NotifyAndDeleteIfDone();
932 void BrowsingDataRemover::ClearNetworkPredictorOnIOThread(
933 chrome_browser_net::Predictor* predictor) {
934 DCHECK_CURRENTLY_ON(BrowserThread::IO);
935 DCHECK(predictor);
937 predictor->DiscardInitialNavigationHistory();
938 predictor->DiscardAllResults();
940 // Notify the UI thread that we are done.
941 BrowserThread::PostTask(
942 BrowserThread::UI,
943 FROM_HERE,
944 base::Bind(&BrowsingDataRemover::OnClearedNetworkPredictor,
945 base::Unretained(this)));
948 void BrowsingDataRemover::OnClearedNetworkingHistory() {
949 DCHECK_CURRENTLY_ON(BrowserThread::UI);
950 waiting_for_clear_networking_history_ = false;
951 NotifyAndDeleteIfDone();
954 void BrowsingDataRemover::ClearedCache() {
955 waiting_for_clear_cache_ = false;
957 NotifyAndDeleteIfDone();
960 #if !defined(DISABLE_NACL)
961 void BrowsingDataRemover::ClearedNaClCache() {
962 // This function should be called on the UI thread.
963 DCHECK_CURRENTLY_ON(BrowserThread::UI);
965 waiting_for_clear_nacl_cache_ = false;
967 NotifyAndDeleteIfDone();
970 void BrowsingDataRemover::ClearedNaClCacheOnIOThread() {
971 // This function should be called on the IO thread.
972 DCHECK_CURRENTLY_ON(BrowserThread::IO);
974 // Notify the UI thread that we are done.
975 BrowserThread::PostTask(
976 BrowserThread::UI, FROM_HERE,
977 base::Bind(&BrowsingDataRemover::ClearedNaClCache,
978 base::Unretained(this)));
981 void BrowsingDataRemover::ClearNaClCacheOnIOThread() {
982 DCHECK_CURRENTLY_ON(BrowserThread::IO);
984 nacl::NaClBrowser::GetInstance()->ClearValidationCache(
985 base::Bind(&BrowsingDataRemover::ClearedNaClCacheOnIOThread,
986 base::Unretained(this)));
989 void BrowsingDataRemover::ClearedPnaclCache() {
990 // This function should be called on the UI thread.
991 DCHECK_CURRENTLY_ON(BrowserThread::UI);
993 waiting_for_clear_pnacl_cache_ = false;
995 NotifyAndDeleteIfDone();
998 void BrowsingDataRemover::ClearedPnaclCacheOnIOThread() {
999 // This function should be called on the IO thread.
1000 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1002 // Notify the UI thread that we are done.
1003 BrowserThread::PostTask(
1004 BrowserThread::UI, FROM_HERE,
1005 base::Bind(&BrowsingDataRemover::ClearedPnaclCache,
1006 base::Unretained(this)));
1009 void BrowsingDataRemover::ClearPnaclCacheOnIOThread(base::Time begin,
1010 base::Time end) {
1011 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1013 pnacl::PnaclHost::GetInstance()->ClearTranslationCacheEntriesBetween(
1014 begin, end,
1015 base::Bind(&BrowsingDataRemover::ClearedPnaclCacheOnIOThread,
1016 base::Unretained(this)));
1018 #endif
1020 void BrowsingDataRemover::OnWaitableEventSignaled(
1021 base::WaitableEvent* waitable_event) {
1022 waiting_for_clear_plugin_data_ = false;
1023 NotifyAndDeleteIfDone();
1026 #if defined(ENABLE_PLUGINS)
1027 void BrowsingDataRemover::OnDeauthorizeContentLicensesCompleted(
1028 uint32 request_id,
1029 bool /* success */) {
1030 DCHECK(waiting_for_clear_content_licenses_);
1031 DCHECK_EQ(request_id, deauthorize_content_licenses_request_id_);
1033 waiting_for_clear_content_licenses_ = false;
1034 NotifyAndDeleteIfDone();
1036 #endif
1038 #if defined(OS_CHROMEOS)
1039 void BrowsingDataRemover::OnClearPlatformKeys(
1040 chromeos::DBusMethodCallStatus call_status,
1041 bool result) {
1042 DCHECK(waiting_for_clear_platform_keys_);
1043 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) {
1044 LOG(ERROR) << "Failed to clear platform keys.";
1046 waiting_for_clear_platform_keys_ = false;
1047 NotifyAndDeleteIfDone();
1049 #endif
1052 void BrowsingDataRemover::OnClearedPasswords() {
1053 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1054 waiting_for_clear_passwords_ = false;
1055 NotifyAndDeleteIfDone();
1058 void BrowsingDataRemover::OnClearedCookies(int num_deleted) {
1059 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
1060 BrowserThread::PostTask(
1061 BrowserThread::UI, FROM_HERE,
1062 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1063 base::Unretained(this), num_deleted));
1064 return;
1067 DCHECK_GT(waiting_for_clear_cookies_count_, 0);
1068 --waiting_for_clear_cookies_count_;
1069 NotifyAndDeleteIfDone();
1072 void BrowsingDataRemover::ClearCookiesOnIOThread(
1073 net::URLRequestContextGetter* rq_context) {
1074 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1075 net::CookieStore* cookie_store = rq_context->
1076 GetURLRequestContext()->cookie_store();
1077 cookie_store->DeleteAllCreatedBetweenAsync(
1078 delete_begin_, delete_end_,
1079 base::Bind(&BrowsingDataRemover::OnClearedCookies,
1080 base::Unretained(this)));
1083 void BrowsingDataRemover::ClearChannelIDsOnIOThread(
1084 net::URLRequestContextGetter* rq_context) {
1085 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1086 net::ChannelIDService* channel_id_service =
1087 rq_context->GetURLRequestContext()->channel_id_service();
1088 channel_id_service->GetChannelIDStore()->DeleteAllCreatedBetween(
1089 delete_begin_, delete_end_,
1090 base::Bind(&BrowsingDataRemover::OnClearedChannelIDsOnIOThread,
1091 base::Unretained(this), base::Unretained(rq_context)));
1094 void BrowsingDataRemover::OnClearedChannelIDsOnIOThread(
1095 net::URLRequestContextGetter* rq_context) {
1096 // Need to close open SSL connections which may be using the channel ids we
1097 // are deleting.
1098 // TODO(mattm): http://crbug.com/166069 Make the server bound cert
1099 // service/store have observers that can notify relevant things directly.
1100 rq_context->GetURLRequestContext()->ssl_config_service()->
1101 NotifySSLConfigChange();
1102 BrowserThread::PostTask(
1103 BrowserThread::UI, FROM_HERE,
1104 base::Bind(&BrowsingDataRemover::OnClearedChannelIDs,
1105 base::Unretained(this)));
1108 void BrowsingDataRemover::OnClearedChannelIDs() {
1109 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1110 waiting_for_clear_channel_ids_ = false;
1111 NotifyAndDeleteIfDone();
1114 void BrowsingDataRemover::OnClearedFormData() {
1115 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1116 waiting_for_clear_form_ = false;
1117 NotifyAndDeleteIfDone();
1120 void BrowsingDataRemover::OnClearedAutofillOriginURLs() {
1121 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1122 waiting_for_clear_autofill_origin_urls_ = false;
1123 NotifyAndDeleteIfDone();
1126 void BrowsingDataRemover::OnClearedStoragePartitionData() {
1127 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1128 waiting_for_clear_storage_partition_data_ = false;
1129 NotifyAndDeleteIfDone();
1132 #if defined(ENABLE_WEBRTC)
1133 void BrowsingDataRemover::OnClearedWebRtcLogs() {
1134 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1135 waiting_for_clear_webrtc_logs_ = false;
1136 NotifyAndDeleteIfDone();
1138 #endif
1140 #if defined(OS_ANDROID)
1141 void BrowsingDataRemover::OnClearedPrecacheHistory() {
1142 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1143 waiting_for_clear_precache_history_ = false;
1144 NotifyAndDeleteIfDone();
1147 void BrowsingDataRemover::OnClearedWebappData() {
1148 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1149 waiting_for_clear_webapp_data_ = false;
1150 NotifyAndDeleteIfDone();
1152 #endif
1154 void BrowsingDataRemover::OnClearedDomainReliabilityMonitor() {
1155 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1156 waiting_for_clear_domain_reliability_monitor_ = false;
1157 NotifyAndDeleteIfDone();
1160 // static
1161 BrowsingDataRemover::CallbackSubscription
1162 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback(
1163 const BrowsingDataRemover::Callback& callback) {
1164 return GetOnBrowsingDataRemovedCallbacks()->Add(callback);