Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.h
blob0333d9fe2762e0d9be5661b4489e6b53842ef0af
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"
25 #include "url/origin.h"
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 net {
40 class URLRequestContextGetter;
43 // BrowsingDataRemover is responsible for removing data related to browsing:
44 // visits in url database, downloads, cookies ...
46 class BrowsingDataRemover
47 #if defined(ENABLE_PLUGINS)
48 : public PepperFlashSettingsManager::Client
49 #endif
51 public:
52 // Time period ranges available when doing browsing data removals.
53 enum TimePeriod {
54 LAST_HOUR = 0,
55 LAST_DAY,
56 LAST_WEEK,
57 FOUR_WEEKS,
58 EVERYTHING
61 // Mask used for Remove.
62 enum RemoveDataMask {
63 REMOVE_APPCACHE = 1 << 0,
64 REMOVE_CACHE = 1 << 1,
65 REMOVE_COOKIES = 1 << 2,
66 REMOVE_DOWNLOADS = 1 << 3,
67 REMOVE_FILE_SYSTEMS = 1 << 4,
68 REMOVE_FORM_DATA = 1 << 5,
69 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
70 REMOVE_HISTORY = 1 << 6,
71 REMOVE_INDEXEDDB = 1 << 7,
72 REMOVE_LOCAL_STORAGE = 1 << 8,
73 REMOVE_PLUGIN_DATA = 1 << 9,
74 REMOVE_PASSWORDS = 1 << 10,
75 REMOVE_WEBSQL = 1 << 11,
76 REMOVE_CHANNEL_IDS = 1 << 12,
77 REMOVE_CONTENT_LICENSES = 1 << 13,
78 REMOVE_SERVICE_WORKERS = 1 << 14,
79 REMOVE_SITE_USAGE_DATA = 1 << 15,
80 // REMOVE_NOCHECKS intentionally does not check if the Profile's prohibited
81 // from deleting history or downloads.
82 REMOVE_NOCHECKS = 1 << 16,
83 REMOVE_WEBRTC_IDENTITY = 1 << 17,
84 REMOVE_CACHE_STORAGE = 1 << 18,
85 // 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,
90 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
91 // storage, webSQL, service workers, cache storage, and plugin data.
92 REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
93 REMOVE_INDEXEDDB |
94 REMOVE_LOCAL_STORAGE |
95 REMOVE_PLUGIN_DATA |
96 REMOVE_SERVICE_WORKERS |
97 REMOVE_CACHE_STORAGE |
98 REMOVE_WEBSQL |
99 REMOVE_CHANNEL_IDS |
100 REMOVE_SITE_USAGE_DATA |
101 REMOVE_WEBRTC_IDENTITY,
103 // Includes all the available remove options. Meant to be used by clients
104 // that wish to wipe as much data as possible from a Profile, to make it
105 // look like a new Profile.
106 REMOVE_ALL = REMOVE_SITE_DATA | REMOVE_CACHE | REMOVE_DOWNLOADS |
107 REMOVE_FORM_DATA |
108 REMOVE_HISTORY |
109 REMOVE_PASSWORDS |
110 REMOVE_CONTENT_LICENSES,
112 // Includes all available remove options. Meant to be used when the Profile
113 // is scheduled to be deleted, and all possible data should be wiped from
114 // disk as soon as possible.
115 REMOVE_WIPE_PROFILE = REMOVE_ALL | REMOVE_NOCHECKS,
118 // A helper enum to report the deletion of cookies and/or cache. Do not
119 // reorder the entries, as this enum is passed to UMA.
120 enum CookieOrCacheDeletionChoice {
121 NEITHER_COOKIES_NOR_CACHE,
122 ONLY_COOKIES,
123 ONLY_CACHE,
124 BOTH_COOKIES_AND_CACHE,
125 MAX_CHOICE_VALUE
128 // When BrowsingDataRemover successfully removes data, a notification of type
129 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
130 // this type.
131 struct NotificationDetails {
132 NotificationDetails();
133 NotificationDetails(const NotificationDetails& details);
134 NotificationDetails(base::Time removal_begin,
135 int removal_mask,
136 int origin_type_mask);
137 ~NotificationDetails();
139 // The beginning of the removal time range.
140 base::Time removal_begin;
142 // The removal mask (see the RemoveDataMask enum for details).
143 int removal_mask;
145 // The origin type mask (see BrowsingDataHelper::OriginTypeMask for
146 // details).
147 int origin_type_mask;
150 // Observer is notified when the removal is done. Done means keywords have
151 // been deleted, cache cleared and all other tasks scheduled.
152 class Observer {
153 public:
154 virtual void OnBrowsingDataRemoverDone() = 0;
156 protected:
157 virtual ~Observer() {}
160 using Callback = base::Callback<void(const NotificationDetails&)>;
161 using CallbackSubscription = scoped_ptr<
162 base::CallbackList<void(const NotificationDetails&)>::Subscription>;
164 // The completion inhibitor can artificially delay completion of the browsing
165 // data removal process. It is used during testing to simulate scenarios in
166 // which the deletion stalls or takes a very long time.
167 class CompletionInhibitor {
168 public:
169 // Invoked when a |remover| is just about to complete clearing browser data,
170 // and will be prevented from completing until after the callback
171 // |continue_to_completion| is run.
172 virtual void OnBrowsingDataRemoverWouldComplete(
173 BrowsingDataRemover* remover,
174 const base::Closure& continue_to_completion) = 0;
176 protected:
177 virtual ~CompletionInhibitor() {}
180 // Creates a BrowsingDataRemover object that removes data regardless of the
181 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
182 // retains ownership of itself, and deletes itself once finished.
183 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
185 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
186 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
187 // deletes itself once finished.
188 static BrowsingDataRemover* CreateForRange(Profile* profile,
189 base::Time delete_begin,
190 base::Time delete_end);
192 // Creates a BrowsingDataRemover bound to a specific period of time (as
193 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
194 // retains ownership of itself, and deletes itself once finished.
195 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
196 TimePeriod period);
198 // Calculate the begin time for the deletion range specified by |time_period|.
199 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
201 // Is the BrowsingDataRemover currently in the process of removing data?
202 static bool is_removing() { return is_removing_; }
204 // Sets a CompletionInhibitor, which will be notified each time an instance is
205 // about to complete a browsing data removal process, and will be able to
206 // artificially delay the completion.
207 static void set_completion_inhibitor_for_testing(
208 CompletionInhibitor* inhibitor) {
209 completion_inhibitor_ = inhibitor;
212 // Add a callback to the list of callbacks to be called during a browsing data
213 // removal event. Returns a subscription object that can be used to
214 // un-register the callback.
215 static CallbackSubscription RegisterOnBrowsingDataRemovedCallback(
216 const Callback& callback);
218 // Removes the specified items related to browsing for all origins that match
219 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask).
220 void Remove(int remove_mask, int origin_type_mask);
222 void AddObserver(Observer* observer);
223 void RemoveObserver(Observer* observer);
225 // Called when history deletion is done.
226 void OnHistoryDeletionDone();
228 // Used for testing.
229 void OverrideStoragePartitionForTesting(
230 content::StoragePartition* storage_partition);
232 private:
233 // The clear API needs to be able to toggle removing_ in order to test that
234 // only one BrowsingDataRemover instance can be called at a time.
235 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
237 // The BrowsingDataRemover tests need to be able to access the implementation
238 // of Remove(), as it exposes details that aren't yet available in the public
239 // API. As soon as those details are exposed via new methods, this should be
240 // removed.
242 // TODO(mkwst): See http://crbug.com/113621
243 friend class BrowsingDataRemoverTest;
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>;
260 // When plugins aren't enabled, there is no base class, so adding an override
261 // specifier would result in a compile error.
262 #if defined(ENABLE_PLUGINS)
263 ~BrowsingDataRemover() override;
264 #else
265 ~BrowsingDataRemover();
266 #endif
268 // Callback for when TemplateURLService has finished loading. Clears the data,
269 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
270 void OnKeywordsLoaded();
272 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
273 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
275 #if defined(ENABLE_PLUGINS)
276 // PepperFlashSettingsManager::Client implementation.
277 void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
278 bool success) override;
279 #endif
281 #if defined (OS_CHROMEOS)
282 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
283 bool result);
284 #endif
286 // Removes the specified items related to browsing for a specific host. If the
287 // provided |remove_url| is empty, data is removed for all origins. The
288 // |origin_type_mask| parameter defines the set of origins from which data
289 // should be removed (protected, unprotected, or both).
290 // TODO(mkwst): The current implementation relies on unique (empty) origins to
291 // signal removal of all origins. Reconsider this behavior if/when we build
292 // a "forget this site" feature.
293 void RemoveImpl(int remove_mask,
294 const GURL& remove_url,
295 int origin_type_mask);
297 // Notifies observers and deletes this object.
298 void NotifyAndDelete();
300 // Checks if we are all done, and if so, calls NotifyAndDelete().
301 void NotifyAndDeleteIfDone();
303 // Callback for when the hostname resolution cache has been cleared.
304 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
305 void OnClearedHostnameResolutionCache();
307 // Invoked on the IO thread to clear the hostname resolution cache.
308 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
310 // Callback for when speculative data in the network Predictor has been
311 // cleared. Clears the respective waiting flag and invokes
312 // NotifyAndDeleteIfDone.
313 void OnClearedNetworkPredictor();
315 // Invoked on the IO thread to clear speculative data related to hostname
316 // pre-resolution from the network Predictor.
317 void ClearNetworkPredictorOnIOThread(
318 chrome_browser_net::Predictor* predictor);
320 // Callback for when network related data in ProfileIOData has been cleared.
321 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
322 void OnClearedNetworkingHistory();
324 // Callback for when the cache has been deleted. Invokes
325 // NotifyAndDeleteIfDone.
326 void ClearedCache();
327 #if !defined(DISABLE_NACL)
328 // Callback for when the NaCl cache has been deleted. Invokes
329 // NotifyAndDeleteIfDone.
330 void ClearedNaClCache();
332 // Invokes the ClearedNaClCache on the UI thread.
333 void ClearedNaClCacheOnIOThread();
335 // Invoked on the IO thread to delete the NaCl cache.
336 void ClearNaClCacheOnIOThread();
338 // Callback for when the PNaCl translation cache has been deleted. Invokes
339 // NotifyAndDeleteIfDone.
340 void ClearedPnaclCache();
342 // Invokes ClearedPnaclCacheOn on the UI thread.
343 void ClearedPnaclCacheOnIOThread();
345 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
346 void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
347 #endif
349 // Callback for when passwords for the requested time range have been cleared.
350 void OnClearedPasswords();
352 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
353 void OnClearedCookies(int num_deleted);
355 // Invoked on the IO thread to delete cookies.
356 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
358 // Invoked on the IO thread to delete channel IDs.
359 void ClearChannelIDsOnIOThread(
360 net::URLRequestContextGetter* rq_context);
362 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
363 // connection pool and posts to UI thread to run OnClearedChannelIDs.
364 void OnClearedChannelIDsOnIOThread(
365 net::URLRequestContextGetter* rq_context);
367 // Callback for when channel IDs have been deleted. Invokes
368 // NotifyAndDeleteIfDone.
369 void OnClearedChannelIDs();
371 // Callback from the above method.
372 void OnClearedFormData();
374 // Callback for when the Autofill profile and credit card origin URLs have
375 // been deleted.
376 void OnClearedAutofillOriginURLs();
378 // Callback on UI thread when the storage partition related data are cleared.
379 void OnClearedStoragePartitionData();
381 #if defined(ENABLE_WEBRTC)
382 // Callback on UI thread when the WebRTC logs have been deleted.
383 void OnClearedWebRtcLogs();
384 #endif
386 #if defined(OS_ANDROID)
387 // Callback on UI thread when the precache history has been cleared.
388 void OnClearedPrecacheHistory();
389 #endif
391 void OnClearedDomainReliabilityMonitor();
393 // Returns true if we're all done.
394 bool AllDone();
396 // Profile we're to remove from.
397 Profile* profile_;
399 // Start time to delete from.
400 const base::Time delete_begin_;
402 // End time to delete to.
403 base::Time delete_end_;
405 // True if Remove has been invoked.
406 static bool is_removing_;
408 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
409 // is about to complete a browsing data removal process, and has the ability
410 // to artificially delay completion. Used for testing.
411 static CompletionInhibitor* completion_inhibitor_;
413 // Used to delete data from HTTP cache.
414 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
415 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
417 #if defined(ENABLE_PLUGINS)
418 // Used to delete plugin data.
419 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
420 base::WaitableEventWatcher watcher_;
422 // Used to deauthorize content licenses for Pepper Flash.
423 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
424 #endif
426 uint32 deauthorize_content_licenses_request_id_ = 0;
427 // True if we're waiting for various data to be deleted.
428 // These may only be accessed from UI thread in order to avoid races!
429 bool waiting_for_clear_autofill_origin_urls_ = false;
430 bool waiting_for_clear_cache_ = false;
431 bool waiting_for_clear_channel_ids_ = false;
432 bool waiting_for_clear_content_licenses_ = false;
433 // Non-zero if waiting for cookies to be cleared.
434 int waiting_for_clear_cookies_count_ = 0;
435 bool waiting_for_clear_domain_reliability_monitor_ = false;
436 bool waiting_for_clear_form_ = false;
437 bool waiting_for_clear_history_ = false;
438 bool waiting_for_clear_hostname_resolution_cache_ = false;
439 bool waiting_for_clear_keyword_data_ = false;
440 bool waiting_for_clear_nacl_cache_ = false;
441 bool waiting_for_clear_network_predictor_ = false;
442 bool waiting_for_clear_networking_history_ = false;
443 bool waiting_for_clear_passwords_ = false;
444 bool waiting_for_clear_platform_keys_ = false;
445 bool waiting_for_clear_plugin_data_ = false;
446 bool waiting_for_clear_pnacl_cache_ = false;
447 #if defined(OS_ANDROID)
448 bool waiting_for_clear_precache_history_ = false;
449 #endif
450 bool waiting_for_clear_storage_partition_data_ = false;
451 #if defined(ENABLE_WEBRTC)
452 bool waiting_for_clear_webrtc_logs_ = false;
453 #endif
455 // The removal mask for the current removal operation.
456 int remove_mask_ = 0;
458 // From which types of origins should we remove data?
459 int origin_type_mask_ = 0;
461 base::ObserverList<Observer> observer_list_;
463 // Used if we need to clear history.
464 base::CancelableTaskTracker history_task_tracker_;
466 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
468 // We do not own this.
469 content::StoragePartition* storage_partition_for_testing_ = nullptr;
471 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
474 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_