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_database.h" | 5 #include "content/browser/indexed_db/indexed_db_database.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <set> | 10 #include <set> |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 } | 396 } |
397 metadata_.object_stores[object_store.id] = object_store; | 397 metadata_.object_stores[object_store.id] = object_store; |
398 } | 398 } |
399 | 399 |
400 void IndexedDBDatabase::RemoveObjectStore(int64_t object_store_id) { | 400 void IndexedDBDatabase::RemoveObjectStore(int64_t object_store_id) { |
401 DCHECK(metadata_.object_stores.find(object_store_id) != | 401 DCHECK(metadata_.object_stores.find(object_store_id) != |
402 metadata_.object_stores.end()); | 402 metadata_.object_stores.end()); |
403 metadata_.object_stores.erase(object_store_id); | 403 metadata_.object_stores.erase(object_store_id); |
404 } | 404 } |
405 | 405 |
| 406 void IndexedDBDatabase::SetObjectStoreName( |
| 407 int64_t object_store_id, const base::string16& name) { |
| 408 DCHECK(metadata_.object_stores.find(object_store_id) != |
| 409 metadata_.object_stores.end()); |
| 410 metadata_.object_stores[object_store_id].name = name; |
| 411 } |
| 412 |
406 void IndexedDBDatabase::AddIndex(int64_t object_store_id, | 413 void IndexedDBDatabase::AddIndex(int64_t object_store_id, |
407 const IndexedDBIndexMetadata& index, | 414 const IndexedDBIndexMetadata& index, |
408 int64_t new_max_index_id) { | 415 int64_t new_max_index_id) { |
409 DCHECK(metadata_.object_stores.find(object_store_id) != | 416 DCHECK(metadata_.object_stores.find(object_store_id) != |
410 metadata_.object_stores.end()); | 417 metadata_.object_stores.end()); |
411 IndexedDBObjectStoreMetadata object_store = | 418 IndexedDBObjectStoreMetadata object_store = |
412 metadata_.object_stores[object_store_id]; | 419 metadata_.object_stores[object_store_id]; |
413 | 420 |
414 DCHECK(object_store.indexes.find(index.id) == object_store.indexes.end()); | 421 DCHECK(object_store.indexes.find(index.id) == object_store.indexes.end()); |
415 object_store.indexes[index.id] = index; | 422 object_store.indexes[index.id] = index; |
416 if (new_max_index_id != IndexedDBIndexMetadata::kInvalidId) { | 423 if (new_max_index_id != IndexedDBIndexMetadata::kInvalidId) { |
417 DCHECK_LT(object_store.max_index_id, new_max_index_id); | 424 DCHECK_LT(object_store.max_index_id, new_max_index_id); |
418 object_store.max_index_id = new_max_index_id; | 425 object_store.max_index_id = new_max_index_id; |
419 } | 426 } |
420 metadata_.object_stores[object_store_id] = object_store; | 427 metadata_.object_stores[object_store_id] = object_store; |
421 } | 428 } |
422 | 429 |
423 void IndexedDBDatabase::RemoveIndex(int64_t object_store_id, int64_t index_id) { | 430 void IndexedDBDatabase::RemoveIndex(int64_t object_store_id, int64_t index_id) { |
424 DCHECK(metadata_.object_stores.find(object_store_id) != | 431 DCHECK(metadata_.object_stores.find(object_store_id) != |
425 metadata_.object_stores.end()); | 432 metadata_.object_stores.end()); |
426 IndexedDBObjectStoreMetadata object_store = | 433 IndexedDBObjectStoreMetadata object_store = |
427 metadata_.object_stores[object_store_id]; | 434 metadata_.object_stores[object_store_id]; |
428 | 435 |
429 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end()); | 436 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end()); |
430 object_store.indexes.erase(index_id); | 437 object_store.indexes.erase(index_id); |
431 metadata_.object_stores[object_store_id] = object_store; | 438 metadata_.object_stores[object_store_id] = object_store; |
432 } | 439 } |
433 | 440 |
| 441 void IndexedDBDatabase::SetIndexName( |
| 442 int64_t object_store_id, int64_t index_id, const base::string16& name) { |
| 443 DCHECK(metadata_.object_stores.find(object_store_id) != |
| 444 metadata_.object_stores.end()); |
| 445 IndexedDBObjectStoreMetadata object_store = |
| 446 metadata_.object_stores[object_store_id]; |
| 447 |
| 448 DCHECK(object_store.indexes.find(index_id) != object_store.indexes.end()); |
| 449 object_store.indexes[index_id].name = name; |
| 450 metadata_.object_stores[object_store_id] = object_store; |
| 451 } |
| 452 |
434 leveldb::Status IndexedDBDatabase::OpenInternal() { | 453 leveldb::Status IndexedDBDatabase::OpenInternal() { |
435 bool success = false; | 454 bool success = false; |
436 leveldb::Status s = backing_store_->GetIDBDatabaseMetaData( | 455 leveldb::Status s = backing_store_->GetIDBDatabaseMetaData( |
437 metadata_.name, &metadata_, &success); | 456 metadata_.name, &metadata_, &success); |
438 DCHECK(success == (metadata_.id != kInvalidId)) << "success = " << success | 457 DCHECK(success == (metadata_.id != kInvalidId)) << "success = " << success |
439 << " id = " << metadata_.id; | 458 << " id = " << metadata_.id; |
440 if (!s.ok()) | 459 if (!s.ok()) |
441 return s; | 460 return s; |
442 if (success) | 461 if (success) |
443 return backing_store_->GetObjectStores(metadata_.id, | 462 return backing_store_->GetObjectStores(metadata_.id, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 | 611 |
593 if (!ValidateObjectStoreId(object_store_id)) | 612 if (!ValidateObjectStoreId(object_store_id)) |
594 return; | 613 return; |
595 | 614 |
596 transaction->ScheduleTask( | 615 transaction->ScheduleTask( |
597 base::Bind(&IndexedDBDatabase::DeleteObjectStoreOperation, | 616 base::Bind(&IndexedDBDatabase::DeleteObjectStoreOperation, |
598 this, | 617 this, |
599 object_store_id)); | 618 object_store_id)); |
600 } | 619 } |
601 | 620 |
| 621 void IndexedDBDatabase::RenameObjectStore(int64_t transaction_id, |
| 622 int64_t object_store_id, |
| 623 const base::string16& new_name) { |
| 624 IDB_TRACE1("IndexedDBDatabase::RenameObjectStore", "txn.id", transaction_id); |
| 625 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
| 626 if (!transaction) |
| 627 return; |
| 628 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange); |
| 629 |
| 630 if (!ValidateObjectStoreId(object_store_id)) |
| 631 return; |
| 632 |
| 633 // Store renaming is done synchronously, as it may be followed by |
| 634 // index creation (also sync) since preemptive OpenCursor/SetIndexKeys |
| 635 // may follow. |
| 636 const IndexedDBObjectStoreMetadata object_store_metadata = |
| 637 metadata_.object_stores[object_store_id]; |
| 638 |
| 639 leveldb::Status s = |
| 640 backing_store_->RenameObjectStore(transaction->BackingStoreTransaction(), |
| 641 transaction->database()->id(), |
| 642 object_store_metadata.id, new_name); |
| 643 if (!s.ok()) { |
| 644 IndexedDBDatabaseError error( |
| 645 blink::WebIDBDatabaseExceptionUnknownError, |
| 646 ASCIIToUTF16("Internal error renaming object store '") + |
| 647 object_store_metadata.name + ASCIIToUTF16("' to '") + new_name + |
| 648 ASCIIToUTF16("'.")); |
| 649 transaction->Abort(error); |
| 650 if (s.IsCorruption()) |
| 651 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); |
| 652 return; |
| 653 } |
| 654 |
| 655 transaction->ScheduleAbortTask( |
| 656 base::Bind(&IndexedDBDatabase::RenameObjectStoreAbortOperation, |
| 657 this, |
| 658 object_store_id, |
| 659 object_store_metadata.name)); |
| 660 SetObjectStoreName(object_store_id, new_name); |
| 661 } |
| 662 |
602 void IndexedDBDatabase::CreateIndex(int64_t transaction_id, | 663 void IndexedDBDatabase::CreateIndex(int64_t transaction_id, |
603 int64_t object_store_id, | 664 int64_t object_store_id, |
604 int64_t index_id, | 665 int64_t index_id, |
605 const base::string16& name, | 666 const base::string16& name, |
606 const IndexedDBKeyPath& key_path, | 667 const IndexedDBKeyPath& key_path, |
607 bool unique, | 668 bool unique, |
608 bool multi_entry) { | 669 bool multi_entry) { |
609 IDB_TRACE1("IndexedDBDatabase::CreateIndex", "txn.id", transaction_id); | 670 IDB_TRACE1("IndexedDBDatabase::CreateIndex", "txn.id", transaction_id); |
610 IndexedDBTransaction* transaction = GetTransaction(transaction_id); | 671 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
611 if (!transaction) | 672 if (!transaction) |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 | 776 |
716 void IndexedDBDatabase::DeleteIndexAbortOperation( | 777 void IndexedDBDatabase::DeleteIndexAbortOperation( |
717 int64_t object_store_id, | 778 int64_t object_store_id, |
718 const IndexedDBIndexMetadata& index_metadata, | 779 const IndexedDBIndexMetadata& index_metadata, |
719 IndexedDBTransaction* transaction) { | 780 IndexedDBTransaction* transaction) { |
720 DCHECK(!transaction); | 781 DCHECK(!transaction); |
721 IDB_TRACE("IndexedDBDatabase::DeleteIndexAbortOperation"); | 782 IDB_TRACE("IndexedDBDatabase::DeleteIndexAbortOperation"); |
722 AddIndex(object_store_id, index_metadata, IndexedDBIndexMetadata::kInvalidId); | 783 AddIndex(object_store_id, index_metadata, IndexedDBIndexMetadata::kInvalidId); |
723 } | 784 } |
724 | 785 |
| 786 void IndexedDBDatabase::RenameIndex(int64_t transaction_id, |
| 787 int64_t object_store_id, |
| 788 int64_t index_id, |
| 789 const base::string16& new_name) { |
| 790 IDB_TRACE1("IndexedDBDatabase::RenameIndex", "txn.id", transaction_id); |
| 791 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
| 792 if (!transaction) |
| 793 return; |
| 794 DCHECK_EQ(transaction->mode(), blink::WebIDBTransactionModeVersionChange); |
| 795 |
| 796 if (!ValidateObjectStoreIdAndIndexId(object_store_id, index_id)) |
| 797 return; |
| 798 |
| 799 // Index renaming is done synchronously since preemptive |
| 800 // OpenCursor/SetIndexKeys may follow. |
| 801 |
| 802 const IndexedDBIndexMetadata index_metadata = |
| 803 metadata_.object_stores[object_store_id].indexes[index_id]; |
| 804 |
| 805 leveldb::Status s = |
| 806 backing_store_->RenameIndex(transaction->BackingStoreTransaction(), |
| 807 transaction->database()->id(), |
| 808 object_store_id, |
| 809 index_id, |
| 810 new_name); |
| 811 if (!s.ok()) { |
| 812 base::string16 error_string = |
| 813 ASCIIToUTF16("Internal error renaming index '") + |
| 814 index_metadata.name + ASCIIToUTF16("' to '") + new_name + |
| 815 ASCIIToUTF16("'."); |
| 816 transaction->Abort(IndexedDBDatabaseError( |
| 817 blink::WebIDBDatabaseExceptionUnknownError, error_string)); |
| 818 return; |
| 819 } |
| 820 |
| 821 transaction->ScheduleAbortTask( |
| 822 base::Bind(&IndexedDBDatabase::RenameIndexAbortOperation, |
| 823 this, |
| 824 object_store_id, |
| 825 index_id, |
| 826 index_metadata.name)); |
| 827 SetIndexName(object_store_id, index_id, new_name); |
| 828 } |
| 829 |
| 830 void IndexedDBDatabase::RenameIndexAbortOperation( |
| 831 int64_t object_store_id, |
| 832 int64_t index_id, |
| 833 const base::string16& old_name, |
| 834 IndexedDBTransaction* transaction) { |
| 835 DCHECK(!transaction); |
| 836 IDB_TRACE("IndexedDBDatabase::RenameIndexAbortOperation"); |
| 837 SetIndexName(object_store_id, index_id, old_name); |
| 838 } |
| 839 |
725 void IndexedDBDatabase::Commit(int64_t transaction_id) { | 840 void IndexedDBDatabase::Commit(int64_t transaction_id) { |
726 // The frontend suggests that we commit, but we may have previously initiated | 841 // The frontend suggests that we commit, but we may have previously initiated |
727 // an abort, and so have disposed of the transaction. on_abort has already | 842 // an abort, and so have disposed of the transaction. on_abort has already |
728 // been dispatched to the frontend, so it will find out about that | 843 // been dispatched to the frontend, so it will find out about that |
729 // asynchronously. | 844 // asynchronously. |
730 IndexedDBTransaction* transaction = GetTransaction(transaction_id); | 845 IndexedDBTransaction* transaction = GetTransaction(transaction_id); |
731 if (transaction) { | 846 if (transaction) { |
732 scoped_refptr<IndexedDBFactory> factory = factory_; | 847 scoped_refptr<IndexedDBFactory> factory = factory_; |
733 leveldb::Status s = transaction->Commit(); | 848 leveldb::Status s = transaction->Commit(); |
734 if (s.IsCorruption()) { | 849 if (s.IsCorruption()) { |
(...skipping 1260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1995 | 2110 |
1996 void IndexedDBDatabase::DeleteObjectStoreAbortOperation( | 2111 void IndexedDBDatabase::DeleteObjectStoreAbortOperation( |
1997 const IndexedDBObjectStoreMetadata& object_store_metadata, | 2112 const IndexedDBObjectStoreMetadata& object_store_metadata, |
1998 IndexedDBTransaction* transaction) { | 2113 IndexedDBTransaction* transaction) { |
1999 DCHECK(!transaction); | 2114 DCHECK(!transaction); |
2000 IDB_TRACE("IndexedDBDatabase::DeleteObjectStoreAbortOperation"); | 2115 IDB_TRACE("IndexedDBDatabase::DeleteObjectStoreAbortOperation"); |
2001 AddObjectStore(object_store_metadata, | 2116 AddObjectStore(object_store_metadata, |
2002 IndexedDBObjectStoreMetadata::kInvalidId); | 2117 IndexedDBObjectStoreMetadata::kInvalidId); |
2003 } | 2118 } |
2004 | 2119 |
| 2120 void IndexedDBDatabase::RenameObjectStoreAbortOperation( |
| 2121 int64_t object_store_id, |
| 2122 const base::string16& old_name, |
| 2123 IndexedDBTransaction* transaction) { |
| 2124 DCHECK(!transaction); |
| 2125 IDB_TRACE("IndexedDBDatabase::RenameObjectStoreAbortOperation"); |
| 2126 SetObjectStoreName(object_store_id, old_name); |
| 2127 } |
| 2128 |
2005 void IndexedDBDatabase::VersionChangeAbortOperation( | 2129 void IndexedDBDatabase::VersionChangeAbortOperation( |
2006 int64_t previous_version, | 2130 int64_t previous_version, |
2007 IndexedDBTransaction* transaction) { | 2131 IndexedDBTransaction* transaction) { |
2008 DCHECK(!transaction); | 2132 DCHECK(!transaction); |
2009 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); | 2133 IDB_TRACE("IndexedDBDatabase::VersionChangeAbortOperation"); |
2010 metadata_.version = previous_version; | 2134 metadata_.version = previous_version; |
2011 } | 2135 } |
2012 | 2136 |
2013 } // namespace content | 2137 } // namespace content |
OLD | NEW |