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