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. 4 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ 7 8 #include <list> 9 #include <map> 10 #include <string> 11 #include <utility> 12 #include <vector> 13 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" 24 25 namespace content { 26 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; 36 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 43 }; 44 45 enum PutMode { 46 ADD_OR_UPDATE, 47 ADD_ONLY, 48 CURSOR_UPDATE 49 }; 50 51 // An index and corresponding set of keys 52 typedef std::pair<int64, std::vector<IndexedDBKey> > IndexKeys; 53 54 // Identifier is pair of (origin url, database name). 55 typedef std::pair<GURL, base::string16> Identifier; 56 57 static const int64 kInvalidId = 0; 58 static const int64 kMinimumIndexId = 30; 59 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); 66 67 const Identifier& identifier() const { return identifier_; } 68 IndexedDBBackingStore* backing_store() { return backing_store_.get(); } 69 70 int64 id() const { return metadata_.id; } 71 const base::string16& name() const { return metadata_.name; } 72 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); 80 81 void OpenConnection(const IndexedDBPendingConnection& connection); 82 void DeleteDatabase(scoped_refptr<IndexedDBCallbacks> callbacks); 83 const IndexedDBDatabaseMetadata& metadata() const { return metadata_; } 84 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(); 97 98 void Commit(int64 transaction_id); 99 void Abort(int64 transaction_id); 100 void Abort(int64 transaction_id, const IndexedDBDatabaseError& error); 101 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); 110 111 IndexedDBTransactionCoordinator& transaction_coordinator() { 112 return transaction_coordinator_; 113 } 114 const IndexedDBTransactionCoordinator& transaction_coordinator() const { 115 return transaction_coordinator_; 116 } 117 118 void TransactionCreated(IndexedDBTransaction* transaction); 119 void TransactionFinished(IndexedDBTransaction* transaction, bool committed); 120 121 // Called by transactions to report failure committing to the backing store. 122 void TransactionCommitFailed(); 123 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); 165 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; 176 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, 194 int64 index_id, 195 IndexedDBTransaction* transaction); 196 void CreateIndexAbortOperation(int64 object_store_id, 197 int64 index_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, 203 int64 index_id, 204 scoped_ptr<IndexedDBKeyRange> key_range, 205 indexed_db::CursorType cursor_type, 206 scoped_refptr<IndexedDBCallbacks> callbacks, 207 IndexedDBTransaction* transaction); 208 struct PutOperationParams; 209 void PutOperation(scoped_ptr<PutOperationParams> params, 210 IndexedDBTransaction* transaction); 211 void SetIndexesReadyOperation(size_t index_count, 212 IndexedDBTransaction* transaction); 213 struct OpenCursorOperationParams; 214 void OpenCursorOperation(scoped_ptr<OpenCursorOperationParams> params, 215 IndexedDBTransaction* transaction); 216 void CountOperation(int64 object_store_id, 217 int64 index_id, 218 scoped_ptr<IndexedDBKeyRange> key_range, 219 scoped_refptr<IndexedDBCallbacks> callbacks, 220 IndexedDBTransaction* transaction); 221 void DeleteRangeOperation(int64 object_store_id, 222 scoped_ptr<IndexedDBKeyRange> key_range, 223 scoped_refptr<IndexedDBCallbacks> callbacks, 224 IndexedDBTransaction* transaction); 225 void ClearOperation(int64 object_store_id, 226 scoped_refptr<IndexedDBCallbacks> callbacks, 227 IndexedDBTransaction* transaction); 228 229 private: 230 friend class base::RefCounted<IndexedDBDatabase>; 231 232 IndexedDBDatabase(const base::string16& name, 233 IndexedDBBackingStore* backing_store, 234 IndexedDBFactory* factory, 235 const Identifier& unique_identifier); 236 ~IndexedDBDatabase(); 237 238 bool IsOpenConnectionBlocked() const; 239 leveldb::Status OpenInternal(); 240 void RunVersionChangeTransaction(scoped_refptr<IndexedDBCallbacks> callbacks, 241 scoped_ptr<IndexedDBConnection> connection, 242 int64 transaction_id, 243 int64 requested_version); 244 void RunVersionChangeTransactionFinal( 245 scoped_refptr<IndexedDBCallbacks> callbacks, 246 scoped_ptr<IndexedDBConnection> connection, 247 int64 transaction_id, 248 int64 requested_version); 249 void ProcessPendingCalls(); 250 251 bool IsDeleteDatabaseBlocked() const; 252 void DeleteDatabaseFinal(scoped_refptr<IndexedDBCallbacks> callbacks); 253 254 scoped_ptr<IndexedDBConnection> CreateConnection( 255 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, 256 int child_process_id); 257 258 IndexedDBTransaction* GetTransaction(int64 transaction_id) const; 259 260 bool ValidateObjectStoreId(int64 object_store_id) const; 261 bool ValidateObjectStoreIdAndIndexId(int64 object_store_id, 262 int64 index_id) const; 263 bool ValidateObjectStoreIdAndOptionalIndexId(int64 object_store_id, 264 int64 index_id) const; 265 bool ValidateObjectStoreIdAndNewIndexId(int64 object_store_id, 266 int64 index_id) const; 267 268 scoped_refptr<IndexedDBBackingStore> backing_store_; 269 IndexedDBDatabaseMetadata metadata_; 270 271 const Identifier identifier_; 272 scoped_refptr<IndexedDBFactory> factory_; 273 274 IndexedDBTransactionCoordinator transaction_coordinator_; 275 276 typedef std::map<int64, IndexedDBTransaction*> TransactionMap; 277 TransactionMap transactions_; 278 279 typedef std::list<IndexedDBPendingConnection> PendingOpenCallList; 280 PendingOpenCallList pending_open_calls_; 281 282 class PendingUpgradeCall; 283 scoped_ptr<PendingUpgradeCall> pending_run_version_change_transaction_call_; 284 class PendingSuccessCall; 285 scoped_ptr<PendingSuccessCall> pending_second_half_open_; 286 287 class PendingDeleteCall; 288 typedef std::list<PendingDeleteCall*> PendingDeleteCallList; 289 PendingDeleteCallList pending_delete_calls_; 290 291 typedef list_set<IndexedDBConnection*> ConnectionSet; 292 ConnectionSet connections_; 293 294 DISALLOW_COPY_AND_ASSIGN(IndexedDBDatabase); 295 }; 296 297 } // namespace content 298 299 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_DATABASE_H_ 300