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/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 bool* is_disk_full) OVERRIDE { | 415 bool* is_disk_full) OVERRIDE { |
416 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); | 416 return LevelDBDatabase::Open(file_name, comparator, db, is_disk_full); |
417 } | 417 } |
418 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) | 418 virtual leveldb::Status DestroyLevelDB(const base::FilePath& file_name) |
419 OVERRIDE { | 419 OVERRIDE { |
420 return LevelDBDatabase::Destroy(file_name); | 420 return LevelDBDatabase::Destroy(file_name); |
421 } | 421 } |
422 }; | 422 }; |
423 | 423 |
424 IndexedDBBackingStore::IndexedDBBackingStore( | 424 IndexedDBBackingStore::IndexedDBBackingStore( |
| 425 IndexedDBFactory* indexed_db_factory, |
425 const GURL& origin_url, | 426 const GURL& origin_url, |
426 scoped_ptr<LevelDBDatabase> db, | 427 scoped_ptr<LevelDBDatabase> db, |
427 scoped_ptr<LevelDBComparator> comparator) | 428 scoped_ptr<LevelDBComparator> comparator, |
428 : origin_url_(origin_url), | 429 base::TaskRunner* task_runner) |
| 430 : indexed_db_factory_(indexed_db_factory), |
| 431 origin_url_(origin_url), |
429 origin_identifier_(ComputeOriginIdentifier(origin_url)), | 432 origin_identifier_(ComputeOriginIdentifier(origin_url)), |
| 433 task_runner_(task_runner), |
430 db_(db.Pass()), | 434 db_(db.Pass()), |
431 comparator_(comparator.Pass()) {} | 435 comparator_(comparator.Pass()), |
| 436 active_blob_registry_(this) {} |
432 | 437 |
433 IndexedDBBackingStore::~IndexedDBBackingStore() { | 438 IndexedDBBackingStore::~IndexedDBBackingStore() { |
434 // db_'s destructor uses comparator_. The order of destruction is important. | 439 // db_'s destructor uses comparator_. The order of destruction is important. |
435 db_.reset(); | 440 db_.reset(); |
436 comparator_.reset(); | 441 comparator_.reset(); |
437 } | 442 } |
438 | 443 |
439 IndexedDBBackingStore::RecordIdentifier::RecordIdentifier( | 444 IndexedDBBackingStore::RecordIdentifier::RecordIdentifier( |
440 const std::string& primary_key, | 445 const std::string& primary_key, |
441 int64 version) | 446 int64 version) |
(...skipping 21 matching lines...) Expand all Loading... |
463 INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, | 468 INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, |
464 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, | 469 INDEXED_DB_BACKING_STORE_OPEN_DISK_FULL_DEPRECATED, |
465 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, | 470 INDEXED_DB_BACKING_STORE_OPEN_ORIGIN_TOO_LONG, |
466 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, | 471 INDEXED_DB_BACKING_STORE_OPEN_NO_RECOVERY, |
467 INDEXED_DB_BACKING_STORE_OPEN_FAILED_PRIOR_CORRUPTION, | 472 INDEXED_DB_BACKING_STORE_OPEN_FAILED_PRIOR_CORRUPTION, |
468 INDEXED_DB_BACKING_STORE_OPEN_MAX, | 473 INDEXED_DB_BACKING_STORE_OPEN_MAX, |
469 }; | 474 }; |
470 | 475 |
471 // static | 476 // static |
472 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 477 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
| 478 IndexedDBFactory* indexed_db_factory, |
473 const GURL& origin_url, | 479 const GURL& origin_url, |
474 const base::FilePath& path_base, | 480 const base::FilePath& path_base, |
475 blink::WebIDBDataLoss* data_loss, | 481 blink::WebIDBDataLoss* data_loss, |
476 std::string* data_loss_message, | 482 std::string* data_loss_message, |
477 bool* disk_full) { | 483 bool* disk_full, |
| 484 base::TaskRunner* task_runner) { |
478 *data_loss = blink::WebIDBDataLossNone; | 485 *data_loss = blink::WebIDBDataLossNone; |
479 DefaultLevelDBFactory leveldb_factory; | 486 DefaultLevelDBFactory leveldb_factory; |
480 return IndexedDBBackingStore::Open(origin_url, | 487 return IndexedDBBackingStore::Open(indexed_db_factory, |
| 488 origin_url, |
481 path_base, | 489 path_base, |
482 data_loss, | 490 data_loss, |
483 data_loss_message, | 491 data_loss_message, |
484 disk_full, | 492 disk_full, |
485 &leveldb_factory); | 493 &leveldb_factory, |
| 494 task_runner); |
486 } | 495 } |
487 | 496 |
488 static std::string OriginToCustomHistogramSuffix(const GURL& origin_url) { | 497 static std::string OriginToCustomHistogramSuffix(const GURL& origin_url) { |
489 if (origin_url.host() == "docs.google.com") | 498 if (origin_url.host() == "docs.google.com") |
490 return ".Docs"; | 499 return ".Docs"; |
491 return std::string(); | 500 return std::string(); |
492 } | 501 } |
493 | 502 |
494 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result, | 503 static void HistogramOpenStatus(IndexedDBBackingStoreOpenResult result, |
495 const GURL& origin_url) { | 504 const GURL& origin_url) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 if (!file) | 630 if (!file) |
622 return false; | 631 return false; |
623 int written = | 632 int written = |
624 base::WritePlatformFile(file, 0, output_js.c_str(), output_js.length()); | 633 base::WritePlatformFile(file, 0, output_js.c_str(), output_js.length()); |
625 base::ClosePlatformFile(file); | 634 base::ClosePlatformFile(file); |
626 return size_t(written) == output_js.length(); | 635 return size_t(written) == output_js.length(); |
627 } | 636 } |
628 | 637 |
629 // static | 638 // static |
630 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( | 639 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Open( |
| 640 IndexedDBFactory* indexed_db_factory, |
631 const GURL& origin_url, | 641 const GURL& origin_url, |
632 const base::FilePath& path_base, | 642 const base::FilePath& path_base, |
633 blink::WebIDBDataLoss* data_loss, | 643 blink::WebIDBDataLoss* data_loss, |
634 std::string* data_loss_message, | 644 std::string* data_loss_message, |
635 bool* is_disk_full, | 645 bool* is_disk_full, |
636 LevelDBFactory* leveldb_factory) { | 646 LevelDBFactory* leveldb_factory, |
| 647 base::TaskRunner* task_runner) { |
637 IDB_TRACE("IndexedDBBackingStore::Open"); | 648 IDB_TRACE("IndexedDBBackingStore::Open"); |
638 DCHECK(!path_base.empty()); | 649 DCHECK(!path_base.empty()); |
639 *data_loss = blink::WebIDBDataLossNone; | 650 *data_loss = blink::WebIDBDataLossNone; |
640 *data_loss_message = ""; | 651 *data_loss_message = ""; |
641 *is_disk_full = false; | 652 *is_disk_full = false; |
642 | 653 |
643 scoped_ptr<LevelDBComparator> comparator(new Comparator()); | 654 scoped_ptr<LevelDBComparator> comparator(new Comparator()); |
644 | 655 |
645 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { | 656 if (!IsStringASCII(path_base.AsUTF8Unsafe())) { |
646 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, | 657 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_ATTEMPT_NON_ASCII, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 origin_url); | 753 origin_url); |
743 } | 754 } |
744 | 755 |
745 if (!db) { | 756 if (!db) { |
746 NOTREACHED(); | 757 NOTREACHED(); |
747 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_ERR, | 758 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_FAILED_UNKNOWN_ERR, |
748 origin_url); | 759 origin_url); |
749 return scoped_refptr<IndexedDBBackingStore>(); | 760 return scoped_refptr<IndexedDBBackingStore>(); |
750 } | 761 } |
751 | 762 |
752 return Create(origin_url, db.Pass(), comparator.Pass()); | 763 return Create(indexed_db_factory, |
753 } | 764 origin_url, |
754 | 765 db.Pass(), |
755 // static | 766 comparator.Pass(), |
756 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( | 767 task_runner); |
757 const GURL& origin_url) { | |
758 DefaultLevelDBFactory leveldb_factory; | |
759 return IndexedDBBackingStore::OpenInMemory(origin_url, &leveldb_factory); | |
760 } | 768 } |
761 | 769 |
762 // static | 770 // static |
763 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( | 771 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( |
764 const GURL& origin_url, | 772 const GURL& origin_url, |
765 LevelDBFactory* leveldb_factory) { | 773 base::TaskRunner* task_runner) { |
| 774 DefaultLevelDBFactory leveldb_factory; |
| 775 return IndexedDBBackingStore::OpenInMemory( |
| 776 origin_url, &leveldb_factory, task_runner); |
| 777 } |
| 778 |
| 779 // static |
| 780 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::OpenInMemory( |
| 781 const GURL& origin_url, |
| 782 LevelDBFactory* leveldb_factory, |
| 783 base::TaskRunner* task_runner) { |
766 IDB_TRACE("IndexedDBBackingStore::OpenInMemory"); | 784 IDB_TRACE("IndexedDBBackingStore::OpenInMemory"); |
767 | 785 |
768 scoped_ptr<LevelDBComparator> comparator(new Comparator()); | 786 scoped_ptr<LevelDBComparator> comparator(new Comparator()); |
769 scoped_ptr<LevelDBDatabase> db = | 787 scoped_ptr<LevelDBDatabase> db = |
770 LevelDBDatabase::OpenInMemory(comparator.get()); | 788 LevelDBDatabase::OpenInMemory(comparator.get()); |
771 if (!db) { | 789 if (!db) { |
772 LOG(ERROR) << "LevelDBDatabase::OpenInMemory failed."; | 790 LOG(ERROR) << "LevelDBDatabase::OpenInMemory failed."; |
773 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, | 791 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_FAILED, |
774 origin_url); | 792 origin_url); |
775 return scoped_refptr<IndexedDBBackingStore>(); | 793 return scoped_refptr<IndexedDBBackingStore>(); |
776 } | 794 } |
777 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_SUCCESS, origin_url); | 795 HistogramOpenStatus(INDEXED_DB_BACKING_STORE_OPEN_MEMORY_SUCCESS, origin_url); |
778 | 796 |
779 return Create(origin_url, db.Pass(), comparator.Pass()); | 797 return Create(NULL /* indexed_db_factory */, |
| 798 origin_url, |
| 799 db.Pass(), |
| 800 comparator.Pass(), |
| 801 task_runner); |
780 } | 802 } |
781 | 803 |
782 // static | 804 // static |
783 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Create( | 805 scoped_refptr<IndexedDBBackingStore> IndexedDBBackingStore::Create( |
| 806 IndexedDBFactory* indexed_db_factory, |
784 const GURL& origin_url, | 807 const GURL& origin_url, |
785 scoped_ptr<LevelDBDatabase> db, | 808 scoped_ptr<LevelDBDatabase> db, |
786 scoped_ptr<LevelDBComparator> comparator) { | 809 scoped_ptr<LevelDBComparator> comparator, |
| 810 base::TaskRunner* task_runner) { |
787 // TODO(jsbell): Handle comparator name changes. | 811 // TODO(jsbell): Handle comparator name changes. |
788 | 812 |
789 scoped_refptr<IndexedDBBackingStore> backing_store( | 813 scoped_refptr<IndexedDBBackingStore> backing_store( |
790 new IndexedDBBackingStore(origin_url, db.Pass(), comparator.Pass())); | 814 new IndexedDBBackingStore(indexed_db_factory, |
| 815 origin_url, |
| 816 db.Pass(), |
| 817 comparator.Pass(), |
| 818 task_runner)); |
791 if (!SetUpMetadata(backing_store->db_.get(), | 819 if (!SetUpMetadata(backing_store->db_.get(), |
792 backing_store->origin_identifier_)) | 820 backing_store->origin_identifier_)) |
793 return scoped_refptr<IndexedDBBackingStore>(); | 821 return scoped_refptr<IndexedDBBackingStore>(); |
794 | 822 |
795 return backing_store; | 823 return backing_store; |
796 } | 824 } |
797 | 825 |
798 std::vector<base::string16> IndexedDBBackingStore::GetDatabaseNames() { | 826 std::vector<base::string16> IndexedDBBackingStore::GetDatabaseNames() { |
799 std::vector<base::string16> found_names; | 827 std::vector<base::string16> found_names; |
800 const std::string start_key = | 828 const std::string start_key = |
(...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1972 return InvalidDBKeyStatus(); | 2000 return InvalidDBKeyStatus(); |
1973 } | 2001 } |
1974 | 2002 |
1975 StringPiece slice(found_encoded_primary_key); | 2003 StringPiece slice(found_encoded_primary_key); |
1976 if (DecodeIDBKey(&slice, found_primary_key) && slice.empty()) | 2004 if (DecodeIDBKey(&slice, found_primary_key) && slice.empty()) |
1977 return s; | 2005 return s; |
1978 else | 2006 else |
1979 return InvalidDBKeyStatus(); | 2007 return InvalidDBKeyStatus(); |
1980 } | 2008 } |
1981 | 2009 |
| 2010 void IndexedDBBackingStore::ReportBlobUnused(int64 database_id, |
| 2011 int64 blob_key) { |
| 2012 // TODO(ericu) |
| 2013 } |
| 2014 |
1982 IndexedDBBackingStore::Cursor::Cursor( | 2015 IndexedDBBackingStore::Cursor::Cursor( |
1983 const IndexedDBBackingStore::Cursor* other) | 2016 const IndexedDBBackingStore::Cursor* other) |
1984 : transaction_(other->transaction_), | 2017 : transaction_(other->transaction_), |
1985 cursor_options_(other->cursor_options_), | 2018 cursor_options_(other->cursor_options_), |
1986 current_key_(new IndexedDBKey(*other->current_key_)) { | 2019 current_key_(new IndexedDBKey(*other->current_key_)) { |
1987 if (other->iterator_) { | 2020 if (other->iterator_) { |
1988 iterator_ = transaction_->CreateIterator(); | 2021 iterator_ = transaction_->CreateIterator(); |
1989 | 2022 |
1990 if (other->iterator_->IsValid()) { | 2023 if (other->iterator_->IsValid()) { |
1991 iterator_->Seek(other->iterator_->Key()); | 2024 iterator_->Seek(other->iterator_->Key()); |
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2768 } | 2801 } |
2769 | 2802 |
2770 void IndexedDBBackingStore::Transaction::Rollback() { | 2803 void IndexedDBBackingStore::Transaction::Rollback() { |
2771 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); | 2804 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); |
2772 DCHECK(transaction_.get()); | 2805 DCHECK(transaction_.get()); |
2773 transaction_->Rollback(); | 2806 transaction_->Rollback(); |
2774 transaction_ = NULL; | 2807 transaction_ = NULL; |
2775 } | 2808 } |
2776 | 2809 |
2777 } // namespace content | 2810 } // namespace content |
OLD | NEW |