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 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 const StringPiece& key, | 150 const StringPiece& key, |
151 int64 value) { | 151 int64 value) { |
152 std::string buffer; | 152 std::string buffer; |
153 EncodeVarInt(value, &buffer); | 153 EncodeVarInt(value, &buffer); |
154 transaction->Put(key, &buffer); | 154 transaction->Put(key, &buffer); |
155 } | 155 } |
156 | 156 |
157 template <typename DBOrTransaction> | 157 template <typename DBOrTransaction> |
158 WARN_UNUSED_RESULT static bool GetString(DBOrTransaction* db, | 158 WARN_UNUSED_RESULT static bool GetString(DBOrTransaction* db, |
159 const StringPiece& key, | 159 const StringPiece& key, |
160 string16* found_string, | 160 base::string16* found_string, |
161 bool* found) { | 161 bool* found) { |
162 std::string result; | 162 std::string result; |
163 *found = false; | 163 *found = false; |
164 bool ok = db->Get(key, &result, found); | 164 bool ok = db->Get(key, &result, found); |
165 if (!ok) | 165 if (!ok) |
166 return false; | 166 return false; |
167 if (!*found) | 167 if (!*found) |
168 return true; | 168 return true; |
169 StringPiece slice(result); | 169 StringPiece slice(result); |
170 return DecodeString(&slice, found_string) && slice.empty(); | 170 return DecodeString(&slice, found_string) && slice.empty(); |
171 } | 171 } |
172 | 172 |
173 static void PutString(LevelDBTransaction* transaction, | 173 static void PutString(LevelDBTransaction* transaction, |
174 const StringPiece& key, | 174 const StringPiece& key, |
175 const string16& value) { | 175 const base::string16& value) { |
176 std::string buffer; | 176 std::string buffer; |
177 EncodeString(value, &buffer); | 177 EncodeString(value, &buffer); |
178 transaction->Put(key, &buffer); | 178 transaction->Put(key, &buffer); |
179 } | 179 } |
180 | 180 |
181 static void PutIDBKeyPath(LevelDBTransaction* transaction, | 181 static void PutIDBKeyPath(LevelDBTransaction* transaction, |
182 const StringPiece& key, | 182 const StringPiece& key, |
183 const IndexedDBKeyPath& value) { | 183 const IndexedDBKeyPath& value) { |
184 std::string buffer; | 184 std::string buffer; |
185 EncodeIDBKeyPath(value, &buffer); | 185 EncodeIDBKeyPath(value, &buffer); |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 | 646 |
647 scoped_refptr<IndexedDBBackingStore> backing_store( | 647 scoped_refptr<IndexedDBBackingStore> backing_store( |
648 new IndexedDBBackingStore(origin_url, db.Pass(), comparator.Pass())); | 648 new IndexedDBBackingStore(origin_url, db.Pass(), comparator.Pass())); |
649 if (!SetUpMetadata(backing_store->db_.get(), | 649 if (!SetUpMetadata(backing_store->db_.get(), |
650 backing_store->origin_identifier_)) | 650 backing_store->origin_identifier_)) |
651 return scoped_refptr<IndexedDBBackingStore>(); | 651 return scoped_refptr<IndexedDBBackingStore>(); |
652 | 652 |
653 return backing_store; | 653 return backing_store; |
654 } | 654 } |
655 | 655 |
656 std::vector<string16> IndexedDBBackingStore::GetDatabaseNames() { | 656 std::vector<base::string16> IndexedDBBackingStore::GetDatabaseNames() { |
657 std::vector<string16> found_names; | 657 std::vector<base::string16> found_names; |
658 const std::string start_key = | 658 const std::string start_key = |
659 DatabaseNameKey::EncodeMinKeyForOrigin(origin_identifier_); | 659 DatabaseNameKey::EncodeMinKeyForOrigin(origin_identifier_); |
660 const std::string stop_key = | 660 const std::string stop_key = |
661 DatabaseNameKey::EncodeStopKeyForOrigin(origin_identifier_); | 661 DatabaseNameKey::EncodeStopKeyForOrigin(origin_identifier_); |
662 | 662 |
663 DCHECK(found_names.empty()); | 663 DCHECK(found_names.empty()); |
664 | 664 |
665 scoped_ptr<LevelDBIterator> it = db_->CreateIterator(); | 665 scoped_ptr<LevelDBIterator> it = db_->CreateIterator(); |
666 for (it->Seek(start_key); | 666 for (it->Seek(start_key); |
667 it->IsValid() && CompareKeys(it->Key(), stop_key) < 0; | 667 it->IsValid() && CompareKeys(it->Key(), stop_key) < 0; |
668 it->Next()) { | 668 it->Next()) { |
669 StringPiece slice(it->Key()); | 669 StringPiece slice(it->Key()); |
670 DatabaseNameKey database_name_key; | 670 DatabaseNameKey database_name_key; |
671 if (!DatabaseNameKey::Decode(&slice, &database_name_key)) { | 671 if (!DatabaseNameKey::Decode(&slice, &database_name_key)) { |
672 INTERNAL_CONSISTENCY_ERROR(GET_DATABASE_NAMES); | 672 INTERNAL_CONSISTENCY_ERROR(GET_DATABASE_NAMES); |
673 continue; | 673 continue; |
674 } | 674 } |
675 found_names.push_back(database_name_key.database_name()); | 675 found_names.push_back(database_name_key.database_name()); |
676 } | 676 } |
677 return found_names; | 677 return found_names; |
678 } | 678 } |
679 | 679 |
680 bool IndexedDBBackingStore::GetIDBDatabaseMetaData( | 680 bool IndexedDBBackingStore::GetIDBDatabaseMetaData( |
681 const string16& name, | 681 const base::string16& name, |
682 IndexedDBDatabaseMetadata* metadata, | 682 IndexedDBDatabaseMetadata* metadata, |
683 bool* found) { | 683 bool* found) { |
684 const std::string key = DatabaseNameKey::Encode(origin_identifier_, name); | 684 const std::string key = DatabaseNameKey::Encode(origin_identifier_, name); |
685 *found = false; | 685 *found = false; |
686 | 686 |
687 bool ok = GetInt(db_.get(), key, &metadata->id, found); | 687 bool ok = GetInt(db_.get(), key, &metadata->id, found); |
688 if (!ok) { | 688 if (!ok) { |
689 INTERNAL_READ_ERROR(GET_IDBDATABASE_METADATA); | 689 INTERNAL_READ_ERROR(GET_IDBDATABASE_METADATA); |
690 return false; | 690 return false; |
691 } | 691 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 max_database_id = 0; | 748 max_database_id = 0; |
749 | 749 |
750 DCHECK_GE(max_database_id, 0); | 750 DCHECK_GE(max_database_id, 0); |
751 | 751 |
752 int64 database_id = max_database_id + 1; | 752 int64 database_id = max_database_id + 1; |
753 PutInt(transaction, MaxDatabaseIdKey::Encode(), database_id); | 753 PutInt(transaction, MaxDatabaseIdKey::Encode(), database_id); |
754 *new_id = database_id; | 754 *new_id = database_id; |
755 return true; | 755 return true; |
756 } | 756 } |
757 | 757 |
758 bool IndexedDBBackingStore::CreateIDBDatabaseMetaData(const string16& name, | 758 bool IndexedDBBackingStore::CreateIDBDatabaseMetaData( |
759 const string16& version, | 759 const base::string16& name, |
760 int64 int_version, | 760 const base::string16& version, |
761 int64* row_id) { | 761 int64 int_version, |
| 762 int64* row_id) { |
762 scoped_refptr<LevelDBTransaction> transaction = | 763 scoped_refptr<LevelDBTransaction> transaction = |
763 new LevelDBTransaction(db_.get()); | 764 new LevelDBTransaction(db_.get()); |
764 | 765 |
765 bool ok = GetNewDatabaseId(transaction.get(), row_id); | 766 bool ok = GetNewDatabaseId(transaction.get(), row_id); |
766 if (!ok) | 767 if (!ok) |
767 return false; | 768 return false; |
768 DCHECK_GE(*row_id, 0); | 769 DCHECK_GE(*row_id, 0); |
769 | 770 |
770 if (int_version == IndexedDBDatabaseMetadata::NO_INT_VERSION) | 771 if (int_version == IndexedDBDatabaseMetadata::NO_INT_VERSION) |
771 int_version = IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION; | 772 int_version = IndexedDBDatabaseMetadata::DEFAULT_INT_VERSION; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
804 | 805 |
805 static void DeleteRange(LevelDBTransaction* transaction, | 806 static void DeleteRange(LevelDBTransaction* transaction, |
806 const std::string& begin, | 807 const std::string& begin, |
807 const std::string& end) { | 808 const std::string& end) { |
808 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator(); | 809 scoped_ptr<LevelDBIterator> it = transaction->CreateIterator(); |
809 for (it->Seek(begin); it->IsValid() && CompareKeys(it->Key(), end) < 0; | 810 for (it->Seek(begin); it->IsValid() && CompareKeys(it->Key(), end) < 0; |
810 it->Next()) | 811 it->Next()) |
811 transaction->Remove(it->Key()); | 812 transaction->Remove(it->Key()); |
812 } | 813 } |
813 | 814 |
814 bool IndexedDBBackingStore::DeleteDatabase(const string16& name) { | 815 bool IndexedDBBackingStore::DeleteDatabase(const base::string16& name) { |
815 IDB_TRACE("IndexedDBBackingStore::DeleteDatabase"); | 816 IDB_TRACE("IndexedDBBackingStore::DeleteDatabase"); |
816 scoped_ptr<LevelDBWriteOnlyTransaction> transaction = | 817 scoped_ptr<LevelDBWriteOnlyTransaction> transaction = |
817 LevelDBWriteOnlyTransaction::Create(db_.get()); | 818 LevelDBWriteOnlyTransaction::Create(db_.get()); |
818 | 819 |
819 IndexedDBDatabaseMetadata metadata; | 820 IndexedDBDatabaseMetadata metadata; |
820 bool success = false; | 821 bool success = false; |
821 bool ok = GetIDBDatabaseMetaData(name, &metadata, &success); | 822 bool ok = GetIDBDatabaseMetaData(name, &metadata, &success); |
822 if (!ok) | 823 if (!ok) |
823 return false; | 824 return false; |
824 if (!success) | 825 if (!success) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); | 889 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); |
889 // Possible stale metadata, but don't fail the load. | 890 // Possible stale metadata, but don't fail the load. |
890 it->Next(); | 891 it->Next(); |
891 continue; | 892 continue; |
892 } | 893 } |
893 | 894 |
894 int64 object_store_id = meta_data_key.ObjectStoreId(); | 895 int64 object_store_id = meta_data_key.ObjectStoreId(); |
895 | 896 |
896 // TODO(jsbell): Do this by direct key lookup rather than iteration, to | 897 // TODO(jsbell): Do this by direct key lookup rather than iteration, to |
897 // simplify. | 898 // simplify. |
898 string16 object_store_name; | 899 base::string16 object_store_name; |
899 { | 900 { |
900 StringPiece slice(it->Value()); | 901 StringPiece slice(it->Value()); |
901 if (!DecodeString(&slice, &object_store_name) || !slice.empty()) | 902 if (!DecodeString(&slice, &object_store_name) || !slice.empty()) |
902 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); | 903 INTERNAL_CONSISTENCY_ERROR(GET_OBJECT_STORES); |
903 } | 904 } |
904 | 905 |
905 it->Next(); | 906 it->Next(); |
906 if (!CheckObjectStoreAndMetaDataType(it.get(), | 907 if (!CheckObjectStoreAndMetaDataType(it.get(), |
907 stop_key, | 908 stop_key, |
908 object_store_id, | 909 object_store_id, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 return false; | 1043 return false; |
1043 } | 1044 } |
1044 PutInt(transaction, max_object_store_id_key, object_store_id); | 1045 PutInt(transaction, max_object_store_id_key, object_store_id); |
1045 return true; | 1046 return true; |
1046 } | 1047 } |
1047 | 1048 |
1048 bool IndexedDBBackingStore::CreateObjectStore( | 1049 bool IndexedDBBackingStore::CreateObjectStore( |
1049 IndexedDBBackingStore::Transaction* transaction, | 1050 IndexedDBBackingStore::Transaction* transaction, |
1050 int64 database_id, | 1051 int64 database_id, |
1051 int64 object_store_id, | 1052 int64 object_store_id, |
1052 const string16& name, | 1053 const base::string16& name, |
1053 const IndexedDBKeyPath& key_path, | 1054 const IndexedDBKeyPath& key_path, |
1054 bool auto_increment) { | 1055 bool auto_increment) { |
1055 IDB_TRACE("IndexedDBBackingStore::CreateObjectStore"); | 1056 IDB_TRACE("IndexedDBBackingStore::CreateObjectStore"); |
1056 if (!KeyPrefix::ValidIds(database_id, object_store_id)) | 1057 if (!KeyPrefix::ValidIds(database_id, object_store_id)) |
1057 return false; | 1058 return false; |
1058 LevelDBTransaction* leveldb_transaction = transaction->transaction(); | 1059 LevelDBTransaction* leveldb_transaction = transaction->transaction(); |
1059 if (!SetMaxObjectStoreId(leveldb_transaction, database_id, object_store_id)) | 1060 if (!SetMaxObjectStoreId(leveldb_transaction, database_id, object_store_id)) |
1060 return false; | 1061 return false; |
1061 | 1062 |
1062 const std::string name_key = ObjectStoreMetaDataKey::Encode( | 1063 const std::string name_key = ObjectStoreMetaDataKey::Encode( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1096 | 1097 |
1097 bool IndexedDBBackingStore::DeleteObjectStore( | 1098 bool IndexedDBBackingStore::DeleteObjectStore( |
1098 IndexedDBBackingStore::Transaction* transaction, | 1099 IndexedDBBackingStore::Transaction* transaction, |
1099 int64 database_id, | 1100 int64 database_id, |
1100 int64 object_store_id) { | 1101 int64 object_store_id) { |
1101 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore"); | 1102 IDB_TRACE("IndexedDBBackingStore::DeleteObjectStore"); |
1102 if (!KeyPrefix::ValidIds(database_id, object_store_id)) | 1103 if (!KeyPrefix::ValidIds(database_id, object_store_id)) |
1103 return false; | 1104 return false; |
1104 LevelDBTransaction* leveldb_transaction = transaction->transaction(); | 1105 LevelDBTransaction* leveldb_transaction = transaction->transaction(); |
1105 | 1106 |
1106 string16 object_store_name; | 1107 base::string16 object_store_name; |
1107 bool found = false; | 1108 bool found = false; |
1108 bool ok = GetString( | 1109 bool ok = GetString( |
1109 leveldb_transaction, | 1110 leveldb_transaction, |
1110 ObjectStoreMetaDataKey::Encode( | 1111 ObjectStoreMetaDataKey::Encode( |
1111 database_id, object_store_id, ObjectStoreMetaDataKey::NAME), | 1112 database_id, object_store_id, ObjectStoreMetaDataKey::NAME), |
1112 &object_store_name, | 1113 &object_store_name, |
1113 &found); | 1114 &found); |
1114 if (!ok) { | 1115 if (!ok) { |
1115 INTERNAL_READ_ERROR(DELETE_OBJECT_STORE); | 1116 INTERNAL_READ_ERROR(DELETE_OBJECT_STORE); |
1116 return false; | 1117 return false; |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1466 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); | 1467 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); |
1467 // Possible stale metadata due to http://webkit.org/b/85557 but don't fail | 1468 // Possible stale metadata due to http://webkit.org/b/85557 but don't fail |
1468 // the load. | 1469 // the load. |
1469 it->Next(); | 1470 it->Next(); |
1470 continue; | 1471 continue; |
1471 } | 1472 } |
1472 | 1473 |
1473 // TODO(jsbell): Do this by direct key lookup rather than iteration, to | 1474 // TODO(jsbell): Do this by direct key lookup rather than iteration, to |
1474 // simplify. | 1475 // simplify. |
1475 int64 index_id = meta_data_key.IndexId(); | 1476 int64 index_id = meta_data_key.IndexId(); |
1476 string16 index_name; | 1477 base::string16 index_name; |
1477 { | 1478 { |
1478 StringPiece slice(it->Value()); | 1479 StringPiece slice(it->Value()); |
1479 if (!DecodeString(&slice, &index_name) || !slice.empty()) | 1480 if (!DecodeString(&slice, &index_name) || !slice.empty()) |
1480 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); | 1481 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); |
1481 } | 1482 } |
1482 | 1483 |
1483 it->Next(); // unique flag | 1484 it->Next(); // unique flag |
1484 if (!CheckIndexAndMetaDataKey( | 1485 if (!CheckIndexAndMetaDataKey( |
1485 it.get(), stop_key, index_id, IndexMetaDataKey::UNIQUE)) { | 1486 it.get(), stop_key, index_id, IndexMetaDataKey::UNIQUE)) { |
1486 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); | 1487 INTERNAL_CONSISTENCY_ERROR(GET_INDEXES); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1546 | 1547 |
1547 PutInt(transaction, max_index_id_key, index_id); | 1548 PutInt(transaction, max_index_id_key, index_id); |
1548 return true; | 1549 return true; |
1549 } | 1550 } |
1550 | 1551 |
1551 bool IndexedDBBackingStore::CreateIndex( | 1552 bool IndexedDBBackingStore::CreateIndex( |
1552 IndexedDBBackingStore::Transaction* transaction, | 1553 IndexedDBBackingStore::Transaction* transaction, |
1553 int64 database_id, | 1554 int64 database_id, |
1554 int64 object_store_id, | 1555 int64 object_store_id, |
1555 int64 index_id, | 1556 int64 index_id, |
1556 const string16& name, | 1557 const base::string16& name, |
1557 const IndexedDBKeyPath& key_path, | 1558 const IndexedDBKeyPath& key_path, |
1558 bool is_unique, | 1559 bool is_unique, |
1559 bool is_multi_entry) { | 1560 bool is_multi_entry) { |
1560 IDB_TRACE("IndexedDBBackingStore::CreateIndex"); | 1561 IDB_TRACE("IndexedDBBackingStore::CreateIndex"); |
1561 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id)) | 1562 if (!KeyPrefix::ValidIds(database_id, object_store_id, index_id)) |
1562 return false; | 1563 return false; |
1563 LevelDBTransaction* leveldb_transaction = transaction->transaction(); | 1564 LevelDBTransaction* leveldb_transaction = transaction->transaction(); |
1564 if (!SetMaxIndexId( | 1565 if (!SetMaxIndexId( |
1565 leveldb_transaction, database_id, object_store_id, index_id)) | 1566 leveldb_transaction, database_id, object_store_id, index_id)) |
1566 return false; | 1567 return false; |
(...skipping 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2605 } | 2606 } |
2606 | 2607 |
2607 void IndexedDBBackingStore::Transaction::Rollback() { | 2608 void IndexedDBBackingStore::Transaction::Rollback() { |
2608 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); | 2609 IDB_TRACE("IndexedDBBackingStore::Transaction::Rollback"); |
2609 DCHECK(transaction_.get()); | 2610 DCHECK(transaction_.get()); |
2610 transaction_->Rollback(); | 2611 transaction_->Rollback(); |
2611 transaction_ = NULL; | 2612 transaction_ = NULL; |
2612 } | 2613 } |
2613 | 2614 |
2614 } // namespace content | 2615 } // namespace content |
OLD | NEW |