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 #include "content/browser/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 bool* is_disk_full) OVERRIDE { | 389 bool* is_disk_full) OVERRIDE { |
390 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); | 390 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); |
391 } | 391 } |
392 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) | 392 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) |
393 OVERRIDE { | 393 OVERRIDE { |
394 return LevelDBDatabase::Destroy(file_name); | 394 return LevelDBDatabase::Destroy(file_name); |
395 } | 395 } |
396 }; | 396 }; |
397 | 397 |
398 IndexedDBBackingStore::IndexedDBBackingStore( | 398 IndexedDBBackingStore::IndexedDBBackingStore( |
| 399 IndexedDBFactory* indexed_db_factory, |
399 const GURL& origin_url, | 400 const GURL& origin_url, |
400 scoped_ptr<LevelDBDatabase> db, | 401 scoped_ptr<LevelDBDatabase> db, |
401 scoped_ptr<LevelDBComparator> comparator) | 402 scoped_ptr<LevelDBComparator> comparator, |
402 : origin_url_(origin_url), | 403 base::TaskRunner* task_runner) |
| 404 : indexed_db_factory_(indexed_db_factory), |
| 405 origin_url_(origin_url), |
403 origin_identifier_(ComputeOriginIdentifier(origin_url)), | 406 origin_identifier_(ComputeOriginIdentifier(origin_url)), |
| 407 task_runner_(task_runner), |
404 db_(db.Pass()), | 408 db_(db.Pass()), |
405 comparator_(comparator.Pass()) {} | 409 comparator_(comparator.Pass()), |
| 410 active_blob_registry_(this) {} |
406 | 411 |
407 IndexedDBBackingStore::~IndexedDBBackingStore() { | 412 IndexedDBBackingStore::~IndexedDBBackingStore() { |
408 // db_'s destructor uses comparator_. The order of destruction is important. | 413 // db_'s destructor uses comparator_. The order of destruction is important. |
409 db_.reset(); | 414 db_.reset(); |
410 comparator_.reset(); | 415 comparator_.reset(); |
411 } | 416 } |
412 | 417 |
413 IndexedDBBackingStore::RecordIdentifier::RecordIdentifier( | 418 IndexedDBBackingStore::RecordIdentifier::RecordIdentifier( |
414 const std::string& primary_key, | 419 const std::string& primary_key, |
415 int64 version) | 420 int64 version) |
(...skipping 20 matching lines...) Expand all Loading... |
436 INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, | 441 INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, |
437 INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, | 442 INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, |
438 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, | 443 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, |
439 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, | 444 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, |
440 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, | 445 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, |
441 INDEXED_DB_BACKING_STORE_OPEN_MAX, | 446 INDEXED_DB_BACKING_STORE_OPEN_MAX, |
442 }; | 447 }; |
443 | 448 |
444 // static | 449 // static |
445 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 450 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
| 451 IndexedDBFactory* indexed_db_factory, |
446 const GURL& origin_url, | 452 const GURL& origin_url, |
447 const base::FilePath& path_base, | 453 const base::FilePath& path_base, |
448 blink::WebIDBDataLoss* data_loss, | 454 blink::WebIDBDataLoss* data_loss, |
449 std::string* data_loss_message, | 455 std::string* data_loss_message, |
450 bool* disk_full) { | 456 bool* disk_full, |
| 457 base::TaskRunner* task_runner) { |
451 *data_loss = blink::WebIDBDataLossNone; | 458 *data_loss = blink::WebIDBDataLossNone; |
452 DefaultLevelDBFactory leveldb_factory; | 459 DefaultLevelDBFactory leveldb_factory; |
453 return IndexedDBBackingStore::Open(origin_url, | 460 return IndexedDBBackingStore::Open(indexed_db_factory, |
| 461 origin_url, |
454 path_base, | 462 path_base, |
455 data_loss, | 463 data_loss, |
456 data_loss_message, | 464 data_loss_message, |
457 disk_full, | 465 disk_full, |
458 &leveldb_factory); | 466 &leveldb_factory, |
| 467 task_runner); |
459 } | 468 } |
460 | 469 |
461 static std::string OriginToCustomHistogramSuffix(const GURL& origin_url) { | 470 static std::string OriginToCustomHistogramSuffix(const GURL& origin_url) { |
462 if (origin_url.host() == "docs.google.com") | 471 if (origin_url.host() == "docs.google.com") |
463 return ".Docs"; | 472 return ".Docs"; |
464 return std::string(); | 473 return std::string(); |
465 } | 474 } |
466 | 475 |
467 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result, | 476 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result, |
468 const GURL& origin_url) { | 477 const GURL& origin_url) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 min, | 518 min, |
510 max, | 519 max, |
511 num_buckets); | 520 num_buckets); |
512 return true; | 521 return true; |
513 } | 522 } |
514 return false; | 523 return false; |
515 } | 524 } |
516 | 525 |
517 // static | 526 // static |
518 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 527 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
| 528 IndexedDBFactory* indexed_db_factory, |
519 const GURL& origin_url, | 529 const GURL& origin_url, |
520 const base::FilePath& path_base, | 530 const base::FilePath& path_base, |
521 blink::WebIDBDataLoss* data_loss, | 531 blink::WebIDBDataLoss* data_loss, |
522 std::string* data_loss_message, | 532 std::string* data_loss_message, |
523 bool* is_disk_full, | 533 bool* is_disk_full, |
524 LevelDBFactory* leveldb_factory) { | 534 LevelDBFactory* leveldb_factory, |
| 535 base::TaskRunner* task_runner) { |
525 IDB_TRACE("IndexedDBBackingStore::Open"); | 536 IDB_TRACE("IndexedDBBackingStore::Open"); |
526 DCHECK(!path_base.empty()); | 537 DCHECK(!path_base.empty()); |
527 *data_loss = blink::WebIDBDataLossNone; | 538 *data_loss = blink::WebIDBDataLossNone; |
528 *data_loss_message = ""; | 539 *data_loss_message = ""; |
529 *is_disk_full = false; | 540 *is_disk_full = false; |
530 | 541 |
531 scoped_ptr<LevelDBComparator> comparator(new Comparator()); | 542 scoped_ptr<LevelDBComparator> comparator(new Comparator()); |
532 | 543 |
533 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { | 544 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { |
534 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, | 545 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 origin_url); | 632 origin_url); |
622 } | 633 } |
623 | 634 |
624 if (!db) { | 635 if (!db) { |
625 NOTREACHED(); | 636 NOTREACHED(); |
626 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_ERR, | 637 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_ERR, |
627 origin_url); | 638 origin_url); |
628 return scoped_refptr<IndexedDBBackingStore>(); | 639 return scoped_refptr<IndexedDBBackingStore>(); |
629 } | 640 } |
630 | 641 |
631 return Create(origin_url, db.Pass(), comparator.Pass()); | 642 return Create(indexed_db_factory, |
632 } | 643 origin_url, |
633 | 644 db.Pass(), |
634 // static | 645 comparator.Pass(), |
635 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( | 646 task_runner); |
636 const GURL& origin_url) { | |
637 DefaultLevelDBFactory leveldb_factory; | |
638 return IndexedDBBackingStore::OpenInMemory(origin_url, &leveldb_factory); | |
639 } | 647 } |
640 | 648 |
641 // static | 649 // static |
642 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( | 650 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( |
643 const GURL& origin_url, | 651 const GURL& origin_url, |
644 LevelDBFactory* leveldb_factory) { | 652 base::TaskRunner* task_runner) { |
| 653 DefaultLevelDBFactory leveldb_factory; |
| 654 return IndexedDBBackingStore::OpenInMemory( |
| 655 origin_url, &leveldb_factory, task_runner); |
| 656 } |
| 657 |
| 658 // static |
| 659 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( |
| 660 const GURL& origin_url, |
| 661 LevelDBFactory* leveldb_factory, |
| 662 base::TaskRunner* task_runner) { |
645 IDB_TRACE("IndexedDBBackingStore::OpenInMemory"); | 663 IDB_TRACE("IndexedDBBackingStore::OpenInMemory"); |
646 | 664 |
647 scoped_ptr<LevelDBComparator> comparator(new Comparator()); | 665 scoped_ptr<LevelDBComparator> comparator(new Comparator()); |
648 scoped_ptr<LevelDBDatabase> db = | 666 scoped_ptr<LevelDBDatabase> db = |
649 LevelDBDatabase::OpenInMemory(comparator.get()); | 667 LevelDBDatabase::OpenInMemory(comparator.get()); |
650 if (!db) { | 668 if (!db) { |
651 LOG(ERROR) << "LevelDBDatabase::OpenInMemory failed."; | 669 LOG(ERROR) << "LevelDBDatabase::OpenInMemory failed."; |
652 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, | 670 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, |
653 origin_url); | 671 origin_url); |
654 return scoped_refptr<IndexedDBBackingStore>(); | 672 return scoped_refptr<IndexedDBBackingStore>(); |
655 } | 673 } |
656 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_SUCCESS, origin_url); | 674 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_SUCCESS, origin_url); |
657 | 675 |
658 return Create(origin_url, db.Pass(), comparator.Pass()); | 676 return Create(NULL /* indexed_db_factory */, |
| 677 origin_url, |
| 678 db.Pass(), |
| 679 comparator.Pass(), |
| 680 task_runner); |
659 } | 681 } |
660 | 682 |
661 // static | 683 // static |
662 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Create( | 684 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Create( |
| 685 IndexedDBFactory* indexed_db_factory, |
663 const GURL& origin_url, | 686 const GURL& origin_url, |
664 scoped_ptr<LevelDBDatabase> db, | 687 scoped_ptr<LevelDBDatabase> db, |
665 scoped_ptr<LevelDBComparator> comparator) { | 688 scoped_ptr<LevelDBComparator> comparator, |
| 689 base::TaskRunner* task_runner) { |
666 // TODO(jsbell): Handle comparator name changes. | 690 // TODO(jsbell): Handle comparator name changes. |
667 | 691 |
668 scoped_refptr<IndexedDBBackingStore> backing_store( | 692 scoped_refptr<IndexedDBBackingStore> backing_store( |
669 new IndexedDBBackingStore(origin_url, db.Pass(), comparator.Pass())); | 693 new IndexedDBBackingStore(indexed_db_factory, |
| 694 origin_url, |
| 695 db.Pass(), |
| 696 comparator.Pass(), |
| 697 task_runner)); |
670 if (!SetUpMetadata(backing_store->db_.get(), | 698 if (!SetUpMetadata(backing_store->db_.get(), |
671 backing_store->origin_identifier_)) | 699 backing_store->origin_identifier_)) |
672 return scoped_refptr<IndexedDBBackingStore>(); | 700 return scoped_refptr<IndexedDBBackingStore>(); |
673 | 701 |
674 return backing_store; | 702 return backing_store; |
675 } | 703 } |
676 | 704 |
677 std::vector<base::string16> IndexedDBBackingStore::GetDatabaseNames() { | 705 std::vector<base::string16> IndexedDBBackingStore::GetDatabaseNames() { |
678 std::vector<base::string16> found_names; | 706 std::vector<base::string16> found_names; |
679 const std::string start_key = | 707 const std::string start_key = |
(...skipping 1169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1849 return InvalidDBKeyStatus(); | 1877 return InvalidDBKeyStatus(); |
1850 } | 1878 } |
1851 | 1879 |
1852 StringPiece slice(found_encoded_primary_key); | 1880 StringPiece slice(found_encoded_primary_key); |
1853 if (DecodeIDBKey(&slice, found_primary_key) && slice.empty()) | 1881 if (DecodeIDBKey(&slice, found_primary_key) && slice.empty()) |
1854 return s; | 1882 return s; |
1855 else | 1883 else |
1856 return InvalidDBKeyStatus(); | 1884 return InvalidDBKeyStatus(); |
1857 } | 1885 } |
1858 | 1886 |
| 1887 void IndexedDBBackingStore::ReportBlobUnused(int64 database_id, |
| 1888 int64 blob_key) { |
| 1889 // TODO(ericu) |
| 1890 } |
| 1891 |
1859 IndexedDBBackingStore::Cursor::Cursor( | 1892 IndexedDBBackingStore::Cursor::Cursor( |
1860 const IndexedDBBackingStore::Cursor* other) | 1893 const IndexedDBBackingStore::Cursor* other) |
1861 : transaction_(other->transaction_), | 1894 : transaction_(other->transaction_), |
1862 cursor_options_(other->cursor_options_), | 1895 cursor_options_(other->cursor_options_), |
1863 current_key_(new IndexedDBKey(*other->current_key_)) { | 1896 current_key_(new IndexedDBKey(*other->current_key_)) { |
1864 if (other->iterator_) { | 1897 if (other->iterator_) { |
1865 iterator_ = transaction_->CreateIterator(); | 1898 iterator_ = transaction_->CreateIterator(); |
1866 | 1899 |
1867 if (other->iterator_->IsValid()) { | 1900 if (other->iterator_->IsValid()) { |
1868 iterator_->Seek(other->iterator_->Key()); | 1901 iterator_->Seek(other->iterator_->Key()); |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2645 } | 2678 } |
2646 | 2679 |
2647 void IndexedDBBackingStore::Transaction::Rollback() { | 2680 void IndexedDBBackingStore::Transaction::Rollback() { |
2648 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); | 2681 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); |
2649 DCHECK(transaction_.get()); | 2682 DCHECK(transaction_.get()); |
2650 transaction_->Rollback(); | 2683 transaction_->Rollback(); |
2651 transaction_ = NULL; | 2684 transaction_ = NULL; |
2652 } | 2685 } |
2653 | 2686 |
2654 } // namespace content | 2687 } // namespace content |
OLD | NEW |