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