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

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: Moved transaction creation to SendObservations 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 787 matching lines...) Expand 10 before | Expand all | Expand 10 after
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;
jsbell 2017/01/13 00:14:12 Why have changes_ptr instead of doing this all in
dmurph 2017/01/13 01:50:42 Done.
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 auto mojo_transaction = ::indexed_db::mojom::ObserverTransaction::New();
837 mojo_transaction->id = connection->NewObserverTransactionId();
838 mojo_transaction->scope.insert(mojo_transaction->scope.end(),
839 observer->object_store_ids().begin(),
840 observer->object_store_ids().end());
841 changes->transaction_map[observer->id()] = std::move(mojo_transaction);
842 }
843 if (value && observer->values() && !changes->observations.back()->value) {
844 IndexedDBValue copy = *value;
845 changes->observations.back()->value =
846 IndexedDBCallbacks::ConvertValue(&copy);
847 }
831 } 848 }
832 } 849 }
833 } 850 }
834 851
835 void IndexedDBDatabase::SendObservations( 852 void IndexedDBDatabase::SendObservations(
836 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) { 853 std::map<int32_t, ::indexed_db::mojom::ObserverChangesPtr> changes_map) {
837 for (auto* conn : connections_) { 854 for (auto* conn : connections_) {
838 auto it = changes_map.find(conn->id()); 855 auto it = changes_map.find(conn->id());
839 if (it != changes_map.end()) 856 if (it == changes_map.end())
840 conn->callbacks()->OnDatabaseChange(std::move(it->second)); 857 continue;
858
859 // Start all of the transactions.
860 ::indexed_db::mojom::ObserverChangesPtr& changes = it->second;
861 for (const auto& transaction_pair : changes->transaction_map) {
862 std::set<int64_t> scope(transaction_pair.second->scope.begin(),
863 transaction_pair.second->scope.end());
864 IndexedDBTransaction* transaction = conn->CreateTransaction(
865 transaction_pair.second->id, scope,
866 blink::WebIDBTransactionModeReadOnly,
867 new IndexedDBBackingStore::Transaction(backing_store_.get()));
868 DCHECK(transaction);
869 transaction_coordinator_.DidCreateObserverTransaction(transaction);
870 transaction_count_++;
871 transaction->GrabSnapshotThenStart();
872 }
873
874 conn->callbacks()->OnDatabaseChange(std::move(it->second));
841 } 875 }
842 } 876 }
843 877
844 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction, 878 void IndexedDBDatabase::GetAll(IndexedDBTransaction* transaction,
845 int64_t object_store_id, 879 int64_t object_store_id,
846 int64_t index_id, 880 int64_t index_id,
847 std::unique_ptr<IndexedDBKeyRange> key_range, 881 std::unique_ptr<IndexedDBKeyRange> key_range,
848 bool key_only, 882 bool key_only,
849 int64_t max_count, 883 int64_t max_count,
850 scoped_refptr<IndexedDBCallbacks> callbacks) { 884 scoped_refptr<IndexedDBCallbacks> callbacks) {
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 } 1368 }
1335 { 1369 {
1336 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id", 1370 IDB_TRACE1("IndexedDBDatabase::PutOperation.Callbacks", "txn.id",
1337 transaction->id()); 1371 transaction->id());
1338 params->callbacks->OnSuccess(*key); 1372 params->callbacks->OnSuccess(*key);
1339 } 1373 }
1340 FilterObservation(transaction, params->object_store_id, 1374 FilterObservation(transaction, params->object_store_id,
1341 params->put_mode == blink::WebIDBPutModeAddOnly 1375 params->put_mode == blink::WebIDBPutModeAddOnly
1342 ? blink::WebIDBAdd 1376 ? blink::WebIDBAdd
1343 : blink::WebIDBPut, 1377 : blink::WebIDBPut,
1344 IndexedDBKeyRange(*key)); 1378 IndexedDBKeyRange(*key), &params->value);
1345 return s; 1379 return s;
1346 } 1380 }
1347 1381
1348 void IndexedDBDatabase::SetIndexKeys( 1382 void IndexedDBDatabase::SetIndexKeys(
1349 IndexedDBTransaction* transaction, 1383 IndexedDBTransaction* transaction,
1350 int64_t object_store_id, 1384 int64_t object_store_id,
1351 std::unique_ptr<IndexedDBKey> primary_key, 1385 std::unique_ptr<IndexedDBKey> primary_key,
1352 const std::vector<IndexedDBIndexKeys>& index_keys) { 1386 const std::vector<IndexedDBIndexKeys>& index_keys) {
1353 DCHECK(transaction); 1387 DCHECK(transaction);
1354 IDB_TRACE1("IndexedDBDatabase::SetIndexKeys", "txn.id", transaction->id()); 1388 IDB_TRACE1("IndexedDBDatabase::SetIndexKeys", "txn.id", transaction->id());
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
1648 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id", 1682 IDB_TRACE1("IndexedDBDatabase::DeleteRangeOperation", "txn.id",
1649 transaction->id()); 1683 transaction->id());
1650 size_t delete_count = 0; 1684 size_t delete_count = 0;
1651 leveldb::Status s = 1685 leveldb::Status s =
1652 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(), 1686 backing_store_->DeleteRange(transaction->BackingStoreTransaction(), id(),
1653 object_store_id, *key_range, &delete_count); 1687 object_store_id, *key_range, &delete_count);
1654 if (!s.ok()) 1688 if (!s.ok())
1655 return s; 1689 return s;
1656 callbacks->OnSuccess(); 1690 callbacks->OnSuccess();
1657 FilterObservation(transaction, object_store_id, blink::WebIDBDelete, 1691 FilterObservation(transaction, object_store_id, blink::WebIDBDelete,
1658 *key_range); 1692 *key_range, nullptr);
1659 return s; 1693 return s;
1660 } 1694 }
1661 1695
1662 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction, 1696 void IndexedDBDatabase::Clear(IndexedDBTransaction* transaction,
1663 int64_t object_store_id, 1697 int64_t object_store_id,
1664 scoped_refptr<IndexedDBCallbacks> callbacks) { 1698 scoped_refptr<IndexedDBCallbacks> callbacks) {
1665 DCHECK(transaction); 1699 DCHECK(transaction);
1666 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id()); 1700 IDB_TRACE1("IndexedDBDatabase::Clear", "txn.id", transaction->id());
1667 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly); 1701 DCHECK_NE(transaction->mode(), blink::WebIDBTransactionModeReadOnly);
1668 1702
1669 if (!ValidateObjectStoreId(object_store_id)) 1703 if (!ValidateObjectStoreId(object_store_id))
1670 return; 1704 return;
1671 1705
1672 transaction->ScheduleTask(base::Bind( 1706 transaction->ScheduleTask(base::Bind(
1673 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks)); 1707 &IndexedDBDatabase::ClearOperation, this, object_store_id, callbacks));
1674 } 1708 }
1675 1709
1676 leveldb::Status IndexedDBDatabase::ClearOperation( 1710 leveldb::Status IndexedDBDatabase::ClearOperation(
1677 int64_t object_store_id, 1711 int64_t object_store_id,
1678 scoped_refptr<IndexedDBCallbacks> callbacks, 1712 scoped_refptr<IndexedDBCallbacks> callbacks,
1679 IndexedDBTransaction* transaction) { 1713 IndexedDBTransaction* transaction) {
1680 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id()); 1714 IDB_TRACE1("IndexedDBDatabase::ClearOperation", "txn.id", transaction->id());
1681 leveldb::Status s = backing_store_->ClearObjectStore( 1715 leveldb::Status s = backing_store_->ClearObjectStore(
1682 transaction->BackingStoreTransaction(), id(), object_store_id); 1716 transaction->BackingStoreTransaction(), id(), object_store_id);
1683 if (!s.ok()) 1717 if (!s.ok())
1684 return s; 1718 return s;
1685 callbacks->OnSuccess(); 1719 callbacks->OnSuccess();
1686 1720
1687 FilterObservation(transaction, object_store_id, blink::WebIDBClear, 1721 FilterObservation(transaction, object_store_id, blink::WebIDBClear,
1688 IndexedDBKeyRange()); 1722 IndexedDBKeyRange(), nullptr);
1689 return s; 1723 return s;
1690 } 1724 }
1691 1725
1692 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation( 1726 leveldb::Status IndexedDBDatabase::DeleteObjectStoreOperation(
1693 int64_t object_store_id, 1727 int64_t object_store_id,
1694 IndexedDBTransaction* transaction) { 1728 IndexedDBTransaction* transaction) {
1695 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation", 1729 IDB_TRACE1("IndexedDBDatabase::DeleteObjectStoreOperation",
1696 "txn.id", 1730 "txn.id",
1697 transaction->id()); 1731 transaction->id());
1698 1732
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
1912 if (status.IsCorruption()) { 1946 if (status.IsCorruption()) {
1913 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError, 1947 IndexedDBDatabaseError error(blink::WebIDBDatabaseExceptionUnknownError,
1914 message); 1948 message);
1915 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error); 1949 factory_->HandleBackingStoreCorruption(backing_store_->origin(), error);
1916 } else { 1950 } else {
1917 factory_->HandleBackingStoreFailure(backing_store_->origin()); 1951 factory_->HandleBackingStoreFailure(backing_store_->origin());
1918 } 1952 }
1919 } 1953 }
1920 1954
1921 } // namespace content 1955 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698