| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
| 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "content/browser/indexed_db/indexed_db.h" | 17 #include "content/browser/indexed_db/indexed_db.h" |
| 18 #include "content/browser/indexed_db/indexed_db_active_blob_registry.h" |
| 18 #include "content/browser/indexed_db/indexed_db_metadata.h" | 19 #include "content/browser/indexed_db/indexed_db_metadata.h" |
| 19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | 20 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
| 20 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" | 21 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" |
| 21 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
| 22 #include "content/common/indexed_db/indexed_db_key.h" | 23 #include "content/common/indexed_db/indexed_db_key.h" |
| 23 #include "content/common/indexed_db/indexed_db_key_path.h" | 24 #include "content/common/indexed_db/indexed_db_key_path.h" |
| 24 #include "content/common/indexed_db/indexed_db_key_range.h" | 25 #include "content/common/indexed_db/indexed_db_key_range.h" |
| 25 #include "third_party/leveldatabase/src/include/leveldb/status.h" | 26 #include "third_party/leveldatabase/src/include/leveldb/status.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 29 namespace base { |
| 30 class TaskRunner; |
| 31 } |
| 32 |
| 28 namespace content { | 33 namespace content { |
| 29 | 34 |
| 35 class IndexedDBFactory; |
| 30 class LevelDBComparator; | 36 class LevelDBComparator; |
| 31 class LevelDBDatabase; | 37 class LevelDBDatabase; |
| 32 struct IndexedDBValue; | 38 struct IndexedDBValue; |
| 33 | 39 |
| 34 class LevelDBFactory { | 40 class LevelDBFactory { |
| 35 public: | 41 public: |
| 36 virtual ~LevelDBFactory() {} | 42 virtual ~LevelDBFactory() {} |
| 37 virtual leveldb::Status OpenLevelDB(const base::FilePath& file_name, | 43 virtual leveldb::Status OpenLevelDB(const base::FilePath& file_name, |
| 38 const LevelDBComparator* comparator, | 44 const LevelDBComparator* comparator, |
| 39 scoped_ptr<LevelDBDatabase>* db, | 45 scoped_ptr<LevelDBDatabase>* db, |
| 40 bool* is_disk_full) = 0; | 46 bool* is_disk_full) = 0; |
| 41 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) = 0; | 47 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) = 0; |
| 42 }; | 48 }; |
| 43 | 49 |
| 44 class CONTENT_EXPORT IndexedDBBackingStore | 50 class CONTENT_EXPORT IndexedDBBackingStore |
| 45 : public base::RefCounted<IndexedDBBackingStore> { | 51 : public base::RefCounted<IndexedDBBackingStore> { |
| 46 public: | 52 public: |
| 47 class CONTENT_EXPORT Transaction; | 53 class CONTENT_EXPORT Transaction; |
| 48 | 54 |
| 49 class Comparator : public LevelDBComparator { | 55 class Comparator : public LevelDBComparator { |
| 50 public: | 56 public: |
| 51 virtual int Compare(const base::StringPiece& a, | 57 virtual int Compare(const base::StringPiece& a, |
| 52 const base::StringPiece& b) const OVERRIDE; | 58 const base::StringPiece& b) const OVERRIDE; |
| 53 virtual const char* Name() const OVERRIDE; | 59 virtual const char* Name() const OVERRIDE; |
| 54 }; | 60 }; |
| 55 | 61 |
| 56 const GURL& origin_url() const { return origin_url_; } | 62 const GURL& origin_url() const { return origin_url_; } |
| 63 IndexedDBFactory* factory() const { return indexed_db_factory_; } |
| 64 base::TaskRunner* task_runner() const { return task_runner_; } |
| 57 base::OneShotTimer<IndexedDBBackingStore>* close_timer() { | 65 base::OneShotTimer<IndexedDBBackingStore>* close_timer() { |
| 58 return &close_timer_; | 66 return &close_timer_; |
| 59 } | 67 } |
| 68 IndexedDBActiveBlobRegistry* active_blob_registry() { |
| 69 return &active_blob_registry_; |
| 70 } |
| 60 | 71 |
| 61 static scoped_refptr<IndexedDBBackingStore> Open( | 72 static scoped_refptr<IndexedDBBackingStore> Open( |
| 62 const GURL& origin_url, | 73 IndexedDBFactory* indexed_db_factory, |
| 63 const base::FilePath& path_base, | |
| 64 blink::WebIDBDataLoss* data_loss, | |
| 65 std::string* data_loss_message, | |
| 66 bool* disk_full); | |
| 67 | |
| 68 static scoped_refptr<IndexedDBBackingStore> Open( | |
| 69 const GURL& origin_url, | 74 const GURL& origin_url, |
| 70 const base::FilePath& path_base, | 75 const base::FilePath& path_base, |
| 71 blink::WebIDBDataLoss* data_loss, | 76 blink::WebIDBDataLoss* data_loss, |
| 72 std::string* data_loss_message, | 77 std::string* data_loss_message, |
| 73 bool* disk_full, | 78 bool* disk_full, |
| 74 LevelDBFactory* factory); | 79 base::TaskRunner* task_runner); |
| 75 static scoped_refptr<IndexedDBBackingStore> OpenInMemory( | 80 |
| 76 const GURL& origin_url); | 81 static scoped_refptr<IndexedDBBackingStore> Open( |
| 82 IndexedDBFactory* indexed_db_factory, |
| 83 const GURL& origin_url, |
| 84 const base::FilePath& path_base, |
| 85 blink::WebIDBDataLoss* data_loss, |
| 86 std::string* data_loss_message, |
| 87 bool* disk_full, |
| 88 LevelDBFactory* leveldb_factory, |
| 89 base::TaskRunner* task_runner); |
| 77 static scoped_refptr<IndexedDBBackingStore> OpenInMemory( | 90 static scoped_refptr<IndexedDBBackingStore> OpenInMemory( |
| 78 const GURL& origin_url, | 91 const GURL& origin_url, |
| 79 LevelDBFactory* factory); | 92 base::TaskRunner* task_runner); |
| 93 static scoped_refptr<IndexedDBBackingStore> OpenInMemory( |
| 94 const GURL& origin_url, |
| 95 LevelDBFactory* level_db_factory, |
| 96 base::TaskRunner* task_runner); |
| 80 | 97 |
| 81 // Compact is public for testing. | 98 // Compact is public for testing. |
| 82 virtual void Compact(); | 99 virtual void Compact(); |
| 83 virtual std::vector<base::string16> GetDatabaseNames(); | 100 virtual std::vector<base::string16> GetDatabaseNames(); |
| 84 virtual leveldb::Status GetIDBDatabaseMetaData( | 101 virtual leveldb::Status GetIDBDatabaseMetaData( |
| 85 const base::string16& name, | 102 const base::string16& name, |
| 86 IndexedDBDatabaseMetadata* metadata, | 103 IndexedDBDatabaseMetadata* metadata, |
| 87 bool* success) WARN_UNUSED_RESULT; | 104 bool* success) WARN_UNUSED_RESULT; |
| 88 virtual leveldb::Status CreateIDBDatabaseMetaData( | 105 virtual leveldb::Status CreateIDBDatabaseMetaData( |
| 89 const base::string16& name, | 106 const base::string16& name, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 scoped_ptr<IndexedDBKey>* primary_key) WARN_UNUSED_RESULT; | 226 scoped_ptr<IndexedDBKey>* primary_key) WARN_UNUSED_RESULT; |
| 210 virtual leveldb::Status KeyExistsInIndex( | 227 virtual leveldb::Status KeyExistsInIndex( |
| 211 IndexedDBBackingStore::Transaction* transaction, | 228 IndexedDBBackingStore::Transaction* transaction, |
| 212 int64 database_id, | 229 int64 database_id, |
| 213 int64 object_store_id, | 230 int64 object_store_id, |
| 214 int64 index_id, | 231 int64 index_id, |
| 215 const IndexedDBKey& key, | 232 const IndexedDBKey& key, |
| 216 scoped_ptr<IndexedDBKey>* found_primary_key, | 233 scoped_ptr<IndexedDBKey>* found_primary_key, |
| 217 bool* exists) WARN_UNUSED_RESULT; | 234 bool* exists) WARN_UNUSED_RESULT; |
| 218 | 235 |
| 236 // Public for IndexedDBActiveBlobRegistry::ReleaseBlobRef. |
| 237 virtual void ReportBlobUnused(int64 database_id, int64 blob_key); |
| 238 |
| 219 class Cursor { | 239 class Cursor { |
| 220 public: | 240 public: |
| 221 virtual ~Cursor(); | 241 virtual ~Cursor(); |
| 222 | 242 |
| 223 enum IteratorState { | 243 enum IteratorState { |
| 224 READY = 0, | 244 READY = 0, |
| 225 SEEK | 245 SEEK |
| 226 }; | 246 }; |
| 227 | 247 |
| 228 struct CursorOptions { | 248 struct CursorOptions { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 } | 335 } |
| 316 | 336 |
| 317 LevelDBTransaction* transaction() { return transaction_; } | 337 LevelDBTransaction* transaction() { return transaction_; } |
| 318 | 338 |
| 319 private: | 339 private: |
| 320 IndexedDBBackingStore* backing_store_; | 340 IndexedDBBackingStore* backing_store_; |
| 321 scoped_refptr<LevelDBTransaction> transaction_; | 341 scoped_refptr<LevelDBTransaction> transaction_; |
| 322 }; | 342 }; |
| 323 | 343 |
| 324 protected: | 344 protected: |
| 325 IndexedDBBackingStore(const GURL& origin_url, | 345 IndexedDBBackingStore(IndexedDBFactory* indexed_db_factory, |
| 346 const GURL& origin_url, |
| 326 scoped_ptr<LevelDBDatabase> db, | 347 scoped_ptr<LevelDBDatabase> db, |
| 327 scoped_ptr<LevelDBComparator> comparator); | 348 scoped_ptr<LevelDBComparator> comparator, |
| 349 base::TaskRunner* task_runner); |
| 328 virtual ~IndexedDBBackingStore(); | 350 virtual ~IndexedDBBackingStore(); |
| 329 friend class base::RefCounted<IndexedDBBackingStore>; | 351 friend class base::RefCounted<IndexedDBBackingStore>; |
| 330 | 352 |
| 331 private: | 353 private: |
| 332 static scoped_refptr<IndexedDBBackingStore> Create( | 354 static scoped_refptr<IndexedDBBackingStore> Create( |
| 355 IndexedDBFactory* indexed_db_factory, |
| 333 const GURL& origin_url, | 356 const GURL& origin_url, |
| 334 scoped_ptr<LevelDBDatabase> db, | 357 scoped_ptr<LevelDBDatabase> db, |
| 335 scoped_ptr<LevelDBComparator> comparator); | 358 scoped_ptr<LevelDBComparator> comparator, |
| 359 base::TaskRunner* task_runner); |
| 360 |
| 336 static bool ReadCorruptionInfo(const base::FilePath& path_base, | 361 static bool ReadCorruptionInfo(const base::FilePath& path_base, |
| 337 const GURL& origin_url, | 362 const GURL& origin_url, |
| 338 std::string& message); | 363 std::string& message); |
| 339 | 364 |
| 340 leveldb::Status FindKeyInIndex( | 365 leveldb::Status FindKeyInIndex( |
| 341 IndexedDBBackingStore::Transaction* transaction, | 366 IndexedDBBackingStore::Transaction* transaction, |
| 342 int64 database_id, | 367 int64 database_id, |
| 343 int64 object_store_id, | 368 int64 object_store_id, |
| 344 int64 index_id, | 369 int64 index_id, |
| 345 const IndexedDBKey& key, | 370 const IndexedDBKey& key, |
| 346 std::string* found_encoded_primary_key, | 371 std::string* found_encoded_primary_key, |
| 347 bool* found); | 372 bool* found); |
| 348 leveldb::Status GetIndexes(int64 database_id, | 373 leveldb::Status GetIndexes(int64 database_id, |
| 349 int64 object_store_id, | 374 int64 object_store_id, |
| 350 IndexedDBObjectStoreMetadata::IndexMap* map) | 375 IndexedDBObjectStoreMetadata::IndexMap* map) |
| 351 WARN_UNUSED_RESULT; | 376 WARN_UNUSED_RESULT; |
| 352 | 377 |
| 378 IndexedDBFactory* indexed_db_factory_; |
| 353 const GURL origin_url_; | 379 const GURL origin_url_; |
| 354 | 380 |
| 355 // The origin identifier is a key prefix unique to the origin used in the | 381 // The origin identifier is a key prefix unique to the origin used in the |
| 356 // leveldb backing store to partition data by origin. It is a normalized | 382 // leveldb backing store to partition data by origin. It is a normalized |
| 357 // version of the origin URL with a versioning suffix appended, e.g. | 383 // version of the origin URL with a versioning suffix appended, e.g. |
| 358 // "http_localhost_81@1" Since only one origin is stored per backing store | 384 // "http_localhost_81@1" Since only one origin is stored per backing store |
| 359 // this is redundant but necessary for backwards compatibility; the suffix | 385 // this is redundant but necessary for backwards compatibility; the suffix |
| 360 // provides for future flexibility. | 386 // provides for future flexibility. |
| 361 const std::string origin_identifier_; | 387 const std::string origin_identifier_; |
| 388 base::TaskRunner* task_runner_; |
| 362 | 389 |
| 363 scoped_ptr<LevelDBDatabase> db_; | 390 scoped_ptr<LevelDBDatabase> db_; |
| 364 scoped_ptr<LevelDBComparator> comparator_; | 391 scoped_ptr<LevelDBComparator> comparator_; |
| 392 // Whenever blobs are registered in active_blob_registry_, indexed_db_factory_ |
| 393 // will hold a reference to this backing store. |
| 394 IndexedDBActiveBlobRegistry active_blob_registry_; |
| 365 base::OneShotTimer<IndexedDBBackingStore> close_timer_; | 395 base::OneShotTimer<IndexedDBBackingStore> close_timer_; |
| 366 }; | 396 }; |
| 367 | 397 |
| 368 } // namespace content | 398 } // namespace content |
| 369 | 399 |
| 370 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ | 400 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_BACKING_STORE_H_ |
| OLD | NEW |