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_
14 #include "base/basictypes.h"
15 #include "base/memory/ref_counted.h"
16 #include "content/browser/indexed_db/indexed_db.h"
17 #include "content/browser/indexed_db/indexed_db_backing_store.h"
18 #include "content/browser/indexed_db/indexed_db_callbacks.h"
19 #include "content/browser/indexed_db/indexed_db_metadata.h"
20 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
21 #include "content/browser/indexed_db/list_set.h"
26 class IndexedDBConnection
;
27 class IndexedDBDatabaseCallbacks
;
28 class IndexedDBFactory
;
30 class IndexedDBKeyPath
;
31 class IndexedDBKeyRange
;
32 class IndexedDBTransaction
;
34 class CONTENT_EXPORT IndexedDBDatabase
35 : NON_EXPORTED_BASE(public base::RefCounted
<IndexedDBDatabase
>) {
48 // An index and corresponding set of keys
49 typedef std::pair
<int64
, std::vector
<IndexedDBKey
> > IndexKeys
;
51 // Identifier is pair of (origin url, database name).
52 typedef std::pair
<GURL
, base::string16
> Identifier
;
54 static const int64 kInvalidId
= 0;
55 static const int64 kMinimumIndexId
= 30;
57 static scoped_refptr
<IndexedDBDatabase
> Create(
58 const base::string16
& name
,
59 IndexedDBBackingStore
* backing_store
,
60 IndexedDBFactory
* factory
,
61 const Identifier
& unique_identifier
);
63 const Identifier
& identifier() const { return identifier_
; }
64 IndexedDBBackingStore
* backing_store() { return backing_store_
.get(); }
66 int64
id() const { return metadata_
.id
; }
67 const base::string16
& name() const { return metadata_
.name
; }
69 void AddObjectStore(const IndexedDBObjectStoreMetadata
& metadata
,
70 int64 new_max_object_store_id
);
71 void RemoveObjectStore(int64 object_store_id
);
72 void AddIndex(int64 object_store_id
,
73 const IndexedDBIndexMetadata
& metadata
,
74 int64 new_max_index_id
);
75 void RemoveIndex(int64 object_store_id
, int64 index_id
);
78 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
79 scoped_refptr
<IndexedDBDatabaseCallbacks
> database_callbacks
,
82 void DeleteDatabase(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
83 const IndexedDBDatabaseMetadata
& metadata() const { return metadata_
; }
85 void CreateObjectStore(int64 transaction_id
,
86 int64 object_store_id
,
87 const base::string16
& name
,
88 const IndexedDBKeyPath
& key_path
,
90 void DeleteObjectStore(int64 transaction_id
, int64 object_store_id
);
91 void CreateTransaction(int64 transaction_id
,
92 IndexedDBConnection
* connection
,
93 const std::vector
<int64
>& object_store_ids
,
95 void Close(IndexedDBConnection
* connection
, bool forced
);
98 void Commit(int64 transaction_id
);
99 void Abort(int64 transaction_id
);
100 void Abort(int64 transaction_id
, const IndexedDBDatabaseError
& error
);
102 void CreateIndex(int64 transaction_id
,
103 int64 object_store_id
,
105 const base::string16
& name
,
106 const IndexedDBKeyPath
& key_path
,
109 void DeleteIndex(int64 transaction_id
, int64 object_store_id
, int64 index_id
);
111 IndexedDBTransactionCoordinator
& transaction_coordinator() {
112 return transaction_coordinator_
;
114 const IndexedDBTransactionCoordinator
& transaction_coordinator() const {
115 return transaction_coordinator_
;
118 void TransactionCreated(IndexedDBTransaction
* transaction
);
119 void TransactionFinished(IndexedDBTransaction
* transaction
, bool committed
);
121 // Called by transactions to report failure committing to the backing store.
122 void TransactionCommitFailed();
124 void Get(int64 transaction_id
,
125 int64 object_store_id
,
127 scoped_ptr
<IndexedDBKeyRange
> key_range
,
129 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
130 void Put(int64 transaction_id
,
131 int64 object_store_id
,
133 scoped_ptr
<IndexedDBKey
> key
,
135 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
136 const std::vector
<IndexKeys
>& index_keys
);
137 void SetIndexKeys(int64 transaction_id
,
138 int64 object_store_id
,
139 scoped_ptr
<IndexedDBKey
> primary_key
,
140 const std::vector
<IndexKeys
>& index_keys
);
141 void SetIndexesReady(int64 transaction_id
,
142 int64 object_store_id
,
143 const std::vector
<int64
>& index_ids
);
144 void OpenCursor(int64 transaction_id
,
145 int64 object_store_id
,
147 scoped_ptr
<IndexedDBKeyRange
> key_range
,
148 indexed_db::CursorDirection
,
151 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
152 void Count(int64 transaction_id
,
153 int64 object_store_id
,
155 scoped_ptr
<IndexedDBKeyRange
> key_range
,
156 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
157 void DeleteRange(int64 transaction_id
,
158 int64 object_store_id
,
159 scoped_ptr
<IndexedDBKeyRange
> key_range
,
160 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
161 void Clear(int64 transaction_id
,
162 int64 object_store_id
,
163 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
165 // Number of connections that have progressed passed initial open call.
166 size_t ConnectionCount() const;
167 // Number of open calls that are blocked on other connections.
168 size_t PendingOpenCount() const;
169 // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
170 size_t PendingUpgradeCount() const;
171 // Number of running upgrades (0 or 1). Also included in ConnectionCount().
172 size_t RunningUpgradeCount() const;
173 // Number of pending deletes, blocked on other connections.
174 size_t PendingDeleteCount() const;
176 // Asynchronous tasks scheduled within transactions:
177 void CreateObjectStoreOperation(
178 const IndexedDBObjectStoreMetadata
& object_store_metadata
,
179 IndexedDBTransaction
* transaction
);
180 void CreateObjectStoreAbortOperation(int64 object_store_id
,
181 IndexedDBTransaction
* transaction
);
182 void DeleteObjectStoreOperation(
183 const IndexedDBObjectStoreMetadata
& object_store_metadata
,
184 IndexedDBTransaction
* transaction
);
185 void DeleteObjectStoreAbortOperation(
186 const IndexedDBObjectStoreMetadata
& object_store_metadata
,
187 IndexedDBTransaction
* transaction
);
188 void VersionChangeOperation(int64 version
,
189 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
190 scoped_ptr
<IndexedDBConnection
> connection
,
191 IndexedDBTransaction
* transaction
);
192 void VersionChangeAbortOperation(const base::string16
& previous_version
,
193 int64 previous_int_version
,
194 IndexedDBTransaction
* transaction
);
195 void CreateIndexOperation(int64 object_store_id
,
196 const IndexedDBIndexMetadata
& index_metadata
,
197 IndexedDBTransaction
* transaction
);
198 void DeleteIndexOperation(int64 object_store_id
,
199 const IndexedDBIndexMetadata
& index_metadata
,
200 IndexedDBTransaction
* transaction
);
201 void CreateIndexAbortOperation(int64 object_store_id
,
203 IndexedDBTransaction
* transaction
);
204 void DeleteIndexAbortOperation(int64 object_store_id
,
205 const IndexedDBIndexMetadata
& index_metadata
,
206 IndexedDBTransaction
* transaction
);
207 void GetOperation(int64 object_store_id
,
209 scoped_ptr
<IndexedDBKeyRange
> key_range
,
210 indexed_db::CursorType cursor_type
,
211 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
212 IndexedDBTransaction
* transaction
);
213 struct PutOperationParams
;
214 void PutOperation(scoped_ptr
<PutOperationParams
> params
,
215 IndexedDBTransaction
* transaction
);
216 void SetIndexesReadyOperation(size_t index_count
,
217 IndexedDBTransaction
* transaction
);
218 struct OpenCursorOperationParams
;
219 void OpenCursorOperation(scoped_ptr
<OpenCursorOperationParams
> params
,
220 IndexedDBTransaction
* transaction
);
221 void CountOperation(int64 object_store_id
,
223 scoped_ptr
<IndexedDBKeyRange
> key_range
,
224 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
225 IndexedDBTransaction
* transaction
);
226 void DeleteRangeOperation(int64 object_store_id
,
227 scoped_ptr
<IndexedDBKeyRange
> key_range
,
228 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
229 IndexedDBTransaction
* transaction
);
230 void ClearOperation(int64 object_store_id
,
231 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
232 IndexedDBTransaction
* transaction
);
235 friend class base::RefCounted
<IndexedDBDatabase
>;
237 IndexedDBDatabase(const base::string16
& name
,
238 IndexedDBBackingStore
* backing_store
,
239 IndexedDBFactory
* factory
,
240 const Identifier
& unique_identifier
);
241 ~IndexedDBDatabase();
243 bool IsOpenConnectionBlocked() const;
244 leveldb::Status
OpenInternal();
245 void RunVersionChangeTransaction(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
246 scoped_ptr
<IndexedDBConnection
> connection
,
247 int64 transaction_id
,
248 int64 requested_version
);
249 void RunVersionChangeTransactionFinal(
250 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
251 scoped_ptr
<IndexedDBConnection
> connection
,
252 int64 transaction_id
,
253 int64 requested_version
);
254 void ProcessPendingCalls();
256 bool IsDeleteDatabaseBlocked() const;
257 void DeleteDatabaseFinal(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
259 IndexedDBTransaction
* GetTransaction(int64 transaction_id
) const;
261 bool ValidateObjectStoreId(int64 object_store_id
) const;
262 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id
,
263 int64 index_id
) const;
264 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id
,
265 int64 index_id
) const;
266 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id
,
267 int64 index_id
) const;
269 scoped_refptr
<IndexedDBBackingStore
> backing_store_
;
270 IndexedDBDatabaseMetadata metadata_
;
272 const Identifier identifier_
;
273 // This might not need to be a scoped_refptr since the factory's lifetime is
274 // that of the page group, but it's better to be conservitive than sorry.
275 scoped_refptr
<IndexedDBFactory
> factory_
;
277 IndexedDBTransactionCoordinator transaction_coordinator_
;
279 typedef std::map
<int64
, IndexedDBTransaction
*> TransactionMap
;
280 TransactionMap transactions_
;
282 class PendingOpenCall
;
283 typedef std::list
<PendingOpenCall
*> PendingOpenCallList
;
284 PendingOpenCallList pending_open_calls_
;
286 class PendingUpgradeCall
;
287 scoped_ptr
<PendingUpgradeCall
> pending_run_version_change_transaction_call_
;
288 class PendingSuccessCall
;
289 scoped_ptr
<PendingSuccessCall
> pending_second_half_open_
;
291 class PendingDeleteCall
;
292 typedef std::list
<PendingDeleteCall
*> PendingDeleteCallList
;
293 PendingDeleteCallList pending_delete_calls_
;
295 typedef list_set
<IndexedDBConnection
*> ConnectionSet
;
296 ConnectionSet connections_
;
299 } // namespace content
301 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_