| 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" |
| 11 #include "modules/indexeddb/IDBDatabase.h" |
| 10 #include "modules/indexeddb/IDBObserverCallback.h" | 12 #include "modules/indexeddb/IDBObserverCallback.h" |
| 11 #include "modules/indexeddb/IDBObserverInit.h" | 13 #include "modules/indexeddb/IDBObserverInit.h" |
| 14 #include "modules/indexeddb/IDBTransaction.h" |
| 15 #include "modules/indexeddb/WebIDBObserverImpl.h" |
| 12 | 16 |
| 13 namespace blink { | 17 namespace blink { |
| 14 | 18 |
| 15 IDBObserver* IDBObserver::create(IDBObserverCallback& callback, const IDBObserve
rInit& options) | 19 IDBObserver* IDBObserver::create(IDBObserverCallback& callback, const IDBObserve
rInit& options) |
| 16 { | 20 { |
| 17 return new IDBObserver(callback, options); | 21 return new IDBObserver(callback, options); |
| 18 } | 22 } |
| 19 | 23 |
| 20 IDBObserver::IDBObserver(IDBObserverCallback& callback, const IDBObserverInit& o
ptions) | 24 IDBObserver::IDBObserver(IDBObserverCallback& callback, const IDBObserverInit& o
ptions) |
| 21 : m_callback(&callback) | 25 : m_callback(&callback) |
| 22 , m_transaction(options.transaction()) | 26 , m_transaction(options.transaction()) |
| 23 , m_values(options.values()) | 27 , m_values(options.values()) |
| 24 , m_noRecords(options.noRecords()) | 28 , m_noRecords(options.noRecords()) |
| 25 { | 29 { |
| 26 } | 30 } |
| 27 | 31 |
| 28 void IDBObserver::observe(IDBDatabase* database, IDBTransaction* transaction, Ex
ceptionState& exceptionState) | 32 void IDBObserver::observe(IDBDatabase* database, IDBTransaction* transaction, Ex
ceptionState& exceptionState) |
| 29 { | 33 { |
| 30 // TODO(palakj): Finish implementation. | 34 if (transaction->isFinished() || transaction->isFinishing()) { |
| 35 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::
transactionFinishedErrorMessage); |
| 36 return; |
| 37 } |
| 38 if (!transaction->isActive()) { |
| 39 exceptionState.throwDOMException(TransactionInactiveError, IDBDatabase::
transactionInactiveErrorMessage); |
| 40 return; |
| 41 } |
| 42 if (!database->backend()) { |
| 43 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas
eClosedErrorMessage); |
| 44 return; |
| 45 } |
| 46 int32_t observerId = database->backend()->addObserver(WebIDBObserverImpl::cr
eate(this).release(), transaction->id()); |
| 47 m_observerIds.insert(observerId); |
| 48 } |
| 49 |
| 50 void IDBObserver::unobserve(IDBDatabase* database, ExceptionState& exceptionStat
e) |
| 51 { |
| 52 if (!database->backend()) { |
| 53 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas
eClosedErrorMessage); |
| 54 return; |
| 55 } |
| 56 std::set<int32_t>::iterator it = m_observerIds.begin(); |
| 57 std::vector<int32_t> observerIdsToRemove; |
| 58 while (it != m_observerIds.end()) { |
| 59 if (database->backend()->containsObserverId(*it)) { |
| 60 observerIdsToRemove.push_back(*it); |
| 61 it = m_observerIds.erase(it); |
| 62 } else { |
| 63 it++; |
| 64 } |
| 65 } |
| 66 if (!observerIdsToRemove.empty()) |
| 67 database->backend()->removeObservers(observerIdsToRemove); |
| 68 } |
| 69 |
| 70 void IDBObserver::removeObserver(int32_t id) |
| 71 { |
| 72 m_observerIds.erase(id); |
| 31 } | 73 } |
| 32 | 74 |
| 33 DEFINE_TRACE(IDBObserver) | 75 DEFINE_TRACE(IDBObserver) |
| 34 { | 76 { |
| 35 visitor->trace(m_callback); | 77 visitor->trace(m_callback); |
| 36 } | 78 } |
| 37 | 79 |
| 38 } // namespace blink | 80 } // namespace blink |
| OLD | NEW |