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/common/cancelable_task_tracker.h"
     19 #include "content/public/browser/notification_observer.h"
     20 #include "content/public/browser/notification_registrar.h"
     21 #include "url/gurl.h"
     22 #include "webkit/common/quota/quota_types.h"
     23 
     24 class ExtensionSpecialStoragePolicy;
     25 class IOThread;
     26 class Profile;
     27 
     28 namespace content {
     29 class PluginDataRemover;
     30 }
     31 
     32 namespace disk_cache {
     33 class Backend;
     34 }
     35 
     36 namespace net {
     37 class URLRequestContextGetter;
     38 }
     39 
     40 namespace quota {
     41 class QuotaManager;
     42 }
     43 
     44 namespace content {
     45 class DOMStorageContext;
     46 struct LocalStorageUsageInfo;
     47 struct SessionStorageUsageInfo;
     48 }
     49 
     50 // BrowsingDataRemover is responsible for removing data related to browsing:
     51 // visits in url database, downloads, cookies ...
     52 
     53 class BrowsingDataRemover : public content::NotificationObserver
     54 #if defined(ENABLE_PLUGINS)
     55                             , public PepperFlashSettingsManager::Client
     56 #endif
     57                             {
     58  public:
     59   // Time period ranges available when doing browsing data removals.
     60   enum TimePeriod {
     61     LAST_HOUR = 0,
     62     LAST_DAY,
     63     LAST_WEEK,
     64     FOUR_WEEKS,
     65     EVERYTHING
     66   };
     67 
     68   // Mask used for Remove.
     69   enum RemoveDataMask {
     70     REMOVE_APPCACHE = 1 << 0,
     71     REMOVE_CACHE = 1 << 1,
     72     REMOVE_COOKIES = 1 << 2,
     73     REMOVE_DOWNLOADS = 1 << 3,
     74     REMOVE_FILE_SYSTEMS = 1 << 4,
     75     REMOVE_FORM_DATA = 1 << 5,
     76     // In addition to visits, REMOVE_HISTORY removes keywords and last session.
     77     REMOVE_HISTORY = 1 << 6,
     78     REMOVE_INDEXEDDB = 1 << 7,
     79     REMOVE_LOCAL_STORAGE = 1 << 8,
     80     REMOVE_PLUGIN_DATA = 1 << 9,
     81     REMOVE_PASSWORDS = 1 << 10,
     82     REMOVE_WEBSQL = 1 << 11,
     83     REMOVE_SERVER_BOUND_CERTS = 1 << 12,
     84     REMOVE_CONTENT_LICENSES = 1 << 13,
     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,
     89 
     90     // "Site data" includes cookies, appcache, file systems, indexedDBs, local
     91     // storage, webSQL, and plugin data.
     92     REMOVE_SITE_DATA = REMOVE_APPCACHE | REMOVE_COOKIES | REMOVE_FILE_SYSTEMS |
     93                        REMOVE_INDEXEDDB | REMOVE_LOCAL_STORAGE |
     94                        REMOVE_PLUGIN_DATA | REMOVE_WEBSQL |
     95                        REMOVE_SERVER_BOUND_CERTS,
     96 
     97     // Includes all the available remove options. Meant to be used by clients
     98     // that wish to wipe as much data as possible from a Profile, to make it
     99     // look like a new Profile.
    100     REMOVE_ALL = REMOVE_APPCACHE | REMOVE_CACHE | REMOVE_COOKIES |
    101                  REMOVE_DOWNLOADS | REMOVE_FILE_SYSTEMS | REMOVE_FORM_DATA |
    102                  REMOVE_HISTORY | REMOVE_INDEXEDDB | REMOVE_LOCAL_STORAGE |
    103                  REMOVE_PLUGIN_DATA | REMOVE_PASSWORDS | REMOVE_WEBSQL |
    104                  REMOVE_SERVER_BOUND_CERTS | REMOVE_CONTENT_LICENSES,
    105   };
    106 
    107   // When BrowsingDataRemover successfully removes data, a notification of type
    108   // NOTIFICATION_BROWSING_DATA_REMOVED is triggered with a Details object of
    109   // this type.
    110   struct NotificationDetails {
    111     NotificationDetails();
    112     NotificationDetails(const NotificationDetails& details);
    113     NotificationDetails(base::Time removal_begin,
    114                        int removal_mask,
    115                        int origin_set_mask);
    116     ~NotificationDetails();
    117 
    118     // The beginning of the removal time range.
    119     base::Time removal_begin;
    120 
    121     // The removal mask (see the RemoveDataMask enum for details).
    122     int removal_mask;
    123 
    124     // The origin set mask (see BrowsingDataHelper::OriginSetMask for details).
    125     int origin_set_mask;
    126   };
    127 
    128   // Observer is notified when the removal is done. Done means keywords have
    129   // been deleted, cache cleared and all other tasks scheduled.
    130   class Observer {
    131    public:
    132     virtual void OnBrowsingDataRemoverDone() = 0;
    133 
    134    protected:
    135     virtual ~Observer() {}
    136   };
    137 
    138   // Creates a BrowsingDataRemover object that removes data regardless of the
    139   // time it was last modified. Returns a raw pointer, as BrowsingDataRemover
    140   // retains ownership of itself, and deletes itself once finished.
    141   static BrowsingDataRemover* CreateForUnboundedRange(Profile* profile);
    142 
    143   // Creates a BrowsingDataRemover object bound on both sides by a time. Returns
    144   // a raw pointer, as BrowsingDataRemover retains ownership of itself, and
    145   // deletes itself once finished.
    146   static BrowsingDataRemover* CreateForRange(Profile* profile,
    147                                              base::Time delete_begin,
    148                                              base::Time delete_end);
    149 
    150   // Creates a BrowsingDataRemover bound to a specific period of time (as
    151   // defined via a TimePeriod). Returns a raw pointer, as BrowsingDataRemover
    152   // retains ownership of itself, and deletes itself once finished.
    153   static BrowsingDataRemover* CreateForPeriod(Profile* profile,
    154                                               TimePeriod period);
    155 
    156   // Calculate the begin time for the deletion range specified by |time_period|.
    157   static base::Time CalculateBeginDeleteTime(TimePeriod time_period);
    158 
    159   // Quota managed data uses a different bitmask for types than
    160   // BrowsingDataRemover uses. This method generates that mask.
    161   static int GenerateQuotaClientMask(int remove_mask);
    162 
    163   // Is the BrowsingDataRemover currently in the process of removing data?
    164   static bool is_removing() { return is_removing_; }
    165 
    166   // Removes the specified items related to browsing for all origins that match
    167   // the provided |origin_set_mask| (see BrowsingDataHelper::OriginSetMask).
    168   void Remove(int remove_mask, int origin_set_mask);
    169 
    170   void AddObserver(Observer* observer);
    171   void RemoveObserver(Observer* observer);
    172 
    173   // Called when history deletion is done.
    174   void OnHistoryDeletionDone();
    175 
    176   // Used for testing.
    177   void OverrideQuotaManagerForTesting(quota::QuotaManager* quota_manager);
    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   // content::NotificationObserver method. Callback when TemplateURLService has
    218   // finished loading. Deletes the entries from the model, and if we're not
    219   // waiting on anything else notifies observers and deletes this
    220   // BrowsingDataRemover.
    221   virtual void Observe(int type,
    222                        const content::NotificationSource& source,
    223                        const content::NotificationDetails& details) OVERRIDE;
    224 
    225   // Called when plug-in data has been cleared. Invokes NotifyAndDeleteIfDone.
    226   void OnWaitableEventSignaled(base::WaitableEvent* waitable_event);
    227 
    228 #if defined(ENABLE_PLUGINS)
    229   // PepperFlashSettingsManager::Client implementation.
    230   virtual void OnDeauthorizeContentLicensesCompleted(uint32 request_id,
    231                                                      bool success) OVERRIDE;
    232 #endif
    233 
    234   // Removes the specified items related to browsing for a specific host. If the
    235   // provided |origin| is empty, data is removed for all origins. The
    236   // |origin_set_mask| parameter defines the set of origins from which data
    237   // should be removed (protected, unprotected, or both).
    238   void RemoveImpl(int remove_mask,
    239                   const GURL& origin,
    240                   int origin_set_mask);
    241 
    242   // If we're not waiting on anything, notifies observers and deletes this
    243   // object.
    244   void NotifyAndDeleteIfDone();
    245 
    246   // Callback when the hostname resolution cache has been cleared.
    247   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
    248   void OnClearedHostnameResolutionCache();
    249 
    250   // Invoked on the IO thread to clear the hostname resolution cache.
    251   void ClearHostnameResolutionCacheOnIOThread(IOThread* io_thread);
    252 
    253   // Callback when the LoggedIn Predictor has been cleared.
    254   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
    255   void OnClearedLoggedInPredictor();
    256 
    257   // Clears the LoggedIn Predictor.
    258   void ClearLoggedInPredictor();
    259 
    260   // Callback when speculative data in the network Predictor has been cleared.
    261   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
    262   void OnClearedNetworkPredictor();
    263 
    264   // Invoked on the IO thread to clear speculative data related to hostname
    265   // pre-resolution from the network Predictor.
    266   void ClearNetworkPredictorOnIOThread();
    267 
    268   // Callback when network related data in ProfileIOData has been cleared.
    269   // Clears the respective waiting flag and invokes NotifyAndDeleteIfDone.
    270   void OnClearedNetworkingHistory();
    271 
    272   // Callback when the cache has been deleted. Invokes NotifyAndDeleteIfDone.
    273   void ClearedCache();
    274 
    275   // Invoked on the IO thread to delete from the cache.
    276   void ClearCacheOnIOThread();
    277 
    278   // Performs the actual work to delete the cache.
    279   void DoClearCache(int rv);
    280 
    281 #if !defined(DISABLE_NACL)
    282   // Callback for when the NaCl cache has been deleted. Invokes
    283   // NotifyAndDeleteIfDone.
    284   void ClearedNaClCache();
    285 
    286   // Invokes the ClearedNaClCache on the UI thread.
    287   void ClearedNaClCacheOnIOThread();
    288 
    289   // Invoked on the IO thread to delete the NaCl cache.
    290   void ClearNaClCacheOnIOThread();
    291 
    292   // Callback for when the PNaCl translation cache has been deleted. Invokes
    293   // NotifyAndDeleteIfDone.
    294   void ClearedPnaclCache();
    295 
    296   // Invokes ClearedPnaclCacheOn on the UI thread.
    297   void ClearedPnaclCacheOnIOThread();
    298 
    299   // Invoked on the IO thread to delete entries in the PNaCl translation cache.
    300   void ClearPnaclCacheOnIOThread(base::Time begin, base::Time end);
    301 #endif
    302 
    303   // Invoked on the UI thread to delete local storage.
    304   void ClearLocalStorageOnUIThread();
    305 
    306   // Callback to deal with the list gathered in ClearLocalStorageOnUIThread.
    307   void OnGotLocalStorageUsageInfo(
    308       const std::vector<content::LocalStorageUsageInfo>& infos);
    309 
    310   // Invoked on the UI thread to delete session storage.
    311   void ClearSessionStorageOnUIThread();
    312 
    313   // Callback to deal with the list gathered in ClearSessionStorageOnUIThread.
    314   void OnGotSessionStorageUsageInfo(
    315       const std::vector<content::SessionStorageUsageInfo>& infos);
    316 
    317   // Invoked on the IO thread to delete all storage types managed by the quota
    318   // system: AppCache, Databases, FileSystems.
    319   void ClearQuotaManagedDataOnIOThread();
    320 
    321   // Callback to respond to QuotaManager::GetOriginsModifiedSince, which is the
    322   // core of 'ClearQuotaManagedDataOnIOThread'.
    323   void OnGotQuotaManagedOrigins(const std::set<GURL>& origins,
    324                                 quota::StorageType type);
    325 
    326   // Callback responding to deletion of a single quota managed origin's
    327   // persistent data
    328   void OnQuotaManagedOriginDeletion(const GURL& origin,
    329                                     quota::StorageType type,
    330                                     quota::QuotaStatusCode);
    331 
    332   // Called to check whether all temporary and persistent origin data that
    333   // should be deleted has been deleted. If everything's good to go, invokes
    334   // OnQuotaManagedDataDeleted on the UI thread.
    335   void CheckQuotaManagedDataDeletionStatus();
    336 
    337   // Completion handler that runs on the UI thread once persistent data has been
    338   // deleted. Updates the waiting flag and invokes NotifyAndDeleteIfDone.
    339   void OnQuotaManagedDataDeleted();
    340 
    341   // Callback when Cookies has been deleted. Invokes NotifyAndDeleteIfDone.
    342   void OnClearedCookies(int num_deleted);
    343 
    344   // Invoked on the IO thread to delete cookies.
    345   void ClearCookiesOnIOThread(net::URLRequestContextGetter* rq_context);
    346 
    347   // Invoked on the IO thread to delete server bound certs.
    348   void ClearServerBoundCertsOnIOThread(
    349       net::URLRequestContextGetter* rq_context);
    350 
    351   // Callback on IO Thread when server bound certs have been deleted. Clears SSL
    352   // connection pool and posts to UI thread to run OnClearedServerBoundCerts.
    353   void OnClearedServerBoundCertsOnIOThread(
    354       net::URLRequestContextGetter* rq_context);
    355 
    356   // Callback when server bound certs have been deleted. Invokes
    357   // NotifyAndDeleteIfDone.
    358   void OnClearedServerBoundCerts();
    359 
    360   // Callback from the above method.
    361   void OnClearedFormData();
    362 
    363   // Callback when the Autofill profile and credit card origin URLs have been
    364   // deleted.
    365   void OnClearedAutofillOriginURLs();
    366 
    367   // Callback when the shader cache has been deleted.
    368   // Invokes NotifyAndDeleteIfDone.
    369   void ClearedShaderCache();
    370 
    371   // Invoked on the IO thread to delete from the shader cache.
    372   void ClearShaderCacheOnUIThread();
    373 
    374   // Callback on UI thread when the WebRTC identities are cleared.
    375   void OnClearWebRTCIdentityStore();
    376 
    377   // Returns true if we're all done.
    378   bool AllDone();
    379 
    380   content::NotificationRegistrar registrar_;
    381 
    382   // Profile we're to remove from.
    383   Profile* profile_;
    384 
    385   // The QuotaManager is owned by the profile; we can use a raw pointer here,
    386   // and rely on the profile to destroy the object whenever it's reasonable.
    387   quota::QuotaManager* quota_manager_;
    388 
    389   // The DOMStorageContext is owned by the profile; we'll store a raw pointer.
    390   content::DOMStorageContext* dom_storage_context_;
    391 
    392   // 'Protected' origins are not subject to data removal.
    393   scoped_refptr<ExtensionSpecialStoragePolicy> special_storage_policy_;
    394 
    395   // Start time to delete from.
    396   const base::Time delete_begin_;
    397 
    398   // End time to delete to.
    399   base::Time delete_end_;
    400 
    401   // True if Remove has been invoked.
    402   static bool is_removing_;
    403 
    404   CacheState next_cache_state_;
    405   disk_cache::Backend* cache_;
    406 
    407   // Used to delete data from HTTP cache.
    408   scoped_refptr<net::URLRequestContextGetter> main_context_getter_;
    409   scoped_refptr<net::URLRequestContextGetter> media_context_getter_;
    410 
    411 #if defined(ENABLE_PLUGINS)
    412   // Used to delete plugin data.
    413   scoped_ptr<content::PluginDataRemover> plugin_data_remover_;
    414   base::WaitableEventWatcher watcher_;
    415 
    416   // Used to deauthorize content licenses for Pepper Flash.
    417   scoped_ptr<PepperFlashSettingsManager> pepper_flash_settings_manager_;
    418 #endif
    419 
    420   uint32 deauthorize_content_licenses_request_id_;
    421   // True if we're waiting for various data to be deleted.
    422   // These may only be accessed from UI thread in order to avoid races!
    423   bool waiting_for_clear_autofill_origin_urls_;
    424   bool waiting_for_clear_cache_;
    425   bool waiting_for_clear_content_licenses_;
    426   // Non-zero if waiting for cookies to be cleared.
    427   int waiting_for_clear_cookies_count_;
    428   bool waiting_for_clear_form_;
    429   bool waiting_for_clear_history_;
    430   bool waiting_for_clear_hostname_resolution_cache_;
    431   bool waiting_for_clear_local_storage_;
    432   bool waiting_for_clear_logged_in_predictor_;
    433   bool waiting_for_clear_nacl_cache_;
    434   bool waiting_for_clear_network_predictor_;
    435   bool waiting_for_clear_networking_history_;
    436   bool waiting_for_clear_plugin_data_;
    437   bool waiting_for_clear_pnacl_cache_;
    438   bool waiting_for_clear_quota_managed_data_;
    439   bool waiting_for_clear_server_bound_certs_;
    440   bool waiting_for_clear_session_storage_;
    441   bool waiting_for_clear_shader_cache_;
    442   bool waiting_for_clear_webrtc_identity_store_;
    443 
    444   // Tracking how many origins need to be deleted, and whether we're finished
    445   // gathering origins.
    446   int quota_managed_origins_to_delete_count_;
    447   int quota_managed_storage_types_to_delete_count_;
    448 
    449   // The removal mask for the current removal operation.
    450   int remove_mask_;
    451 
    452   // The origin for the current removal operation.
    453   GURL remove_origin_;
    454 
    455   // From which types of origins should we remove data?
    456   int origin_set_mask_;
    457 
    458   ObserverList<Observer> observer_list_;
    459 
    460   // Used if we need to clear history.
    461   CancelableTaskTracker history_task_tracker_;
    462 
    463   DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemover);
    464 };
    465 
    466 #endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_REMOVER_H_
    467