OLD | NEW |
---|---|
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 "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/modules/v8/ToV8ForModules.h" | 8 #include "bindings/modules/v8/ToV8ForModules.h" |
9 #include "bindings/modules/v8/V8BindingForModules.h" | 9 #include "bindings/modules/v8/V8BindingForModules.h" |
10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
11 #include "modules/indexeddb/IDBDatabase.h" | 11 #include "modules/indexeddb/IDBDatabase.h" |
12 #include "modules/indexeddb/IDBObserverCallback.h" | 12 #include "modules/indexeddb/IDBObserverCallback.h" |
13 #include "modules/indexeddb/IDBObserverChanges.h" | |
13 #include "modules/indexeddb/IDBObserverInit.h" | 14 #include "modules/indexeddb/IDBObserverInit.h" |
14 #include "modules/indexeddb/IDBTransaction.h" | 15 #include "modules/indexeddb/IDBTransaction.h" |
15 #include "modules/indexeddb/WebIDBObserverImpl.h" | 16 #include "modules/indexeddb/WebIDBObserverImpl.h" |
16 | 17 |
17 namespace blink { | 18 namespace blink { |
18 | 19 |
19 IDBObserver* IDBObserver::create(IDBObserverCallback& callback, const IDBObserve rInit& options) | 20 IDBObserver* IDBObserver::create(IDBObserverCallback& callback, const IDBObserve rInit& options) |
20 { | 21 { |
21 return new IDBObserver(callback, options); | 22 return new IDBObserver(callback, options); |
22 } | 23 } |
23 | 24 |
24 IDBObserver::IDBObserver(IDBObserverCallback& callback, const IDBObserverInit& o ptions) | 25 IDBObserver::IDBObserver(IDBObserverCallback& callback, const IDBObserverInit& o ptions) |
25 : m_callback(&callback) | 26 : m_callback(&callback) |
26 , m_transaction(options.transaction()) | 27 , m_transaction(options.transaction()) |
27 , m_values(options.values()) | 28 , m_values(options.values()) |
28 , m_noRecords(options.noRecords()) | 29 , m_noRecords(options.noRecords()) |
29 { | 30 { |
31 // TODO(palakj): Throw an exception if unknown operation type. | |
32 m_operationTypes.reset(); | |
33 m_operationTypes[0] = options.operationTypes().contains("add"); | |
cmumford
2016/07/20 00:25:13
Use WebIDBAdd, etc. instead of hard-coded constant
palakj1
2016/07/20 01:33:36
changed
| |
34 m_operationTypes[1] = options.operationTypes().contains("put"); | |
35 m_operationTypes[2] = options.operationTypes().contains("delete"); | |
36 m_operationTypes[3] = options.operationTypes().contains("clear"); | |
cmumford
2016/07/20 00:25:13
Also:
DCHECK(m_operationTypes.size(), WebIDBOpe
palakj1
2016/07/20 01:33:36
Done
| |
30 } | 37 } |
31 | 38 |
32 IDBObserver::~IDBObserver() {} | 39 IDBObserver::~IDBObserver() {} |
33 | 40 |
34 void IDBObserver::observe(IDBDatabase* database, IDBTransaction* transaction, Ex ceptionState& exceptionState) | 41 void IDBObserver::observe(IDBDatabase* database, IDBTransaction* transaction, Ex ceptionState& exceptionState) |
35 { | 42 { |
36 if (transaction->isFinished() || transaction->isFinishing()) { | 43 if (transaction->isFinished() || transaction->isFinishing()) { |
37 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase:: transactionFinishedErrorMessage); | 44 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase:: transactionFinishedErrorMessage); |
38 return; | 45 return; |
39 } | 46 } |
40 if (!transaction->isActive()) { | 47 if (!transaction->isActive()) { |
41 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase:: transactionInactiveErrorMessage); | 48 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase:: transactionInactiveErrorMessage); |
42 return; | 49 return; |
43 } | 50 } |
44 if (!database->backend()) { | 51 if (!database->backend()) { |
45 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); | 52 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); |
46 return; | 53 return; |
47 } | 54 } |
48 | 55 |
49 std::unique_ptr<WebIDBObserverImpl> observer = WebIDBObserverImpl::create(th is); | 56 std::unique_ptr<WebIDBObserverImpl> observer = WebIDBObserverImpl::create(th is); |
50 WebIDBObserverImpl* observerPtr = observer.get(); | 57 WebIDBObserverImpl* observerPtr = observer.get(); |
51 int32_t observerId = database->backend()->addObserver(std::move(observer), t ransaction->id()); | 58 int32_t observerId = database->backend()->addObserver(std::move(observer), t ransaction->id()); |
52 m_observerIds.insert(observerId); | 59 m_observerIds.add(observerId, database); |
53 observerPtr->setId(observerId); | 60 observerPtr->setId(observerId); |
54 } | 61 } |
55 | 62 |
56 void IDBObserver::unobserve(IDBDatabase* database, ExceptionState& exceptionStat e) | 63 void IDBObserver::unobserve(IDBDatabase* database, ExceptionState& exceptionStat e) |
57 { | 64 { |
58 if (!database->backend()) { | 65 if (!database->backend()) { |
59 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); | 66 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); |
60 return; | 67 return; |
61 } | 68 } |
62 auto it = m_observerIds.begin(); | 69 |
63 std::vector<int32_t> observerIdsToRemove; | 70 HeapHashMap<int32_t, WeakMember<IDBDatabase>> observersToRetain; |
64 while (it != m_observerIds.end()) { | 71 Vector<int32_t> observerIdsToRemove; |
65 if (database->backend()->containsObserverId(*it)) { | 72 for (const auto& it : m_observerIds) { |
66 observerIdsToRemove.push_back(*it); | 73 if (it.value == database) |
67 it = m_observerIds.erase(it); | 74 observerIdsToRemove.append(it.key); |
68 } else { | 75 else |
69 it++; | 76 observersToRetain.add(it.key, it.value); |
70 } | |
71 } | 77 } |
72 if (!observerIdsToRemove.empty()) | 78 m_observerIds.swap(observersToRetain); |
79 | |
80 if (!observerIdsToRemove.isEmpty()) | |
73 database->backend()->removeObservers(observerIdsToRemove); | 81 database->backend()->removeObservers(observerIdsToRemove); |
74 } | 82 } |
75 | 83 |
76 void IDBObserver::removeObserver(int32_t id) | 84 void IDBObserver::removeObserver(int32_t id) |
77 { | 85 { |
78 m_observerIds.erase(id); | 86 m_observerIds.remove(id); |
87 } | |
88 | |
89 void IDBObserver::onChange(int32_t id, const WebVector<WebIDBObservation>& obser vations, const WebVector<int32_t>& observationIndex) | |
90 { | |
91 auto it = m_observerIds.find(id); | |
92 DCHECK(it != m_observerIds.end()); | |
93 m_callback->handleChanges(*IDBObserverChanges::create(it->value, observation s, observationIndex), *this); | |
79 } | 94 } |
80 | 95 |
81 DEFINE_TRACE(IDBObserver) | 96 DEFINE_TRACE(IDBObserver) |
82 { | 97 { |
83 visitor->trace(m_callback); | 98 visitor->trace(m_callback); |
99 visitor->trace(m_observerIds); | |
84 } | 100 } |
85 | 101 |
86 } // namespace blink | 102 } // namespace blink |
OLD | NEW |