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