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