summaryrefslogtreecommitdiff
path: root/chromium/content/browser/indexed_db/indexed_db_database.h
blob: bcde7190c503bc495e4206f3252a51225424892c (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 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 CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
#define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_

#include <list>
#include <map>
#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "content/browser/indexed_db/indexed_db.h"
#include "content/browser/indexed_db/indexed_db_backing_store.h"
#include "content/browser/indexed_db/indexed_db_callbacks.h"
#include "content/browser/indexed_db/indexed_db_metadata.h"
#include "content/browser/indexed_db/indexed_db_pending_connection.h"
#include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
#include "content/browser/indexed_db/list_set.h"
#include "third_party/WebKit/public/platform/WebIDBTypes.h"
#include "url/gurl.h"

namespace content {

class IndexedDBBlobInfo;
class IndexedDBConnection;
class IndexedDBDatabaseCallbacks;
class IndexedDBFactory;
class IndexedDBKey;
class IndexedDBKeyPath;
class IndexedDBKeyRange;
class IndexedDBTransaction;
struct IndexedDBValue;

class CONTENT_EXPORT IndexedDBDatabase
    : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) {
 public:
  // An index and corresponding set of keys
  typedef std::pair<int64, std::vector<IndexedDBKey> > IndexKeys;

  // Identifier is pair of (origin url, database name).
  typedef std::pair<GURL, base::string16> Identifier;

  static const int64 kInvalidId = 0;
  static const int64 kMinimumIndexId = 30;

  static scoped_refptr<IndexedDBDatabase> Create(
      const base::string16& name,
      IndexedDBBackingStore* backing_store,
      IndexedDBFactory* factory,
      const Identifier& unique_identifier,
      leveldb::Status* s);

  const Identifier& identifier() const { return identifier_; }
  IndexedDBBackingStore* backing_store() { return backing_store_.get(); }

  int64 id() const { return metadata_.id; }
  const base::string16& name() const { return metadata_.name; }

  void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata,
                      int64 new_max_object_store_id);
  void RemoveObjectStore(int64 object_store_id);
  void AddIndex(int64 object_store_id,
                const IndexedDBIndexMetadata& metadata,
                int64 new_max_index_id);
  void RemoveIndex(int64 object_store_id, int64 index_id);

  void OpenConnection(const IndexedDBPendingConnection& connection);
  void DeleteDatabase(scoped_refptr<IndexedDBCallbacks> callbacks);
  const IndexedDBDatabaseMetadata& metadata() const { return metadata_; }

  void CreateObjectStore(int64 transaction_id,
                         int64 object_store_id,
                         const base::string16& name,
                         const IndexedDBKeyPath& key_path,
                         bool auto_increment);
  void DeleteObjectStore(int64 transaction_id, int64 object_store_id);
  void CreateTransaction(int64 transaction_id,
                         IndexedDBConnection* connection,
                         const std::vector<int64>& object_store_ids,
                         blink::WebIDBTransactionMode mode);
  void Close(IndexedDBConnection* connection, bool forced);
  void ForceClose();

  // Ack that one of the connections notified with a "versionchange" event did
  // not promptly close. Therefore a "blocked" event should be fired at the
  // pending connection.
  void VersionChangeIgnored();

  void Commit(int64 transaction_id);
  void Abort(int64 transaction_id);
  void Abort(int64 transaction_id, const IndexedDBDatabaseError& error);

  void CreateIndex(int64 transaction_id,
                   int64 object_store_id,
                   int64 index_id,
                   const base::string16& name,
                   const IndexedDBKeyPath& key_path,
                   bool unique,
                   bool multi_entry);
  void DeleteIndex(int64 transaction_id, int64 object_store_id, int64 index_id);

  IndexedDBTransactionCoordinator& transaction_coordinator() {
    return transaction_coordinator_;
  }
  const IndexedDBTransactionCoordinator& transaction_coordinator() const {
    return transaction_coordinator_;
  }

  void TransactionCreated(IndexedDBTransaction* transaction);
  void TransactionFinished(IndexedDBTransaction* transaction, bool committed);

  // Called by transactions to report failure committing to the backing store.
  void TransactionCommitFailed(const leveldb::Status& status);

  void Get(int64 transaction_id,
           int64 object_store_id,
           int64 index_id,
           scoped_ptr<IndexedDBKeyRange> key_range,
           bool key_only,
           scoped_refptr<IndexedDBCallbacks> callbacks);
  void Put(int64 transaction_id,
           int64 object_store_id,
           IndexedDBValue* value,
           ScopedVector<storage::BlobDataHandle>* handles,
           scoped_ptr<IndexedDBKey> key,
           blink::WebIDBPutMode mode,
           scoped_refptr<IndexedDBCallbacks> callbacks,
           const std::vector<IndexKeys>& index_keys);
  void SetIndexKeys(int64 transaction_id,
                    int64 object_store_id,
                    scoped_ptr<IndexedDBKey> primary_key,
                    const std::vector<IndexKeys>& index_keys);
  void SetIndexesReady(int64 transaction_id,
                       int64 object_store_id,
                       const std::vector<int64>& index_ids);
  void OpenCursor(int64 transaction_id,
                  int64 object_store_id,
                  int64 index_id,
                  scoped_ptr<IndexedDBKeyRange> key_range,
                  blink::WebIDBCursorDirection,
                  bool key_only,
                  blink::WebIDBTaskType task_type,
                  scoped_refptr<IndexedDBCallbacks> callbacks);
  void Count(int64 transaction_id,
             int64 object_store_id,
             int64 index_id,
             scoped_ptr<IndexedDBKeyRange> key_range,
             scoped_refptr<IndexedDBCallbacks> callbacks);
  void DeleteRange(int64 transaction_id,
                   int64 object_store_id,
                   scoped_ptr<IndexedDBKeyRange> key_range,
                   scoped_refptr<IndexedDBCallbacks> callbacks);
  void Clear(int64 transaction_id,
             int64 object_store_id,
             scoped_refptr<IndexedDBCallbacks> callbacks);

  // Number of connections that have progressed passed initial open call.
  size_t ConnectionCount() const;
  // Number of open calls that are blocked on other connections.
  size_t PendingOpenCount() const;
  // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
  size_t PendingUpgradeCount() const;
  // Number of running upgrades (0 or 1). Also included in ConnectionCount().
  size_t RunningUpgradeCount() const;
  // Number of pending deletes, blocked on other connections.
  size_t PendingDeleteCount() const;

  // Asynchronous tasks scheduled within transactions:
  void CreateObjectStoreAbortOperation(int64 object_store_id,
                                       IndexedDBTransaction* transaction);
  void DeleteObjectStoreOperation(
      int64 object_store_id,
      IndexedDBTransaction* transaction);
  void DeleteObjectStoreAbortOperation(
      const IndexedDBObjectStoreMetadata& object_store_metadata,
      IndexedDBTransaction* transaction);
  void VersionChangeOperation(int64 version,
                              scoped_refptr<IndexedDBCallbacks> callbacks,
                              scoped_ptr<IndexedDBConnection> connection,
                              IndexedDBTransaction* transaction);
  void VersionChangeAbortOperation(const base::string16& previous_version,
                                   int64 previous_int_version,
                                   IndexedDBTransaction* transaction);
  void DeleteIndexOperation(int64 object_store_id,
                            int64 index_id,
                            IndexedDBTransaction* transaction);
  void CreateIndexAbortOperation(int64 object_store_id,
                                 int64 index_id,
                                 IndexedDBTransaction* transaction);
  void DeleteIndexAbortOperation(int64 object_store_id,
                                 const IndexedDBIndexMetadata& index_metadata,
                                 IndexedDBTransaction* transaction);
  void GetOperation(int64 object_store_id,
                    int64 index_id,
                    scoped_ptr<IndexedDBKeyRange> key_range,
                    indexed_db::CursorType cursor_type,
                    scoped_refptr<IndexedDBCallbacks> callbacks,
                    IndexedDBTransaction* transaction);
  struct PutOperationParams;
  void PutOperation(scoped_ptr<PutOperationParams> params,
                    IndexedDBTransaction* transaction);
  void SetIndexesReadyOperation(size_t index_count,
                                IndexedDBTransaction* transaction);
  struct OpenCursorOperationParams;
  void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params,
                           IndexedDBTransaction* transaction);
  void CountOperation(int64 object_store_id,
                      int64 index_id,
                      scoped_ptr<IndexedDBKeyRange> key_range,
                      scoped_refptr<IndexedDBCallbacks> callbacks,
                      IndexedDBTransaction* transaction);
  void DeleteRangeOperation(int64 object_store_id,
                            scoped_ptr<IndexedDBKeyRange> key_range,
                            scoped_refptr<IndexedDBCallbacks> callbacks,
                            IndexedDBTransaction* transaction);
  void ClearOperation(int64 object_store_id,
                      scoped_refptr<IndexedDBCallbacks> callbacks,
                      IndexedDBTransaction* transaction);

 private:
  friend class base::RefCounted<IndexedDBDatabase>;

  class PendingDeleteCall;
  class PendingSuccessCall;
  class PendingUpgradeCall;

  typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
  typedef std::list<IndexedDBPendingConnection> PendingOpenCallList;
  typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
  typedef list_set<IndexedDBConnection*> ConnectionSet;

  IndexedDBDatabase(const base::string16& name,
                    IndexedDBBackingStore* backing_store,
                    IndexedDBFactory* factory,
                    const Identifier& unique_identifier);
  ~IndexedDBDatabase();

  bool IsOpenConnectionBlocked() const;
  leveldb::Status OpenInternal();
  void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
                                   scoped_ptr<IndexedDBConnection> connection,
                                   int64 transaction_id,
                                   int64 requested_version);
  void RunVersionChangeTransactionFinal(
      scoped_refptr<IndexedDBCallbacks> callbacks,
      scoped_ptr<IndexedDBConnection> connection,
      int64 transaction_id,
      int64 requested_version);
  void ProcessPendingCalls();

  bool IsDeleteDatabaseBlocked() const;
  void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);

  scoped_ptr<IndexedDBConnection> CreateConnection(
      scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
      int child_process_id);

  IndexedDBTransaction* GetTransaction(int64 transaction_id) const;

  bool ValidateObjectStoreId(int64 object_store_id) const;
  bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
                                       int64 index_id) const;
  bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
                                               int64 index_id) const;
  bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
                                          int64 index_id) const;

  scoped_refptr<IndexedDBBackingStore> backing_store_;
  IndexedDBDatabaseMetadata metadata_;

  const Identifier identifier_;
  scoped_refptr<IndexedDBFactory> factory_;

  IndexedDBTransactionCoordinator transaction_coordinator_;

  TransactionMap transactions_;
  PendingOpenCallList pending_open_calls_;
  scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
  scoped_ptr<PendingSuccessCall> pending_second_half_open_;
  PendingDeleteCallList pending_delete_calls_;

  ConnectionSet connections_;

  DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabase);
};

}  // namespace content

#endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_