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 IndexedDBDatabase(const base::string16
& name
,
240 IndexedDBBackingStore
* backing_store
,
241 IndexedDBFactory
* factory
,
242 const Identifier
& unique_identifier
);
243 virtual ~IndexedDBDatabase();
245 // May be overridden in tests.
246 virtual size_t GetMaxMessageSizeInBytes() const;
249 friend class base::RefCounted
<IndexedDBDatabase
>;
250 friend class IndexedDBClassFactory
;
252 class PendingDeleteCall
;
253 class PendingSuccessCall
;
254 class PendingUpgradeCall
;
256 typedef std::map
<int64
, IndexedDBTransaction
*> TransactionMap
;
257 typedef std::list
<IndexedDBPendingConnection
> PendingOpenCallList
;
258 typedef std::list
<PendingDeleteCall
*> PendingDeleteCallList
;
259 typedef list_set
<IndexedDBConnection
*> ConnectionSet
;
261 bool IsOpenConnectionBlocked() const;
262 leveldb::Status
OpenInternal();
263 void RunVersionChangeTransaction(scoped_refptr
<IndexedDBCallbacks
> callbacks
,
264 scoped_ptr
<IndexedDBConnection
> connection
,
265 int64 transaction_id
,
266 int64 requested_version
);
267 void RunVersionChangeTransactionFinal(
268 scoped_refptr
<IndexedDBCallbacks
> callbacks
,
269 scoped_ptr
<IndexedDBConnection
> connection
,
270 int64 transaction_id
,
271 int64 requested_version
);
272 void ProcessPendingCalls();
274 bool IsDeleteDatabaseBlocked() const;
275 void DeleteDatabaseFinal(scoped_refptr
<IndexedDBCallbacks
> callbacks
);
277 scoped_ptr
<IndexedDBConnection
> CreateConnection(
278 scoped_refptr
<IndexedDBDatabaseCallbacks
> database_callbacks
,
279 int child_process_id
);
281 IndexedDBTransaction
* GetTransaction(int64 transaction_id
) const;
283 bool ValidateObjectStoreId(int64 object_store_id
) const;
284 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id
,
285 int64 index_id
) const;
286 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id
,
287 int64 index_id
) const;
288 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id
,
289 int64 index_id
) const;
291 scoped_refptr
<IndexedDBBackingStore
> backing_store_
;
292 IndexedDBDatabaseMetadata metadata_
;
294 const Identifier identifier_
;
295 scoped_refptr
<IndexedDBFactory
> factory_
;
297 IndexedDBTransactionCoordinator transaction_coordinator_
;
299 TransactionMap transactions_
;
300 PendingOpenCallList pending_open_calls_
;
301 scoped_ptr
<PendingUpgradeCall
> pending_run_version_change_transaction_call_
;
302 scoped_ptr
<PendingSuccessCall
> pending_second_half_open_
;
303 PendingDeleteCallList pending_delete_calls_
;
305 ConnectionSet connections_
;
307 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabase
);
310 } // namespace content
312 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_