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
,
127 scoped_ptr
<IndexedDBKeyRange
> key_range
,
129 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
130 void Put(int64 transaction_id
,
131 int64 object_store_id
,
132 IndexedDBValue
* value
,
133 ScopedVector
<storage::BlobDataHandle
>* handles
,
134 scoped_ptr
<IndexedDBKey
> key
,
135 blink::WebIDBPutMode mode
,
136 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
137 const std::vector
<IndexKeys
>& index_keys
);
138 void SetIndexKeys(int64 transaction_id
,
139 int64 object_store_id
,
140 scoped_ptr
<IndexedDBKey
> primary_key
,
141 const std::vector
<IndexKeys
>& index_keys
);
142 void SetIndexesReady(int64 transaction_id
,
143 int64 object_store_id
,
144 const std::vector
<int64
>& index_ids
);
145 void OpenCursor(int64 transaction_id
,
146 int64 object_store_id
,
148 scoped_ptr
<IndexedDBKeyRange
> key_range
,
149 blink::WebIDBCursorDirection
,
151 blink::WebIDBTaskType task_type
,
152 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
153 void Count(int64 transaction_id
,
154 int64 object_store_id
,
156 scoped_ptr
<IndexedDBKeyRange
> key_range
,
157 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
158 void DeleteRange(int64 transaction_id
,
159 int64 object_store_id
,
160 scoped_ptr
<IndexedDBKeyRange
> key_range
,
161 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
162 void Clear(int64 transaction_id
,
163 int64 object_store_id
,
164 scoped_refptr
<IndexedDBCallbacks
> callbacks
);
166 // Number of connections that have progressed passed initial open call.
167 size_t ConnectionCount() const;
168 // Number of open calls that are blocked on other connections.
169 size_t PendingOpenCount() const;
170 // Number of pending upgrades (0 or 1). Also included in ConnectionCount().
171 size_t PendingUpgradeCount() const;
172 // Number of running upgrades (0 or 1). Also included in ConnectionCount().
173 size_t RunningUpgradeCount() const;
174 // Number of pending deletes, blocked on other connections.
175 size_t PendingDeleteCount() const;
177 // Asynchronous tasks scheduled within transactions:
178 void CreateObjectStoreAbortOperation(int64 object_store_id
,
179 IndexedDBTransaction
* transaction
);
180 void DeleteObjectStoreOperation(
181 int64 object_store_id
,
182 IndexedDBTransaction
* transaction
);
183 void DeleteObjectStoreAbortOperation(
184 const IndexedDBObjectStoreMetadata
& object_store_metadata
,
185 IndexedDBTransaction
* transaction
);
186 void VersionChangeOperation(int64 version
,
187 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
188 scoped_ptr
<IndexedDBConnection
> connection
,
189 IndexedDBTransaction
* transaction
);
190 void VersionChangeAbortOperation(const base::string16
& previous_version
,
191 int64 previous_int_version
,
192 IndexedDBTransaction
* transaction
);
193 void DeleteIndexOperation(int64 object_store_id
,
195 IndexedDBTransaction
* transaction
);
196 void CreateIndexAbortOperation(int64 object_store_id
,
198 IndexedDBTransaction
* transaction
);
199 void DeleteIndexAbortOperation(int64 object_store_id
,
200 const IndexedDBIndexMetadata
& index_metadata
,
201 IndexedDBTransaction
* transaction
);
202 void GetOperation(int64 object_store_id
,
204 scoped_ptr
<IndexedDBKeyRange
> key_range
,
205 indexed_db::CursorType cursor_type
,
206 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
207 IndexedDBTransaction
* transaction
);
208 void GetAllOperation(int64 object_store_id
,
209 scoped_ptr
<IndexedDBKeyRange
> key_range
,
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 class PendingDeleteCall
;
238 class PendingSuccessCall
;
239 class PendingUpgradeCall
;
241 typedef std::map
<int64
, IndexedDBTransaction
*> TransactionMap
;
242 typedef std::list
<IndexedDBPendingConnection
> PendingOpenCallList
;
243 typedef std::list
<PendingDeleteCall
*> PendingDeleteCallList
;
244 typedef list_set
<IndexedDBConnection
*> ConnectionSet
;
246 IndexedDBDatabase(const base::string16
& name
,
247 IndexedDBBackingStore
* backing_store
,
248 IndexedDBFactory
* factory
,
249 const Identifier
& unique_identifier
);
250 ~IndexedDBDatabase();
252 bool IsOpenConnectionBlocked() const;
253 leveldb::Status
OpenInternal();
254 void RunVersionChangeTransaction(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
255 scoped_ptr
<IndexedDBConnection
> connection
,
256 int64 transaction_id
,
257 int64 requested_version
);
258 void RunVersionChangeTransactionFinal(
259 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
260 scoped_ptr
<IndexedDBConnection
> connection
,
261 int64 transaction_id
,
262 int64 requested_version
);
263 void ProcessPendingCalls();
265 bool IsDeleteDatabaseBlocked() const;
266 void DeleteDatabaseFinal(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
268 scoped_ptr
<IndexedDBConnection
> CreateConnection(
269 scoped_refptr
<IndexedDBDatabaseCallbacks
> database_callbacks
,
270 int child_process_id
);
272 IndexedDBTransaction
* GetTransaction(int64 transaction_id
) const;
274 bool ValidateObjectStoreId(int64 object_store_id
) const;
275 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id
,
276 int64 index_id
) const;
277 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id
,
278 int64 index_id
) const;
279 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id
,
280 int64 index_id
) const;
282 scoped_refptr
<IndexedDBBackingStore
> backing_store_
;
283 IndexedDBDatabaseMetadata metadata_
;
285 const Identifier identifier_
;
286 scoped_refptr
<IndexedDBFactory
> factory_
;
288 IndexedDBTransactionCoordinator transaction_coordinator_
;
290 TransactionMap transactions_
;
291 PendingOpenCallList pending_open_calls_
;
292 scoped_ptr
<PendingUpgradeCall
> pending_run_version_change_transaction_call_
;
293 scoped_ptr
<PendingSuccessCall
> pending_second_half_open_
;
294 PendingDeleteCallList pending_delete_calls_
;
296 ConnectionSet connections_
;
298 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabase
);
301 } // namespace content
303 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_