Home | History | Annotate | Download | only in quota
      1 // Copyright 2013 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 WEBKIT_BROWSER_QUOTA_QUOTA_DATABASE_H_
      6 #define WEBKIT_BROWSER_QUOTA_QUOTA_DATABASE_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/files/file_path.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/time/time.h"
     17 #include "base/timer/timer.h"
     18 #include "url/gurl.h"
     19 #include "webkit/browser/webkit_storage_browser_export.h"
     20 #include "webkit/common/quota/quota_types.h"
     21 
     22 namespace sql {
     23 class Connection;
     24 class MetaTable;
     25 }
     26 
     27 class GURL;
     28 
     29 namespace quota {
     30 
     31 class SpecialStoragePolicy;
     32 
     33 // All the methods of this class must run on the DB thread.
     34 class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE QuotaDatabase {
     35  public:
     36   // Constants for {Get,Set}QuotaConfigValue keys.
     37   static const char kDesiredAvailableSpaceKey[];
     38   static const char kTemporaryQuotaOverrideKey[];
     39 
     40   // If 'path' is empty, an in memory database will be used.
     41   explicit QuotaDatabase(const base::FilePath& path);
     42   ~QuotaDatabase();
     43 
     44   void CloseConnection();
     45 
     46   bool GetHostQuota(const std::string& host, StorageType type, int64* quota);
     47   bool SetHostQuota(const std::string& host, StorageType type, int64 quota);
     48   bool DeleteHostQuota(const std::string& host, StorageType type);
     49 
     50   bool SetOriginLastAccessTime(const GURL& origin,
     51                                StorageType type,
     52                                base::Time last_access_time);
     53 
     54   bool SetOriginLastModifiedTime(const GURL& origin,
     55                                  StorageType type,
     56                                  base::Time last_modified_time);
     57 
     58   // Register initial |origins| info |type| to the database.
     59   // This method is assumed to be called only after the installation or
     60   // the database schema reset.
     61   bool RegisterInitialOriginInfo(
     62       const std::set<GURL>& origins, StorageType type);
     63 
     64   bool DeleteOriginInfo(const GURL& origin, StorageType type);
     65 
     66   bool GetQuotaConfigValue(const char* key, int64* value);
     67   bool SetQuotaConfigValue(const char* key, int64 value);
     68 
     69   // Sets |origin| to the least recently used origin of origins not included
     70   // in |exceptions| and not granted the special unlimited storage right.
     71   // It returns false when it failed in accessing the database.
     72   // |origin| is set to empty when there is no matching origin.
     73   bool GetLRUOrigin(StorageType type,
     74                     const std::set<GURL>& exceptions,
     75                     SpecialStoragePolicy* special_storage_policy,
     76                     GURL* origin);
     77 
     78   // Populates |origins| with the ones that have been modified since
     79   // the |modified_since|.
     80   bool GetOriginsModifiedSince(StorageType type,
     81                                std::set<GURL>* origins,
     82                                base::Time modified_since);
     83 
     84   // Returns false if SetOriginDatabaseBootstrapped has never
     85   // been called before, which means existing origins may not have been
     86   // registered.
     87   bool IsOriginDatabaseBootstrapped();
     88   bool SetOriginDatabaseBootstrapped(bool bootstrap_flag);
     89 
     90  private:
     91   struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE QuotaTableEntry {
     92     QuotaTableEntry();
     93     QuotaTableEntry(
     94         const std::string& host,
     95         StorageType type,
     96         int64 quota);
     97     std::string host;
     98     StorageType type;
     99     int64 quota;
    100   };
    101   friend WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE bool operator <(
    102       const QuotaTableEntry& lhs, const QuotaTableEntry& rhs);
    103 
    104   struct WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE OriginInfoTableEntry {
    105     OriginInfoTableEntry();
    106     OriginInfoTableEntry(
    107         const GURL& origin,
    108         StorageType type,
    109         int used_count,
    110         const base::Time& last_access_time,
    111         const base::Time& last_modified_time);
    112     GURL origin;
    113     StorageType type;
    114     int used_count;
    115     base::Time last_access_time;
    116     base::Time last_modified_time;
    117   };
    118   friend WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE bool operator <(
    119       const OriginInfoTableEntry& lhs, const OriginInfoTableEntry& rhs);
    120 
    121   // Structures used for CreateSchema.
    122   struct TableSchema {
    123     const char* table_name;
    124     const char* columns;
    125   };
    126   struct IndexSchema {
    127     const char* index_name;
    128     const char* table_name;
    129     const char* columns;
    130     bool unique;
    131   };
    132 
    133   typedef base::Callback<bool (const QuotaTableEntry&)> QuotaTableCallback;
    134   typedef base::Callback<bool (const OriginInfoTableEntry&)>
    135       OriginInfoTableCallback;
    136 
    137   struct QuotaTableImporter;
    138 
    139   // For long-running transactions support.  We always keep a transaction open
    140   // so that multiple transactions can be batched.  They are flushed
    141   // with a delay after a modification has been made.  We support neither
    142   // nested transactions nor rollback (as we don't need them for now).
    143   void Commit();
    144   void ScheduleCommit();
    145 
    146   bool FindOriginUsedCount(const GURL& origin,
    147                            StorageType type,
    148                            int* used_count);
    149 
    150   bool LazyOpen(bool create_if_needed);
    151   bool EnsureDatabaseVersion();
    152   bool ResetSchema();
    153   bool UpgradeSchema(int current_version);
    154 
    155   static bool CreateSchema(
    156       sql::Connection* database,
    157       sql::MetaTable* meta_table,
    158       int schema_version, int compatible_version,
    159       const TableSchema* tables, size_t tables_size,
    160       const IndexSchema* indexes, size_t indexes_size);
    161 
    162   // |callback| may return false to stop reading data.
    163   bool DumpQuotaTable(QuotaTableCallback* callback);
    164   bool DumpOriginInfoTable(OriginInfoTableCallback* callback);
    165 
    166   base::FilePath db_file_path_;
    167 
    168   scoped_ptr<sql::Connection> db_;
    169   scoped_ptr<sql::MetaTable> meta_table_;
    170   bool is_recreating_;
    171   bool is_disabled_;
    172 
    173   base::OneShotTimer<QuotaDatabase> timer_;
    174 
    175   friend class QuotaDatabaseTest;
    176   friend class QuotaManager;
    177 
    178   static const TableSchema kTables[];
    179   static const IndexSchema kIndexes[];
    180 
    181   DISALLOW_COPY_AND_ASSIGN(QuotaDatabase);
    182 };
    183 
    184 }  // namespace quota
    185 
    186 #endif  // WEBKIT_BROWSER_QUOTA_QUOTA_DATABASE_H_
    187