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"
26 class ExtensionSpecialStoragePolicy
;
30 namespace chrome_browser_net
{
35 class PluginDataRemover
;
36 class StoragePartition
;
39 namespace disk_cache
{
44 class URLRequestContextGetter
;
52 class DOMStorageContext
;
53 struct LocalStorageUsageInfo
;
54 struct SessionStorageUsageInfo
;
57 // BrowsingDataRemover is responsible for removing data related to browsing:
58 // visits in url database, downloads, cookies ...
60 class BrowsingDataRemover
61 #if defined(ENABLE_PLUGINS)
62 : public PepperFlashSettingsManager::Client
66 // Time period ranges available when doing browsing data removals.
75 // Mask used for Remove.
77 REMOVE_APPCACHE
= 1 << 0,
78 REMOVE_CACHE
= 1 << 1,
79 REMOVE_COOKIES
= 1 << 2,
80 REMOVE_DOWNLOADS
= 1 << 3,
81 REMOVE_FILE_SYSTEMS
= 1 << 4,
82 REMOVE_FORM_DATA
= 1 << 5,
83 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
84 REMOVE_HISTORY
= 1 << 6,
85 REMOVE_INDEXEDDB
= 1 << 7,
86 REMOVE_LOCAL_STORAGE
= 1 << 8,
87 REMOVE_PLUGIN_DATA
= 1 << 9,
88 REMOVE_PASSWORDS
= 1 << 10,
89 REMOVE_WEBSQL
= 1 << 11,
90 REMOVE_CHANNEL_IDS
= 1 << 12,
91 REMOVE_CONTENT_LICENSES
= 1 << 13,
92 REMOVE_SERVICE_WORKERS
= 1 << 14,
93 #if defined(OS_ANDROID)
94 REMOVE_APP_BANNER_DATA
= 1 << 15,
96 // The following flag is used only in tests. In normal usage, hosted app
97 // data is controlled by the REMOVE_COOKIES flag, applied to the
98 // protected-web origin.
99 REMOVE_HOSTED_APP_DATA_TESTONLY
= 1 << 31,
101 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
102 // storage, webSQL, service workers, and plugin data.
103 REMOVE_SITE_DATA
= REMOVE_APPCACHE
| REMOVE_COOKIES
| REMOVE_FILE_SYSTEMS
|
105 REMOVE_LOCAL_STORAGE
|
107 REMOVE_SERVICE_WORKERS
|
109 #if defined(OS_ANDROID)
110 REMOVE_APP_BANNER_DATA
|
114 // Includes all the available remove options. Meant to be used by clients
115 // that wish to wipe as much data as possible from a Profile, to make it
116 // look like a new Profile.
117 REMOVE_ALL
= REMOVE_SITE_DATA
| REMOVE_CACHE
| REMOVE_DOWNLOADS
|
121 REMOVE_CONTENT_LICENSES
,
124 // When BrowsingDataRemover successfully removes data, a notification of type
125 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
127 struct NotificationDetails
{
128 NotificationDetails();
129 NotificationDetails(const NotificationDetails
& details
);
130 NotificationDetails(base::Time removal_begin
,
132 int origin_set_mask
);
133 ~NotificationDetails();
135 // The beginning of the removal time range.
136 base::Time removal_begin
;
138 // The removal mask (see the RemoveDataMask enum for details).
141 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
145 // Observer is notified when the removal is done. Done means keywords have
146 // been deleted, cache cleared and all other tasks scheduled.
149 virtual void OnBrowsingDataRemoverDone() = 0;
152 virtual ~Observer() {}
155 using Callback
= base::Callback
<void(const NotificationDetails
&)>;
156 using CallbackSubscription
= scoped_ptr
<
157 base::CallbackList
<void(const NotificationDetails
&)>::Subscription
>;
159 // The completion inhibitor can artificially delay completion of the browsing
160 // data removal process. It is used during testing to simulate scenarios in
161 // which the deletion stalls or takes a very long time.
162 class CompletionInhibitor
{
164 // Invoked when a |remover| is just about to complete clearing browser data,
165 // and will be prevented from completing until after the callback
166 // |continue_to_completion| is run.
167 virtual void OnBrowsingDataRemoverWouldComplete(
168 BrowsingDataRemover
* remover
,
169 const base::Closure
& continue_to_completion
) = 0;
172 virtual ~CompletionInhibitor() {}
175 // Creates a BrowsingDataRemover object that removes data regardless of the
176 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
177 // retains ownership of itself, and deletes itself once finished.
178 static BrowsingDataRemover
* CreateForUnboundedRange(Profile
* profile
);
180 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
181 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
182 // deletes itself once finished.
183 static BrowsingDataRemover
* CreateForRange(Profile
* profile
,
184 base::Time delete_begin
,
185 base::Time delete_end
);
187 // Creates a BrowsingDataRemover bound to a specific period of time (as
188 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
189 // retains ownership of itself, and deletes itself once finished.
190 static BrowsingDataRemover
* CreateForPeriod(Profile
* profile
,
193 // Calculate the begin time for the deletion range specified by |time_period|.
194 static base::Time
CalculateBeginDeleteTime(TimePeriod time_period
);
196 // Is the BrowsingDataRemover currently in the process of removing data?
197 static bool is_removing() { return is_removing_
; }
199 // Sets a CompletionInhibitor, which will be notified each time an instance is
200 // about to complete a browsing data removal process, and will be able to
201 // artificially delay the completion.
202 static void set_completion_inhibitor_for_testing(
203 CompletionInhibitor
* inhibitor
) {
204 completion_inhibitor_
= inhibitor
;
207 // Add a callback to the list of callbacks to be called during a browsing data
208 // removal event. Returns a subscription object that can be used to
209 // un-register the callback.
210 static CallbackSubscription
RegisterOnBrowsingDataRemovedCallback(
211 const Callback
& callback
);
213 // Removes the specified items related to browsing for all origins that match
214 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
215 void Remove(int remove_mask
, int origin_set_mask
);
217 void AddObserver(Observer
* observer
);
218 void RemoveObserver(Observer
* observer
);
220 // Called when history deletion is done.
221 void OnHistoryDeletionDone();
224 void OverrideStoragePartitionForTesting(
225 content::StoragePartition
* storage_partition
);
228 // The clear API needs to be able to toggle removing_ in order to test that
229 // only one BrowsingDataRemover instance can be called at a time.
230 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest
, OneAtATime
);
232 // The BrowsingDataRemover tests need to be able to access the implementation
233 // of Remove(), as it exposes details that aren't yet available in the public
234 // API. As soon as those details are exposed via new methods, this should be
237 // TODO(mkwst): See http://crbug.com/113621
238 friend class BrowsingDataRemoverTest
;
249 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
250 // not already removing, and vice-versa.
251 static void set_removing(bool is_removing
);
253 // Creates a BrowsingDataRemover to remove browser data from the specified
254 // profile in the specified time range. Use Remove to initiate the removal.
255 BrowsingDataRemover(Profile
* profile
,
256 base::Time delete_begin
,
257 base::Time delete_end
);
259 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
260 // to be deleted by other objects so make destructor private and DeleteHelper
262 friend class base::DeleteHelper
<BrowsingDataRemover
>;
264 // When plugins aren't enabled, there is no base class, so adding an override
265 // specifier would result in a compile error.
266 #if defined(ENABLE_PLUGINS)
267 ~BrowsingDataRemover() override
;
269 ~BrowsingDataRemover();
272 // Callback for when TemplateURLService has finished loading. Clears the data,
273 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
274 void OnKeywordsLoaded();
276 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
277 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
279 #if defined(ENABLE_PLUGINS)
280 // PepperFlashSettingsManager::Client implementation.
281 void OnDeauthorizeContentLicensesCompleted(uint32 request_id
,
282 bool success
) override
;
285 #if defined (OS_CHROMEOS)
286 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status
,
290 // Removes the specified items related to browsing for a specific host. If the
291 // provided |origin| is empty, data is removed for all origins. The
292 // |origin_set_mask| parameter defines the set of origins from which data
293 // should be removed (protected, unprotected, or both).
294 void RemoveImpl(int remove_mask
,
296 int origin_set_mask
);
298 // Notifies observers and deletes this object.
299 void NotifyAndDelete();
301 // Checks if we are all done, and if so, calls NotifyAndDelete().
302 void NotifyAndDeleteIfDone();
304 // Callback for when the hostname resolution cache has been cleared.
305 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
306 void OnClearedHostnameResolutionCache();
308 // Invoked on the IO thread to clear the hostname resolution cache.
309 void ClearHostnameResolutionCacheOnIOThread(IOThread
* io_thread
);
311 // Callback for when the LoggedIn Predictor has been cleared.
312 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
313 void OnClearedLoggedInPredictor();
315 // Clears the LoggedIn Predictor.
316 void ClearLoggedInPredictor();
318 // Callback for when speculative data in the network Predictor has been
319 // cleared. Clears the respective waiting flag and invokes
320 // NotifyAndDeleteIfDone.
321 void OnClearedNetworkPredictor();
323 // Invoked on the IO thread to clear speculative data related to hostname
324 // pre-resolution from the network Predictor.
325 void ClearNetworkPredictorOnIOThread(
326 chrome_browser_net::Predictor
* predictor
);
328 // Callback for when network related data in ProfileIOData has been cleared.
329 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
330 void OnClearedNetworkingHistory();
332 // Callback for when the cache has been deleted. Invokes
333 // NotifyAndDeleteIfDone.
336 // Invoked on the IO thread to delete from the cache.
337 void ClearCacheOnIOThread();
339 // Performs the actual work to delete the cache.
340 void DoClearCache(int rv
);
342 #if !defined(DISABLE_NACL)
343 // Callback for when the NaCl cache has been deleted. Invokes
344 // NotifyAndDeleteIfDone.
345 void ClearedNaClCache();
347 // Invokes the ClearedNaClCache on the UI thread.
348 void ClearedNaClCacheOnIOThread();
350 // Invoked on the IO thread to delete the NaCl cache.
351 void ClearNaClCacheOnIOThread();
353 // Callback for when the PNaCl translation cache has been deleted. Invokes
354 // NotifyAndDeleteIfDone.
355 void ClearedPnaclCache();
357 // Invokes ClearedPnaclCacheOn on the UI thread.
358 void ClearedPnaclCacheOnIOThread();
360 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
361 void ClearPnaclCacheOnIOThread(base::Time begin
, base::Time end
);
364 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
365 void OnClearedCookies(int num_deleted
);
367 // Invoked on the IO thread to delete cookies.
368 void ClearCookiesOnIOThread(net::URLRequestContextGetter
* rq_context
);
370 // Invoked on the IO thread to delete channel IDs.
371 void ClearChannelIDsOnIOThread(
372 net::URLRequestContextGetter
* rq_context
);
374 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
375 // connection pool and posts to UI thread to run OnClearedChannelIDs.
376 void OnClearedChannelIDsOnIOThread(
377 net::URLRequestContextGetter
* rq_context
);
379 // Callback for when channel IDs have been deleted. Invokes
380 // NotifyAndDeleteIfDone.
381 void OnClearedChannelIDs();
383 // Callback from the above method.
384 void OnClearedFormData();
386 // Callback for when the Autofill profile and credit card origin URLs have
388 void OnClearedAutofillOriginURLs();
390 // Callback on UI thread when the storage partition related data are cleared.
391 void OnClearedStoragePartitionData();
393 #if defined(ENABLE_WEBRTC)
394 // Callback on UI thread when the WebRTC logs have been deleted.
395 void OnClearedWebRtcLogs();
398 void OnClearedDomainReliabilityMonitor();
400 // Returns true if we're all done.
403 // Profile we're to remove from.
406 // Start time to delete from.
407 const base::Time delete_begin_
;
409 // End time to delete to.
410 base::Time delete_end_
;
412 // True if Remove has been invoked.
413 static bool is_removing_
;
415 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
416 // is about to complete a browsing data removal process, and has the ability
417 // to artificially delay completion. Used for testing.
418 static CompletionInhibitor
* completion_inhibitor_
;
420 CacheState next_cache_state_
;
421 disk_cache::Backend
* cache_
;
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_
;
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_
;
440 bool waiting_for_clear_cache_
;
441 bool waiting_for_clear_channel_ids_
;
442 bool waiting_for_clear_content_licenses_
;
443 // Non-zero if waiting for cookies to be cleared.
444 int waiting_for_clear_cookies_count_
;
445 bool waiting_for_clear_domain_reliability_monitor_
;
446 bool waiting_for_clear_form_
;
447 bool waiting_for_clear_history_
;
448 bool waiting_for_clear_hostname_resolution_cache_
;
449 bool waiting_for_clear_keyword_data_
;
450 bool waiting_for_clear_logged_in_predictor_
;
451 bool waiting_for_clear_nacl_cache_
;
452 bool waiting_for_clear_network_predictor_
;
453 bool waiting_for_clear_networking_history_
;
454 bool waiting_for_clear_platform_keys_
;
455 bool waiting_for_clear_plugin_data_
;
456 bool waiting_for_clear_pnacl_cache_
;
457 bool waiting_for_clear_storage_partition_data_
;
458 #if defined(ENABLE_WEBRTC)
459 bool waiting_for_clear_webrtc_logs_
;
462 // The removal mask for the current removal operation.
465 // The origin for the current removal operation.
468 // From which types of origins should we remove data?
469 int origin_set_mask_
;
471 ObserverList
<Observer
> observer_list_
;
473 // Used if we need to clear history.
474 base::CancelableTaskTracker history_task_tracker_
;
476 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
478 // We do not own this.
479 content::StoragePartition
* storage_partition_for_testing_
;
481 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover
);
484 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_