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_DATABASE_HELPER_H_
      6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_
      7 
      8 #include <list>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/synchronization/lock.h"
     16 #include "chrome/common/url_constants.h"
     17 #include "url/gurl.h"
     18 #include "webkit/browser/database/database_tracker.h"
     19 #include "webkit/common/database/database_identifier.h"
     20 
     21 class Profile;
     22 
     23 // This class fetches database information in the FILE thread, and notifies
     24 // the UI thread upon completion.
     25 // A client of this class need to call StartFetching from the UI thread to
     26 // initiate the flow, and it'll be notified by the callback in its UI
     27 // thread at some later point.
     28 class BrowsingDataDatabaseHelper
     29     : public base::RefCountedThreadSafe<BrowsingDataDatabaseHelper> {
     30  public:
     31   // Contains detailed information about a web database.
     32   struct DatabaseInfo {
     33     DatabaseInfo(const webkit_database::DatabaseIdentifier& identifier,
     34                  const std::string& database_name,
     35                  const std::string& description,
     36                  int64 size,
     37                  base::Time last_modified);
     38     ~DatabaseInfo();
     39 
     40     webkit_database::DatabaseIdentifier identifier;
     41     std::string database_name;
     42     std::string description;
     43     int64 size;
     44     base::Time last_modified;
     45   };
     46 
     47   explicit BrowsingDataDatabaseHelper(Profile* profile);
     48 
     49   // Starts the fetching process, which will notify its completion via
     50   // callback.
     51   // This must be called only in the UI thread.
     52   virtual void StartFetching(
     53       const base::Callback<void(const std::list<DatabaseInfo>&)>& callback);
     54 
     55   // Requests a single database to be deleted in the FILE thread. This must be
     56   // called in the UI thread.
     57   virtual void DeleteDatabase(const std::string& origin_identifier,
     58                               const std::string& name);
     59 
     60  protected:
     61   friend class base::RefCountedThreadSafe<BrowsingDataDatabaseHelper>;
     62   virtual ~BrowsingDataDatabaseHelper();
     63 
     64   // Notifies the completion callback. This must be called in the UI thread.
     65   void NotifyInUIThread();
     66 
     67   // Access to |database_info_| is triggered indirectly via the UI thread and
     68   // guarded by |is_fetching_|. This means |database_info_| is only accessed
     69   // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on
     70   // the UI thread.
     71   // In the context of this class |database_info_| is only accessed on the FILE
     72   // thread.
     73   std::list<DatabaseInfo> database_info_;
     74 
     75   // This only mutates on the UI thread.
     76   base::Callback<void(const std::list<DatabaseInfo>&)> completion_callback_;
     77 
     78   // Indicates whether or not we're currently fetching information:
     79   // it's true when StartFetching() is called in the UI thread, and it's reset
     80   // after we notify the callback in the UI thread.
     81   // This only mutates on the UI thread.
     82   bool is_fetching_;
     83 
     84  private:
     85   // Enumerates all databases. This must be called in the FILE thread.
     86   void FetchDatabaseInfoOnFileThread();
     87 
     88   // Delete a single database file. This must be called in the FILE thread.
     89   void DeleteDatabaseOnFileThread(const std::string& origin,
     90                                   const std::string& name);
     91 
     92   scoped_refptr<webkit_database::DatabaseTracker> tracker_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(BrowsingDataDatabaseHelper);
     95 };
     96 
     97 // This class is a thin wrapper around BrowsingDataDatabaseHelper that does not
     98 // fetch its information from the database tracker, but gets them passed as
     99 // a parameter during construction.
    100 class CannedBrowsingDataDatabaseHelper : public BrowsingDataDatabaseHelper {
    101  public:
    102   struct PendingDatabaseInfo {
    103     PendingDatabaseInfo(const GURL& origin,
    104                         const std::string& name,
    105                         const std::string& description);
    106     ~PendingDatabaseInfo();
    107 
    108     // The operator is needed to store |PendingDatabaseInfo| objects in a set.
    109     bool operator<(const PendingDatabaseInfo& other) const;
    110 
    111     GURL origin;
    112     std::string name;
    113     std::string description;
    114   };
    115 
    116   explicit CannedBrowsingDataDatabaseHelper(Profile* profile);
    117 
    118   // Return a copy of the database helper. Only one consumer can use the
    119   // StartFetching method at a time, so we need to create a copy of the helper
    120   // everytime we instantiate a cookies tree model for it.
    121   CannedBrowsingDataDatabaseHelper* Clone();
    122 
    123   // Add a database to the set of canned databases that is returned by this
    124   // helper.
    125   void AddDatabase(const GURL& origin,
    126                    const std::string& name,
    127                    const std::string& description);
    128 
    129   // Clear the list of canned databases.
    130   void Reset();
    131 
    132   // True if no databases are currently stored.
    133   bool empty() const;
    134 
    135   // Returns the number of currently stored databases.
    136   size_t GetDatabaseCount() const;
    137 
    138   // Returns the current list of web databases.
    139   const std::set<PendingDatabaseInfo>& GetPendingDatabaseInfo();
    140 
    141   // BrowsingDataDatabaseHelper implementation.
    142   virtual void StartFetching(
    143       const base::Callback<void(const std::list<DatabaseInfo>&)>& callback)
    144           OVERRIDE;
    145   virtual void DeleteDatabase(const std::string& origin_identifier,
    146                               const std::string& name) OVERRIDE;
    147 
    148  private:
    149   virtual ~CannedBrowsingDataDatabaseHelper();
    150 
    151   std::set<PendingDatabaseInfo> pending_database_info_;
    152 
    153   Profile* profile_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataDatabaseHelper);
    156 };
    157 
    158 #endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_DATABASE_HELPER_H_
    159