Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_remover.h
blob0e470f25fa2319684f9867681a1a9fd5fd2375ff
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 #if defined(OS_ANDROID)
79 REMOVE_APP_BANNER_DATA = 1 << 15,
80 #endif
81 // The following flag is used only in tests. In normal usage, hosted app
82 // data is controlled by the REMOVE_COOKIES flag, applied to the
83 // protected-web origin.
84 REMOVE_HOSTED_APP_DATA_TESTONLY = 1 << 31,
86 // "Site data" includes cookies, appcache, file systems, indexedDBs, local
87 // storage, webSQL, service workers, and plugin data.
88 REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
89 REMOVE_INDEXEDDB |
90 REMOVE_LOCAL_STORAGE |
91 REMOVE_PLUGIN_DATA |
92 REMOVE_SERVICE_WORKERS |
93 REMOVE_WEBSQL |
94 #if defined(OS_ANDROID)
95 REMOVE_APP_BANNER_DATA |
96 #endif
97 REMOVE_CHANNEL_IDS,
99 // Includes all the available remove options. Meant to be used by clients
100 // that wish to wipe as much data as possible from a Profile, to make it
101 // look like a new Profile.
102 REMOVE_ALL = REMOVE_SITE_DATA | REMOVE_CACHE | REMOVE_DOWNLOADS |
103 REMOVE_FORM_DATA |
104 REMOVE_HISTORY |
105 REMOVE_PASSWORDS |
106 REMOVE_CONTENT_LICENSES,
109 // When BrowsingDataRemover successfully removes data, a notification of type
110 // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
111 // this type.
112 struct NotificationDetails {
113 NotificationDetails();
114 NotificationDetails(const NotificationDetails& details);
115 NotificationDetails(base::Time removal_begin,
116 int removal_mask,
117 int origin_set_mask);
118 ~NotificationDetails();
120 // The beginning of the removal time range.
121 base::Time removal_begin;
123 // The removal mask (see the RemoveDataMask enum for details).
124 int removal_mask;
126 // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
127 int origin_set_mask;
130 // Observer is notified when the removal is done. Done means keywords have
131 // been deleted, cache cleared and all other tasks scheduled.
132 class Observer {
133 public:
134 virtual void OnBrowsingDataRemoverDone() = 0;
136 protected:
137 virtual ~Observer() {}
140 using Callback = base::Callback<void(const NotificationDetails&)>;
141 using CallbackSubscription = scoped_ptr<
142 base::CallbackList<void(const NotificationDetails&)>::Subscription>;
144 // The completion inhibitor can artificially delay completion of the browsing
145 // data removal process. It is used during testing to simulate scenarios in
146 // which the deletion stalls or takes a very long time.
147 class CompletionInhibitor {
148 public:
149 // Invoked when a |remover| is just about to complete clearing browser data,
150 // and will be prevented from completing until after the callback
151 // |continue_to_completion| is run.
152 virtual void OnBrowsingDataRemoverWouldComplete(
153 BrowsingDataRemover* remover,
154 const base::Closure& continue_to_completion) = 0;
156 protected:
157 virtual ~CompletionInhibitor() {}
160 // Creates a BrowsingDataRemover object that removes data regardless of the
161 // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
162 // retains ownership of itself, and deletes itself once finished.
163 static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
165 // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
166 // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
167 // deletes itself once finished.
168 static BrowsingDataRemover* CreateForRange(Profile* profile,
169 base::Time delete_begin,
170 base::Time delete_end);
172 // Creates a BrowsingDataRemover bound to a specific period of time (as
173 // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
174 // retains ownership of itself, and deletes itself once finished.
175 static BrowsingDataRemover* CreateForPeriod(Profile* profile,
176 TimePeriod period);
178 // Calculate the begin time for the deletion range specified by |time_period|.
179 static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
181 // Is the BrowsingDataRemover currently in the process of removing data?
182 static bool is_removing() { return is_removing_; }
184 // Sets a CompletionInhibitor, which will be notified each time an instance is
185 // about to complete a browsing data removal process, and will be able to
186 // artificially delay the completion.
187 static void set_completion_inhibitor_for_testing(
188 CompletionInhibitor* inhibitor) {
189 completion_inhibitor_ = inhibitor;
192 // Add a callback to the list of callbacks to be called during a browsing data
193 // removal event. Returns a subscription object that can be used to
194 // un-register the callback.
195 static CallbackSubscription RegisterOnBrowsingDataRemovedCallback(
196 const Callback& callback);
198 // Removes the specified items related to browsing for all origins that match
199 // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
200 void Remove(int remove_mask, int origin_set_mask);
202 void AddObserver(Observer* observer);
203 void RemoveObserver(Observer* observer);
205 // Called when history deletion is done.
206 void OnHistoryDeletionDone();
208 // Used for testing.
209 void OverrideStoragePartitionForTesting(
210 content::StoragePartition* storage_partition);
212 private:
213 // The clear API needs to be able to toggle removing_ in order to test that
214 // only one BrowsingDataRemover instance can be called at a time.
215 FRIEND_TEST_ALL_PREFIXES(ExtensionBrowsingDataTest, OneAtATime);
217 // The BrowsingDataRemover tests need to be able to access the implementation
218 // of Remove(), as it exposes details that aren't yet available in the public
219 // API. As soon as those details are exposed via new methods, this should be
220 // removed.
222 // TODO(mkwst): See http://crbug.com/113621
223 friend class BrowsingDataRemoverTest;
225 // Setter for |is_removing_|; DCHECKs that we can only start removing if we're
226 // not already removing, and vice-versa.
227 static void set_removing(bool is_removing);
229 // Creates a BrowsingDataRemover to remove browser data from the specified
230 // profile in the specified time range. Use Remove to initiate the removal.
231 BrowsingDataRemover(Profile* profile,
232 base::Time delete_begin,
233 base::Time delete_end);
235 // BrowsingDataRemover deletes itself (using DeleteHelper) and is not supposed
236 // to be deleted by other objects so make destructor private and DeleteHelper
237 // a friend.
238 friend class base::DeleteHelper<BrowsingDataRemover>;
240 // When plugins aren't enabled, there is no base class, so adding an override
241 // specifier would result in a compile error.
242 #if defined(ENABLE_PLUGINS)
243 ~BrowsingDataRemover() override;
244 #else
245 ~BrowsingDataRemover();
246 #endif
248 // Callback for when TemplateURLService has finished loading. Clears the data,
249 // clears the respective waiting flag, and invokes NotifyAndDeleteIfDone.
250 void OnKeywordsLoaded();
252 // Called when plugin data has been cleared. Invokes NotifyAndDeleteIfDone.
253 void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
255 #if defined(ENABLE_PLUGINS)
256 // PepperFlashSettingsManager::Client implementation.
257 void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
258 bool success) override;
259 #endif
261 #if defined (OS_CHROMEOS)
262 void OnClearPlatformKeys(chromeos::DBusMethodCallStatus call_status,
263 bool result);
264 #endif
266 // Removes the specified items related to browsing for a specific host. If the
267 // provided |origin| is empty, data is removed for all origins. The
268 // |origin_set_mask| parameter defines the set of origins from which data
269 // should be removed (protected, unprotected, or both).
270 void RemoveImpl(int remove_mask,
271 const GURL& origin,
272 int origin_set_mask);
274 // Notifies observers and deletes this object.
275 void NotifyAndDelete();
277 // Checks if we are all done, and if so, calls NotifyAndDelete().
278 void NotifyAndDeleteIfDone();
280 // Callback for when the hostname resolution cache has been cleared.
281 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
282 void OnClearedHostnameResolutionCache();
284 // Invoked on the IO thread to clear the hostname resolution cache.
285 void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
287 // Callback for when the LoggedIn Predictor has been cleared.
288 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
289 void OnClearedLoggedInPredictor();
291 // Clears the LoggedIn Predictor.
292 void ClearLoggedInPredictor();
294 // Callback for when speculative data in the network Predictor has been
295 // cleared. Clears the respective waiting flag and invokes
296 // NotifyAndDeleteIfDone.
297 void OnClearedNetworkPredictor();
299 // Invoked on the IO thread to clear speculative data related to hostname
300 // pre-resolution from the network Predictor.
301 void ClearNetworkPredictorOnIOThread(
302 chrome_browser_net::Predictor* predictor);
304 // Callback for when network related data in ProfileIOData has been cleared.
305 // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
306 void OnClearedNetworkingHistory();
308 // Callback for when the cache has been deleted. Invokes
309 // NotifyAndDeleteIfDone.
310 void ClearedCache();
311 #if !defined(DISABLE_NACL)
312 // Callback for when the NaCl cache has been deleted. Invokes
313 // NotifyAndDeleteIfDone.
314 void ClearedNaClCache();
316 // Invokes the ClearedNaClCache on the UI thread.
317 void ClearedNaClCacheOnIOThread();
319 // Invoked on the IO thread to delete the NaCl cache.
320 void ClearNaClCacheOnIOThread();
322 // Callback for when the PNaCl translation cache has been deleted. Invokes
323 // NotifyAndDeleteIfDone.
324 void ClearedPnaclCache();
326 // Invokes ClearedPnaclCacheOn on the UI thread.
327 void ClearedPnaclCacheOnIOThread();
329 // Invoked on the IO thread to delete entries in the PNaCl translation cache.
330 void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
331 #endif
333 // Callback for when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
334 void OnClearedCookies(int num_deleted);
336 // Invoked on the IO thread to delete cookies.
337 void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
339 // Invoked on the IO thread to delete channel IDs.
340 void ClearChannelIDsOnIOThread(
341 net::URLRequestContextGetter* rq_context);
343 // Callback on IO Thread when channel IDs have been deleted. Clears SSL
344 // connection pool and posts to UI thread to run OnClearedChannelIDs.
345 void OnClearedChannelIDsOnIOThread(
346 net::URLRequestContextGetter* rq_context);
348 // Callback for when channel IDs have been deleted. Invokes
349 // NotifyAndDeleteIfDone.
350 void OnClearedChannelIDs();
352 // Callback from the above method.
353 void OnClearedFormData();
355 // Callback for when the Autofill profile and credit card origin URLs have
356 // been deleted.
357 void OnClearedAutofillOriginURLs();
359 // Callback on UI thread when the storage partition related data are cleared.
360 void OnClearedStoragePartitionData();
362 #if defined(ENABLE_WEBRTC)
363 // Callback on UI thread when the WebRTC logs have been deleted.
364 void OnClearedWebRtcLogs();
365 #endif
367 void OnClearedDomainReliabilityMonitor();
369 // Returns true if we're all done.
370 bool AllDone();
372 // Profile we're to remove from.
373 Profile* profile_;
375 // Start time to delete from.
376 const base::Time delete_begin_;
378 // End time to delete to.
379 base::Time delete_end_;
381 // True if Remove has been invoked.
382 static bool is_removing_;
384 // If non-NULL, the |completion_inhibitor_| is notified each time an instance
385 // is about to complete a browsing data removal process, and has the ability
386 // to artificially delay completion. Used for testing.
387 static CompletionInhibitor* completion_inhibitor_;
389 // Used to delete data from HTTP cache.
390 scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
391 scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
393 #if defined(ENABLE_PLUGINS)
394 // Used to delete plugin data.
395 scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
396 base::WaitableEventWatcher watcher_;
398 // Used to deauthorize content licenses for Pepper Flash.
399 scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
400 #endif
402 uint32 deauthorize_content_licenses_request_id_;
403 // True if we're waiting for various data to be deleted.
404 // These may only be accessed from UI thread in order to avoid races!
405 bool waiting_for_clear_autofill_origin_urls_;
406 bool waiting_for_clear_cache_;
407 bool waiting_for_clear_channel_ids_;
408 bool waiting_for_clear_content_licenses_;
409 // Non-zero if waiting for cookies to be cleared.
410 int waiting_for_clear_cookies_count_;
411 bool waiting_for_clear_domain_reliability_monitor_;
412 bool waiting_for_clear_form_;
413 bool waiting_for_clear_history_;
414 bool waiting_for_clear_hostname_resolution_cache_;
415 bool waiting_for_clear_keyword_data_;
416 bool waiting_for_clear_logged_in_predictor_;
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_platform_keys_;
421 bool waiting_for_clear_plugin_data_;
422 bool waiting_for_clear_pnacl_cache_;
423 bool waiting_for_clear_storage_partition_data_;
424 #if defined(ENABLE_WEBRTC)
425 bool waiting_for_clear_webrtc_logs_;
426 #endif
428 // The removal mask for the current removal operation.
429 int remove_mask_;
431 // The origin for the current removal operation.
432 GURL remove_origin_;
434 // From which types of origins should we remove data?
435 int origin_set_mask_;
437 ObserverList<Observer> observer_list_;
439 // Used if we need to clear history.
440 base::CancelableTaskTracker history_task_tracker_;
442 scoped_ptr<TemplateURLService::Subscription> template_url_sub_;
444 // We do not own this.
445 content::StoragePartition* storage_partition_for_testing_;
447 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
450 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_