Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.h
blob3871455e713cb56d3a1cea2ef2aa7f5afd201932
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_
8 #include <set>
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"
22 #endif
23 #include "storage/common/quota/quota_types.h"
24 #include "url/gurl.h"
26 class ExtensionSpecialStoragePolicy;
27 class IOThread;
28 class Profile;
30 namespace chrome_browser_net {
31 class Predictor;
34 namespace content {
35 class PluginDataRemover;
36 class StoragePartition;
39 namespace disk_cache {
40 class Backend;
43 namespace net {
44 class URLRequestContextGetter;
47 namespace storage {
48 class QuotaManager;
51 namespace content {
52 class DOMStorageContext;
53 struct LocalStorageUsageInfo;
54 struct SessionStorageUsageInfo;
57 // BrowsingDataRemover is responsible for removing data related to browsing:
58 // visits in url database, downloads, cookies ...
60 class BrowsingDataRemover
61 #if defined(ENABLE_PLUGINS)
62 : public PepperFlashSettingsManager::Client
63 #endif
65 public:
66 // Time period ranges available when doing browsing data removals.
67 enum TimePeriod {
68 LAST_HOUR = 0,
69 LAST_DAY,
70 LAST_WEEK,
71 FOUR_WEEKS,
72 EVERYTHING
75 // Mask used for Remove.
76 enum RemoveDataMask {
77 REMOVE_APPCACHE = 1 << 0,
78 REMOVE_CACHE = 1 << 1,
79 REMOVE_COOKIES = 1 << 2,
80 REMOVE_DOWNLOADS = 1 << 3,
81 REMOVE_FILE_SYSTEMS = 1 << 4,
82 REMOVE_FORM_DATA = 1 << 5,
83 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
84 REMOVE_HISTORY = 1 << 6,
85 REMOVE_INDEXEDDB = 1 << 7,
86 REMOVE_LOCAL_STORAGE = 1 << 8,
87 REMOVE_PLUGIN_DATA = 1 << 9,
88 REMOVE_PASSWORDS = 1 << 10,
89 REMOVE_WEBSQL = 1 << 11,
90 REMOVE_CHANNEL_IDS = 1 << 12,
91 REMOVE_CONTENT_LICENSES = 1 << 13,
92 REMOVE_SERVICE_WORKERS = 1 << 14,
93 #if defined(OS_ANDROID)
94 REMOVE_APP_BANNER_DATA = 1 << 15,
95 #endif
96 #if !defined(OS_ANDROID)
97 REMOVE_GCM = 1 << 16,
98 #endif
99 // The following flag is used only in tests. In normal usage, hosted app
100 // data is controlled by the REMOVE_COOKIES flag, applied to the
101 // protected-web origin.
102 REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
104 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
105 // storage, webSQL, service workers, and plugin data.
106 REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
107 REMOVE_INDEXEDDB |
108 REMOVE_LOCAL_STORAGE |
109 REMOVE_PLUGIN_DATA |
110 REMOVE_SERVICE_WORKERS |
111 REMOVE_WEBSQL |
112 #if defined(OS_ANDROID)
113 REMOVE_APP_BANNER_DATA |
114 #endif
115 #if !defined(OS_ANDROID)
116 REMOVE_GCM |
117 #endif
118 REMOVE_CHANNEL_IDS,
120 // Includes all the available remove options. Meant to be used by clients
121 // that wish to wipe as much data as possible from a Profile, to make it
122 // look like a new Profile.
123 REMOVE_ALL = REMOVE_SITE_DATA | REMOVE_CACHE | REMOVE_DOWNLOADS |
124 REMOVE_FORM_DATA |
125 REMOVE_HISTORY |
126 REMOVE_PASSWORDS |
127 REMOVE_CONTENT_LICENSES,
130 // When BrowsingDataRemover successfully removes data, a notification of type
131 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
132 // this type.
133 struct NotificationDetails {
134 NotificationDetails();
135 NotificationDetails(const NotificationDetails& details);
136 NotificationDetails(base::Time removal_begin,
137 int removal_mask,
138 int origin_set_mask);
139 ~NotificationDetails();
141 // The beginning of the removal time range.
142 base::Time removal_begin;
144 // The removal mask (see the RemoveDataMask enum for details).
145 int removal_mask;
147 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
148 int origin_set_mask;
151 // Observer is notified when the removal is done. Done means keywords have
152 // been deleted, cache cleared and all other tasks scheduled.
153 class Observer {
154 public:
155 virtual void OnBrowsingDataRemoverDone() = 0;
157 protected:
158 virtual ~Observer() {}
161 // The completion inhibitor can artificially delay completion of the browsing
162 // data removal process. It is used during testing to simulate scenarios in
163 // which the deletion stalls or takes a very long time.
164 class CompletionInhibitor {
165 public:
166 // Invoked when a |remover| is just about to complete clearing browser data,
167 // and will be prevented from completing until after the callback
168 // |continue_to_completion| is run.
169 virtual void OnBrowsingDataRemoverWouldComplete(
170 BrowsingDataRemover* remover,
171 const base::Closure& continue_to_completion) = 0;
173 protected:
174 virtual ~CompletionInhibitor() {}
177 // Creates a BrowsingDataRemover object that removes data regardless of the
178 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
179 // retains ownership of itself, and deletes itself once finished.
180 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
182 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
183 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
184 // deletes itself once finished.
185 static BrowsingDataRemover* CreateForRange(Profile* profile,
186 base::Time delete_begin,
187 base::Time delete_end);
189 // Creates a BrowsingDataRemover bound to a specific period of time (as
190 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
191 // retains ownership of itself, and deletes itself once finished.
192 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
193 TimePeriod period);
195 // Calculate the begin time for the deletion range specified by |time_period|.
196 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
198 // Is the BrowsingDataRemover currently in the process of removing data?
199 static bool is_removing() { return is_removing_; }
201 // Sets a CompletionInhibitor, which will be notified each time an instance is
202 // about to complete a browsing data removal process, and will be able to
203 // artificially delay the completion.
204 static void set_completion_inhibitor_for_testing(
205 CompletionInhibitor* inhibitor) {
206 completion_inhibitor_ = inhibitor;
209 // Removes the specified items related to browsing for all origins that match
210 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
211 void Remove(int remove_mask, int origin_set_mask);
213 void AddObserver(Observer* observer);
214 void RemoveObserver(Observer* observer);
216 // Called when history deletion is done.
217 void OnHistoryDeletionDone();
219 // Used for testing.
220 void OverrideStoragePartitionForTesting(
221 content::StoragePartition* storage_partition);
223 private:
224 // The clear API needs to be able to toggle removing_ in order to test that
225 // only one BrowsingDataRemover instance can be called at a time.
226 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
228 // The BrowsingDataRemover tests need to be able to access the implementation
229 // of Remove(), as it exposes details that aren't yet available in the public
230 // API. As soon as those details are exposed via new methods, this should be
231 // removed.
233 // TODO(mkwst): See http://crbug.com/113621
234 friend class BrowsingDataRemoverTest;
236 enum CacheState {
237 STATE_NONE,
238 STATE_CREATE_MAIN,
239 STATE_CREATE_MEDIA,
240 STATE_DELETE_MAIN,
241 STATE_DELETE_MEDIA,
242 STATE_DONE
245 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
246 // not already removing, and vice-versa.
247 static void set_removing(bool is_removing);
249 // Creates a BrowsingDataRemover to remove browser data from the specified
250 // profile in the specified time range. Use Remove to initiate the removal.
251 BrowsingDataRemover(Profile* profile,
252 base::Time delete_begin,
253 base::Time delete_end);
255 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
256 // to be deleted by other objects so make destructor private and DeleteHelper
257 // a friend.
258 friend class base::DeleteHelper<BrowsingDataRemover>;
259 virtual ~BrowsingDataRemover();
261 // Callback for when TemplateURLService has finished loading. Clears the data,
262 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
263 void OnKeywordsLoaded();
265 // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
266 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
268 #if defined(ENABLE_PLUGINS)
269 // PepperFlashSettingsManager::Client implementation.
270 virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
271 bool success) OVERRIDE;
272 #endif
274 #if defined (OS_CHROMEOS)
275 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
276 bool result);
277 #endif
279 // Removes the specified items related to browsing for a specific host. If the
280 // provided |origin| is empty, data is removed for all origins. The
281 // |origin_set_mask| parameter defines the set of origins from which data
282 // should be removed (protected, unprotected, or both).
283 void RemoveImpl(int remove_mask,
284 const GURL& origin,
285 int origin_set_mask);
287 // Notifies observers and deletes this object.
288 void NotifyAndDelete();
290 // Checks if we are all done, and if so, calls NotifyAndDelete().
291 void NotifyAndDeleteIfDone();
293 // Callback for when the hostname resolution cache has been cleared.
294 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
295 void OnClearedHostnameResolutionCache();
297 // Invoked on the IO thread to clear the hostname resolution cache.
298 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
300 // Callback for when the LoggedIn Predictor has been cleared.
301 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
302 void OnClearedLoggedInPredictor();
304 // Clears the LoggedIn Predictor.
305 void ClearLoggedInPredictor();
307 // Callback for when speculative data in the network Predictor has been
308 // cleared. Clears the respective waiting flag and invokes
309 // NotifyAndDeleteIfDone.
310 void OnClearedNetworkPredictor();
312 // Invoked on the IO thread to clear speculative data related to hostname
313 // pre-resolution from the network Predictor.
314 void ClearNetworkPredictorOnIOThread(
315 chrome_browser_net::Predictor* predictor);
317 // Callback for when network related data in ProfileIOData has been cleared.
318 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
319 void OnClearedNetworkingHistory();
321 // Callback for when the cache has been deleted. Invokes
322 // NotifyAndDeleteIfDone.
323 void ClearedCache();
325 // Invoked on the IO thread to delete from the cache.
326 void ClearCacheOnIOThread();
328 // Performs the actual work to delete the cache.
329 void DoClearCache(int rv);
331 #if !defined(DISABLE_NACL)
332 // Callback for when the NaCl cache has been deleted. Invokes
333 // NotifyAndDeleteIfDone.
334 void ClearedNaClCache();
336 // Invokes the ClearedNaClCache on the UI thread.
337 void ClearedNaClCacheOnIOThread();
339 // Invoked on the IO thread to delete the NaCl cache.
340 void ClearNaClCacheOnIOThread();
342 // Callback for when the PNaCl translation cache has been deleted. Invokes
343 // NotifyAndDeleteIfDone.
344 void ClearedPnaclCache();
346 // Invokes ClearedPnaclCacheOn on the UI thread.
347 void ClearedPnaclCacheOnIOThread();
349 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
350 void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
351 #endif
353 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
354 void OnClearedCookies(int num_deleted);
356 // Invoked on the IO thread to delete cookies.
357 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
359 // Invoked on the IO thread to delete channel IDs.
360 void ClearChannelIDsOnIOThread(
361 net::URLRequestContextGetter* rq_context);
363 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
364 // connection pool and posts to UI thread to run OnClearedChannelIDs.
365 void OnClearedChannelIDsOnIOThread(
366 net::URLRequestContextGetter* rq_context);
368 // Callback for when channel IDs have been deleted. Invokes
369 // NotifyAndDeleteIfDone.
370 void OnClearedChannelIDs();
372 // Callback from the above method.
373 void OnClearedFormData();
375 // Callback for when the Autofill profile and credit card origin URLs have
376 // been deleted.
377 void OnClearedAutofillOriginURLs();
379 // Callback on UI thread when the storage partition related data are cleared.
380 void OnClearedStoragePartitionData();
382 #if defined(ENABLE_WEBRTC)
383 // Callback on UI thread when the WebRTC logs have been deleted.
384 void OnClearedWebRtcLogs();
385 #endif
387 void OnClearedDomainReliabilityMonitor();
389 // Returns true if we're all done.
390 bool AllDone();
392 // Profile we're to remove from.
393 Profile* profile_;
395 // Start time to delete from.
396 const base::Time delete_begin_;
398 // End time to delete to.
399 base::Time delete_end_;
401 // True if Remove has been invoked.
402 static bool is_removing_;
404 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
405 // is about to complete a browsing data removal process, and has the ability
406 // to artificially delay completion. Used for testing.
407 static CompletionInhibitor* completion_inhibitor_;
409 CacheState next_cache_state_;
410 disk_cache::Backend* cache_;
412 // Used to delete data from HTTP cache.
413 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
414 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
416 #if defined(ENABLE_PLUGINS)
417 // Used to delete plugin data.
418 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
419 base::WaitableEventWatcher watcher_;
421 // Used to deauthorize content licenses for Pepper Flash.
422 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
423 #endif
425 uint32 deauthorize_content_licenses_request_id_;
426 // True if we're waiting for various data to be deleted.
427 // These may only be accessed from UI thread in order to avoid races!
428 bool waiting_for_clear_autofill_origin_urls_;
429 bool waiting_for_clear_cache_;
430 bool waiting_for_clear_channel_ids_;
431 bool waiting_for_clear_content_licenses_;
432 // Non-zero if waiting for cookies to be cleared.
433 int waiting_for_clear_cookies_count_;
434 bool waiting_for_clear_domain_reliability_monitor_;
435 bool waiting_for_clear_form_;
436 bool waiting_for_clear_history_;
437 bool waiting_for_clear_hostname_resolution_cache_;
438 bool waiting_for_clear_keyword_data_;
439 bool waiting_for_clear_logged_in_predictor_;
440 bool waiting_for_clear_nacl_cache_;
441 bool waiting_for_clear_network_predictor_;
442 bool waiting_for_clear_networking_history_;
443 bool waiting_for_clear_platform_keys_;
444 bool waiting_for_clear_plugin_data_;
445 bool waiting_for_clear_pnacl_cache_;
446 bool waiting_for_clear_storage_partition_data_;
447 #if defined(ENABLE_WEBRTC)
448 bool waiting_for_clear_webrtc_logs_;
449 #endif
451 // The removal mask for the current removal operation.
452 int remove_mask_;
454 // The origin for the current removal operation.
455 GURL remove_origin_;
457 // From which types of origins should we remove data?
458 int origin_set_mask_;
460 ObserverList<Observer> observer_list_;
462 // Used if we need to clear history.
463 base::CancelableTaskTracker history_task_tracker_;
465 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
467 // We do not own this.
468 content::StoragePartition* storage_partition_for_testing_;
470 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
473 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_