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"
29 namespace chrome_browser_net
{
34 class PluginDataRemover
;
35 class StoragePartition
;
39 class URLRequestContextGetter
;
42 // BrowsingDataRemover is responsible for removing data related to browsing:
43 // visits in url database, downloads, cookies ...
45 class BrowsingDataRemover
46 #if defined(ENABLE_PLUGINS)
47 : public PepperFlashSettingsManager::Client
51 // Time period ranges available when doing browsing data removals.
60 // Mask used for Remove.
62 REMOVE_APPCACHE
= 1 << 0,
63 REMOVE_CACHE
= 1 << 1,
64 REMOVE_COOKIES
= 1 << 2,
65 REMOVE_DOWNLOADS
= 1 << 3,
66 REMOVE_FILE_SYSTEMS
= 1 << 4,
67 REMOVE_FORM_DATA
= 1 << 5,
68 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
69 REMOVE_HISTORY
= 1 << 6,
70 REMOVE_INDEXEDDB
= 1 << 7,
71 REMOVE_LOCAL_STORAGE
= 1 << 8,
72 REMOVE_PLUGIN_DATA
= 1 << 9,
73 REMOVE_PASSWORDS
= 1 << 10,
74 REMOVE_WEBSQL
= 1 << 11,
75 REMOVE_CHANNEL_IDS
= 1 << 12,
76 REMOVE_CONTENT_LICENSES
= 1 << 13,
77 REMOVE_SERVICE_WORKERS
= 1 << 14,
78 REMOVE_SITE_USAGE_DATA
= 1 << 15,
79 // REMOVE_NOCHECKS intentionally does not check if the Profile's prohibited
80 // from deleting history or downloads.
81 REMOVE_NOCHECKS
= 1 << 16,
82 REMOVE_WEBRTC_IDENTITY
= 1 << 17,
83 // The following flag is used only in tests. In normal usage, hosted app
84 // data is controlled by the REMOVE_COOKIES flag, applied to the
85 // protected-web origin.
86 REMOVE_HOSTED_APP_DATA_TESTONLY
= 1 << 31,
88 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
89 // storage, webSQL, service workers, and plugin data.
90 REMOVE_SITE_DATA
= REMOVE_APPCACHE
| REMOVE_COOKIES
| REMOVE_FILE_SYSTEMS
|
92 REMOVE_LOCAL_STORAGE
|
94 REMOVE_SERVICE_WORKERS
|
97 REMOVE_SITE_USAGE_DATA
|
98 REMOVE_WEBRTC_IDENTITY
,
100 // Includes all the available remove options. Meant to be used by clients
101 // that wish to wipe as much data as possible from a Profile, to make it
102 // look like a new Profile.
103 REMOVE_ALL
= REMOVE_SITE_DATA
| REMOVE_CACHE
| REMOVE_DOWNLOADS
|
107 REMOVE_CONTENT_LICENSES
,
109 // Includes all available remove options. Meant to be used when the Profile
110 // is scheduled to be deleted, and all possible data should be wiped from
111 // disk as soon as possible.
112 REMOVE_WIPE_PROFILE
= REMOVE_ALL
| REMOVE_NOCHECKS
,
115 // When BrowsingDataRemover successfully removes data, a notification of type
116 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
118 struct NotificationDetails
{
119 NotificationDetails();
120 NotificationDetails(const NotificationDetails
& details
);
121 NotificationDetails(base::Time removal_begin
,
123 int origin_type_mask
);
124 ~NotificationDetails();
126 // The beginning of the removal time range.
127 base::Time removal_begin
;
129 // The removal mask (see the RemoveDataMask enum for details).
132 // The origin type mask (see BrowsingDataHelper::OriginTypeMask for
134 int origin_type_mask
;
137 // Observer is notified when the removal is done. Done means keywords have
138 // been deleted, cache cleared and all other tasks scheduled.
141 virtual void OnBrowsingDataRemoverDone() = 0;
144 virtual ~Observer() {}
147 using Callback
= base::Callback
<void(const NotificationDetails
&)>;
148 using CallbackSubscription
= scoped_ptr
<
149 base::CallbackList
<void(const NotificationDetails
&)>::Subscription
>;
151 // The completion inhibitor can artificially delay completion of the browsing
152 // data removal process. It is used during testing to simulate scenarios in
153 // which the deletion stalls or takes a very long time.
154 class CompletionInhibitor
{
156 // Invoked when a |remover| is just about to complete clearing browser data,
157 // and will be prevented from completing until after the callback
158 // |continue_to_completion| is run.
159 virtual void OnBrowsingDataRemoverWouldComplete(
160 BrowsingDataRemover
* remover
,
161 const base::Closure
& continue_to_completion
) = 0;
164 virtual ~CompletionInhibitor() {}
167 // Creates a BrowsingDataRemover object that removes data regardless of the
168 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
169 // retains ownership of itself, and deletes itself once finished.
170 static BrowsingDataRemover
* CreateForUnboundedRange(Profile
* profile
);
172 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
173 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
174 // deletes itself once finished.
175 static BrowsingDataRemover
* CreateForRange(Profile
* profile
,
176 base::Time delete_begin
,
177 base::Time delete_end
);
179 // Creates a BrowsingDataRemover bound to a specific period of time (as
180 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
181 // retains ownership of itself, and deletes itself once finished.
182 static BrowsingDataRemover
* CreateForPeriod(Profile
* profile
,
185 // Calculate the begin time for the deletion range specified by |time_period|.
186 static base::Time
CalculateBeginDeleteTime(TimePeriod time_period
);
188 // Is the BrowsingDataRemover currently in the process of removing data?
189 static bool is_removing() { return is_removing_
; }
191 // Sets a CompletionInhibitor, which will be notified each time an instance is
192 // about to complete a browsing data removal process, and will be able to
193 // artificially delay the completion.
194 static void set_completion_inhibitor_for_testing(
195 CompletionInhibitor
* inhibitor
) {
196 completion_inhibitor_
= inhibitor
;
199 // Add a callback to the list of callbacks to be called during a browsing data
200 // removal event. Returns a subscription object that can be used to
201 // un-register the callback.
202 static CallbackSubscription
RegisterOnBrowsingDataRemovedCallback(
203 const Callback
& callback
);
205 // Removes the specified items related to browsing for all origins that match
206 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask).
207 void Remove(int remove_mask
, int origin_type_mask
);
209 void AddObserver(Observer
* observer
);
210 void RemoveObserver(Observer
* observer
);
212 // Called when history deletion is done.
213 void OnHistoryDeletionDone();
216 void OverrideStoragePartitionForTesting(
217 content::StoragePartition
* storage_partition
);
220 // The clear API needs to be able to toggle removing_ in order to test that
221 // only one BrowsingDataRemover instance can be called at a time.
222 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest
, OneAtATime
);
224 // The BrowsingDataRemover tests need to be able to access the implementation
225 // of Remove(), as it exposes details that aren't yet available in the public
226 // API. As soon as those details are exposed via new methods, this should be
229 // TODO(mkwst): See http://crbug.com/113621
230 friend class BrowsingDataRemoverTest
;
232 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
233 // not already removing, and vice-versa.
234 static void set_removing(bool is_removing
);
236 // Creates a BrowsingDataRemover to remove browser data from the specified
237 // profile in the specified time range. Use Remove to initiate the removal.
238 BrowsingDataRemover(Profile
* profile
,
239 base::Time delete_begin
,
240 base::Time delete_end
);
242 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
243 // to be deleted by other objects so make destructor private and DeleteHelper
245 friend class base::DeleteHelper
<BrowsingDataRemover
>;
247 // When plugins aren't enabled, there is no base class, so adding an override
248 // specifier would result in a compile error.
249 #if defined(ENABLE_PLUGINS)
250 ~BrowsingDataRemover() override
;
252 ~BrowsingDataRemover();
255 // Callback for when TemplateURLService has finished loading. Clears the data,
256 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
257 void OnKeywordsLoaded();
259 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
260 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
262 #if defined(ENABLE_PLUGINS)
263 // PepperFlashSettingsManager::Client implementation.
264 void OnDeauthorizeContentLicensesCompleted(uint32 request_id
,
265 bool success
) override
;
268 #if defined (OS_CHROMEOS)
269 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status
,
273 // Removes the specified items related to browsing for a specific host. If the
274 // provided |origin| is empty, data is removed for all origins. The
275 // |origin_type_mask| parameter defines the set of origins from which data
276 // should be removed (protected, unprotected, or both).
277 void RemoveImpl(int remove_mask
,
279 int origin_type_mask
);
281 // Notifies observers and deletes this object.
282 void NotifyAndDelete();
284 // Checks if we are all done, and if so, calls NotifyAndDelete().
285 void NotifyAndDeleteIfDone();
287 // Callback for when the hostname resolution cache has been cleared.
288 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
289 void OnClearedHostnameResolutionCache();
291 // Invoked on the IO thread to clear the hostname resolution cache.
292 void ClearHostnameResolutionCacheOnIOThread(IOThread
* io_thread
);
294 // Callback for when speculative data in the network Predictor has been
295 // cleared. Clears the respective waiting flag and invokes
296 // NotifyAndDeleteIfDone.
297 void OnClearedNetworkPredictor();
299 // Invoked on the IO thread to clear speculative data related to hostname
300 // pre-resolution from the network Predictor.
301 void ClearNetworkPredictorOnIOThread(
302 chrome_browser_net::Predictor
* predictor
);
304 // Callback for when network related data in ProfileIOData has been cleared.
305 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
306 void OnClearedNetworkingHistory();
308 // Callback for when the cache has been deleted. Invokes
309 // NotifyAndDeleteIfDone.
311 #if !defined(DISABLE_NACL)
312 // Callback for when the NaCl cache has been deleted. Invokes
313 // NotifyAndDeleteIfDone.
314 void ClearedNaClCache();
316 // Invokes the ClearedNaClCache on the UI thread.
317 void ClearedNaClCacheOnIOThread();
319 // Invoked on the IO thread to delete the NaCl cache.
320 void ClearNaClCacheOnIOThread();
322 // Callback for when the PNaCl translation cache has been deleted. Invokes
323 // NotifyAndDeleteIfDone.
324 void ClearedPnaclCache();
326 // Invokes ClearedPnaclCacheOn on the UI thread.
327 void ClearedPnaclCacheOnIOThread();
329 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
330 void ClearPnaclCacheOnIOThread(base::Time begin
, base::Time end
);
333 // Callback for when passwords for the requested time range have been cleared.
334 void OnClearedPasswords();
336 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
337 void OnClearedCookies(int num_deleted
);
339 // Invoked on the IO thread to delete cookies.
340 void ClearCookiesOnIOThread(net::URLRequestContextGetter
* rq_context
);
342 // Invoked on the IO thread to delete channel IDs.
343 void ClearChannelIDsOnIOThread(
344 net::URLRequestContextGetter
* rq_context
);
346 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
347 // connection pool and posts to UI thread to run OnClearedChannelIDs.
348 void OnClearedChannelIDsOnIOThread(
349 net::URLRequestContextGetter
* rq_context
);
351 // Callback for when channel IDs have been deleted. Invokes
352 // NotifyAndDeleteIfDone.
353 void OnClearedChannelIDs();
355 // Callback from the above method.
356 void OnClearedFormData();
358 // Callback for when the Autofill profile and credit card origin URLs have
360 void OnClearedAutofillOriginURLs();
362 // Callback on UI thread when the storage partition related data are cleared.
363 void OnClearedStoragePartitionData();
365 #if defined(ENABLE_WEBRTC)
366 // Callback on UI thread when the WebRTC logs have been deleted.
367 void OnClearedWebRtcLogs();
370 #if defined(OS_ANDROID)
371 // Callback on UI thread when the precache history has been cleared.
372 void OnClearedPrecacheHistory();
375 void OnClearedDomainReliabilityMonitor();
377 // Returns true if we're all done.
380 // Profile we're to remove from.
383 // Start time to delete from.
384 const base::Time delete_begin_
;
386 // End time to delete to.
387 base::Time delete_end_
;
389 // True if Remove has been invoked.
390 static bool is_removing_
;
392 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
393 // is about to complete a browsing data removal process, and has the ability
394 // to artificially delay completion. Used for testing.
395 static CompletionInhibitor
* completion_inhibitor_
;
397 // Used to delete data from HTTP cache.
398 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
399 scoped_refptr
<net::URLRequestContextGetter
> media_context_getter_
;
401 #if defined(ENABLE_PLUGINS)
402 // Used to delete plugin data.
403 scoped_ptr
<content::PluginDataRemover
> plugin_data_remover_
;
404 base::WaitableEventWatcher watcher_
;
406 // Used to deauthorize content licenses for Pepper Flash.
407 scoped_ptr
<PepperFlashSettingsManager
> pepper_flash_settings_manager_
;
410 uint32 deauthorize_content_licenses_request_id_
;
411 // True if we're waiting for various data to be deleted.
412 // These may only be accessed from UI thread in order to avoid races!
413 bool waiting_for_clear_autofill_origin_urls_
;
414 bool waiting_for_clear_cache_
;
415 bool waiting_for_clear_channel_ids_
;
416 bool waiting_for_clear_content_licenses_
;
417 // Non-zero if waiting for cookies to be cleared.
418 int waiting_for_clear_cookies_count_
;
419 bool waiting_for_clear_domain_reliability_monitor_
;
420 bool waiting_for_clear_form_
;
421 bool waiting_for_clear_history_
;
422 bool waiting_for_clear_hostname_resolution_cache_
;
423 bool waiting_for_clear_keyword_data_
;
424 bool waiting_for_clear_nacl_cache_
;
425 bool waiting_for_clear_network_predictor_
;
426 bool waiting_for_clear_networking_history_
;
427 bool waiting_for_clear_passwords_
;
428 bool waiting_for_clear_platform_keys_
;
429 bool waiting_for_clear_plugin_data_
;
430 bool waiting_for_clear_pnacl_cache_
;
431 #if defined(OS_ANDROID)
432 bool waiting_for_clear_precache_history_
;
434 bool waiting_for_clear_storage_partition_data_
;
435 #if defined(ENABLE_WEBRTC)
436 bool waiting_for_clear_webrtc_logs_
;
439 // The removal mask for the current removal operation.
442 // The origin for the current removal operation.
445 // From which types of origins should we remove data?
446 int origin_type_mask_
;
448 base::ObserverList
<Observer
> observer_list_
;
450 // Used if we need to clear history.
451 base::CancelableTaskTracker history_task_tracker_
;
453 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
455 // We do not own this.
456 content::StoragePartition
* storage_partition_for_testing_
;
458 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover
);
461 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_