ExtensionSyncService cleanup: process uninstalls in one step
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.h
blobb052bfce62fbdb076531ae1919010bdf1b3f85b5
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 IOThread;
27 class Profile;
29 namespace chrome_browser_net {
30 class Predictor;
33 namespace content {
34 class PluginDataRemover;
35 class StoragePartition;
38 namespace net {
39 class URLRequestContextGetter;
42 // BrowsingDataRemover is responsible for removing data related to browsing:
43 // visits in url database, downloads, cookies ...
45 class BrowsingDataRemover
46 #if defined(ENABLE_PLUGINS)
47 : public PepperFlashSettingsManager::Client
48 #endif
50 public:
51 // Time period ranges available when doing browsing data removals.
52 enum TimePeriod {
53 LAST_HOUR = 0,
54 LAST_DAY,
55 LAST_WEEK,
56 FOUR_WEEKS,
57 EVERYTHING
60 // Mask used for Remove.
61 enum RemoveDataMask {
62 REMOVE_APPCACHE = 1 << 0,
63 REMOVE_CACHE = 1 << 1,
64 REMOVE_COOKIES = 1 << 2,
65 REMOVE_DOWNLOADS = 1 << 3,
66 REMOVE_FILE_SYSTEMS = 1 << 4,
67 REMOVE_FORM_DATA = 1 << 5,
68 // In addition to visits, REMOVE_HISTORY removes keywords and last session.
69 REMOVE_HISTORY = 1 << 6,
70 REMOVE_INDEXEDDB = 1 << 7,
71 REMOVE_LOCAL_STORAGE = 1 << 8,
72 REMOVE_PLUGIN_DATA = 1 << 9,
73 REMOVE_PASSWORDS = 1 << 10,
74 REMOVE_WEBSQL = 1 << 11,
75 REMOVE_CHANNEL_IDS = 1 << 12,
76 REMOVE_CONTENT_LICENSES = 1 << 13,
77 REMOVE_SERVICE_WORKERS = 1 << 14,
78 REMOVE_SITE_USAGE_DATA = 1 << 15,
79 // REMOVE_NOCHECKS intentionally does not check if the Profile's prohibited
80 // from deleting history or downloads.
81 REMOVE_NOCHECKS = 1 << 16,
82 // The following flag is used only in tests. In normal usage, hosted app
83 // data is controlled by the REMOVE_COOKIES flag, applied to the
84 // protected-web origin.
85 REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
87 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
88 // storage, webSQL, service workers, and plugin data.
89 REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
90 REMOVE_INDEXEDDB |
91 REMOVE_LOCAL_STORAGE |
92 REMOVE_PLUGIN_DATA |
93 REMOVE_SERVICE_WORKERS |
94 REMOVE_WEBSQL |
95 REMOVE_CHANNEL_IDS |
96 REMOVE_SITE_USAGE_DATA,
98 // Includes all the available remove options. Meant to be used by clients
99 // that wish to wipe as much data as possible from a Profile, to make it
100 // look like a new Profile.
101 REMOVE_ALL = REMOVE_SITE_DATA | REMOVE_CACHE | REMOVE_DOWNLOADS |
102 REMOVE_FORM_DATA |
103 REMOVE_HISTORY |
104 REMOVE_PASSWORDS |
105 REMOVE_CONTENT_LICENSES,
107 // Includes all available remove options. Meant to be used when the Profile
108 // is scheduled to be deleted, and all possible data should be wiped from
109 // disk as soon as possible.
110 REMOVE_WIPE_PROFILE = REMOVE_ALL | REMOVE_NOCHECKS,
113 // When BrowsingDataRemover successfully removes data, a notification of type
114 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
115 // this type.
116 struct NotificationDetails {
117 NotificationDetails();
118 NotificationDetails(const NotificationDetails& details);
119 NotificationDetails(base::Time removal_begin,
120 int removal_mask,
121 int origin_type_mask);
122 ~NotificationDetails();
124 // The beginning of the removal time range.
125 base::Time removal_begin;
127 // The removal mask (see the RemoveDataMask enum for details).
128 int removal_mask;
130 // The origin type mask (see BrowsingDataHelper::OriginTypeMask for
131 // details).
132 int origin_type_mask;
135 // Observer is notified when the removal is done. Done means keywords have
136 // been deleted, cache cleared and all other tasks scheduled.
137 class Observer {
138 public:
139 virtual void OnBrowsingDataRemoverDone() = 0;
141 protected:
142 virtual ~Observer() {}
145 using Callback = base::Callback<void(const NotificationDetails&)>;
146 using CallbackSubscription = scoped_ptr<
147 base::CallbackList<void(const NotificationDetails&)>::Subscription>;
149 // The completion inhibitor can artificially delay completion of the browsing
150 // data removal process. It is used during testing to simulate scenarios in
151 // which the deletion stalls or takes a very long time.
152 class CompletionInhibitor {
153 public:
154 // Invoked when a |remover| is just about to complete clearing browser data,
155 // and will be prevented from completing until after the callback
156 // |continue_to_completion| is run.
157 virtual void OnBrowsingDataRemoverWouldComplete(
158 BrowsingDataRemover* remover,
159 const base::Closure& continue_to_completion) = 0;
161 protected:
162 virtual ~CompletionInhibitor() {}
165 // Creates a BrowsingDataRemover object that removes data regardless of the
166 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
167 // retains ownership of itself, and deletes itself once finished.
168 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
170 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
171 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
172 // deletes itself once finished.
173 static BrowsingDataRemover* CreateForRange(Profile* profile,
174 base::Time delete_begin,
175 base::Time delete_end);
177 // Creates a BrowsingDataRemover bound to a specific period of time (as
178 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
179 // retains ownership of itself, and deletes itself once finished.
180 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
181 TimePeriod period);
183 // Calculate the begin time for the deletion range specified by |time_period|.
184 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
186 // Is the BrowsingDataRemover currently in the process of removing data?
187 static bool is_removing() { return is_removing_; }
189 // Sets a CompletionInhibitor, which will be notified each time an instance is
190 // about to complete a browsing data removal process, and will be able to
191 // artificially delay the completion.
192 static void set_completion_inhibitor_for_testing(
193 CompletionInhibitor* inhibitor) {
194 completion_inhibitor_ = inhibitor;
197 // Add a callback to the list of callbacks to be called during a browsing data
198 // removal event. Returns a subscription object that can be used to
199 // un-register the callback.
200 static CallbackSubscription RegisterOnBrowsingDataRemovedCallback(
201 const Callback& callback);
203 // Removes the specified items related to browsing for all origins that match
204 // the provided |origin_type_mask| (see BrowsingDataHelper::OriginTypeMask).
205 void Remove(int remove_mask, int origin_type_mask);
207 void AddObserver(Observer* observer);
208 void RemoveObserver(Observer* observer);
210 // Called when history deletion is done.
211 void OnHistoryDeletionDone();
213 // Used for testing.
214 void OverrideStoragePartitionForTesting(
215 content::StoragePartition* storage_partition);
217 private:
218 // The clear API needs to be able to toggle removing_ in order to test that
219 // only one BrowsingDataRemover instance can be called at a time.
220 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
222 // The BrowsingDataRemover tests need to be able to access the implementation
223 // of Remove(), as it exposes details that aren't yet available in the public
224 // API. As soon as those details are exposed via new methods, this should be
225 // removed.
227 // TODO(mkwst): See http://crbug.com/113621
228 friend class BrowsingDataRemoverTest;
230 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
231 // not already removing, and vice-versa.
232 static void set_removing(bool is_removing);
234 // Creates a BrowsingDataRemover to remove browser data from the specified
235 // profile in the specified time range. Use Remove to initiate the removal.
236 BrowsingDataRemover(Profile* profile,
237 base::Time delete_begin,
238 base::Time delete_end);
240 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
241 // to be deleted by other objects so make destructor private and DeleteHelper
242 // a friend.
243 friend class base::DeleteHelper<BrowsingDataRemover>;
245 // When plugins aren't enabled, there is no base class, so adding an override
246 // specifier would result in a compile error.
247 #if defined(ENABLE_PLUGINS)
248 ~BrowsingDataRemover() override;
249 #else
250 ~BrowsingDataRemover();
251 #endif
253 // Callback for when TemplateURLService has finished loading. Clears the data,
254 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
255 void OnKeywordsLoaded();
257 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
258 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
260 #if defined(ENABLE_PLUGINS)
261 // PepperFlashSettingsManager::Client implementation.
262 void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
263 bool success) override;
264 #endif
266 #if defined (OS_CHROMEOS)
267 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
268 bool result);
269 #endif
271 // Removes the specified items related to browsing for a specific host. If the
272 // provided |origin| is empty, data is removed for all origins. The
273 // |origin_type_mask| parameter defines the set of origins from which data
274 // should be removed (protected, unprotected, or both).
275 void RemoveImpl(int remove_mask,
276 const GURL& origin,
277 int origin_type_mask);
279 // Notifies observers and deletes this object.
280 void NotifyAndDelete();
282 // Checks if we are all done, and if so, calls NotifyAndDelete().
283 void NotifyAndDeleteIfDone();
285 // Callback for when the hostname resolution cache has been cleared.
286 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
287 void OnClearedHostnameResolutionCache();
289 // Invoked on the IO thread to clear the hostname resolution cache.
290 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
292 // Callback for when speculative data in the network Predictor has been
293 // cleared. Clears the respective waiting flag and invokes
294 // NotifyAndDeleteIfDone.
295 void OnClearedNetworkPredictor();
297 // Invoked on the IO thread to clear speculative data related to hostname
298 // pre-resolution from the network Predictor.
299 void ClearNetworkPredictorOnIOThread(
300 chrome_browser_net::Predictor* predictor);
302 // Callback for when network related data in ProfileIOData has been cleared.
303 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
304 void OnClearedNetworkingHistory();
306 // Callback for when the cache has been deleted. Invokes
307 // NotifyAndDeleteIfDone.
308 void ClearedCache();
309 #if !defined(DISABLE_NACL)
310 // Callback for when the NaCl cache has been deleted. Invokes
311 // NotifyAndDeleteIfDone.
312 void ClearedNaClCache();
314 // Invokes the ClearedNaClCache on the UI thread.
315 void ClearedNaClCacheOnIOThread();
317 // Invoked on the IO thread to delete the NaCl cache.
318 void ClearNaClCacheOnIOThread();
320 // Callback for when the PNaCl translation cache has been deleted. Invokes
321 // NotifyAndDeleteIfDone.
322 void ClearedPnaclCache();
324 // Invokes ClearedPnaclCacheOn on the UI thread.
325 void ClearedPnaclCacheOnIOThread();
327 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
328 void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
329 #endif
331 // Callback for when passwords for the requested time range have been cleared.
332 void OnClearedPasswords();
334 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
335 void OnClearedCookies(int num_deleted);
337 // Invoked on the IO thread to delete cookies.
338 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
340 // Invoked on the IO thread to delete channel IDs.
341 void ClearChannelIDsOnIOThread(
342 net::URLRequestContextGetter* rq_context);
344 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
345 // connection pool and posts to UI thread to run OnClearedChannelIDs.
346 void OnClearedChannelIDsOnIOThread(
347 net::URLRequestContextGetter* rq_context);
349 // Callback for when channel IDs have been deleted. Invokes
350 // NotifyAndDeleteIfDone.
351 void OnClearedChannelIDs();
353 // Callback from the above method.
354 void OnClearedFormData();
356 // Callback for when the Autofill profile and credit card origin URLs have
357 // been deleted.
358 void OnClearedAutofillOriginURLs();
360 // Callback on UI thread when the storage partition related data are cleared.
361 void OnClearedStoragePartitionData();
363 #if defined(ENABLE_WEBRTC)
364 // Callback on UI thread when the WebRTC logs have been deleted.
365 void OnClearedWebRtcLogs();
366 #endif
368 void OnClearedDomainReliabilityMonitor();
370 // Returns true if we're all done.
371 bool AllDone();
373 // Profile we're to remove from.
374 Profile* profile_;
376 // Start time to delete from.
377 const base::Time delete_begin_;
379 // End time to delete to.
380 base::Time delete_end_;
382 // True if Remove has been invoked.
383 static bool is_removing_;
385 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
386 // is about to complete a browsing data removal process, and has the ability
387 // to artificially delay completion. Used for testing.
388 static CompletionInhibitor* completion_inhibitor_;
390 // Used to delete data from HTTP cache.
391 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
392 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
394 #if defined(ENABLE_PLUGINS)
395 // Used to delete plugin data.
396 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
397 base::WaitableEventWatcher watcher_;
399 // Used to deauthorize content licenses for Pepper Flash.
400 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
401 #endif
403 uint32 deauthorize_content_licenses_request_id_;
404 // True if we're waiting for various data to be deleted.
405 // These may only be accessed from UI thread in order to avoid races!
406 bool waiting_for_clear_autofill_origin_urls_;
407 bool waiting_for_clear_cache_;
408 bool waiting_for_clear_channel_ids_;
409 bool waiting_for_clear_content_licenses_;
410 // Non-zero if waiting for cookies to be cleared.
411 int waiting_for_clear_cookies_count_;
412 bool waiting_for_clear_domain_reliability_monitor_;
413 bool waiting_for_clear_form_;
414 bool waiting_for_clear_history_;
415 bool waiting_for_clear_hostname_resolution_cache_;
416 bool waiting_for_clear_keyword_data_;
417 bool waiting_for_clear_nacl_cache_;
418 bool waiting_for_clear_network_predictor_;
419 bool waiting_for_clear_networking_history_;
420 bool waiting_for_clear_passwords_;
421 bool waiting_for_clear_platform_keys_;
422 bool waiting_for_clear_plugin_data_;
423 bool waiting_for_clear_pnacl_cache_;
424 bool waiting_for_clear_storage_partition_data_;
425 #if defined(ENABLE_WEBRTC)
426 bool waiting_for_clear_webrtc_logs_;
427 #endif
429 // The removal mask for the current removal operation.
430 int remove_mask_;
432 // The origin for the current removal operation.
433 GURL remove_origin_;
435 // From which types of origins should we remove data?
436 int origin_type_mask_;
438 base::ObserverList<Observer> observer_list_;
440 // Used if we need to clear history.
441 base::CancelableTaskTracker history_task_tracker_;
443 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
445 // We do not own this.
446 content::StoragePartition* storage_partition_for_testing_;
448 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
451 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_