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 // The following flag is used only in tests. In normal usage, hosted app
85 // data is controlled by the REMOVE_COOKIES flag, applied to the
86 // protected-web origin.
87 REMOVE_HOSTED_APP_DATA_TESTONLY
= 1 << 31,
89 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
90 // storage, webSQL, service workers, and plugin data.
91 REMOVE_SITE_DATA
= REMOVE_APPCACHE
| REMOVE_COOKIES
| REMOVE_FILE_SYSTEMS
|
93 REMOVE_LOCAL_STORAGE
|
95 REMOVE_SERVICE_WORKERS
|
98 REMOVE_SITE_USAGE_DATA
|
99 REMOVE_WEBRTC_IDENTITY
,
101 // Includes all the available remove options. Meant to be used by clients
102 // that wish to wipe as much data as possible from a Profile, to make it
103 // look like a new Profile.
104 REMOVE_ALL
= REMOVE_SITE_DATA
| REMOVE_CACHE
| REMOVE_DOWNLOADS
|
108 REMOVE_CONTENT_LICENSES
,
110 // Includes all available remove options. Meant to be used when the Profile
111 // is scheduled to be deleted, and all possible data should be wiped from
112 // disk as soon as possible.
113 REMOVE_WIPE_PROFILE
= REMOVE_ALL
| REMOVE_NOCHECKS
,
116 // When BrowsingDataRemover successfully removes data, a notification of type
117 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
119 struct NotificationDetails
{
120 NotificationDetails();
121 NotificationDetails(const NotificationDetails
& details
);
122 NotificationDetails(base::Time removal_begin
,
124 int origin_type_mask
);
125 ~NotificationDetails();
127 // The beginning of the removal time range.
128 base::Time removal_begin
;
130 // The removal mask (see the RemoveDataMask enum for details).
133 // The origin type mask (see BrowsingDataHelper::OriginTypeMask for
135 int origin_type_mask
;
138 // Observer is notified when the removal is done. Done means keywords have
139 // been deleted, cache cleared and all other tasks scheduled.
142 virtual void OnBrowsingDataRemoverDone() = 0;
145 virtual ~Observer() {}
148 using Callback
= base::Callback
<void(const NotificationDetails
&)>;
149 using CallbackSubscription
= scoped_ptr
<
150 base::CallbackList
<void(const NotificationDetails
&)>::Subscription
>;
152 // The completion inhibitor can artificially delay completion of the browsing
153 // data removal process. It is used during testing to simulate scenarios in
154 // which the deletion stalls or takes a very long time.
155 class CompletionInhibitor
{
157 // Invoked when a |remover| is just about to complete clearing browser data,
158 // and will be prevented from completing until after the callback
159 // |continue_to_completion| is run.
160 virtual void OnBrowsingDataRemoverWouldComplete(
161 BrowsingDataRemover
* remover
,
162 const base::Closure
& continue_to_completion
) = 0;
165 virtual ~CompletionInhibitor() {}
168 // Creates a BrowsingDataRemover object that removes data regardless of the
169 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
170 // retains ownership of itself, and deletes itself once finished.
171 static BrowsingDataRemover
* CreateForUnboundedRange(Profile
* profile
);
173 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
174 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
175 // deletes itself once finished.
176 static BrowsingDataRemover
* CreateForRange(Profile
* profile
,
177 base::Time delete_begin
,
178 base::Time delete_end
);
180 // Creates a BrowsingDataRemover bound to a specific period of time (as
181 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
182 // retains ownership of itself, and deletes itself once finished.
183 static BrowsingDataRemover
* CreateForPeriod(Profile
* profile
,
186 // Calculate the begin time for the deletion range specified by |time_period|.
187 static base::Time
CalculateBeginDeleteTime(TimePeriod time_period
);
189 // Is the BrowsingDataRemover currently in the process of removing data?
190 static bool is_removing() { return is_removing_
; }
192 // Sets a CompletionInhibitor, which will be notified each time an instance is
193 // about to complete a browsing data removal process, and will be able to
194 // artificially delay the completion.
195 static void set_completion_inhibitor_for_testing(
196 CompletionInhibitor
* inhibitor
) {
197 completion_inhibitor_
= inhibitor
;
200 // Add a callback to the list of callbacks to be called during a browsing data
201 // removal event. Returns a subscription object that can be used to
202 // un-register the callback.
203 static CallbackSubscription
RegisterOnBrowsingDataRemovedCallback(
204 const Callback
& callback
);
206 // Removes the specified items related to browsing for all origins that match
207 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask).
208 void Remove(int remove_mask
, int origin_type_mask
);
210 void AddObserver(Observer
* observer
);
211 void RemoveObserver(Observer
* observer
);
213 // Called when history deletion is done.
214 void OnHistoryDeletionDone();
217 void OverrideStoragePartitionForTesting(
218 content::StoragePartition
* storage_partition
);
221 // The clear API needs to be able to toggle removing_ in order to test that
222 // only one BrowsingDataRemover instance can be called at a time.
223 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest
, OneAtATime
);
225 // The BrowsingDataRemover tests need to be able to access the implementation
226 // of Remove(), as it exposes details that aren't yet available in the public
227 // API. As soon as those details are exposed via new methods, this should be
230 // TODO(mkwst): See http://crbug.com/113621
231 friend class BrowsingDataRemoverTest
;
233 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
234 // not already removing, and vice-versa.
235 static void set_removing(bool is_removing
);
237 // Creates a BrowsingDataRemover to remove browser data from the specified
238 // profile in the specified time range. Use Remove to initiate the removal.
239 BrowsingDataRemover(Profile
* profile
,
240 base::Time delete_begin
,
241 base::Time delete_end
);
243 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
244 // to be deleted by other objects so make destructor private and DeleteHelper
246 friend class base::DeleteHelper
<BrowsingDataRemover
>;
248 // When plugins aren't enabled, there is no base class, so adding an override
249 // specifier would result in a compile error.
250 #if defined(ENABLE_PLUGINS)
251 ~BrowsingDataRemover() override
;
253 ~BrowsingDataRemover();
256 // Callback for when TemplateURLService has finished loading. Clears the data,
257 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
258 void OnKeywordsLoaded();
260 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
261 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
263 #if defined(ENABLE_PLUGINS)
264 // PepperFlashSettingsManager::Client implementation.
265 void OnDeauthorizeContentLicensesCompleted(uint32 request_id
,
266 bool success
) override
;
269 #if defined (OS_CHROMEOS)
270 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status
,
274 // Removes the specified items related to browsing for a specific host. If the
275 // provided |remove_url| is empty, data is removed for all origins. The
276 // |origin_type_mask| parameter defines the set of origins from which data
277 // should be removed (protected, unprotected, or both).
278 // TODO(mkwst): The current implementation relies on unique (empty) origins to
279 // signal removal of all origins. Reconsider this behavior if/when we build
280 // a "forget this site" feature.
281 void RemoveImpl(int remove_mask
,
282 const GURL
& remove_url
,
283 int origin_type_mask
);
285 // Notifies observers and deletes this object.
286 void NotifyAndDelete();
288 // Checks if we are all done, and if so, calls NotifyAndDelete().
289 void NotifyAndDeleteIfDone();
291 // Callback for when the hostname resolution cache has been cleared.
292 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
293 void OnClearedHostnameResolutionCache();
295 // Invoked on the IO thread to clear the hostname resolution cache.
296 void ClearHostnameResolutionCacheOnIOThread(IOThread
* io_thread
);
298 // Callback for when speculative data in the network Predictor has been
299 // cleared. Clears the respective waiting flag and invokes
300 // NotifyAndDeleteIfDone.
301 void OnClearedNetworkPredictor();
303 // Invoked on the IO thread to clear speculative data related to hostname
304 // pre-resolution from the network Predictor.
305 void ClearNetworkPredictorOnIOThread(
306 chrome_browser_net::Predictor
* predictor
);
308 // Callback for when network related data in ProfileIOData has been cleared.
309 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
310 void OnClearedNetworkingHistory();
312 // Callback for when the cache has been deleted. Invokes
313 // NotifyAndDeleteIfDone.
315 #if !defined(DISABLE_NACL)
316 // Callback for when the NaCl cache has been deleted. Invokes
317 // NotifyAndDeleteIfDone.
318 void ClearedNaClCache();
320 // Invokes the ClearedNaClCache on the UI thread.
321 void ClearedNaClCacheOnIOThread();
323 // Invoked on the IO thread to delete the NaCl cache.
324 void ClearNaClCacheOnIOThread();
326 // Callback for when the PNaCl translation cache has been deleted. Invokes
327 // NotifyAndDeleteIfDone.
328 void ClearedPnaclCache();
330 // Invokes ClearedPnaclCacheOn on the UI thread.
331 void ClearedPnaclCacheOnIOThread();
333 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
334 void ClearPnaclCacheOnIOThread(base::Time begin
, base::Time end
);
337 // Callback for when passwords for the requested time range have been cleared.
338 void OnClearedPasswords();
340 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
341 void OnClearedCookies(int num_deleted
);
343 // Invoked on the IO thread to delete cookies.
344 void ClearCookiesOnIOThread(net::URLRequestContextGetter
* rq_context
);
346 // Invoked on the IO thread to delete channel IDs.
347 void ClearChannelIDsOnIOThread(
348 net::URLRequestContextGetter
* rq_context
);
350 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
351 // connection pool and posts to UI thread to run OnClearedChannelIDs.
352 void OnClearedChannelIDsOnIOThread(
353 net::URLRequestContextGetter
* rq_context
);
355 // Callback for when channel IDs have been deleted. Invokes
356 // NotifyAndDeleteIfDone.
357 void OnClearedChannelIDs();
359 // Callback from the above method.
360 void OnClearedFormData();
362 // Callback for when the Autofill profile and credit card origin URLs have
364 void OnClearedAutofillOriginURLs();
366 // Callback on UI thread when the storage partition related data are cleared.
367 void OnClearedStoragePartitionData();
369 #if defined(ENABLE_WEBRTC)
370 // Callback on UI thread when the WebRTC logs have been deleted.
371 void OnClearedWebRtcLogs();
374 #if defined(OS_ANDROID)
375 // Callback on UI thread when the precache history has been cleared.
376 void OnClearedPrecacheHistory();
379 void OnClearedDomainReliabilityMonitor();
381 // Returns true if we're all done.
384 // Profile we're to remove from.
387 // Start time to delete from.
388 const base::Time delete_begin_
;
390 // End time to delete to.
391 base::Time delete_end_
;
393 // True if Remove has been invoked.
394 static bool is_removing_
;
396 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
397 // is about to complete a browsing data removal process, and has the ability
398 // to artificially delay completion. Used for testing.
399 static CompletionInhibitor
* completion_inhibitor_
;
401 // Used to delete data from HTTP cache.
402 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
403 scoped_refptr
<net::URLRequestContextGetter
> media_context_getter_
;
405 #if defined(ENABLE_PLUGINS)
406 // Used to delete plugin data.
407 scoped_ptr
<content::PluginDataRemover
> plugin_data_remover_
;
408 base::WaitableEventWatcher watcher_
;
410 // Used to deauthorize content licenses for Pepper Flash.
411 scoped_ptr
<PepperFlashSettingsManager
> pepper_flash_settings_manager_
;
414 uint32 deauthorize_content_licenses_request_id_
= 0;
415 // True if we're waiting for various data to be deleted.
416 // These may only be accessed from UI thread in order to avoid races!
417 bool waiting_for_clear_autofill_origin_urls_
= false;
418 bool waiting_for_clear_cache_
= false;
419 bool waiting_for_clear_channel_ids_
= false;
420 bool waiting_for_clear_content_licenses_
= false;
421 // Non-zero if waiting for cookies to be cleared.
422 int waiting_for_clear_cookies_count_
= 0;
423 bool waiting_for_clear_domain_reliability_monitor_
= false;
424 bool waiting_for_clear_form_
= false;
425 bool waiting_for_clear_history_
= false;
426 bool waiting_for_clear_hostname_resolution_cache_
= false;
427 bool waiting_for_clear_keyword_data_
= false;
428 bool waiting_for_clear_nacl_cache_
= false;
429 bool waiting_for_clear_network_predictor_
= false;
430 bool waiting_for_clear_networking_history_
= false;
431 bool waiting_for_clear_passwords_
= false;
432 bool waiting_for_clear_platform_keys_
= false;
433 bool waiting_for_clear_plugin_data_
= false;
434 bool waiting_for_clear_pnacl_cache_
= false;
435 #if defined(OS_ANDROID)
436 bool waiting_for_clear_precache_history_
= false;
438 bool waiting_for_clear_storage_partition_data_
= false;
439 #if defined(ENABLE_WEBRTC)
440 bool waiting_for_clear_webrtc_logs_
= false;
443 // The removal mask for the current removal operation.
444 int remove_mask_
= 0;
446 // From which types of origins should we remove data?
447 int origin_type_mask_
= 0;
449 base::ObserverList
<Observer
> observer_list_
;
451 // Used if we need to clear history.
452 base::CancelableTaskTracker history_task_tracker_
;
454 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
456 // We do not own this.
457 content::StoragePartition
* storage_partition_for_testing_
= nullptr;
459 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover
);
462 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_