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 "chrome/browser/search_engines/template_url_service.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 #if defined(OS_ANDROID)
89 REMOVE_APP_BANNER_DATA = 1 << 14,
91 // The following flag is used only in tests. In normal usage, hosted app
92 // data is controlled by the REMOVE_COOKIES flag, applied to the
93 // protected-web origin.
94 REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
96 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
97 // storage, webSQL, and plugin data.
98 REMOVE_SITE_DATA = REMOVE_APPCACHE |
100 REMOVE_FILE_SYSTEMS |
102 REMOVE_LOCAL_STORAGE |
105 #if defined(OS_ANDROID)
106 REMOVE_APP_BANNER_DATA |
108 REMOVE_SERVER_BOUND_CERTS,
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 |
119 REMOVE_CONTENT_LICENSES,
122 // When BrowsingDataRemover successfully removes data, a notification of type
123 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
125 struct NotificationDetails {
126 NotificationDetails();
127 NotificationDetails(const NotificationDetails& details);
128 NotificationDetails(base::Time removal_begin,
130 int origin_set_mask);
131 ~NotificationDetails();
133 // The beginning of the removal time range.
134 base::Time removal_begin;
136 // The removal mask (see the RemoveDataMask enum for details).
139 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
143 // Observer is notified when the removal is done. Done means keywords have
144 // been deleted, cache cleared and all other tasks scheduled.
147 virtual void OnBrowsingDataRemoverDone() = 0;
150 virtual ~Observer() {}
153 // Creates a BrowsingDataRemover object that removes data regardless of the
154 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
155 // retains ownership of itself, and deletes itself once finished.
156 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
158 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
159 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
160 // deletes itself once finished.
161 static BrowsingDataRemover* CreateForRange(Profile* profile,
162 base::Time delete_begin,
163 base::Time delete_end);
165 // Creates a BrowsingDataRemover bound to a specific period of time (as
166 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
167 // retains ownership of itself, and deletes itself once finished.
168 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
171 // Calculate the begin time for the deletion range specified by |time_period|.
172 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
174 // Is the BrowsingDataRemover currently in the process of removing data?
175 static bool is_removing() { return is_removing_; }
177 // Removes the specified items related to browsing for all origins that match
178 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
179 void Remove(int remove_mask, int origin_set_mask);
181 void AddObserver(Observer* observer);
182 void RemoveObserver(Observer* observer);
184 // Called when history deletion is done.
185 void OnHistoryDeletionDone();
188 void OverrideStoragePartitionForTesting(
189 content::StoragePartition* storage_partition);
192 // The clear API needs to be able to toggle removing_ in order to test that
193 // only one BrowsingDataRemover instance can be called at a time.
194 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
196 // The BrowsingDataRemover tests need to be able to access the implementation
197 // of Remove(), as it exposes details that aren't yet available in the public
198 // API. As soon as those details are exposed via new methods, this should be
201 // TODO(mkwst): See http://crbug.com/113621
202 friend class BrowsingDataRemoverTest;
213 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
214 // not already removing, and vice-versa.
215 static void set_removing(bool is_removing);
217 // Creates a BrowsingDataRemover to remove browser data from the specified
218 // profile in the specified time range. Use Remove to initiate the removal.
219 BrowsingDataRemover(Profile* profile,
220 base::Time delete_begin,
221 base::Time delete_end);
223 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
224 // to be deleted by other objects so make destructor private and DeleteHelper
226 friend class base::DeleteHelper<BrowsingDataRemover>;
227 virtual ~BrowsingDataRemover();
229 // Callback for when TemplateURLService has finished loading. Clears the data,
230 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
231 void OnKeywordsLoaded();
233 // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
234 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
236 #if defined(ENABLE_PLUGINS)
237 // PepperFlashSettingsManager::Client implementation.
238 virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
239 bool success) OVERRIDE;
242 #if defined (OS_CHROMEOS)
243 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
247 // Removes the specified items related to browsing for a specific host. If the
248 // provided |origin| is empty, data is removed for all origins. The
249 // |origin_set_mask| parameter defines the set of origins from which data
250 // should be removed (protected, unprotected, or both).
251 void RemoveImpl(int remove_mask,
253 int origin_set_mask);
255 // If we're not waiting on anything, notifies observers and deletes this
257 void NotifyAndDeleteIfDone();
259 // Callback for when the hostname resolution cache has been cleared.
260 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
261 void OnClearedHostnameResolutionCache();
263 // Invoked on the IO thread to clear the hostname resolution cache.
264 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
266 // Callback for when the LoggedIn Predictor has been cleared.
267 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
268 void OnClearedLoggedInPredictor();
270 // Clears the LoggedIn Predictor.
271 void ClearLoggedInPredictor();
273 // Callback for when speculative data in the network Predictor has been
274 // cleared. Clears the respective waiting flag and invokes
275 // NotifyAndDeleteIfDone.
276 void OnClearedNetworkPredictor();
278 // Invoked on the IO thread to clear speculative data related to hostname
279 // pre-resolution from the network Predictor.
280 void ClearNetworkPredictorOnIOThread();
282 // Callback for when network related data in ProfileIOData has been cleared.
283 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
284 void OnClearedNetworkingHistory();
286 // Callback for when the cache has been deleted. Invokes
287 // NotifyAndDeleteIfDone.
290 // Invoked on the IO thread to delete from the cache.
291 void ClearCacheOnIOThread();
293 // Performs the actual work to delete the cache.
294 void DoClearCache(int rv);
296 #if !defined(DISABLE_NACL)
297 // Callback for when the NaCl cache has been deleted. Invokes
298 // NotifyAndDeleteIfDone.
299 void ClearedNaClCache();
301 // Invokes the ClearedNaClCache on the UI thread.
302 void ClearedNaClCacheOnIOThread();
304 // Invoked on the IO thread to delete the NaCl cache.
305 void ClearNaClCacheOnIOThread();
307 // Callback for when the PNaCl translation cache has been deleted. Invokes
308 // NotifyAndDeleteIfDone.
309 void ClearedPnaclCache();
311 // Invokes ClearedPnaclCacheOn on the UI thread.
312 void ClearedPnaclCacheOnIOThread();
314 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
315 void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
318 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
319 void OnClearedCookies(int num_deleted);
321 // Invoked on the IO thread to delete cookies.
322 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
324 // Invoked on the IO thread to delete server bound certs.
325 void ClearServerBoundCertsOnIOThread(
326 net::URLRequestContextGetter* rq_context);
328 // Callback on IO Thread when server bound certs have been deleted. Clears SSL
329 // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
330 void OnClearedServerBoundCertsOnIOThread(
331 net::URLRequestContextGetter* rq_context);
333 // Callback for when server bound certs have been deleted. Invokes
334 // NotifyAndDeleteIfDone.
335 void OnClearedServerBoundCerts();
337 // Callback from the above method.
338 void OnClearedFormData();
340 // Callback for when the Autofill profile and credit card origin URLs have
342 void OnClearedAutofillOriginURLs();
345 // Callback on UI thread when the storage partition related data are cleared.
346 void OnClearedStoragePartitionData();
348 // Returns true if we're all done.
351 // Profile we're to remove from.
354 // 'Protected' origins are not subject to data removal.
355 scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
357 // Start time to delete from.
358 const base::Time delete_begin_;
360 // End time to delete to.
361 base::Time delete_end_;
363 // True if Remove has been invoked.
364 static bool is_removing_;
366 CacheState next_cache_state_;
367 disk_cache::Backend* cache_;
369 // Used to delete data from HTTP cache.
370 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
371 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
373 #if defined(ENABLE_PLUGINS)
374 // Used to delete plugin data.
375 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
376 base::WaitableEventWatcher watcher_;
378 // Used to deauthorize content licenses for Pepper Flash.
379 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
382 uint32 deauthorize_content_licenses_request_id_;
383 // True if we're waiting for various data to be deleted.
384 // These may only be accessed from UI thread in order to avoid races!
385 bool waiting_for_clear_autofill_origin_urls_;
386 bool waiting_for_clear_cache_;
387 bool waiting_for_clear_content_licenses_;
388 // Non-zero if waiting for cookies to be cleared.
389 int waiting_for_clear_cookies_count_;
390 bool waiting_for_clear_form_;
391 bool waiting_for_clear_history_;
392 bool waiting_for_clear_hostname_resolution_cache_;
393 bool waiting_for_clear_keyword_data_;
394 bool waiting_for_clear_logged_in_predictor_;
395 bool waiting_for_clear_nacl_cache_;
396 bool waiting_for_clear_network_predictor_;
397 bool waiting_for_clear_networking_history_;
398 bool waiting_for_clear_platform_keys_;
399 bool waiting_for_clear_plugin_data_;
400 bool waiting_for_clear_pnacl_cache_;
401 bool waiting_for_clear_server_bound_certs_;
402 bool waiting_for_clear_storage_partition_data_;
404 // The removal mask for the current removal operation.
407 // The origin for the current removal operation.
410 // From which types of origins should we remove data?
411 int origin_set_mask_;
413 ObserverList<Observer> observer_list_;
415 // Used if we need to clear history.
416 base::CancelableTaskTracker history_task_tracker_;
418 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
420 // We do not own this.
421 content::StoragePartition* storage_partition_for_testing_;
423 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
426 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_