1 // Copyright (c) 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.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_
13 #include "base/basictypes.h"
14 #include "base/memory/ref_counted.h"
15 #include "content/browser/indexed_db/indexed_db.h"
16 #include "content/browser/indexed_db/indexed_db_backing_store.h"
17 #include "content/browser/indexed_db/indexed_db_callbacks.h"
18 #include "content/browser/indexed_db/indexed_db_metadata.h"
19 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
20 #include "content/browser/indexed_db/list_set.h"
25 class IndexedDBConnection;
26 class IndexedDBDatabaseCallbacks;
27 class IndexedDBFactory;
29 class IndexedDBKeyPath;
30 class IndexedDBKeyRange;
31 class IndexedDBTransaction;
33 class CONTENT_EXPORT IndexedDBDatabase
34 : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) {
47 typedef std::vector<IndexedDBKey> IndexKeys;
48 // Identifier is pair of (origin url, database name).
49 typedef std::pair<GURL, base::string16> Identifier;
51 static const int64 kInvalidId = 0;
52 static const int64 kMinimumIndexId = 30;
54 static scoped_refptr<IndexedDBDatabase> Create(
56 IndexedDBBackingStore* backing_store,
57 IndexedDBFactory* factory,
58 const Identifier& unique_identifier);
60 const Identifier& identifier() const { return identifier_; }
61 IndexedDBBackingStore* backing_store() { return backing_store_.get(); }
63 int64 id() const { return metadata_.id; }
64 const base::string16& name() const { return metadata_.name; }
66 void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata,
67 int64 new_max_object_store_id);
68 void RemoveObjectStore(int64 object_store_id);
69 void AddIndex(int64 object_store_id,
70 const IndexedDBIndexMetadata& metadata,
71 int64 new_max_index_id);
72 void RemoveIndex(int64 object_store_id, int64 index_id);
75 scoped_refptr<IndexedDBCallbacks> callbacks,
76 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
80 scoped_refptr<IndexedDBCallbacks> callbacks,
81 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
84 WebKit::WebIDBCallbacks::DataLoss data_loss,
85 std::string data_loss_message);
86 void DeleteDatabase(scoped_refptr<IndexedDBCallbacks> callbacks);
87 const IndexedDBDatabaseMetadata& metadata() const { return metadata_; }
89 void CreateObjectStore(int64 transaction_id,
90 int64 object_store_id,
92 const IndexedDBKeyPath& key_path,
94 void DeleteObjectStore(int64 transaction_id, int64 object_store_id);
95 void CreateTransaction(int64 transaction_id,
96 IndexedDBConnection* connection,
97 const std::vector<int64>& object_store_ids,
99 void Close(IndexedDBConnection* connection, bool forced);
101 void Commit(int64 transaction_id);
102 void Abort(int64 transaction_id);
103 void Abort(int64 transaction_id, const IndexedDBDatabaseError& error);
105 void CreateIndex(int64 transaction_id,
106 int64 object_store_id,
108 const string16& name,
109 const IndexedDBKeyPath& key_path,
112 void DeleteIndex(int64 transaction_id, int64 object_store_id, int64 index_id);
114 IndexedDBTransactionCoordinator& transaction_coordinator() {
115 return transaction_coordinator_;
117 const IndexedDBTransactionCoordinator& transaction_coordinator() const {
118 return transaction_coordinator_;
121 void TransactionStarted(IndexedDBTransaction* transaction);
122 void TransactionFinished(IndexedDBTransaction* transaction);
123 void TransactionFinishedAndCompleteFired(IndexedDBTransaction* transaction);
124 void TransactionFinishedAndAbortFired(IndexedDBTransaction* transaction);
126 // Called by transactions to report failure committing to the backing store.
127 void TransactionCommitFailed();
129 void Get(int64 transaction_id,
130 int64 object_store_id,
132 scoped_ptr<IndexedDBKeyRange> key_range,
134 scoped_refptr<IndexedDBCallbacks> callbacks);
135 void Put(int64 transaction_id,
136 int64 object_store_id,
138 scoped_ptr<IndexedDBKey> key,
140 scoped_refptr<IndexedDBCallbacks> callbacks,
141 const std::vector<int64>& index_ids,
142 const std::vector<IndexKeys>& index_keys);
143 void SetIndexKeys(int64 transaction_id,
144 int64 object_store_id,
145 scoped_ptr<IndexedDBKey> primary_key,
146 const std::vector<int64>& index_ids,
147 const std::vector<IndexKeys>& index_keys);
148 void SetIndexesReady(int64 transaction_id,
149 int64 object_store_id,
150 const std::vector<int64>& index_ids);
151 void OpenCursor(int64 transaction_id,
152 int64 object_store_id,
154 scoped_ptr<IndexedDBKeyRange> key_range,
155 indexed_db::CursorDirection,
158 scoped_refptr<IndexedDBCallbacks> callbacks);
159 void Count(int64 transaction_id,
160 int64 object_store_id,
162 scoped_ptr<IndexedDBKeyRange> key_range,
163 scoped_refptr<IndexedDBCallbacks> callbacks);
164 void DeleteRange(int64 transaction_id,
165 int64 object_store_id,
166 scoped_ptr<IndexedDBKeyRange> key_range,
167 scoped_refptr<IndexedDBCallbacks> callbacks);
168 void Clear(int64 transaction_id,
169 int64 object_store_id,
170 scoped_refptr<IndexedDBCallbacks> callbacks);
172 // Number of connections that have progressed passed initial open call.
173 size_t ConnectionCount() const;
174 // Number of open calls that are blocked on other connections.
175 size_t PendingOpenCount() const;
176 // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
177 size_t PendingUpgradeCount() const;
178 // Number of running upgrades (0 or 1). Also included in ConnectionCount().
179 size_t RunningUpgradeCount() const;
180 // Number of pending deletes, blocked on other connections.
181 size_t PendingDeleteCount() const;
183 // Asynchronous tasks scheduled within transactions:
184 void CreateObjectStoreOperation(
185 const IndexedDBObjectStoreMetadata& object_store_metadata,
186 IndexedDBTransaction* transaction);
187 void CreateObjectStoreAbortOperation(int64 object_store_id,
188 IndexedDBTransaction* transaction);
189 void DeleteObjectStoreOperation(
190 const IndexedDBObjectStoreMetadata& object_store_metadata,
191 IndexedDBTransaction* transaction);
192 void DeleteObjectStoreAbortOperation(
193 const IndexedDBObjectStoreMetadata& object_store_metadata,
194 IndexedDBTransaction* transaction);
195 void VersionChangeOperation(int64 version,
196 scoped_refptr<IndexedDBCallbacks> callbacks,
197 scoped_ptr<IndexedDBConnection> connection,
198 WebKit::WebIDBCallbacks::DataLoss data_loss,
199 std::string data_loss_message,
200 IndexedDBTransaction* transaction);
201 void VersionChangeAbortOperation(const string16& previous_version,
202 int64 previous_int_version,
203 IndexedDBTransaction* transaction);
204 void CreateIndexOperation(int64 object_store_id,
205 const IndexedDBIndexMetadata& index_metadata,
206 IndexedDBTransaction* transaction);
207 void DeleteIndexOperation(int64 object_store_id,
208 const IndexedDBIndexMetadata& index_metadata,
209 IndexedDBTransaction* transaction);
210 void CreateIndexAbortOperation(int64 object_store_id,
212 IndexedDBTransaction* transaction);
213 void DeleteIndexAbortOperation(int64 object_store_id,
214 const IndexedDBIndexMetadata& index_metadata,
215 IndexedDBTransaction* transaction);
216 void GetOperation(int64 object_store_id,
218 scoped_ptr<IndexedDBKeyRange> key_range,
219 indexed_db::CursorType cursor_type,
220 scoped_refptr<IndexedDBCallbacks> callbacks,
221 IndexedDBTransaction* transaction);
222 struct PutOperationParams;
223 void PutOperation(scoped_ptr<PutOperationParams> params,
224 IndexedDBTransaction* transaction);
225 void SetIndexesReadyOperation(size_t index_count,
226 IndexedDBTransaction* transaction);
227 struct OpenCursorOperationParams;
228 void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params,
229 IndexedDBTransaction* transaction);
230 void CountOperation(int64 object_store_id,
232 scoped_ptr<IndexedDBKeyRange> key_range,
233 scoped_refptr<IndexedDBCallbacks> callbacks,
234 IndexedDBTransaction* transaction);
235 void DeleteRangeOperation(int64 object_store_id,
236 scoped_ptr<IndexedDBKeyRange> key_range,
237 scoped_refptr<IndexedDBCallbacks> callbacks,
238 IndexedDBTransaction* transaction);
239 void ClearOperation(int64 object_store_id,
240 scoped_refptr<IndexedDBCallbacks> callbacks,
241 IndexedDBTransaction* transaction);
244 friend class base::RefCounted<IndexedDBDatabase>;
246 IndexedDBDatabase(const string16& name,
247 IndexedDBBackingStore* backing_store,
248 IndexedDBFactory* factory,
249 const Identifier& unique_identifier);
250 ~IndexedDBDatabase();
252 bool IsOpenConnectionBlocked() const;
254 void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
255 scoped_ptr<IndexedDBConnection> connection,
256 int64 transaction_id,
257 int64 requested_version,
258 WebKit::WebIDBCallbacks::DataLoss data_loss,
259 std::string data_loss_message);
260 void RunVersionChangeTransactionFinal(
261 scoped_refptr<IndexedDBCallbacks> callbacks,
262 scoped_ptr<IndexedDBConnection> connection,
263 int64 transaction_id,
264 int64 requested_version);
265 void RunVersionChangeTransactionFinal(
266 scoped_refptr<IndexedDBCallbacks> callbacks,
267 scoped_ptr<IndexedDBConnection> connection,
268 int64 transaction_id,
269 int64 requested_version,
270 WebKit::WebIDBCallbacks::DataLoss data_loss,
271 std::string data_loss_message);
272 void ProcessPendingCalls();
274 bool IsDeleteDatabaseBlocked() const;
275 void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);
277 IndexedDBTransaction* GetTransaction(int64 transaction_id) const;
279 bool ValidateObjectStoreId(int64 object_store_id) const;
280 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
281 int64 index_id) const;
282 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
283 int64 index_id) const;
284 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
285 int64 index_id) const;
287 scoped_refptr<IndexedDBBackingStore> backing_store_;
288 IndexedDBDatabaseMetadata metadata_;
290 const Identifier identifier_;
291 // This might not need to be a scoped_refptr since the factory's lifetime is
292 // that of the page group, but it's better to be conservitive than sorry.
293 scoped_refptr<IndexedDBFactory> factory_;
295 IndexedDBTransactionCoordinator transaction_coordinator_;
296 IndexedDBTransaction* running_version_change_transaction_;
298 typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
299 TransactionMap transactions_;
301 class PendingOpenCall;
302 typedef std::list<PendingOpenCall*> PendingOpenCallList;
303 PendingOpenCallList pending_open_calls_;
305 class PendingUpgradeCall;
306 scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
307 class PendingSuccessCall;
308 scoped_ptr<PendingSuccessCall> pending_second_half_open_;
310 class PendingDeleteCall;
311 typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
312 PendingDeleteCallList pending_delete_calls_;
314 typedef list_set<IndexedDBConnection*> ConnectionSet;
315 ConnectionSet connections_;
318 } // namespace content
320 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_