blob: e75735e2bd1906049537e9742acbc066ad00f6c8 [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/dom_storage_context.h"
20#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010022#include "url/gurl.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010023#include "webkit/common/quota/quota_types.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024
25class ExtensionSpecialStoragePolicy;
26class IOThread;
27class Profile;
28
29namespace content {
30class PluginDataRemover;
31}
32
33namespace disk_cache {
34class Backend;
35}
36
37namespace net {
38class URLRequestContextGetter;
39}
40
41namespace quota {
42class QuotaManager;
43}
44
45namespace dom_storage {
46struct 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,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000096 };
97
98 // When BrowsingDataRemover successfully removes data, a notification of type
99 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
100 // this type.
101 struct NotificationDetails {
102 NotificationDetails();
103 NotificationDetails(const NotificationDetails& details);
104 NotificationDetails(base::Time removal_begin,
105 int removal_mask,
106 int origin_set_mask);
107 ~NotificationDetails();
108
109 // The beginning of the removal time range.
110 base::Time removal_begin;
111
112 // The removal mask (see the RemoveDataMask enum for details).
113 int removal_mask;
114
115 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
116 int origin_set_mask;
117 };
118
119 // Observer is notified when the removal is done. Done means keywords have
120 // been deleted, cache cleared and all other tasks scheduled.
121 class Observer {
122 public:
123 virtual void OnBrowsingDataRemoverDone() = 0;
124
125 protected:
126 virtual ~Observer() {}
127 };
128
129 // Creates a BrowsingDataRemover object that removes data regardless of the
130 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
131 // retains ownership of itself, and deletes itself once finished.
132 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
133
134 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
135 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
136 // deletes itself once finished.
137 static BrowsingDataRemover* CreateForRange(Profile* profile,
138 base::Time delete_begin,
139 base::Time delete_end);
140
141 // Creates a BrowsingDataRemover bound to a specific period of time (as
142 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
143 // retains ownership of itself, and deletes itself once finished.
144 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
145 TimePeriod period);
146
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000147 // Calculate the begin time for the deletion range specified by |time_period|.
148 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
149
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000150 // Quota managed data uses a different bitmask for types than
151 // BrowsingDataRemover uses. This method generates that mask.
152 static int GenerateQuotaClientMask(int remove_mask);
153
154 // Is the BrowsingDataRemover currently in the process of removing data?
155 static bool is_removing() { return is_removing_; }
156
157 // Removes the specified items related to browsing for all origins that match
158 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
159 void Remove(int remove_mask, int origin_set_mask);
160
161 void AddObserver(Observer* observer);
162 void RemoveObserver(Observer* observer);
163
164 // Called when history deletion is done.
165 void OnHistoryDeletionDone();
166
167 // Used for testing.
168 void OverrideQuotaManagerForTesting(quota::QuotaManager* quota_manager);
169
170 private:
171 // The clear API needs to be able to toggle removing_ in order to test that
172 // only one BrowsingDataRemover instance can be called at a time.
173 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
174
175 // The BrowsingDataRemover tests need to be able to access the implementation
176 // of Remove(), as it exposes details that aren't yet available in the public
177 // API. As soon as those details are exposed via new methods, this should be
178 // removed.
179 //
180 // TODO(mkwst): See http://crbug.com/113621
181 friend class BrowsingDataRemoverTest;
182
183 enum CacheState {
184 STATE_NONE,
185 STATE_CREATE_MAIN,
186 STATE_CREATE_MEDIA,
187 STATE_DELETE_MAIN,
188 STATE_DELETE_MEDIA,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000189 STATE_DONE
190 };
191
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000192 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
193 // not already removing, and vice-versa.
194 static void set_removing(bool is_removing);
195
196 // Creates a BrowsingDataRemover to remove browser data from the specified
197 // profile in the specified time range. Use Remove to initiate the removal.
198 BrowsingDataRemover(Profile* profile,
199 base::Time delete_begin,
200 base::Time delete_end);
201
202 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
203 // to be deleted by other objects so make destructor private and DeleteHelper
204 // a friend.
205 friend class base::DeleteHelper<BrowsingDataRemover>;
206 virtual ~BrowsingDataRemover();
207
208 // content::NotificationObserver method. Callback when TemplateURLService has
209 // finished loading. Deletes the entries from the model, and if we're not
210 // waiting on anything else notifies observers and deletes this
211 // BrowsingDataRemover.
212 virtual void Observe(int type,
213 const content::NotificationSource& source,
214 const content::NotificationDetails& details) OVERRIDE;
215
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000216 // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000217 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000218
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000219#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000220 // PepperFlashSettingsManager::Client implementation.
221 virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
222 bool success) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000223#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000224
225 // Removes the specified items related to browsing for a specific host. If the
226 // provided |origin| is empty, data is removed for all origins. The
227 // |origin_set_mask| parameter defines the set of origins from which data
228 // should be removed (protected, unprotected, or both).
229 void RemoveImpl(int remove_mask,
230 const GURL& origin,
231 int origin_set_mask);
232
233 // If we're not waiting on anything, notifies observers and deletes this
234 // object.
235 void NotifyAndDeleteIfDone();
236
237 // Callback when the hostname resolution cache has been cleared.
238 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
239 void OnClearedHostnameResolutionCache();
240
241 // Invoked on the IO thread to clear the hostname resolution cache.
242 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
243
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100244 // Callback when the LoggedIn Predictor has been cleared.
245 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
246 void OnClearedLoggedInPredictor();
247
248 // Clears the LoggedIn Predictor.
249 void ClearLoggedInPredictor();
250
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000251 // Callback when speculative data in the network Predictor has been cleared.
252 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
253 void OnClearedNetworkPredictor();
254
255 // Invoked on the IO thread to clear speculative data related to hostname
256 // pre-resolution from the network Predictor.
257 void ClearNetworkPredictorOnIOThread();
258
259 // Callback when network related data in ProfileIOData has been cleared.
260 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
261 void OnClearedNetworkingHistory();
262
263 // Callback when the cache has been deleted. Invokes NotifyAndDeleteIfDone.
264 void ClearedCache();
265
266 // Invoked on the IO thread to delete from the cache.
267 void ClearCacheOnIOThread();
268
269 // Performs the actual work to delete the cache.
270 void DoClearCache(int rv);
271
272#if !defined(DISABLE_NACL)
273 // Callback for when the NaCl cache has been deleted. Invokes
274 // NotifyAndDeleteIfDone.
275 void ClearedNaClCache();
276
277 // Invokes the ClearedNaClCache on the UI thread.
278 void ClearedNaClCacheOnIOThread();
279
280 // Invoked on the IO thread to delete the NaCl cache.
281 void ClearNaClCacheOnIOThread();
282#endif
283
284 // Invoked on the UI thread to delete local storage.
285 void ClearLocalStorageOnUIThread();
286
287 // Callback to deal with the list gathered in ClearLocalStorageOnUIThread.
288 void OnGotLocalStorageUsageInfo(
289 const std::vector<dom_storage::LocalStorageUsageInfo>& infos);
290
291 // Invoked on the UI thread to delete session storage.
292 void ClearSessionStorageOnUIThread();
293
294 // Callback to deal with the list gathered in ClearSessionStorageOnUIThread.
295 void OnGotSessionStorageUsageInfo(
296 const std::vector<dom_storage::SessionStorageUsageInfo>& infos);
297
298 // Invoked on the IO thread to delete all storage types managed by the quota
299 // system: AppCache, Databases, FileSystems.
300 void ClearQuotaManagedDataOnIOThread();
301
302 // Callback to respond to QuotaManager::GetOriginsModifiedSince, which is the
303 // core of 'ClearQuotaManagedDataOnIOThread'.
304 void OnGotQuotaManagedOrigins(const std::set<GURL>& origins,
305 quota::StorageType type);
306
307 // Callback responding to deletion of a single quota managed origin's
308 // persistent data
309 void OnQuotaManagedOriginDeletion(const GURL& origin,
310 quota::StorageType type,
311 quota::QuotaStatusCode);
312
313 // Called to check whether all temporary and persistent origin data that
314 // should be deleted has been deleted. If everything's good to go, invokes
315 // OnQuotaManagedDataDeleted on the UI thread.
316 void CheckQuotaManagedDataDeletionStatus();
317
318 // Completion handler that runs on the UI thread once persistent data has been
319 // deleted. Updates the waiting flag and invokes NotifyAndDeleteIfDone.
320 void OnQuotaManagedDataDeleted();
321
322 // Callback when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
323 void OnClearedCookies(int num_deleted);
324
325 // Invoked on the IO thread to delete cookies.
326 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
327
328 // Invoked on the IO thread to delete server bound certs.
329 void ClearServerBoundCertsOnIOThread(
330 net::URLRequestContextGetter* rq_context);
331
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000332 // Callback on IO Thread when server bound certs have been deleted. Clears SSL
333 // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
334 void OnClearedServerBoundCertsOnIOThread(
335 net::URLRequestContextGetter* rq_context);
336
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000337 // Callback when server bound certs have been deleted. Invokes
338 // NotifyAndDeleteIfDone.
339 void OnClearedServerBoundCerts();
340
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000341 // Callback from the above method.
342 void OnClearedFormData();
343
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100344 // Callback when the Autofill profile and credit card origin URLs have been
345 // deleted.
346 void OnClearedAutofillOriginURLs();
347
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100348 // Callback when the shader cache has been deleted.
349 // Invokes NotifyAndDeleteIfDone.
350 void ClearedShaderCache();
351
352 // Invoked on the IO thread to delete from the shader cache.
353 void ClearShaderCacheOnUIThread();
354
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000355 // Returns true if we're all done.
356 bool AllDone();
357
358 content::NotificationRegistrar registrar_;
359
360 // Profile we're to remove from.
361 Profile* profile_;
362
363 // The QuotaManager is owned by the profile; we can use a raw pointer here,
364 // and rely on the profile to destroy the object whenever it's reasonable.
365 quota::QuotaManager* quota_manager_;
366
367 // The DOMStorageContext is owned by the profile; we'll store a raw pointer.
368 content::DOMStorageContext* dom_storage_context_;
369
370 // 'Protected' origins are not subject to data removal.
371 scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
372
373 // Start time to delete from.
374 const base::Time delete_begin_;
375
376 // End time to delete to.
377 base::Time delete_end_;
378
379 // True if Remove has been invoked.
380 static bool is_removing_;
381
382 CacheState next_cache_state_;
383 disk_cache::Backend* cache_;
384
385 // Used to delete data from HTTP cache.
386 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
387 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
388
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000389#if defined(ENABLE_PLUGINS)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000390 // Used to delete plugin data.
391 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
392 base::WaitableEventWatcher watcher_;
393
394 // Used to deauthorize content licenses for Pepper Flash.
395 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000396#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000397
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000398 uint32 deauthorize_content_licenses_request_id_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000399 // True if we're waiting for various data to be deleted.
400 // These may only be accessed from UI thread in order to avoid races!
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100401 bool waiting_for_clear_autofill_origin_urls_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000402 bool waiting_for_clear_cache_;
403 bool waiting_for_clear_content_licenses_;
404 // Non-zero if waiting for cookies to be cleared.
405 int waiting_for_clear_cookies_count_;
406 bool waiting_for_clear_form_;
407 bool waiting_for_clear_history_;
408 bool waiting_for_clear_hostname_resolution_cache_;
409 bool waiting_for_clear_local_storage_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100410 bool waiting_for_clear_logged_in_predictor_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000411 bool waiting_for_clear_nacl_cache_;
412 bool waiting_for_clear_network_predictor_;
413 bool waiting_for_clear_networking_history_;
414 bool waiting_for_clear_plugin_data_;
415 bool waiting_for_clear_quota_managed_data_;
416 bool waiting_for_clear_server_bound_certs_;
417 bool waiting_for_clear_session_storage_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100418 bool waiting_for_clear_shader_cache_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000419
420 // Tracking how many origins need to be deleted, and whether we're finished
421 // gathering origins.
422 int quota_managed_origins_to_delete_count_;
423 int quota_managed_storage_types_to_delete_count_;
424
425 // The removal mask for the current removal operation.
426 int remove_mask_;
427
428 // The origin for the current removal operation.
429 GURL remove_origin_;
430
431 // From which types of origins should we remove data?
432 int origin_set_mask_;
433
434 ObserverList<Observer> observer_list_;
435
436 // Used if we need to clear history.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000437 CancelableTaskTracker history_task_tracker_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000438
439 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
440};
441
442#endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_