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 <bitset> |
| 8 |
7 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/modules/v8/IDBObserverCallback.h" | 10 #include "bindings/modules/v8/IDBObserverCallback.h" |
9 #include "bindings/modules/v8/ToV8ForModules.h" | 11 #include "bindings/modules/v8/ToV8ForModules.h" |
10 #include "bindings/modules/v8/V8BindingForModules.h" | 12 #include "bindings/modules/v8/V8BindingForModules.h" |
11 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
12 #include "modules/IndexedDBNames.h" | 14 #include "modules/IndexedDBNames.h" |
13 #include "modules/indexeddb/IDBDatabase.h" | 15 #include "modules/indexeddb/IDBDatabase.h" |
14 #include "modules/indexeddb/IDBObserverChanges.h" | 16 #include "modules/indexeddb/IDBObserverChanges.h" |
15 #include "modules/indexeddb/IDBObserverInit.h" | 17 #include "modules/indexeddb/IDBObserverInit.h" |
16 #include "modules/indexeddb/IDBTransaction.h" | 18 #include "modules/indexeddb/IDBTransaction.h" |
17 #include "modules/indexeddb/WebIDBObserverImpl.h" | 19 #include "modules/indexeddb/WebIDBObserverImpl.h" |
18 | 20 |
19 namespace blink { | 21 namespace blink { |
20 | 22 |
21 IDBObserver* IDBObserver::create(IDBObserverCallback* callback, | 23 IDBObserver* IDBObserver::create(IDBObserverCallback* callback) { |
22 const IDBObserverInit& options) { | 24 return new IDBObserver(callback); |
23 return new IDBObserver(callback, options); | |
24 } | 25 } |
25 | 26 |
26 IDBObserver::IDBObserver(IDBObserverCallback* callback, | 27 IDBObserver::IDBObserver(IDBObserverCallback* callback) |
27 const IDBObserverInit& options) | 28 : m_callback(callback) {} |
28 : m_callback(callback), | |
29 m_transaction(options.transaction()), | |
30 m_values(options.values()), | |
31 m_noRecords(options.noRecords()) { | |
32 DCHECK_EQ(m_operationTypes.size(), | |
33 static_cast<size_t>(WebIDBOperationTypeCount)); | |
34 m_operationTypes.reset(); | |
35 m_operationTypes[WebIDBAdd] = | |
36 options.operationTypes().contains(IndexedDBNames::add); | |
37 m_operationTypes[WebIDBPut] = | |
38 options.operationTypes().contains(IndexedDBNames::put); | |
39 m_operationTypes[WebIDBDelete] = | |
40 options.operationTypes().contains(IndexedDBNames::kDelete); | |
41 m_operationTypes[WebIDBClear] = | |
42 options.operationTypes().contains(IndexedDBNames::clear); | |
43 } | |
44 | 29 |
45 void IDBObserver::observe(IDBDatabase* database, | 30 void IDBObserver::observe(IDBDatabase* database, |
46 IDBTransaction* transaction, | 31 IDBTransaction* transaction, |
| 32 const IDBObserverInit& options, |
47 ExceptionState& exceptionState) { | 33 ExceptionState& exceptionState) { |
48 if (transaction->isFinished() || transaction->isFinishing()) { | 34 if (transaction->isFinished() || transaction->isFinishing()) { |
49 exceptionState.throwDOMException( | 35 exceptionState.throwDOMException( |
50 TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage); | 36 TransactionInactiveError, IDBDatabase::transactionFinishedErrorMessage); |
51 return; | 37 return; |
52 } | 38 } |
53 if (!transaction->isActive()) { | 39 if (!transaction->isActive()) { |
54 exceptionState.throwDOMException( | 40 exceptionState.throwDOMException( |
55 TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage); | 41 TransactionInactiveError, IDBDatabase::transactionInactiveErrorMessage); |
56 return; | 42 return; |
57 } | 43 } |
58 if (!database->backend()) { | 44 if (!database->backend()) { |
59 exceptionState.throwDOMException(InvalidStateError, | 45 exceptionState.throwDOMException(InvalidStateError, |
60 IDBDatabase::databaseClosedErrorMessage); | 46 IDBDatabase::databaseClosedErrorMessage); |
61 return; | 47 return; |
62 } | 48 } |
| 49 if (!options.hasOperationTypes()) { |
| 50 exceptionState.throwTypeError( |
| 51 "operationTypes not specified in observe options."); |
| 52 return; |
| 53 } |
| 54 if (options.operationTypes().size() == 0) { |
| 55 exceptionState.throwTypeError("operationTypes must be populated."); |
| 56 return; |
| 57 } |
| 58 |
| 59 std::bitset<WebIDBOperationTypeCount> types; |
| 60 for (const auto& operationType : options.operationTypes()) { |
| 61 if (operationType == IndexedDBNames::add) { |
| 62 types[WebIDBAdd] = true; |
| 63 } else if (operationType == IndexedDBNames::put) { |
| 64 types[WebIDBPut] = true; |
| 65 } else if (operationType == IndexedDBNames::kDelete) { |
| 66 types[WebIDBDelete] = true; |
| 67 } else if (operationType == IndexedDBNames::clear) { |
| 68 types[WebIDBClear] = true; |
| 69 } else { |
| 70 exceptionState.throwTypeError( |
| 71 "Unknown operation type in observe options: " + operationType); |
| 72 return; |
| 73 } |
| 74 } |
63 | 75 |
64 std::unique_ptr<WebIDBObserverImpl> observer = | 76 std::unique_ptr<WebIDBObserverImpl> observer = |
65 WebIDBObserverImpl::create(this); | 77 WebIDBObserverImpl::create(this, options.transaction(), options.values(), |
| 78 options.noRecords(), types); |
66 WebIDBObserverImpl* observerPtr = observer.get(); | 79 WebIDBObserverImpl* observerPtr = observer.get(); |
67 int32_t observerId = | 80 int32_t observerId = |
68 database->backend()->addObserver(std::move(observer), transaction->id()); | 81 database->backend()->addObserver(std::move(observer), transaction->id()); |
69 m_observerIds.add(observerId, database); | 82 m_observerIds.add(observerId, database); |
70 observerPtr->setId(observerId); | 83 observerPtr->setId(observerId); |
71 } | 84 } |
72 | 85 |
73 void IDBObserver::unobserve(IDBDatabase* database, | 86 void IDBObserver::unobserve(IDBDatabase* database, |
74 ExceptionState& exceptionState) { | 87 ExceptionState& exceptionState) { |
75 if (!database->backend()) { | 88 if (!database->backend()) { |
(...skipping 25 matching lines...) Expand all Loading... |
101 m_callback->call(this, IDBObserverChanges::create(it->value, observations, | 114 m_callback->call(this, IDBObserverChanges::create(it->value, observations, |
102 observationIndex)); | 115 observationIndex)); |
103 } | 116 } |
104 | 117 |
105 DEFINE_TRACE(IDBObserver) { | 118 DEFINE_TRACE(IDBObserver) { |
106 visitor->trace(m_callback); | 119 visitor->trace(m_callback); |
107 visitor->trace(m_observerIds); | 120 visitor->trace(m_observerIds); |
108 } | 121 } |
109 | 122 |
110 } // namespace blink | 123 } // namespace blink |
OLD | NEW |