Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/frame/PlatformEventDispatcher.h" | 6 #include "core/frame/PlatformEventDispatcher.h" |
| 7 | 7 |
| 8 #include "core/frame/PlatformEventController.h" | 8 #include "core/frame/PlatformEventController.h" |
| 9 #include "wtf/TemporaryChange.h" | 9 #include "wtf/TemporaryChange.h" |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 PlatformEventDispatcher::PlatformEventDispatcher() | 13 PlatformEventDispatcher::PlatformEventDispatcher() |
| 14 : m_needsPurge(false) | 14 : m_isDispatching(false) |
| 15 , m_isDispatching(false) | 15 , m_isListening(false) |
| 16 { | 16 { |
| 17 } | 17 } |
| 18 | 18 |
| 19 void PlatformEventDispatcher::addController(PlatformEventController* controller) | 19 void PlatformEventDispatcher::addController(PlatformEventController* controller) |
| 20 { | 20 { |
| 21 bool wasEmpty = m_controllers.isEmpty(); | 21 ASSERT(controller); |
| 22 if (!m_controllers.contains(controller)) | 22 ASSERT(!m_controllers.contains(controller)); |
|
timvolodine
2015/08/27 12:31:53
nit: looks like it's fine, because we add the cont
peria
2015/08/27 12:52:39
Yes, this ASSERT assumes that one controller is no
| |
| 23 m_controllers.append(controller); | 23 |
| 24 if (wasEmpty) | 24 m_controllers.add(controller); |
| 25 if (!m_isListening) { | |
| 25 startListening(); | 26 startListening(); |
| 27 m_isListening = true; | |
| 28 } | |
| 26 } | 29 } |
| 27 | 30 |
| 28 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) | 31 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) |
| 29 { | 32 { |
| 30 // Do not actually remove the controller from the vector, instead zero them out. | 33 ASSERT(m_controllers.contains(controller)); |
| 31 // The zeros are removed in these two cases: | |
| 32 // 1. either immediately if we are not dispatching any events, | |
| 33 // 2. or after events to all controllers have dispatched (see notifyControll ers()). | |
| 34 // This is to correctly handle the re-entrancy case when a controller is des troyed | |
| 35 // while the events are still being dispatched. | |
| 36 size_t index = m_controllers.find(controller); | |
| 37 if (index == kNotFound) | |
| 38 return; | |
| 39 | 34 |
| 40 m_controllers[index] = nullptr; | 35 m_controllers.remove(controller); |
| 41 m_needsPurge = true; | 36 if (!m_isDispatching && m_controllers.isEmpty()) { |
| 42 | 37 stopListening(); |
| 43 if (!m_isDispatching) | 38 m_isListening = false; |
| 44 purgeControllers(); | |
| 45 } | |
| 46 | |
| 47 void PlatformEventDispatcher::purgeControllers() | |
| 48 { | |
| 49 ASSERT(m_needsPurge); | |
| 50 | |
| 51 size_t i = 0; | |
| 52 while (i < m_controllers.size()) { | |
| 53 if (!m_controllers[i]) { | |
| 54 m_controllers[i] = m_controllers.last(); | |
| 55 m_controllers.removeLast(); | |
| 56 } else { | |
| 57 ++i; | |
| 58 } | |
| 59 } | 39 } |
| 60 | |
| 61 m_needsPurge = false; | |
| 62 | |
| 63 if (m_controllers.isEmpty()) | |
| 64 stopListening(); | |
| 65 } | 40 } |
| 66 | 41 |
| 67 void PlatformEventDispatcher::notifyControllers() | 42 void PlatformEventDispatcher::notifyControllers() |
| 68 { | 43 { |
| 44 if (m_controllers.isEmpty()) | |
| 45 return; | |
| 46 | |
| 69 { | 47 { |
| 70 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | 48 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); |
| 71 // Don't notify controllers removed or added during event dispatch. | 49 // HashSet m_controllers can be updated during an iteration, and it stop s the iteration. |
| 72 size_t size = m_controllers.size(); | 50 // Thus we store it into a Vector to access all elements. |
| 73 for (size_t i = 0; i < size; ++i) { | 51 WillBeHeapVector<RawPtrWillBeMember<PlatformEventController>> snapshotVe ctor; |
| 74 if (m_controllers[i]) | 52 copyToVector(m_controllers, snapshotVector); |
| 75 m_controllers[i]->didUpdateData(); | 53 for (PlatformEventController* controller : snapshotVector) { |
| 54 if (m_controllers.contains(controller)) | |
| 55 controller->didUpdateData(); | |
| 76 } | 56 } |
| 77 } | 57 } |
| 78 | 58 |
| 79 if (m_needsPurge) | 59 if (m_controllers.isEmpty()) { |
| 80 purgeControllers(); | 60 stopListening(); |
| 61 m_isListening = false; | |
| 62 } | |
| 81 } | 63 } |
| 82 | 64 |
| 83 DEFINE_TRACE(PlatformEventDispatcher) | 65 DEFINE_TRACE(PlatformEventDispatcher) |
| 84 { | 66 { |
| 85 #if ENABLE(OILPAN) | 67 #if ENABLE(OILPAN) |
| 86 // Trace the backing store, the weak(&bare) element references won't be. | |
| 87 visitor->trace(m_controllers); | 68 visitor->trace(m_controllers); |
| 88 visitor->template registerWeakMembers<PlatformEventDispatcher, &PlatformEven tDispatcher::clearWeakMembers>(this); | |
| 89 #endif | 69 #endif |
| 90 } | 70 } |
| 91 | 71 |
| 92 #if ENABLE(OILPAN) | |
| 93 void PlatformEventDispatcher::clearWeakMembers(Visitor* visitor) | |
| 94 { | |
| 95 for (size_t i = 0; i < m_controllers.size(); ++i) { | |
| 96 if (!Heap::isHeapObjectAlive(m_controllers[i])) { | |
| 97 m_controllers[i] = nullptr; | |
| 98 m_needsPurge = true; | |
| 99 } | |
| 100 } | |
| 101 // Next notification will purge the empty slots. | |
| 102 } | |
| 103 #endif | |
| 104 | |
| 105 } // namespace blink | 72 } // namespace blink |
| OLD | NEW |