| 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 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 } | 798 } |
| 799 | 799 |
| 800 void IndexedDBDatabase::AddPendingObserver( | 800 void IndexedDBDatabase::AddPendingObserver( |
| 801 IndexedDBTransaction* transaction, | 801 IndexedDBTransaction* transaction, |
| 802 int32_t observer_id, | 802 int32_t observer_id, |
| 803 const IndexedDBObserver::Options& options) { | 803 const IndexedDBObserver::Options& options) { |
| 804 DCHECK(transaction); | 804 DCHECK(transaction); |
| 805 transaction->AddPendingObserver(observer_id, options); | 805 transaction->AddPendingObserver(observer_id, options); |
| 806 } | 806 } |
| 807 | 807 |
| 808 // TODO(palakj): Augment the function with IDBValue later. Issue | |
| 809 // crbug.com/609934. | |
| 810 void IndexedDBDatabase::FilterObservation(IndexedDBTransaction* transaction, | 808 void IndexedDBDatabase::FilterObservation(IndexedDBTransaction* transaction, |
| 811 int64_t object_store_id, | 809 int64_t object_store_id, |
| 812 blink::WebIDBOperationType type, | 810 blink::WebIDBOperationType type, |
| 813 const IndexedDBKeyRange& key_range) { | 811 const IndexedDBKeyRange& key_range, |
| 812 const IndexedDBValue* value) { |
| 814 for (auto* connection : connections_) { | 813 for (auto* connection : connections_) { |
| 815 bool recorded = false; | 814 bool recorded = false; |
| 816 for (const auto& observer : connection->active_observers()) { | 815 for (const auto& observer : connection->active_observers()) { |
| 817 if (!observer->IsRecordingType(type) || | 816 if (!observer->IsRecordingType(type) || |
| 818 !observer->IsRecordingObjectStore(object_store_id)) | 817 !observer->IsRecordingObjectStore(object_store_id)) |
| 819 continue; | 818 continue; |
| 820 if (!recorded) { | 819 if (!recorded) { |
| 821 auto observation = ::indexed_db::mojom::Observation::New(); | 820 auto observation = ::indexed_db::mojom::Observation::New(); |
| 822 observation->object_store_id = object_store_id; | 821 observation->object_store_id = object_store_id; |
| 823 observation->type = type; | 822 observation->type = type; |
| 824 if (type != blink::WebIDBClear) | 823 if (type != blink::WebIDBClear) |
| 825 observation->key_range = key_range; | 824 observation->key_range = key_range; |
| 826 transaction->AddObservation(connection->id(), std::move(observation)); | 825 transaction->AddObservation(connection->id(), std::move(observation)); |
| 827 recorded = true; | 826 recorded = true; |
| 828 } | 827 } |
| 829 transaction->RecordObserverForLastObservation(connection->id(), | 828 auto* changes_ptr = |
| 830 observer->id()); | 829 transaction->GetPendingChangesForConnection(connection->id()); |
| 830 ::indexed_db::mojom::ObserverChangesPtr& changes = *changes_ptr; |
| 831 |
| 832 changes->observation_index_map[observer->id()].push_back( |
| 833 changes->observations.size() - 1); |
| 834 if (observer->include_transaction() && |
| 835 !base::ContainsKey(changes->transaction_map, observer->id())) { |
| 836 IndexedDBTransaction* observer_transaction = |
| 837 connection->CreateTransaction( |
| 838 connection->NewObserverTransactionId(), |
| 839 observer->object_store_ids(), |
| 840 blink::WebIDBTransactionModeReadOnly, |
| 841 new IndexedDBBackingStore::Transaction(backing_store_.get())); |
| 842 transaction_coordinator_.DidCreateObserverTransaction( |
| 843 observer_transaction); |
| 844 transaction_count_++; |
| 845 observer_transaction->Start(); |
| 846 |
| 847 auto mojo_transaction = ::indexed_db::mojom::ObserverTransaction::New(); |
| 848 mojo_transaction->id = observer_transaction->id(); |
| 849 mojo_transaction->scope.insert(mojo_transaction->scope.end(), |
| 850 observer->object_store_ids().begin(), |
| 851 observer->object_store_ids().end()); |
| 852 changes->transaction_map[observer->id()] = std::move(mojo_transaction); |
| 853 } |
| 854 if (value && observer->values() && !changes->observations.back()->value) { |
| 855 IndexedDBValue copy = *value; |
| 856 changes->observations.back()->value = |
| 857 IndexedDBCallbacks::ConvertValue(©); |
| 858 } |
| 831 } | 859 } |
| 832 } | 860 } |
| 833 } | 861 } |
| 834 | 862 |
| 835 void IndexedDBDatabase::SendObservations( | 863 void IndexedDBDatabase::SendObservations( |
| 836 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) { | 864 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) { |
| 837 for (auto* conn : connections_) { | 865 for (auto* conn : connections_) { |
| 838 auto it = changes_map.find(conn->id()); | 866 auto it = changes_map.find(conn->id()); |
| 839 if (it != changes_map.end()) | 867 if (it == changes_map.end()) |
| 840 conn->callbacks()->OnDatabaseChange(std::move(it->second)); | 868 continue; |
| 869 |
| 870 // Create our transactions if applicable. |
| 871 conn->callbacks()->OnDatabaseChange(std::move(it->second)); |
| 841 } | 872 } |
| 842 } | 873 } |
| 843 | 874 |
| 844 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction, | 875 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction, |
| 845 int64_t object_store_id, | 876 int64_t object_store_id, |
| 846 int64_t index_id, | 877 int64_t index_id, |
| 847 std::unique_ptr<IndexedDBKeyRange> key_range, | 878 std::unique_ptr<IndexedDBKeyRange> key_range, |
| 848 bool key_only, | 879 bool key_only, |
| 849 int64_t max_count, | 880 int64_t max_count, |
| 850 scoped_refptr<IndexedDBCallbacks> callbacks) { | 881 scoped_refptr<IndexedDBCallbacks> callbacks) { |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1334 } | 1365 } |
| 1335 { | 1366 { |
| 1336 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id", | 1367 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id", |
| 1337 transaction->id()); | 1368 transaction->id()); |
| 1338 params->callbacks->OnSuccess(*key); | 1369 params->callbacks->OnSuccess(*key); |
| 1339 } | 1370 } |
| 1340 FilterObservation(transaction, params->object_store_id, | 1371 FilterObservation(transaction, params->object_store_id, |
| 1341 params->put_mode == blink::WebIDBPutModeAddOnly | 1372 params->put_mode == blink::WebIDBPutModeAddOnly |
| 1342 ? blink::WebIDBAdd | 1373 ? blink::WebIDBAdd |
| 1343 : blink::WebIDBPut, | 1374 : blink::WebIDBPut, |
| 1344 IndexedDBKeyRange(*key)); | 1375 IndexedDBKeyRange(*key), ¶ms->value); |
| 1345 return s; | 1376 return s; |
| 1346 } | 1377 } |
| 1347 | 1378 |
| 1348 void IndexedDBDatabase::SetIndexKeys( | 1379 void IndexedDBDatabase::SetIndexKeys( |
| 1349 IndexedDBTransaction* transaction, | 1380 IndexedDBTransaction* transaction, |
| 1350 int64_t object_store_id, | 1381 int64_t object_store_id, |
| 1351 std::unique_ptr<IndexedDBKey> primary_key, | 1382 std::unique_ptr<IndexedDBKey> primary_key, |
| 1352 const std::vector<IndexedDBIndexKeys>& index_keys) { | 1383 const std::vector<IndexedDBIndexKeys>& index_keys) { |
| 1353 DCHECK(transaction); | 1384 DCHECK(transaction); |
| 1354 IDB_TRACE1("IndexedDBDatabase::SetIndexKeys", "txn.id", transaction->id()); | 1385 IDB_TRACE1("IndexedDBDatabase::SetIndexKeys", "txn.id", transaction->id()); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1648 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id", | 1679 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id", |
| 1649 transaction->id()); | 1680 transaction->id()); |
| 1650 size_t delete_count = 0; | 1681 size_t delete_count = 0; |
| 1651 leveldb::Status s = | 1682 leveldb::Status s = |
| 1652 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(), | 1683 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(), |
| 1653 object_store_id, *key_range, &delete_count); | 1684 object_store_id, *key_range, &delete_count); |
| 1654 if (!s.ok()) | 1685 if (!s.ok()) |
| 1655 return s; | 1686 return s; |
| 1656 callbacks->OnSuccess(); | 1687 callbacks->OnSuccess(); |
| 1657 FilterObservation(transaction, object_store_id, blink::WebIDBDelete, | 1688 FilterObservation(transaction, object_store_id, blink::WebIDBDelete, |
| 1658 *key_range); | 1689 *key_range, nullptr); |
| 1659 return s; | 1690 return s; |
| 1660 } | 1691 } |
| 1661 | 1692 |
| 1662 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction, | 1693 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction, |
| 1663 int64_t object_store_id, | 1694 int64_t object_store_id, |
| 1664 scoped_refptr<IndexedDBCallbacks> callbacks) { | 1695 scoped_refptr<IndexedDBCallbacks> callbacks) { |
| 1665 DCHECK(transaction); | 1696 DCHECK(transaction); |
| 1666 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id()); | 1697 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id()); |
| 1667 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); | 1698 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); |
| 1668 | 1699 |
| 1669 if (!ValidateObjectStoreId(object_store_id)) | 1700 if (!ValidateObjectStoreId(object_store_id)) |
| 1670 return; | 1701 return; |
| 1671 | 1702 |
| 1672 transaction->ScheduleTask(base::Bind( | 1703 transaction->ScheduleTask(base::Bind( |
| 1673 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks)); | 1704 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks)); |
| 1674 } | 1705 } |
| 1675 | 1706 |
| 1676 leveldb::Status IndexedDBDatabase::ClearOperation( | 1707 leveldb::Status IndexedDBDatabase::ClearOperation( |
| 1677 int64_t object_store_id, | 1708 int64_t object_store_id, |
| 1678 scoped_refptr<IndexedDBCallbacks> callbacks, | 1709 scoped_refptr<IndexedDBCallbacks> callbacks, |
| 1679 IndexedDBTransaction* transaction) { | 1710 IndexedDBTransaction* transaction) { |
| 1680 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id()); | 1711 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id()); |
| 1681 leveldb::Status s = backing_store_->ClearObjectStore( | 1712 leveldb::Status s = backing_store_->ClearObjectStore( |
| 1682 transaction->BackingStoreTransaction(), id(), object_store_id); | 1713 transaction->BackingStoreTransaction(), id(), object_store_id); |
| 1683 if (!s.ok()) | 1714 if (!s.ok()) |
| 1684 return s; | 1715 return s; |
| 1685 callbacks->OnSuccess(); | 1716 callbacks->OnSuccess(); |
| 1686 | 1717 |
| 1687 FilterObservation(transaction, object_store_id, blink::WebIDBClear, | 1718 FilterObservation(transaction, object_store_id, blink::WebIDBClear, |
| 1688 IndexedDBKeyRange()); | 1719 IndexedDBKeyRange(), nullptr); |
| 1689 return s; | 1720 return s; |
| 1690 } | 1721 } |
| 1691 | 1722 |
| 1692 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation( | 1723 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation( |
| 1693 int64_t object_store_id, | 1724 int64_t object_store_id, |
| 1694 IndexedDBTransaction* transaction) { | 1725 IndexedDBTransaction* transaction) { |
| 1695 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation", | 1726 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation", |
| 1696 "txn.id", | 1727 "txn.id", |
| 1697 transaction->id()); | 1728 transaction->id()); |
| 1698 | 1729 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1912 if (status.IsCorruption()) { | 1943 if (status.IsCorruption()) { |
| 1913 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, | 1944 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, |
| 1914 message); | 1945 message); |
| 1915 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); | 1946 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); |
| 1916 } else { | 1947 } else { |
| 1917 factory_->HandleBackingStoreFailure(backing_store_->origin()); | 1948 factory_->HandleBackingStoreFailure(backing_store_->origin()); |
| 1918 } | 1949 } |
| 1919 } | 1950 } |
| 1920 | 1951 |
| 1921 } // namespace content | 1952 } // namespace content |
| OLD | NEW |