blob: af1b441c90d935bb2056d23d229d2f448b96aa6c [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
6#define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
7
8#include <set>
9
10#include "base/gtest_prod_util.h"
11#include "base/memory/ref_counted.h"
12#include "base/observer_list.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013#include "base/prefs/pref_member.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/sequenced_task_runner_helpers.h"
15#include "base/synchronization/waitable_event_watcher.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010016#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "chrome/browser/pepper_flash_settings_manager.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "chrome/common/cancelable_task_tracker.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010021#include "url/gurl.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010022#include "webkit/common/quota/quota_types.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023
24class ExtensionSpecialStoragePolicy;
25class IOThread;
26class Profile;
27
28namespace content {
29class PluginDataRemover;
30}
31
32namespace disk_cache {
33class Backend;
34}
35
36namespace net {
37class URLRequestContextGetter;
38}
39
40namespace quota {
41class QuotaManager;
42}
43
Ben Murdochbb1529c2013-08-08 10:24:53 +010044namespace content {
45class DOMStorageContext;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046struct LocalStorageUsageInfo;
47struct SessionStorageUsageInfo;
48}
49
50// BrowsingDataRemover is responsible for removing data related to browsing:
51// visits in url database, downloads, cookies ...
52
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000053class BrowsingDataRemover : public content::NotificationObserver
54#if defined(ENABLE_PLUGINS)
55 , public PepperFlashSettingsManager::Client
56#endif
57 {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058 public:
59 // Time period ranges available when doing browsing data removals.
60 enum TimePeriod {
61 LAST_HOUR = 0,
62 LAST_DAY,
63 LAST_WEEK,
64 FOUR_WEEKS,
65 EVERYTHING
66 };
67
68 // Mask used for Remove.
69 enum RemoveDataMask {
70 REMOVE_APPCACHE = 1 << 0,
71 REMOVE_CACHE = 1 << 1,
72 REMOVE_COOKIES = 1 << 2,
73 REMOVE_DOWNLOADS = 1 << 3,
74 REMOVE_FILE_SYSTEMS = 1 << 4,
75 REMOVE_FORM_DATA = 1 << 5,
76 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
77 REMOVE_HISTORY = 1 << 6,
78 REMOVE_INDEXEDDB = 1 << 7,
79 REMOVE_LOCAL_STORAGE = 1 << 8,
80 REMOVE_PLUGIN_DATA = 1 << 9,
81 REMOVE_PASSWORDS = 1 << 10,
82 REMOVE_WEBSQL = 1 << 11,
83 REMOVE_SERVER_BOUND_CERTS = 1 << 12,
84 REMOVE_CONTENT_LICENSES = 1 << 13,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000085 // The following flag is used only in tests. In normal usage, hosted app
86 // data is controlled by the REMOVE_COOKIES flag, applied to the
87 // protected-web origin.
88 REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089
90 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010091 // storage, webSQL, and plugin data.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
93 REMOVE_INDEXEDDB | REMOVE_LOCAL_STORAGE |
94 REMOVE_PLUGIN_DATA | REMOVE_WEBSQL |
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010095 REMOVE_SERVER_BOUND_CERTS,
Ben Murdoch558790d2013-07-30 15:19:42 +010096
97 // Includes all the available remove options. Meant to be used by clients
98 // that wish to wipe as much data as possible from a Profile, to make it
99 // look like a new Profile.
100 REMOVE_ALL = REMOVE_APPCACHE | REMOVE_CACHE | REMOVE_COOKIES |
101 REMOVE_DOWNLOADS | REMOVE_FILE_SYSTEMS | REMOVE_FORM_DATA |
102 REMOVE_HISTORY | REMOVE_INDEXEDDB | REMOVE_LOCAL_STORAGE |
103 REMOVE_PLUGIN_DATA | REMOVE_PASSWORDS | REMOVE_WEBSQL |
104 REMOVE_SERVER_BOUND_CERTS | REMOVE_CONTENT_LICENSES,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000105 };
106
107 // When BrowsingDataRemover successfully removes data, a notification of type
108 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
109 // this type.
110 struct NotificationDetails {
111 NotificationDetails();
112 NotificationDetails(const NotificationDetails& details);
113 NotificationDetails(base::Time removal_begin,
114 int removal_mask,
115 int origin_set_mask);
116 ~NotificationDetails();
117
118 // The beginning of the removal time range.
119 base::Time removal_begin;
120
121 // The removal mask (see the RemoveDataMask enum for details).
122 int removal_mask;
123
124 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
125 int origin_set_mask;
126 };
127
128 // Observer is notified when the removal is done. Done means keywords have
129 // been deleted, cache cleared and all other tasks scheduled.
130 class Observer {
131 public:
132 virtual void OnBrowsingDataRemoverDone() = 0;
133
134 protected:
135 virtual ~Observer() {}
136 };
137
138 // Creates a BrowsingDataRemover object that removes data regardless of the
139 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
140 // retains ownership of itself, and deletes itself once finished.
141 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
142
143 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
144 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
145 // deletes itself once finished.
146 static BrowsingDataRemover* CreateForRange(Profile* profile,
147 base::Time delete_begin,
148 base::Time delete_end);
149
150 // Creates a BrowsingDataRemover bound to a specific period of time (as
151 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
152 // retains ownership of itself, and deletes itself once finished.
153 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
154 TimePeriod period);
155
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000156 // Calculate the begin time for the deletion range specified by |time_period|.
157 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
158
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000159 // Quota managed data uses a different bitmask for types than
160 // BrowsingDataRemover uses. This method generates that mask.
161 static int GenerateQuotaClientMask(int remove_mask);
162
163 // Is the BrowsingDataRemover currently in the process of removing data?
164 static bool is_removing() { return is_removing_; }
165
166 // Removes the specified items related to browsing for all origins that match
167 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
168 void Remove(int remove_mask, int origin_set_mask);
169
170 void AddObserver(Observer* observer);
171 void RemoveObserver(Observer* observer);
172
173 // Called when history deletion is done.
174 void OnHistoryDeletionDone();
175
176 // Used for testing.
177 void OverrideQuotaManagerForTesting(quota::QuotaManager* quota_manager);
178
179 private:
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);
183
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
187 // removed.
188 //
189 // TODO(mkwst): See http://crbug.com/113621
190 friend class BrowsingDataRemoverTest;
191
192 enum CacheState {
193 STATE_NONE,
194 STATE_CREATE_MAIN,
195 STATE_CREATE_MEDIA,
196 STATE_DELETE_MAIN,
197 STATE_DELETE_MEDIA,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000198 STATE_DONE
199 };
200
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000201 // 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);
204
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);
210
211 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
212 // to be deleted by other objects so make destructor private and DeleteHelper
213 // a friend.
214 friend class base::DeleteHelper<BrowsingDataRemover>;
215 virtual ~BrowsingDataRemover();
216
217 // content::NotificationObserver method. Callback when TemplateURLService has
218 // finished loading. Deletes the entries from the model, and if we're not
219 // waiting on anything else notifies observers and deletes this
220 // BrowsingDataRemover.
221 virtual void Observe(int type,
222 const content::NotificationSource& source,
223 const content::NotificationDetails& details) OVERRIDE;
224
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000225 // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000226 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000227
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000228#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000229 // PepperFlashSettingsManager::Client implementation.
230 virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
231 bool success) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000232#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000233
234 // Removes the specified items related to browsing for a specific host. If the
235 // provided |origin| is empty, data is removed for all origins. The
236 // |origin_set_mask| parameter defines the set of origins from which data
237 // should be removed (protected, unprotected, or both).
238 void RemoveImpl(int remove_mask,
239 const GURL& origin,
240 int origin_set_mask);
241
242 // If we're not waiting on anything, notifies observers and deletes this
243 // object.
244 void NotifyAndDeleteIfDone();
245
246 // Callback when the hostname resolution cache has been cleared.
247 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
248 void OnClearedHostnameResolutionCache();
249
250 // Invoked on the IO thread to clear the hostname resolution cache.
251 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
252
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100253 // Callback when the LoggedIn Predictor has been cleared.
254 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
255 void OnClearedLoggedInPredictor();
256
257 // Clears the LoggedIn Predictor.
258 void ClearLoggedInPredictor();
259
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000260 // Callback when speculative data in the network Predictor has been cleared.
261 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
262 void OnClearedNetworkPredictor();
263
264 // Invoked on the IO thread to clear speculative data related to hostname
265 // pre-resolution from the network Predictor.
266 void ClearNetworkPredictorOnIOThread();
267
268 // Callback when network related data in ProfileIOData has been cleared.
269 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
270 void OnClearedNetworkingHistory();
271
272 // Callback when the cache has been deleted. Invokes NotifyAndDeleteIfDone.
273 void ClearedCache();
274
275 // Invoked on the IO thread to delete from the cache.
276 void ClearCacheOnIOThread();
277
278 // Performs the actual work to delete the cache.
279 void DoClearCache(int rv);
280
281#if !defined(DISABLE_NACL)
282 // Callback for when the NaCl cache has been deleted. Invokes
283 // NotifyAndDeleteIfDone.
284 void ClearedNaClCache();
285
286 // Invokes the ClearedNaClCache on the UI thread.
287 void ClearedNaClCacheOnIOThread();
288
289 // Invoked on the IO thread to delete the NaCl cache.
290 void ClearNaClCacheOnIOThread();
291#endif
292
293 // Invoked on the UI thread to delete local storage.
294 void ClearLocalStorageOnUIThread();
295
296 // Callback to deal with the list gathered in ClearLocalStorageOnUIThread.
297 void OnGotLocalStorageUsageInfo(
Ben Murdochbb1529c2013-08-08 10:24:53 +0100298 const std::vector<content::LocalStorageUsageInfo>& infos);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000299
300 // Invoked on the UI thread to delete session storage.
301 void ClearSessionStorageOnUIThread();
302
303 // Callback to deal with the list gathered in ClearSessionStorageOnUIThread.
304 void OnGotSessionStorageUsageInfo(
Ben Murdochbb1529c2013-08-08 10:24:53 +0100305 const std::vector<content::SessionStorageUsageInfo>& infos);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000306
307 // Invoked on the IO thread to delete all storage types managed by the quota
308 // system: AppCache, Databases, FileSystems.
309 void ClearQuotaManagedDataOnIOThread();
310
311 // Callback to respond to QuotaManager::GetOriginsModifiedSince, which is the
312 // core of 'ClearQuotaManagedDataOnIOThread'.
313 void OnGotQuotaManagedOrigins(const std::set<GURL>& origins,
314 quota::StorageType type);
315
316 // Callback responding to deletion of a single quota managed origin's
317 // persistent data
318 void OnQuotaManagedOriginDeletion(const GURL& origin,
319 quota::StorageType type,
320 quota::QuotaStatusCode);
321
322 // Called to check whether all temporary and persistent origin data that
323 // should be deleted has been deleted. If everything's good to go, invokes
324 // OnQuotaManagedDataDeleted on the UI thread.
325 void CheckQuotaManagedDataDeletionStatus();
326
327 // Completion handler that runs on the UI thread once persistent data has been
328 // deleted. Updates the waiting flag and invokes NotifyAndDeleteIfDone.
329 void OnQuotaManagedDataDeleted();
330
331 // Callback when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
332 void OnClearedCookies(int num_deleted);
333
334 // Invoked on the IO thread to delete cookies.
335 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
336
337 // Invoked on the IO thread to delete server bound certs.
338 void ClearServerBoundCertsOnIOThread(
339 net::URLRequestContextGetter* rq_context);
340
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000341 // Callback on IO Thread when server bound certs have been deleted. Clears SSL
342 // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
343 void OnClearedServerBoundCertsOnIOThread(
344 net::URLRequestContextGetter* rq_context);
345
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000346 // Callback when server bound certs have been deleted. Invokes
347 // NotifyAndDeleteIfDone.
348 void OnClearedServerBoundCerts();
349
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000350 // Callback from the above method.
351 void OnClearedFormData();
352
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100353 // Callback when the Autofill profile and credit card origin URLs have been
354 // deleted.
355 void OnClearedAutofillOriginURLs();
356
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100357 // Callback when the shader cache has been deleted.
358 // Invokes NotifyAndDeleteIfDone.
359 void ClearedShaderCache();
360
361 // Invoked on the IO thread to delete from the shader cache.
362 void ClearShaderCacheOnUIThread();
363
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000364 // Returns true if we're all done.
365 bool AllDone();
366
367 content::NotificationRegistrar registrar_;
368
369 // Profile we're to remove from.
370 Profile* profile_;
371
372 // The QuotaManager is owned by the profile; we can use a raw pointer here,
373 // and rely on the profile to destroy the object whenever it's reasonable.
374 quota::QuotaManager* quota_manager_;
375
376 // The DOMStorageContext is owned by the profile; we'll store a raw pointer.
377 content::DOMStorageContext* dom_storage_context_;
378
379 // 'Protected' origins are not subject to data removal.
380 scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
381
382 // Start time to delete from.
383 const base::Time delete_begin_;
384
385 // End time to delete to.
386 base::Time delete_end_;
387
388 // True if Remove has been invoked.
389 static bool is_removing_;
390
391 CacheState next_cache_state_;
392 disk_cache::Backend* cache_;
393
394 // Used to delete data from HTTP cache.
395 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
396 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
397
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000398#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000399 // Used to delete plugin data.
400 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
401 base::WaitableEventWatcher watcher_;
402
403 // Used to deauthorize content licenses for Pepper Flash.
404 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000405#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000406
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000407 uint32 deauthorize_content_licenses_request_id_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000408 // True if we're waiting for various data to be deleted.
409 // These may only be accessed from UI thread in order to avoid races!
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100410 bool waiting_for_clear_autofill_origin_urls_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000411 bool waiting_for_clear_cache_;
412 bool waiting_for_clear_content_licenses_;
413 // Non-zero if waiting for cookies to be cleared.
414 int waiting_for_clear_cookies_count_;
415 bool waiting_for_clear_form_;
416 bool waiting_for_clear_history_;
417 bool waiting_for_clear_hostname_resolution_cache_;
418 bool waiting_for_clear_local_storage_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100419 bool waiting_for_clear_logged_in_predictor_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000420 bool waiting_for_clear_nacl_cache_;
421 bool waiting_for_clear_network_predictor_;
422 bool waiting_for_clear_networking_history_;
423 bool waiting_for_clear_plugin_data_;
424 bool waiting_for_clear_quota_managed_data_;
425 bool waiting_for_clear_server_bound_certs_;
426 bool waiting_for_clear_session_storage_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100427 bool waiting_for_clear_shader_cache_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000428
429 // Tracking how many origins need to be deleted, and whether we're finished
430 // gathering origins.
431 int quota_managed_origins_to_delete_count_;
432 int quota_managed_storage_types_to_delete_count_;
433
434 // The removal mask for the current removal operation.
435 int remove_mask_;
436
437 // The origin for the current removal operation.
438 GURL remove_origin_;
439
440 // From which types of origins should we remove data?
441 int origin_set_mask_;
442
443 ObserverList<Observer> observer_list_;
444
445 // Used if we need to clear history.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000446 CancelableTaskTracker history_task_tracker_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000447
448 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
449};
450
451#endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_