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

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

Issue 2125213002: [IndexedDB] Propogating changes to observers : Renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lifetime
Patch Set: Implements IDBObserverChanges IDL records map Created 4 years, 5 months 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 "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 }
(...skipping 19 matching lines...) Expand all
42 return; 43 return;
43 } 44 }
44 if (!database->backend()) { 45 if (!database->backend()) {
45 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); 46 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage);
46 return; 47 return;
47 } 48 }
48 49
49 std::unique_ptr<WebIDBObserverImpl> observer = WebIDBObserverImpl::create(th is); 50 std::unique_ptr<WebIDBObserverImpl> observer = WebIDBObserverImpl::create(th is);
50 WebIDBObserverImpl* observerPtr = observer.get(); 51 WebIDBObserverImpl* observerPtr = observer.get();
51 int32_t observerId = database->backend()->addObserver(std::move(observer), t ransaction->id()); 52 int32_t observerId = database->backend()->addObserver(std::move(observer), t ransaction->id());
52 m_observerIds.insert(observerId); 53 m_observerIds.add(observerId, database);
53 observerPtr->setId(observerId); 54 observerPtr->setId(observerId);
54 } 55 }
55 56
56 void IDBObserver::unobserve(IDBDatabase* database, ExceptionState& exceptionStat e) 57 void IDBObserver::unobserve(IDBDatabase* database, ExceptionState& exceptionStat e)
57 { 58 {
58 if (!database->backend()) { 59 if (!database->backend()) {
59 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage); 60 exceptionState.throwDOMException(InvalidStateError, IDBDatabase::databas eClosedErrorMessage);
60 return; 61 return;
61 } 62 }
62 auto it = m_observerIds.begin(); 63
64 HeapHashMap<int32_t, WeakMember<IDBDatabase>> observersToRetain;
63 std::vector<int32_t> observerIdsToRemove; 65 std::vector<int32_t> observerIdsToRemove;
64 while (it != m_observerIds.end()) { 66 for (const auto& it : m_observerIds) {
65 if (database->backend()->containsObserverId(*it)) { 67 if (it.value == database)
66 observerIdsToRemove.push_back(*it); 68 observersToRetain.add(it.key, it.value);
67 it = m_observerIds.erase(it); 69 else
68 } else { 70 observerIdsToRemove.push_back(it.key);
palakj1 2016/07/13 17:47:18 typo : the if and else condition must be swapped.
69 it++;
70 }
71 } 71 }
72 m_observerIds.swap(observersToRetain);
73
72 if (!observerIdsToRemove.empty()) 74 if (!observerIdsToRemove.empty())
73 database->backend()->removeObservers(observerIdsToRemove); 75 database->backend()->removeObservers(observerIdsToRemove);
74 } 76 }
75 77
76 void IDBObserver::removeObserver(int32_t id) 78 void IDBObserver::removeObserver(int32_t id)
77 { 79 {
78 m_observerIds.erase(id); 80 m_observerIds.remove(id);
81 }
82
83 void IDBObserver::onChange(int32_t id, const std::vector<WebIDBObservation>& obs ervations, const std::vector<int32_t>& observationIndex)
84 {
85 auto it = m_observerIds.find(id);
86 DCHECK_NE(it, m_observerIds.end());
87 m_callback->handleEvent(*IDBObserverChanges::create(it->value, observations, observationIndex), *this);
79 } 88 }
80 89
81 DEFINE_TRACE(IDBObserver) 90 DEFINE_TRACE(IDBObserver)
82 { 91 {
83 visitor->trace(m_callback); 92 visitor->trace(m_callback);
93 visitor->trace(m_observerIds);
84 } 94 }
85 95
86 } // namespace blink 96 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698