summaryrefslogtreecommitdiffstats
path: root/storage/browser/quota/quota_database.h
blob: e95fba801dd5a148d8d895b09983a1e2c921e518 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_QUOTA_QUOTA_DATABASE_H_
#define STORAGE_BROWSER_QUOTA_QUOTA_DATABASE_H_

#include <set>
#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "storage/browser/storage_browser_export.h"
#include "storage/common/quota/quota_types.h"
#include "url/gurl.h"

namespace content {
class QuotaDatabaseTest;
}

namespace sql {
class Connection;
class MetaTable;
}

class GURL;

namespace storage {

class SpecialStoragePolicy;

// All the methods of this class must run on the DB thread.
class STORAGE_EXPORT_PRIVATE QuotaDatabase {
 public:
  // Constants for {Get,Set}QuotaConfigValue keys.
  static const char kDesiredAvailableSpaceKey[];
  static const char kTemporaryQuotaOverrideKey[];

  // If 'path' is empty, an in memory database will be used.
  explicit QuotaDatabase(const base::FilePath& path);
  ~QuotaDatabase();

  void CloseConnection();

  bool GetHostQuota(const std::string& host, StorageType type, int64* quota);
  bool SetHostQuota(const std::string& host, StorageType type, int64 quota);
  bool DeleteHostQuota(const std::string& host, StorageType type);

  bool SetOriginLastAccessTime(const GURL& origin,
                               StorageType type,
                               base::Time last_access_time);

  bool SetOriginLastModifiedTime(const GURL& origin,
                                 StorageType type,
                                 base::Time last_modified_time);

  bool GetOriginLastEvictionTime(const GURL& origin,
                                 StorageType type,
                                 base::Time* last_eviction_time);
  bool SetOriginLastEvictionTime(const GURL& origin,
                                 StorageType type,
                                 base::Time last_eviction_time);
  bool DeleteOriginLastEvictionTime(const GURL& origin, StorageType type);

  // Register initial |origins| info |type| to the database.
  // This method is assumed to be called only after the installation or
  // the database schema reset.
  bool RegisterInitialOriginInfo(
      const std::set<GURL>& origins, StorageType type);

  bool DeleteOriginInfo(const GURL& origin, StorageType type);

  bool GetQuotaConfigValue(const char* key, int64* value);
  bool SetQuotaConfigValue(const char* key, int64 value);

  // Sets |origin| to the least recently used origin of origins not included
  // in |exceptions| and not granted the special unlimited storage right.
  // It returns false when it failed in accessing the database.
  // |origin| is set to empty when there is no matching origin.
  bool GetLRUOrigin(StorageType type,
                    const std::set<GURL>& exceptions,
                    SpecialStoragePolicy* special_storage_policy,
                    GURL* origin);

  // Populates |origins| with the ones that have been modified since
  // the |modified_since|.
  bool GetOriginsModifiedSince(StorageType type,
                               std::set<GURL>* origins,
                               base::Time modified_since);

  // Returns false if SetOriginDatabaseBootstrapped has never
  // been called before, which means existing origins may not have been
  // registered.
  bool IsOriginDatabaseBootstrapped();
  bool SetOriginDatabaseBootstrapped(bool bootstrap_flag);

 private:
  struct STORAGE_EXPORT_PRIVATE QuotaTableEntry {
    QuotaTableEntry();
    QuotaTableEntry(
        const std::string& host,
        StorageType type,
        int64 quota);
    std::string host;
    StorageType type;
    int64 quota;
  };
  friend STORAGE_EXPORT_PRIVATE bool operator <(
      const QuotaTableEntry& lhs, const QuotaTableEntry& rhs);

  struct STORAGE_EXPORT_PRIVATE OriginInfoTableEntry {
    OriginInfoTableEntry();
    OriginInfoTableEntry(
        const GURL& origin,
        StorageType type,
        int used_count,
        const base::Time& last_access_time,
        const base::Time& last_modified_time);
    GURL origin;
    StorageType type;
    int used_count;
    base::Time last_access_time;
    base::Time last_modified_time;
  };
  friend STORAGE_EXPORT_PRIVATE bool operator <(
      const OriginInfoTableEntry& lhs, const OriginInfoTableEntry& rhs);

  // Structures used for CreateSchema.
  struct TableSchema {
    const char* table_name;
    const char* columns;
  };
  struct IndexSchema {
    const char* index_name;
    const char* table_name;
    const char* columns;
    bool unique;
  };

  typedef base::Callback<bool (const QuotaTableEntry&)> QuotaTableCallback;
  typedef base::Callback<bool (const OriginInfoTableEntry&)>
      OriginInfoTableCallback;

  struct QuotaTableImporter;

  // For long-running transactions support.  We always keep a transaction open
  // so that multiple transactions can be batched.  They are flushed
  // with a delay after a modification has been made.  We support neither
  // nested transactions nor rollback (as we don't need them for now).
  void Commit();
  void ScheduleCommit();

  bool FindOriginUsedCount(const GURL& origin,
                           StorageType type,
                           int* used_count);

  bool LazyOpen(bool create_if_needed);
  bool EnsureDatabaseVersion();
  bool ResetSchema();
  bool UpgradeSchema(int current_version);
  bool InsertOrReplaceHostQuota(
      const std::string& host, StorageType type, int64 quota);

  static bool CreateSchema(
      sql::Connection* database,
      sql::MetaTable* meta_table,
      int schema_version, int compatible_version,
      const TableSchema* tables, size_t tables_size,
      const IndexSchema* indexes, size_t indexes_size);

  // |callback| may return false to stop reading data.
  bool DumpQuotaTable(const QuotaTableCallback& callback);
  bool DumpOriginInfoTable(const OriginInfoTableCallback& callback);

  base::FilePath db_file_path_;

  scoped_ptr<sql::Connection> db_;
  scoped_ptr<sql::MetaTable> meta_table_;
  bool is_recreating_;
  bool is_disabled_;

  base::OneShotTimer timer_;

  friend class content::QuotaDatabaseTest;
  friend class QuotaManager;

  static const TableSchema kTables[];
  static const IndexSchema kIndexes[];

  DISALLOW_COPY_AND_ASSIGN(QuotaDatabase);
};

}  // namespace storage

#endif  // STORAGE_BROWSER_QUOTA_QUOTA_DATABASE_H_