Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: content/browser/indexed_db/indexed_db_database.cc

Issue 2601983002: [IndexedDB] Adding transaction and value support to observers (Closed)
Patch Set: added comments and bug link Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
793 } 793 }
794 794
795 void IndexedDBDatabase::AddPendingObserver( 795 void IndexedDBDatabase::AddPendingObserver(
796 IndexedDBTransaction* transaction, 796 IndexedDBTransaction* transaction,
797 int32_t observer_id, 797 int32_t observer_id,
798 const IndexedDBObserver::Options& options) { 798 const IndexedDBObserver::Options& options) {
799 DCHECK(transaction); 799 DCHECK(transaction);
800 transaction->AddPendingObserver(observer_id, options); 800 transaction->AddPendingObserver(observer_id, options);
801 } 801 }
802 802
803 // TODO(palakj): Augment the function with IDBValue later. Issue
804 // crbug.com/609934.
805 void IndexedDBDatabase::FilterObservation(IndexedDBTransaction* transaction, 803 void IndexedDBDatabase::FilterObservation(IndexedDBTransaction* transaction,
806 int64_t object_store_id, 804 int64_t object_store_id,
807 blink::WebIDBOperationType type, 805 blink::WebIDBOperationType type,
808 const IndexedDBKeyRange& key_range) { 806 const IndexedDBKeyRange& key_range,
807 const IndexedDBValue* value) {
809 for (auto* connection : connections_) { 808 for (auto* connection : connections_) {
810 bool recorded = false; 809 bool recorded = false;
811 for (const auto& observer : connection->active_observers()) { 810 for (const auto& observer : connection->active_observers()) {
812 if (!observer->IsRecordingType(type) || 811 if (!observer->IsRecordingType(type) ||
813 !observer->IsRecordingObjectStore(object_store_id)) 812 !observer->IsRecordingObjectStore(object_store_id))
814 continue; 813 continue;
815 if (!recorded) { 814 if (!recorded) {
816 auto observation = ::indexed_db::mojom::Observation::New(); 815 auto observation = ::indexed_db::mojom::Observation::New();
817 observation->object_store_id = object_store_id; 816 observation->object_store_id = object_store_id;
818 observation->type = type; 817 observation->type = type;
819 if (type != blink::WebIDBClear) 818 if (type != blink::WebIDBClear)
820 observation->key_range = key_range; 819 observation->key_range = key_range;
821 transaction->AddObservation(connection->id(), std::move(observation)); 820 transaction->AddObservation(connection->id(), std::move(observation));
822 recorded = true; 821 recorded = true;
823 } 822 }
824 transaction->RecordObserverForLastObservation(connection->id(), 823 ::indexed_db::mojom::ObserverChangesPtr& changes =
825 observer->id()); 824 *transaction->GetPendingChangesForConnection(connection->id());
825
826 changes->observation_index_map[observer->id()].push_back(
827 changes->observations.size() - 1);
828 if (observer->include_transaction() &&
829 !base::ContainsKey(changes->transaction_map, observer->id())) {
830 auto mojo_transaction = ::indexed_db::mojom::ObserverTransaction::New();
831 mojo_transaction->id = connection->NewObserverTransactionId();
832 mojo_transaction->scope.insert(mojo_transaction->scope.end(),
833 observer->object_store_ids().begin(),
834 observer->object_store_ids().end());
835 changes->transaction_map[observer->id()] = std::move(mojo_transaction);
836 }
837 if (value && observer->values() && !changes->observations.back()->value) {
838 // TODO(dmurph): Avoid any and all IndexedDBValue copies. Perhaps defer
839 // this until the end of the transaction, where we can safely erase the
840 // indexeddb value. crbug.com/682363
841 IndexedDBValue copy = *value;
842 changes->observations.back()->value =
843 IndexedDBCallbacks::ConvertAndEraseValue(&copy);
844 }
826 } 845 }
827 } 846 }
828 } 847 }
829 848
830 void IndexedDBDatabase::SendObservations( 849 void IndexedDBDatabase::SendObservations(
831 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) { 850 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) {
832 for (auto* conn : connections_) { 851 for (auto* conn : connections_) {
833 auto it = changes_map.find(conn->id()); 852 auto it = changes_map.find(conn->id());
834 if (it != changes_map.end()) 853 if (it == changes_map.end())
835 conn->callbacks()->OnDatabaseChange(std::move(it->second)); 854 continue;
855
856 // Start all of the transactions.
857 ::indexed_db::mojom::ObserverChangesPtr& changes = it->second;
858 for (const auto& transaction_pair : changes->transaction_map) {
859 std::set<int64_t> scope(transaction_pair.second->scope.begin(),
860 transaction_pair.second->scope.end());
861 IndexedDBTransaction* transaction = conn->CreateTransaction(
862 transaction_pair.second->id, scope,
863 blink::WebIDBTransactionModeReadOnly,
864 new IndexedDBBackingStore::Transaction(backing_store_.get()));
865 DCHECK(transaction);
866 transaction_coordinator_.DidCreateObserverTransaction(transaction);
867 transaction_count_++;
868 transaction->GrabSnapshotThenStart();
869 }
870
871 conn->callbacks()->OnDatabaseChange(std::move(it->second));
836 } 872 }
837 } 873 }
838 874
839 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction, 875 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction,
840 int64_t object_store_id, 876 int64_t object_store_id,
841 int64_t index_id, 877 int64_t index_id,
842 std::unique_ptr<IndexedDBKeyRange> key_range, 878 std::unique_ptr<IndexedDBKeyRange> key_range,
843 bool key_only, 879 bool key_only,
844 int64_t max_count, 880 int64_t max_count,
845 scoped_refptr<IndexedDBCallbacks> callbacks) { 881 scoped_refptr<IndexedDBCallbacks> callbacks) {
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
1329 } 1365 }
1330 { 1366 {
1331 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id", 1367 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id",
1332 transaction->id()); 1368 transaction->id());
1333 params->callbacks->OnSuccess(*key); 1369 params->callbacks->OnSuccess(*key);
1334 } 1370 }
1335 FilterObservation(transaction, params->object_store_id, 1371 FilterObservation(transaction, params->object_store_id,
1336 params->put_mode == blink::WebIDBPutModeAddOnly 1372 params->put_mode == blink::WebIDBPutModeAddOnly
1337 ? blink::WebIDBAdd 1373 ? blink::WebIDBAdd
1338 : blink::WebIDBPut, 1374 : blink::WebIDBPut,
1339 IndexedDBKeyRange(*key)); 1375 IndexedDBKeyRange(*key), &params->value);
1340 return s; 1376 return s;
1341 } 1377 }
1342 1378
1343 void IndexedDBDatabase::SetIndexKeys( 1379 void IndexedDBDatabase::SetIndexKeys(
1344 IndexedDBTransaction* transaction, 1380 IndexedDBTransaction* transaction,
1345 int64_t object_store_id, 1381 int64_t object_store_id,
1346 std::unique_ptr<IndexedDBKey> primary_key, 1382 std::unique_ptr<IndexedDBKey> primary_key,
1347 const std::vector<IndexedDBIndexKeys>& index_keys) { 1383 const std::vector<IndexedDBIndexKeys>& index_keys) {
1348 DCHECK(transaction); 1384 DCHECK(transaction);
1349 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
1643 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id", 1679 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id",
1644 transaction->id()); 1680 transaction->id());
1645 size_t delete_count = 0; 1681 size_t delete_count = 0;
1646 leveldb::Status s = 1682 leveldb::Status s =
1647 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(), 1683 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(),
1648 object_store_id, *key_range, &delete_count); 1684 object_store_id, *key_range, &delete_count);
1649 if (!s.ok()) 1685 if (!s.ok())
1650 return s; 1686 return s;
1651 callbacks->OnSuccess(); 1687 callbacks->OnSuccess();
1652 FilterObservation(transaction, object_store_id, blink::WebIDBDelete, 1688 FilterObservation(transaction, object_store_id, blink::WebIDBDelete,
1653 *key_range); 1689 *key_range, nullptr);
1654 return s; 1690 return s;
1655 } 1691 }
1656 1692
1657 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction, 1693 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction,
1658 int64_t object_store_id, 1694 int64_t object_store_id,
1659 scoped_refptr<IndexedDBCallbacks> callbacks) { 1695 scoped_refptr<IndexedDBCallbacks> callbacks) {
1660 DCHECK(transaction); 1696 DCHECK(transaction);
1661 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id()); 1697 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id());
1662 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); 1698 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly);
1663 1699
1664 if (!ValidateObjectStoreId(object_store_id)) 1700 if (!ValidateObjectStoreId(object_store_id))
1665 return; 1701 return;
1666 1702
1667 transaction->ScheduleTask(base::Bind( 1703 transaction->ScheduleTask(base::Bind(
1668 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks)); 1704 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks));
1669 } 1705 }
1670 1706
1671 leveldb::Status IndexedDBDatabase::ClearOperation( 1707 leveldb::Status IndexedDBDatabase::ClearOperation(
1672 int64_t object_store_id, 1708 int64_t object_store_id,
1673 scoped_refptr<IndexedDBCallbacks> callbacks, 1709 scoped_refptr<IndexedDBCallbacks> callbacks,
1674 IndexedDBTransaction* transaction) { 1710 IndexedDBTransaction* transaction) {
1675 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id()); 1711 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id());
1676 leveldb::Status s = backing_store_->ClearObjectStore( 1712 leveldb::Status s = backing_store_->ClearObjectStore(
1677 transaction->BackingStoreTransaction(), id(), object_store_id); 1713 transaction->BackingStoreTransaction(), id(), object_store_id);
1678 if (!s.ok()) 1714 if (!s.ok())
1679 return s; 1715 return s;
1680 callbacks->OnSuccess(); 1716 callbacks->OnSuccess();
1681 1717
1682 FilterObservation(transaction, object_store_id, blink::WebIDBClear, 1718 FilterObservation(transaction, object_store_id, blink::WebIDBClear,
1683 IndexedDBKeyRange()); 1719 IndexedDBKeyRange(), nullptr);
1684 return s; 1720 return s;
1685 } 1721 }
1686 1722
1687 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation( 1723 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation(
1688 int64_t object_store_id, 1724 int64_t object_store_id,
1689 IndexedDBTransaction* transaction) { 1725 IndexedDBTransaction* transaction) {
1690 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation", 1726 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation",
1691 "txn.id", 1727 "txn.id",
1692 transaction->id()); 1728 transaction->id());
1693 1729
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 if (status.IsCorruption()) { 1943 if (status.IsCorruption()) {
1908 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, 1944 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError,
1909 message); 1945 message);
1910 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); 1946 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error);
1911 } else { 1947 } else {
1912 factory_->HandleBackingStoreFailure(backing_store_->origin()); 1948 factory_->HandleBackingStoreFailure(backing_store_->origin());
1913 } 1949 }
1914 } 1950 }
1915 1951
1916 } // namespace content 1952 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_database.h ('k') | content/browser/indexed_db/indexed_db_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698