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/time/time.h"
17 #include "chrome/browser/pepper_flash_settings_manager.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/common/cancelable_task_tracker.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/dbus/dbus_method_call_status.h"
24 #include "webkit/common/quota/quota_types.h"
26 class ExtensionSpecialStoragePolicy
;
31 class PluginDataRemover
;
32 class StoragePartition
;
35 namespace disk_cache
{
40 class URLRequestContextGetter
;
48 class DOMStorageContext
;
49 struct LocalStorageUsageInfo
;
50 struct SessionStorageUsageInfo
;
53 // BrowsingDataRemover is responsible for removing data related to browsing:
54 // visits in url database, downloads, cookies ...
56 class BrowsingDataRemover
57 #if defined(ENABLE_PLUGINS)
58 : public PepperFlashSettingsManager::Client
62 // Time period ranges available when doing browsing data removals.
71 // Mask used for Remove.
73 REMOVE_APPCACHE
= 1 << 0,
74 REMOVE_CACHE
= 1 << 1,
75 REMOVE_COOKIES
= 1 << 2,
76 REMOVE_DOWNLOADS
= 1 << 3,
77 REMOVE_FILE_SYSTEMS
= 1 << 4,
78 REMOVE_FORM_DATA
= 1 << 5,
79 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
80 REMOVE_HISTORY
= 1 << 6,
81 REMOVE_INDEXEDDB
= 1 << 7,
82 REMOVE_LOCAL_STORAGE
= 1 << 8,
83 REMOVE_PLUGIN_DATA
= 1 << 9,
84 REMOVE_PASSWORDS
= 1 << 10,
85 REMOVE_WEBSQL
= 1 << 11,
86 REMOVE_SERVER_BOUND_CERTS
= 1 << 12,
87 REMOVE_CONTENT_LICENSES
= 1 << 13,
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, and plugin data.
95 REMOVE_SITE_DATA
= REMOVE_APPCACHE
| REMOVE_COOKIES
| REMOVE_FILE_SYSTEMS
|
96 REMOVE_INDEXEDDB
| REMOVE_LOCAL_STORAGE
|
97 REMOVE_PLUGIN_DATA
| REMOVE_WEBSQL
|
98 REMOVE_SERVER_BOUND_CERTS
,
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_APPCACHE
| REMOVE_CACHE
| REMOVE_COOKIES
|
104 REMOVE_DOWNLOADS
| REMOVE_FILE_SYSTEMS
| REMOVE_FORM_DATA
|
105 REMOVE_HISTORY
| REMOVE_INDEXEDDB
| REMOVE_LOCAL_STORAGE
|
106 REMOVE_PLUGIN_DATA
| REMOVE_PASSWORDS
| REMOVE_WEBSQL
|
107 REMOVE_SERVER_BOUND_CERTS
| REMOVE_CONTENT_LICENSES
,
110 // When BrowsingDataRemover successfully removes data, a notification of type
111 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
113 struct NotificationDetails
{
114 NotificationDetails();
115 NotificationDetails(const NotificationDetails
& details
);
116 NotificationDetails(base::Time removal_begin
,
118 int origin_set_mask
);
119 ~NotificationDetails();
121 // The beginning of the removal time range.
122 base::Time removal_begin
;
124 // The removal mask (see the RemoveDataMask enum for details).
127 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
131 // Observer is notified when the removal is done. Done means keywords have
132 // been deleted, cache cleared and all other tasks scheduled.
135 virtual void OnBrowsingDataRemoverDone() = 0;
138 virtual ~Observer() {}
141 // Creates a BrowsingDataRemover object that removes data regardless of the
142 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
143 // retains ownership of itself, and deletes itself once finished.
144 static BrowsingDataRemover
* CreateForUnboundedRange(Profile
* profile
);
146 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
147 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
148 // deletes itself once finished.
149 static BrowsingDataRemover
* CreateForRange(Profile
* profile
,
150 base::Time delete_begin
,
151 base::Time delete_end
);
153 // Creates a BrowsingDataRemover bound to a specific period of time (as
154 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
155 // retains ownership of itself, and deletes itself once finished.
156 static BrowsingDataRemover
* CreateForPeriod(Profile
* profile
,
159 // Calculate the begin time for the deletion range specified by |time_period|.
160 static base::Time
CalculateBeginDeleteTime(TimePeriod time_period
);
162 // Is the BrowsingDataRemover currently in the process of removing data?
163 static bool is_removing() { return is_removing_
; }
165 // Removes the specified items related to browsing for all origins that match
166 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
167 void Remove(int remove_mask
, int origin_set_mask
);
169 void AddObserver(Observer
* observer
);
170 void RemoveObserver(Observer
* observer
);
172 // Called when history deletion is done.
173 void OnHistoryDeletionDone();
176 void OverrideStoragePartitionForTesting(
177 content::StoragePartition
* storage_partition
);
180 // The clear API needs to be able to toggle removing_ in order to test that
181 // only one BrowsingDataRemover instance can be called at a time.
182 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest
, OneAtATime
);
184 // The BrowsingDataRemover tests need to be able to access the implementation
185 // of Remove(), as it exposes details that aren't yet available in the public
186 // API. As soon as those details are exposed via new methods, this should be
189 // TODO(mkwst): See http://crbug.com/113621
190 friend class BrowsingDataRemoverTest
;
201 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
202 // not already removing, and vice-versa.
203 static void set_removing(bool is_removing
);
205 // Creates a BrowsingDataRemover to remove browser data from the specified
206 // profile in the specified time range. Use Remove to initiate the removal.
207 BrowsingDataRemover(Profile
* profile
,
208 base::Time delete_begin
,
209 base::Time delete_end
);
211 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
212 // to be deleted by other objects so make destructor private and DeleteHelper
214 friend class base::DeleteHelper
<BrowsingDataRemover
>;
215 virtual ~BrowsingDataRemover();
217 // Callback for when TemplateURLService has finished loading. Clears the data,
218 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
219 void OnKeywordsLoaded();
221 // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
222 void OnWaitableEventSignaled(base::WaitableEvent
* waitable_event
);
224 #if defined(ENABLE_PLUGINS)
225 // PepperFlashSettingsManager::Client implementation.
226 virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id
,
227 bool success
) OVERRIDE
;
230 #if defined (OS_CHROMEOS)
231 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status
,
235 // Removes the specified items related to browsing for a specific host. If the
236 // provided |origin| is empty, data is removed for all origins. The
237 // |origin_set_mask| parameter defines the set of origins from which data
238 // should be removed (protected, unprotected, or both).
239 void RemoveImpl(int remove_mask
,
241 int origin_set_mask
);
243 // If we're not waiting on anything, notifies observers and deletes this
245 void NotifyAndDeleteIfDone();
247 // Callback for when the hostname resolution cache has been cleared.
248 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
249 void OnClearedHostnameResolutionCache();
251 // Invoked on the IO thread to clear the hostname resolution cache.
252 void ClearHostnameResolutionCacheOnIOThread(IOThread
* io_thread
);
254 // Callback for when the LoggedIn Predictor has been cleared.
255 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
256 void OnClearedLoggedInPredictor();
258 // Clears the LoggedIn Predictor.
259 void ClearLoggedInPredictor();
261 // Callback for when speculative data in the network Predictor has been
262 // cleared. Clears the respective waiting flag and invokes
263 // NotifyAndDeleteIfDone.
264 void OnClearedNetworkPredictor();
266 // Invoked on the IO thread to clear speculative data related to hostname
267 // pre-resolution from the network Predictor.
268 void ClearNetworkPredictorOnIOThread();
270 // Callback for when network related data in ProfileIOData has been cleared.
271 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
272 void OnClearedNetworkingHistory();
274 // Callback for when the cache has been deleted. Invokes
275 // NotifyAndDeleteIfDone.
278 // Invoked on the IO thread to delete from the cache.
279 void ClearCacheOnIOThread();
281 // Performs the actual work to delete the cache.
282 void DoClearCache(int rv
);
284 #if !defined(DISABLE_NACL)
285 // Callback for when the NaCl cache has been deleted. Invokes
286 // NotifyAndDeleteIfDone.
287 void ClearedNaClCache();
289 // Invokes the ClearedNaClCache on the UI thread.
290 void ClearedNaClCacheOnIOThread();
292 // Invoked on the IO thread to delete the NaCl cache.
293 void ClearNaClCacheOnIOThread();
295 // Callback for when the PNaCl translation cache has been deleted. Invokes
296 // NotifyAndDeleteIfDone.
297 void ClearedPnaclCache();
299 // Invokes ClearedPnaclCacheOn on the UI thread.
300 void ClearedPnaclCacheOnIOThread();
302 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
303 void ClearPnaclCacheOnIOThread(base::Time begin
, base::Time end
);
306 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
307 void OnClearedCookies(int num_deleted
);
309 // Invoked on the IO thread to delete cookies.
310 void ClearCookiesOnIOThread(net::URLRequestContextGetter
* rq_context
);
312 // Invoked on the IO thread to delete server bound certs.
313 void ClearServerBoundCertsOnIOThread(
314 net::URLRequestContextGetter
* rq_context
);
316 // Callback on IO Thread when server bound certs have been deleted. Clears SSL
317 // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
318 void OnClearedServerBoundCertsOnIOThread(
319 net::URLRequestContextGetter
* rq_context
);
321 // Callback for when server bound certs have been deleted. Invokes
322 // NotifyAndDeleteIfDone.
323 void OnClearedServerBoundCerts();
325 // Callback from the above method.
326 void OnClearedFormData();
328 // Callback for when the Autofill profile and credit card origin URLs have
330 void OnClearedAutofillOriginURLs();
333 // Callback on UI thread when the storage partition related data are cleared.
334 void OnClearedStoragePartitionData();
336 // Returns true if we're all done.
339 // Profile we're to remove from.
342 // 'Protected' origins are not subject to data removal.
343 scoped_refptr
<ExtensionSpecialStoragePolicy
> special_storage_policy_
;
345 // Start time to delete from.
346 const base::Time delete_begin_
;
348 // End time to delete to.
349 base::Time delete_end_
;
351 // True if Remove has been invoked.
352 static bool is_removing_
;
354 CacheState next_cache_state_
;
355 disk_cache::Backend
* cache_
;
357 // Used to delete data from HTTP cache.
358 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
359 scoped_refptr
<net::URLRequestContextGetter
> media_context_getter_
;
361 #if defined(ENABLE_PLUGINS)
362 // Used to delete plugin data.
363 scoped_ptr
<content::PluginDataRemover
> plugin_data_remover_
;
364 base::WaitableEventWatcher watcher_
;
366 // Used to deauthorize content licenses for Pepper Flash.
367 scoped_ptr
<PepperFlashSettingsManager
> pepper_flash_settings_manager_
;
370 uint32 deauthorize_content_licenses_request_id_
;
371 // True if we're waiting for various data to be deleted.
372 // These may only be accessed from UI thread in order to avoid races!
373 bool waiting_for_clear_autofill_origin_urls_
;
374 bool waiting_for_clear_cache_
;
375 bool waiting_for_clear_content_licenses_
;
376 // Non-zero if waiting for cookies to be cleared.
377 int waiting_for_clear_cookies_count_
;
378 bool waiting_for_clear_form_
;
379 bool waiting_for_clear_history_
;
380 bool waiting_for_clear_hostname_resolution_cache_
;
381 bool waiting_for_clear_keyword_data_
;
382 bool waiting_for_clear_logged_in_predictor_
;
383 bool waiting_for_clear_nacl_cache_
;
384 bool waiting_for_clear_network_predictor_
;
385 bool waiting_for_clear_networking_history_
;
386 bool waiting_for_clear_platform_keys_
;
387 bool waiting_for_clear_plugin_data_
;
388 bool waiting_for_clear_pnacl_cache_
;
389 bool waiting_for_clear_server_bound_certs_
;
390 bool waiting_for_clear_storage_partition_data_
;
392 // The removal mask for the current removal operation.
395 // The origin for the current removal operation.
398 // From which types of origins should we remove data?
399 int origin_set_mask_
;
401 ObserverList
<Observer
> observer_list_
;
403 // Used if we need to clear history.
404 CancelableTaskTracker history_task_tracker_
;
406 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
408 // We do not own this.
409 content::StoragePartition
* storage_partition_for_testing_
;
411 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover
);
414 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_