Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_database.h
bloba6e30f366a74162594d23af0e5e05f095d8c4ba9
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_
8 #include <list>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
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 "url/gurl.h"
25 namespace content {
27 class IndexedDBBlobInfo;
28 class IndexedDBConnection;
29 class IndexedDBDatabaseCallbacks;
30 class IndexedDBFactory;
31 class IndexedDBKey;
32 class IndexedDBKeyPath;
33 class IndexedDBKeyRange;
34 class IndexedDBTransaction;
35 struct IndexedDBValue;
37 class CONTENT_EXPORT IndexedDBDatabase
38 : NON_EXPORTED_BASE(public base::RefCounted<IndexedDBDatabase>) {
39 public:
40 enum TaskType {
41 NORMAL_TASK = 0,
42 PREEMPTIVE_TASK
45 enum PutMode {
46 ADD_OR_UPDATE,
47 ADD_ONLY,
48 CURSOR_UPDATE
51 // An index and corresponding set of keys
52 typedef std::pair<int64, std::vector<IndexedDBKey> > IndexKeys;
54 // Identifier is pair of (origin url, database name).
55 typedef std::pair<GURL, base::string16> Identifier;
57 static const int64 kInvalidId = 0;
58 static const int64 kMinimumIndexId = 30;
60 static scoped_refptr<IndexedDBDatabase> Create(
61 const base::string16& name,
62 IndexedDBBackingStore* backing_store,
63 IndexedDBFactory* factory,
64 const Identifier& unique_identifier,
65 leveldb::Status* s);
67 const Identifier& identifier() const { return identifier_; }
68 IndexedDBBackingStore* backing_store() { return backing_store_.get(); }
70 int64 id() const { return metadata_.id; }
71 const base::string16& name() const { return metadata_.name; }
73 void AddObjectStore(const IndexedDBObjectStoreMetadata& metadata,
74 int64 new_max_object_store_id);
75 void RemoveObjectStore(int64 object_store_id);
76 void AddIndex(int64 object_store_id,
77 const IndexedDBIndexMetadata& metadata,
78 int64 new_max_index_id);
79 void RemoveIndex(int64 object_store_id, int64 index_id);
81 void OpenConnection(const IndexedDBPendingConnection& connection);
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,
89 bool auto_increment);
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,
94 uint16 mode);
95 void Close(IndexedDBConnection* connection, bool forced);
96 void ForceClose();
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,
104 int64 index_id,
105 const base::string16& name,
106 const IndexedDBKeyPath& key_path,
107 bool unique,
108 bool multi_entry);
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,
126 int64 index_id,
127 scoped_ptr<IndexedDBKeyRange> key_range,
128 bool key_only,
129 scoped_refptr<IndexedDBCallbacks> callbacks);
130 void Put(int64 transaction_id,
131 int64 object_store_id,
132 IndexedDBValue* value,
133 ScopedVector<webkit_blob::BlobDataHandle>* handles,
134 scoped_ptr<IndexedDBKey> key,
135 PutMode 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,
147 int64 index_id,
148 scoped_ptr<IndexedDBKeyRange> key_range,
149 indexed_db::CursorDirection,
150 bool key_only,
151 TaskType task_type,
152 scoped_refptr<IndexedDBCallbacks> callbacks);
153 void Count(int64 transaction_id,
154 int64 object_store_id,
155 int64 index_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 CreateObjectStoreOperation(
179 const IndexedDBObjectStoreMetadata& object_store_metadata,
180 IndexedDBTransaction* transaction);
181 void CreateObjectStoreAbortOperation(int64 object_store_id,
182 IndexedDBTransaction* transaction);
183 void DeleteObjectStoreOperation(
184 const IndexedDBObjectStoreMetadata& object_store_metadata,
185 IndexedDBTransaction* transaction);
186 void DeleteObjectStoreAbortOperation(
187 const IndexedDBObjectStoreMetadata& object_store_metadata,
188 IndexedDBTransaction* transaction);
189 void VersionChangeOperation(int64 version,
190 scoped_refptr<IndexedDBCallbacks> callbacks,
191 scoped_ptr<IndexedDBConnection> connection,
192 IndexedDBTransaction* transaction);
193 void VersionChangeAbortOperation(const base::string16& previous_version,
194 int64 previous_int_version,
195 IndexedDBTransaction* transaction);
196 void CreateIndexOperation(int64 object_store_id,
197 const IndexedDBIndexMetadata& index_metadata,
198 IndexedDBTransaction* transaction);
199 void DeleteIndexOperation(int64 object_store_id,
200 const IndexedDBIndexMetadata& index_metadata,
201 IndexedDBTransaction* transaction);
202 void CreateIndexAbortOperation(int64 object_store_id,
203 int64 index_id,
204 IndexedDBTransaction* transaction);
205 void DeleteIndexAbortOperation(int64 object_store_id,
206 const IndexedDBIndexMetadata& index_metadata,
207 IndexedDBTransaction* transaction);
208 void GetOperation(int64 object_store_id,
209 int64 index_id,
210 scoped_ptr<IndexedDBKeyRange> key_range,
211 indexed_db::CursorType cursor_type,
212 scoped_refptr<IndexedDBCallbacks> callbacks,
213 IndexedDBTransaction* transaction);
214 struct PutOperationParams;
215 void PutOperation(scoped_ptr<PutOperationParams> params,
216 IndexedDBTransaction* transaction);
217 void SetIndexesReadyOperation(size_t index_count,
218 IndexedDBTransaction* transaction);
219 struct OpenCursorOperationParams;
220 void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params,
221 IndexedDBTransaction* transaction);
222 void CountOperation(int64 object_store_id,
223 int64 index_id,
224 scoped_ptr<IndexedDBKeyRange> key_range,
225 scoped_refptr<IndexedDBCallbacks> callbacks,
226 IndexedDBTransaction* transaction);
227 void DeleteRangeOperation(int64 object_store_id,
228 scoped_ptr<IndexedDBKeyRange> key_range,
229 scoped_refptr<IndexedDBCallbacks> callbacks,
230 IndexedDBTransaction* transaction);
231 void ClearOperation(int64 object_store_id,
232 scoped_refptr<IndexedDBCallbacks> callbacks,
233 IndexedDBTransaction* transaction);
235 private:
236 friend class base::RefCounted<IndexedDBDatabase>;
238 IndexedDBDatabase(const base::string16& name,
239 IndexedDBBackingStore* backing_store,
240 IndexedDBFactory* factory,
241 const Identifier& unique_identifier);
242 ~IndexedDBDatabase();
244 bool IsOpenConnectionBlocked() const;
245 leveldb::Status OpenInternal();
246 void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks,
247 scoped_ptr<IndexedDBConnection> connection,
248 int64 transaction_id,
249 int64 requested_version);
250 void RunVersionChangeTransactionFinal(
251 scoped_refptr<IndexedDBCallbacks> callbacks,
252 scoped_ptr<IndexedDBConnection> connection,
253 int64 transaction_id,
254 int64 requested_version);
255 void ProcessPendingCalls();
257 bool IsDeleteDatabaseBlocked() const;
258 void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks);
260 scoped_ptr<IndexedDBConnection> CreateConnection(
261 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
262 int child_process_id);
264 IndexedDBTransaction* GetTransaction(int64 transaction_id) const;
266 bool ValidateObjectStoreId(int64 object_store_id) const;
267 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id,
268 int64 index_id) const;
269 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id,
270 int64 index_id) const;
271 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id,
272 int64 index_id) const;
274 scoped_refptr<IndexedDBBackingStore> backing_store_;
275 IndexedDBDatabaseMetadata metadata_;
277 const Identifier identifier_;
278 scoped_refptr<IndexedDBFactory> factory_;
280 IndexedDBTransactionCoordinator transaction_coordinator_;
282 typedef std::map<int64, IndexedDBTransaction*> TransactionMap;
283 TransactionMap transactions_;
285 typedef std::list<IndexedDBPendingConnection> PendingOpenCallList;
286 PendingOpenCallList pending_open_calls_;
288 class PendingUpgradeCall;
289 scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_;
290 class PendingSuccessCall;
291 scoped_ptr<PendingSuccessCall> pending_second_half_open_;
293 class PendingDeleteCall;
294 typedef std::list<PendingDeleteCall*> PendingDeleteCallList;
295 PendingDeleteCallList pending_delete_calls_;
297 typedef list_set<IndexedDBConnection*> ConnectionSet;
298 ConnectionSet connections_;
301 } // namespace content
303 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_