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

Side by Side Diff: third_party/WebKit/Source/modules/indexeddb/IDBObserver.cpp

Issue 2511403003: Send IndexedDB observations through IDBDatabaseCallbacks. (Closed)
Patch Set: Rebased. Created 4 years, 1 month 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "modules/indexeddb/IDBObserver.h" 5 #include "modules/indexeddb/IDBObserver.h"
6 6
7 #include <bitset> 7 #include <bitset>
8 8
9 #include "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/modules/v8/IDBObserverCallback.h" 10 #include "bindings/modules/v8/IDBObserverCallback.h"
11 #include "bindings/modules/v8/ToV8ForModules.h" 11 #include "bindings/modules/v8/ToV8ForModules.h"
12 #include "bindings/modules/v8/V8BindingForModules.h" 12 #include "bindings/modules/v8/V8BindingForModules.h"
13 #include "core/dom/ExceptionCode.h" 13 #include "core/dom/ExceptionCode.h"
14 #include "modules/IndexedDBNames.h" 14 #include "modules/IndexedDBNames.h"
15 #include "modules/indexeddb/IDBDatabase.h" 15 #include "modules/indexeddb/IDBDatabase.h"
16 #include "modules/indexeddb/IDBObserverChanges.h" 16 #include "modules/indexeddb/IDBObserverChanges.h"
17 #include "modules/indexeddb/IDBObserverInit.h" 17 #include "modules/indexeddb/IDBObserverInit.h"
18 #include "modules/indexeddb/IDBTransaction.h" 18 #include "modules/indexeddb/IDBTransaction.h"
19 #include "modules/indexeddb/WebIDBObserverImpl.h"
20 19
21 namespace blink { 20 namespace blink {
22 21
23 IDBObserver* IDBObserver::create(IDBObserverCallback* callback) { 22 IDBObserver* IDBObserver::create(IDBObserverCallback* callback) {
24 return new IDBObserver(callback); 23 return new IDBObserver(callback);
25 } 24 }
26 25
27 IDBObserver::IDBObserver(IDBObserverCallback* callback) 26 IDBObserver::IDBObserver(IDBObserverCallback* callback)
28 : m_callback(callback) {} 27 : m_callback(callback) {}
29 28
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 types[WebIDBDelete] = true; 65 types[WebIDBDelete] = true;
67 } else if (operationType == IndexedDBNames::clear) { 66 } else if (operationType == IndexedDBNames::clear) {
68 types[WebIDBClear] = true; 67 types[WebIDBClear] = true;
69 } else { 68 } else {
70 exceptionState.throwTypeError( 69 exceptionState.throwTypeError(
71 "Unknown operation type in observe options: " + operationType); 70 "Unknown operation type in observe options: " + operationType);
72 return; 71 return;
73 } 72 }
74 } 73 }
75 74
76 std::unique_ptr<WebIDBObserverImpl> observer =
77 WebIDBObserverImpl::create(this, options.transaction(), options.values(),
78 options.noRecords(), types);
79 WebIDBObserverImpl* observerPtr = observer.get();
80 int32_t observerId = 75 int32_t observerId =
81 database->backend()->addObserver(std::move(observer), transaction->id()); 76 database->addObserver(this, transaction->id(), options.transaction(),
77 options.values(), options.noRecords(), types);
82 m_observerIds.add(observerId, database); 78 m_observerIds.add(observerId, database);
83 observerPtr->setId(observerId);
84 } 79 }
85 80
86 void IDBObserver::unobserve(IDBDatabase* database, 81 void IDBObserver::unobserve(IDBDatabase* database,
87 ExceptionState& exceptionState) { 82 ExceptionState& exceptionState) {
88 if (!database->backend()) { 83 if (!database->backend()) {
89 exceptionState.throwDOMException(InvalidStateError, 84 exceptionState.throwDOMException(InvalidStateError,
90 IDBDatabase::databaseClosedErrorMessage); 85 IDBDatabase::databaseClosedErrorMessage);
91 return; 86 return;
92 } 87 }
93 88
94 Vector<int32_t> observerIdsToRemove; 89 Vector<int32_t> observerIdsToRemove;
95 for (const auto& it : m_observerIds) { 90 for (const auto& it : m_observerIds) {
96 if (it.value == database) 91 if (it.value == database)
97 observerIdsToRemove.append(it.key); 92 observerIdsToRemove.append(it.key);
98 } 93 }
99 m_observerIds.removeAll(observerIdsToRemove); 94 m_observerIds.removeAll(observerIdsToRemove);
100 95
101 if (!observerIdsToRemove.isEmpty()) 96 if (!observerIdsToRemove.isEmpty())
102 database->backend()->removeObservers(observerIdsToRemove); 97 database->removeObservers(observerIdsToRemove);
103 }
104
105 void IDBObserver::removeObserver(int32_t id) {
106 m_observerIds.remove(id);
107 }
108
109 void IDBObserver::onChange(int32_t id,
110 const WebVector<WebIDBObservation>& observations,
111 const WebVector<int32_t>& observationIndex) {
112 auto it = m_observerIds.find(id);
113 DCHECK(it != m_observerIds.end());
114 m_callback->call(this, IDBObserverChanges::create(it->value, observations,
115 observationIndex));
116 } 98 }
117 99
118 DEFINE_TRACE(IDBObserver) { 100 DEFINE_TRACE(IDBObserver) {
119 visitor->trace(m_callback); 101 visitor->trace(m_callback);
120 visitor->trace(m_observerIds); 102 visitor->trace(m_observerIds);
121 } 103 }
122 104
123 } // namespace blink 105 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698