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