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 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_member.h"
14 #include "base/sequenced_task_runner_helpers.h"
15 #include "base/synchronization/waitable_event_watcher.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "base/time/time.h"
18 #include "chrome/browser/pepper_flash_settings_manager.h"
19 #include "components/search_engines/template_url_service.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/dbus/dbus_method_call_status.h"
23 #include "storage/common/quota/quota_types.h"
25 #include "url/origin.h"
30 namespace chrome_browser_net
{
35 class PluginDataRemover
;
36 class StoragePartition
;
40 class URLRequestContextGetter
;
43 // BrowsingDataRemover is responsible for removing data related to browsing:
44 // visits in url database, downloads, cookies ...
46 class BrowsingDataRemover
47 #if defined(ENABLE_PLUGINS)
48 : public PepperFlashSettingsManager::Client
52 // Time period ranges available when doing browsing data removals.
61 // Mask used for Remove.
63 REMOVE_APPCACHE
= 1 << 0,
64 REMOVE_CACHE
= 1 << 1,
65 REMOVE_COOKIES
= 1 << 2,
66 REMOVE_DOWNLOADS
= 1 << 3,
67 REMOVE_FILE_SYSTEMS
= 1 << 4,
68 REMOVE_FORM_DATA
= 1 << 5,
69 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
70 REMOVE_HISTORY
= 1 << 6,
71 REMOVE_INDEXEDDB
= 1 << 7,
72 REMOVE_LOCAL_STORAGE
= 1 << 8,
73 REMOVE_PLUGIN_DATA
= 1 << 9,
74 REMOVE_PASSWORDS
= 1 << 10,
75 REMOVE_WEBSQL
= 1 << 11,
76 REMOVE_CHANNEL_IDS
= 1 << 12,
77 REMOVE_CONTENT_LICENSES
= 1 << 13,
78 REMOVE_SERVICE_WORKERS
= 1 << 14,
79 REMOVE_SITE_USAGE_DATA
= 1 << 15,
80 // REMOVE_NOCHECKS intentionally does not check if the Profile's prohibited
81 // from deleting history or downloads.
82 REMOVE_NOCHECKS
= 1 << 16,
83 REMOVE_WEBRTC_IDENTITY
= 1 << 17,
84 REMOVE_CACHE_STORAGE
= 1 << 18,
85 #if defined(OS_ANDROID)
86 REMOVE_WEBAPP_DATA
= 1 << 19,
88 // The following flag is used only in tests. In normal usage, hosted app
89 // data is controlled by the REMOVE_COOKIES flag, applied to the
90 // protected-web origin.
91 REMOVE_HOSTED_APP_DATA_TESTONLY
= 1 << 31,
93 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
94 // storage, webSQL, service workers, cache storage, plugin data, and web app
96 REMOVE_SITE_DATA
= REMOVE_APPCACHE
| REMOVE_COOKIES
| REMOVE_FILE_SYSTEMS
|
98 REMOVE_LOCAL_STORAGE
|
100 REMOVE_SERVICE_WORKERS
|
101 REMOVE_CACHE_STORAGE
|
104 REMOVE_SITE_USAGE_DATA
|
105 #if defined(OS_ANDROID)
108 REMOVE_WEBRTC_IDENTITY
,
110 // Includes all the available remove options. Meant to be used by clients
111 // that wish to wipe as much data as possible from a Profile, to make it
112 // look like a new Profile.
113 REMOVE_ALL
= REMOVE_SITE_DATA
| REMOVE_CACHE
| REMOVE_DOWNLOADS
|
117 REMOVE_CONTENT_LICENSES
,
119 // Includes all available remove options. Meant to be used when the Profile
120 // is scheduled to be deleted, and all possible data should be wiped from
121 // disk as soon as possible.
122 REMOVE_WIPE_PROFILE
= REMOVE_ALL
| REMOVE_NOCHECKS
,
125 // A helper enum to report the deletion of cookies and/or cache. Do not
126 // reorder the entries, as this enum is passed to UMA.
127 enum CookieOrCacheDeletionChoice
{
128 NEITHER_COOKIES_NOR_CACHE
,
131 BOTH_COOKIES_AND_CACHE
,
135 // When BrowsingDataRemover successfully removes data, a notification of type
136 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
138 struct NotificationDetails
{
139 NotificationDetails();
140 NotificationDetails(const NotificationDetails
& details
);
141 NotificationDetails(base::Time removal_begin
,
143 int origin_type_mask
);
144 ~NotificationDetails();
146 // The beginning of the removal time range.
147 base::Time removal_begin
;
149 // The removal mask (see the RemoveDataMask enum for details).
152 // The origin type mask (see BrowsingDataHelper::OriginTypeMask for
154 int origin_type_mask
;
157 // Observer is notified when the removal is done. Done means keywords have
158 // been deleted, cache cleared and all other tasks scheduled.
161 virtual void OnBrowsingDataRemoverDone() = 0;
164 virtual ~Observer() {}
167 using Callback
= base::Callback
<void(const NotificationDetails
&)>;
168 using CallbackSubscription
= scoped_ptr
<
169 base::CallbackList
<void(const NotificationDetails
&)>::Subscription
>;
171 // The completion inhibitor can artificially delay completion of the browsing
172 // data removal process. It is used during testing to simulate scenarios in
173 // which the deletion stalls or takes a very long time.
174 class CompletionInhibitor
{
176 // Invoked when a |remover| is just about to complete clearing browser data,
177 // and will be prevented from completing until after the callback
178 // |continue_to_completion| is run.
179 virtual void OnBrowsingDataRemoverWouldComplete(
180 BrowsingDataRemover
* remover
,
181 const base::Closure
& continue_to_completion
) = 0;
184 virtual ~CompletionInhibitor() {}
187 // Creates a BrowsingDataRemover object that removes data regardless of the
188 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
189 // retains ownership of itself, and deletes itself once finished.
190 static BrowsingDataRemover
* CreateForUnboundedRange(Profile
* profile
);
192 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
193 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
194 // deletes itself once finished.
195 static BrowsingDataRemover
* CreateForRange(Profile
* profile
,
196 base::Time delete_begin
,
197 base::Time delete_end
);
199 // Creates a BrowsingDataRemover bound to a specific period of time (as
200 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
201 // retains ownership of itself, and deletes itself once finished.
202 static BrowsingDataRemover
* CreateForPeriod(Profile
* profile
,
205 // Calculate the begin time for the deletion range specified by |time_period|.
206 static base::Time
CalculateBeginDeleteTime(TimePeriod time_period
);
208 // Is the BrowsingDataRemover currently in the process of removing data?
209 static bool is_removing() { return is_removing_
; }
211 // Sets a CompletionInhibitor, which will be notified each time an instance is
212 // about to complete a browsing data removal process, and will be able to
213 // artificially delay the completion.
214 static void set_completion_inhibitor_for_testing(
215 CompletionInhibitor
* inhibitor
) {
216 completion_inhibitor_
= inhibitor
;
219 // Add a callback to the list of callbacks to be called during a browsing data
220 // removal event. Returns a subscription object that can be used to
221 // un-register the callback.
222 static CallbackSubscription
RegisterOnBrowsingDataRemovedCallback(
223 const Callback
& callback
);
225 // Removes the specified items related to browsing for all origins that match
226 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask).
227 void Remove(int remove_mask
, int origin_type_mask
);
229 void AddObserver(Observer
* observer
);
230 void RemoveObserver(Observer
* observer
);
232 // Called when history deletion is done.
233 void OnHistoryDeletionDone();
236 void OverrideStoragePartitionForTesting(
237 content::StoragePartition
* storage_partition
);
240 // The clear API needs to be able to toggle removing_ in order to test that
241 // only one BrowsingDataRemover instance can be called at a time.
242 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest
, OneAtATime
);
244 // The BrowsingDataRemover tests need to be able to access the implementation
245 // of Remove(), as it exposes details that aren't yet available in the public
246 // API. As soon as those details are exposed via new methods, this should be
249 // TODO(mkwst): See http://crbug.com/113621
250 friend class BrowsingDataRemoverTest
;
252 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
253 // not already removing, and vice-versa.
254 static void set_removing(bool is_removing
);
256 // Creates a BrowsingDataRemover to remove browser data from the specified
257 // profile in the specified time range. Use Remove to initiate the removal.
258 BrowsingDataRemover(Profile
* profile
,
259 base::Time delete_begin
,
260 base::Time delete_end
);
262 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
263 // to be deleted by other objects so make destructor private and DeleteHelper
265 friend class base::DeleteHelper
<BrowsingDataRemover
>;
267 // When plugins aren't enabled, there is no base class, so adding an override
268 // specifier would result in a compile error.
269 #if defined(ENABLE_PLUGINS)
270 ~BrowsingDataRemover() override
;
272 ~BrowsingDataRemover();
275 // Callback for when TemplateURLService has finished loading. Clears the data,
276 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
277 void OnKeywordsLoaded();
279 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
280 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
282 #if defined(ENABLE_PLUGINS)
283 // PepperFlashSettingsManager::Client implementation.
284 void OnDeauthorizeContentLicensesCompleted(uint32 request_id
,
285 bool success
) override
;
288 #if defined (OS_CHROMEOS)
289 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status
,
293 // Removes the specified items related to browsing for a specific host. If the
294 // provided |remove_url| is empty, data is removed for all origins. The
295 // |origin_type_mask| parameter defines the set of origins from which data
296 // should be removed (protected, unprotected, or both).
297 // TODO(mkwst): The current implementation relies on unique (empty) origins to
298 // signal removal of all origins. Reconsider this behavior if/when we build
299 // a "forget this site" feature.
300 void RemoveImpl(int remove_mask
,
301 const GURL
& remove_url
,
302 int origin_type_mask
);
304 // Notifies observers and deletes this object.
305 void NotifyAndDelete();
307 // Checks if we are all done, and if so, calls NotifyAndDelete().
308 void NotifyAndDeleteIfDone();
310 // Callback for when the hostname resolution cache has been cleared.
311 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
312 void OnClearedHostnameResolutionCache();
314 // Invoked on the IO thread to clear the hostname resolution cache.
315 void ClearHostnameResolutionCacheOnIOThread(IOThread
* io_thread
);
317 // Callback for when speculative data in the network Predictor has been
318 // cleared. Clears the respective waiting flag and invokes
319 // NotifyAndDeleteIfDone.
320 void OnClearedNetworkPredictor();
322 // Invoked on the IO thread to clear speculative data related to hostname
323 // pre-resolution from the network Predictor.
324 void ClearNetworkPredictorOnIOThread(
325 chrome_browser_net::Predictor
* predictor
);
327 // Callback for when network related data in ProfileIOData has been cleared.
328 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
329 void OnClearedNetworkingHistory();
331 // Callback for when the cache has been deleted. Invokes
332 // NotifyAndDeleteIfDone.
334 #if !defined(DISABLE_NACL)
335 // Callback for when the NaCl cache has been deleted. Invokes
336 // NotifyAndDeleteIfDone.
337 void ClearedNaClCache();
339 // Invokes the ClearedNaClCache on the UI thread.
340 void ClearedNaClCacheOnIOThread();
342 // Invoked on the IO thread to delete the NaCl cache.
343 void ClearNaClCacheOnIOThread();
345 // Callback for when the PNaCl translation cache has been deleted. Invokes
346 // NotifyAndDeleteIfDone.
347 void ClearedPnaclCache();
349 // Invokes ClearedPnaclCacheOn on the UI thread.
350 void ClearedPnaclCacheOnIOThread();
352 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
353 void ClearPnaclCacheOnIOThread(base::Time begin
, base::Time end
);
356 // Callback for when passwords for the requested time range have been cleared.
357 void OnClearedPasswords();
359 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
360 void OnClearedCookies(int num_deleted
);
362 // Invoked on the IO thread to delete cookies.
363 void ClearCookiesOnIOThread(net::URLRequestContextGetter
* rq_context
);
365 // Invoked on the IO thread to delete channel IDs.
366 void ClearChannelIDsOnIOThread(
367 net::URLRequestContextGetter
* rq_context
);
369 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
370 // connection pool and posts to UI thread to run OnClearedChannelIDs.
371 void OnClearedChannelIDsOnIOThread(
372 net::URLRequestContextGetter
* rq_context
);
374 // Callback for when channel IDs have been deleted. Invokes
375 // NotifyAndDeleteIfDone.
376 void OnClearedChannelIDs();
378 // Callback from the above method.
379 void OnClearedFormData();
381 // Callback for when the Autofill profile and credit card origin URLs have
383 void OnClearedAutofillOriginURLs();
385 // Callback on UI thread when the storage partition related data are cleared.
386 void OnClearedStoragePartitionData();
388 #if defined(ENABLE_WEBRTC)
389 // Callback on UI thread when the WebRTC logs have been deleted.
390 void OnClearedWebRtcLogs();
393 #if defined(OS_ANDROID)
394 // Callback on UI thread when the precache history has been cleared.
395 void OnClearedPrecacheHistory();
397 // Callback on UI thread when the webapp data has been cleared.
398 void OnClearedWebappData();
401 void OnClearedDomainReliabilityMonitor();
403 // Returns true if we're all done.
406 // Profile we're to remove from.
409 // Start time to delete from.
410 const base::Time delete_begin_
;
412 // End time to delete to.
413 base::Time delete_end_
;
415 // True if Remove has been invoked.
416 static bool is_removing_
;
418 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
419 // is about to complete a browsing data removal process, and has the ability
420 // to artificially delay completion. Used for testing.
421 static CompletionInhibitor
* completion_inhibitor_
;
423 // Used to delete data from HTTP cache.
424 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
425 scoped_refptr
<net::URLRequestContextGetter
> media_context_getter_
;
427 #if defined(ENABLE_PLUGINS)
428 // Used to delete plugin data.
429 scoped_ptr
<content::PluginDataRemover
> plugin_data_remover_
;
430 base::WaitableEventWatcher watcher_
;
432 // Used to deauthorize content licenses for Pepper Flash.
433 scoped_ptr
<PepperFlashSettingsManager
> pepper_flash_settings_manager_
;
436 uint32 deauthorize_content_licenses_request_id_
= 0;
437 // True if we're waiting for various data to be deleted.
438 // These may only be accessed from UI thread in order to avoid races!
439 bool waiting_for_clear_autofill_origin_urls_
= false;
440 bool waiting_for_clear_cache_
= false;
441 bool waiting_for_clear_channel_ids_
= false;
442 bool waiting_for_clear_content_licenses_
= false;
443 // Non-zero if waiting for cookies to be cleared.
444 int waiting_for_clear_cookies_count_
= 0;
445 bool waiting_for_clear_domain_reliability_monitor_
= false;
446 bool waiting_for_clear_form_
= false;
447 bool waiting_for_clear_history_
= false;
448 bool waiting_for_clear_hostname_resolution_cache_
= false;
449 bool waiting_for_clear_keyword_data_
= false;
450 bool waiting_for_clear_nacl_cache_
= false;
451 bool waiting_for_clear_network_predictor_
= false;
452 bool waiting_for_clear_networking_history_
= false;
453 bool waiting_for_clear_passwords_
= false;
454 bool waiting_for_clear_platform_keys_
= false;
455 bool waiting_for_clear_plugin_data_
= false;
456 bool waiting_for_clear_pnacl_cache_
= false;
457 #if defined(OS_ANDROID)
458 bool waiting_for_clear_precache_history_
= false;
459 bool waiting_for_clear_webapp_data_
= false;
461 bool waiting_for_clear_storage_partition_data_
= false;
462 #if defined(ENABLE_WEBRTC)
463 bool waiting_for_clear_webrtc_logs_
= false;
466 // The removal mask for the current removal operation.
467 int remove_mask_
= 0;
469 // From which types of origins should we remove data?
470 int origin_type_mask_
= 0;
472 base::ObserverList
<Observer
> observer_list_
;
474 // Used if we need to clear history.
475 base::CancelableTaskTracker history_task_tracker_
;
477 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
479 // We do not own this.
480 content::StoragePartition
* storage_partition_for_testing_
= nullptr;
482 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover
);
485 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_