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_pending_connection.h"
21 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h"
22 #include "content/browser/indexed_db/list_set.h"
23 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h"
28 class IndexedDBBlobInfo
;
29 class IndexedDBConnection
;
30 class IndexedDBDatabaseCallbacks
;
31 class IndexedDBFactory
;
33 class IndexedDBKeyPath
;
34 class IndexedDBKeyRange
;
35 class IndexedDBTransaction
;
36 struct IndexedDBValue
;
38 class CONTENT_EXPORT IndexedDBDatabase
39 : NON_EXPORTED_BASE(public base::RefCounted
<IndexedDBDatabase
>) {
41 // An index and corresponding set of keys
42 typedef std::pair
<int64
, std::vector
<IndexedDBKey
> > IndexKeys
;
44 // Identifier is pair of (origin url, database name).
45 typedef std::pair
<GURL
, base::string16
> Identifier
;
47 static const int64 kInvalidId
= 0;
48 static const int64 kMinimumIndexId
= 30;
50 static scoped_refptr
<IndexedDBDatabase
> Create(
51 const base::string16
& name
,
52 IndexedDBBackingStore
* backing_store
,
53 IndexedDBFactory
* factory
,
54 const Identifier
& unique_identifier
,
57 const Identifier
& identifier() const { return identifier_
; }
58 IndexedDBBackingStore
* backing_store() { return backing_store_
.get(); }
60 int64
id() const { return metadata_
.id
; }
61 const base::string16
& name() const { return metadata_
.name
; }
63 void AddObjectStore(const IndexedDBObjectStoreMetadata
& metadata
,
64 int64 new_max_object_store_id
);
65 void RemoveObjectStore(int64 object_store_id
);
66 void AddIndex(int64 object_store_id
,
67 const IndexedDBIndexMetadata
& metadata
,
68 int64 new_max_index_id
);
69 void RemoveIndex(int64 object_store_id
, int64 index_id
);
71 void OpenConnection(const IndexedDBPendingConnection
& connection
);
72 void DeleteDatabase(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
73 const IndexedDBDatabaseMetadata
& metadata() const { return metadata_
; }
75 void CreateObjectStore(int64 transaction_id
,
76 int64 object_store_id
,
77 const base::string16
& name
,
78 const IndexedDBKeyPath
& key_path
,
80 void DeleteObjectStore(int64 transaction_id
, int64 object_store_id
);
81 void CreateTransaction(int64 transaction_id
,
82 IndexedDBConnection
* connection
,
83 const std::vector
<int64
>& object_store_ids
,
84 blink::WebIDBTransactionMode mode
);
85 void Close(IndexedDBConnection
* connection
, bool forced
);
88 // Ack that one of the connections notified with a "versionchange" event did
89 // not promptly close. Therefore a "blocked" event should be fired at the
90 // pending connection.
91 void VersionChangeIgnored();
93 void Commit(int64 transaction_id
);
94 void Abort(int64 transaction_id
);
95 void Abort(int64 transaction_id
, const IndexedDBDatabaseError
& error
);
97 void CreateIndex(int64 transaction_id
,
98 int64 object_store_id
,
100 const base::string16
& name
,
101 const IndexedDBKeyPath
& key_path
,
104 void DeleteIndex(int64 transaction_id
, int64 object_store_id
, int64 index_id
);
106 IndexedDBTransactionCoordinator
& transaction_coordinator() {
107 return transaction_coordinator_
;
109 const IndexedDBTransactionCoordinator
& transaction_coordinator() const {
110 return transaction_coordinator_
;
113 void TransactionCreated(IndexedDBTransaction
* transaction
);
114 void TransactionFinished(IndexedDBTransaction
* transaction
, bool committed
);
116 // Called by transactions to report failure committing to the backing store.
117 void TransactionCommitFailed(const leveldb::Status
& status
);
119 void Get(int64 transaction_id
,
120 int64 object_store_id
,
122 scoped_ptr
<IndexedDBKeyRange
> key_range
,
124 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
125 void GetAll(int64 transaction_id
,
126 int64 object_store_id
,
128 scoped_ptr
<IndexedDBKeyRange
> key_range
,
131 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
132 void Put(int64 transaction_id
,
133 int64 object_store_id
,
134 IndexedDBValue
* value
,
135 ScopedVector
<storage::BlobDataHandle
>* handles
,
136 scoped_ptr
<IndexedDBKey
> key
,
137 blink::WebIDBPutMode mode
,
138 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
139 const std::vector
<IndexKeys
>& index_keys
);
140 void SetIndexKeys(int64 transaction_id
,
141 int64 object_store_id
,
142 scoped_ptr
<IndexedDBKey
> primary_key
,
143 const std::vector
<IndexKeys
>& index_keys
);
144 void SetIndexesReady(int64 transaction_id
,
145 int64 object_store_id
,
146 const std::vector
<int64
>& index_ids
);
147 void OpenCursor(int64 transaction_id
,
148 int64 object_store_id
,
150 scoped_ptr
<IndexedDBKeyRange
> key_range
,
151 blink::WebIDBCursorDirection
,
153 blink::WebIDBTaskType task_type
,
154 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
155 void Count(int64 transaction_id
,
156 int64 object_store_id
,
158 scoped_ptr
<IndexedDBKeyRange
> key_range
,
159 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
160 void DeleteRange(int64 transaction_id
,
161 int64 object_store_id
,
162 scoped_ptr
<IndexedDBKeyRange
> key_range
,
163 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
164 void Clear(int64 transaction_id
,
165 int64 object_store_id
,
166 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
168 // Number of connections that have progressed passed initial open call.
169 size_t ConnectionCount() const;
170 // Number of open calls that are blocked on other connections.
171 size_t PendingOpenCount() const;
172 // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
173 size_t PendingUpgradeCount() const;
174 // Number of running upgrades (0 or 1). Also included in ConnectionCount().
175 size_t RunningUpgradeCount() const;
176 // Number of pending deletes, blocked on other connections.
177 size_t PendingDeleteCount() const;
179 // Asynchronous tasks scheduled within transactions:
180 void CreateObjectStoreAbortOperation(int64 object_store_id
,
181 IndexedDBTransaction
* transaction
);
182 void DeleteObjectStoreOperation(
183 int64 object_store_id
,
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 DeleteIndexOperation(int64 object_store_id
,
197 IndexedDBTransaction
* transaction
);
198 void CreateIndexAbortOperation(int64 object_store_id
,
200 IndexedDBTransaction
* transaction
);
201 void DeleteIndexAbortOperation(int64 object_store_id
,
202 const IndexedDBIndexMetadata
& index_metadata
,
203 IndexedDBTransaction
* transaction
);
204 void GetOperation(int64 object_store_id
,
206 scoped_ptr
<IndexedDBKeyRange
> key_range
,
207 indexed_db::CursorType cursor_type
,
208 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
209 IndexedDBTransaction
* transaction
);
210 void GetAllOperation(int64 object_store_id
,
212 scoped_ptr
<IndexedDBKeyRange
> key_range
,
213 indexed_db::CursorType cursor_type
,
215 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
216 IndexedDBTransaction
* transaction
);
217 struct PutOperationParams
;
218 void PutOperation(scoped_ptr
<PutOperationParams
> params
,
219 IndexedDBTransaction
* transaction
);
220 void SetIndexesReadyOperation(size_t index_count
,
221 IndexedDBTransaction
* transaction
);
222 struct OpenCursorOperationParams
;
223 void OpenCursorOperation(scoped_ptr
<OpenCursorOperationParams
> params
,
224 IndexedDBTransaction
* transaction
);
225 void CountOperation(int64 object_store_id
,
227 scoped_ptr
<IndexedDBKeyRange
> key_range
,
228 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
229 IndexedDBTransaction
* transaction
);
230 void DeleteRangeOperation(int64 object_store_id
,
231 scoped_ptr
<IndexedDBKeyRange
> key_range
,
232 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
233 IndexedDBTransaction
* transaction
);
234 void ClearOperation(int64 object_store_id
,
235 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
236 IndexedDBTransaction
* transaction
);
239 friend class base::RefCounted
<IndexedDBDatabase
>;
241 class PendingDeleteCall
;
242 class PendingSuccessCall
;
243 class PendingUpgradeCall
;
245 typedef std::map
<int64
, IndexedDBTransaction
*> TransactionMap
;
246 typedef std::list
<IndexedDBPendingConnection
> PendingOpenCallList
;
247 typedef std::list
<PendingDeleteCall
*> PendingDeleteCallList
;
248 typedef list_set
<IndexedDBConnection
*> ConnectionSet
;
250 IndexedDBDatabase(const base::string16
& name
,
251 IndexedDBBackingStore
* backing_store
,
252 IndexedDBFactory
* factory
,
253 const Identifier
& unique_identifier
);
254 ~IndexedDBDatabase();
256 bool IsOpenConnectionBlocked() const;
257 leveldb::Status
OpenInternal();
258 void RunVersionChangeTransaction(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
259 scoped_ptr
<IndexedDBConnection
> connection
,
260 int64 transaction_id
,
261 int64 requested_version
);
262 void RunVersionChangeTransactionFinal(
263 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
264 scoped_ptr
<IndexedDBConnection
> connection
,
265 int64 transaction_id
,
266 int64 requested_version
);
267 void ProcessPendingCalls();
269 bool IsDeleteDatabaseBlocked() const;
270 void DeleteDatabaseFinal(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
272 scoped_ptr
<IndexedDBConnection
> CreateConnection(
273 scoped_refptr
<IndexedDBDatabaseCallbacks
> database_callbacks
,
274 int child_process_id
);
276 IndexedDBTransaction
* GetTransaction(int64 transaction_id
) const;
278 bool ValidateObjectStoreId(int64 object_store_id
) const;
279 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id
,
280 int64 index_id
) const;
281 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id
,
282 int64 index_id
) const;
283 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id
,
284 int64 index_id
) const;
286 scoped_refptr
<IndexedDBBackingStore
> backing_store_
;
287 IndexedDBDatabaseMetadata metadata_
;
289 const Identifier identifier_
;
290 scoped_refptr
<IndexedDBFactory
> factory_
;
292 IndexedDBTransactionCoordinator transaction_coordinator_
;
294 TransactionMap transactions_
;
295 PendingOpenCallList pending_open_calls_
;
296 scoped_ptr
<PendingUpgradeCall
> pending_run_version_change_transaction_call_
;
297 scoped_ptr
<PendingSuccessCall
> pending_second_half_open_
;
298 PendingDeleteCallList pending_delete_calls_
;
300 ConnectionSet connections_
;
302 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabase
);
305 } // namespace content
307 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_