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) | |
| 16 { | 15 { |
| 17 } | 16 } |
| 18 | 17 |
| 19 void PlatformEventDispatcher::addController(PlatformEventController* controller) | 18 void PlatformEventDispatcher::addController(PlatformEventController* controller) |
| 20 { | 19 { |
| 20 ASSERT(controller); | |
| 21 | |
| 21 bool wasEmpty = m_controllers.isEmpty(); | 22 bool wasEmpty = m_controllers.isEmpty(); |
| 22 if (!m_controllers.contains(controller)) | 23 m_controllers.add(controller); |
| 23 m_controllers.append(controller); | |
| 24 if (wasEmpty) | 24 if (wasEmpty) |
| 25 startListening(); | 25 startListening(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) | 28 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) |
| 29 { | 29 { |
| 30 // Do not actually remove the controller from the vector, instead zero them out. | 30 if (!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; | 31 return; |
| 39 | 32 |
| 40 m_controllers[index] = nullptr; | 33 m_controllers.remove(controller); |
| 41 m_needsPurge = true; | 34 if (!m_isDispatching && m_controllers.isEmpty()) |
| 42 | |
| 43 if (!m_isDispatching) | |
| 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 } | |
| 60 | |
| 61 m_needsPurge = false; | |
| 62 | |
| 63 if (m_controllers.isEmpty()) | |
| 64 stopListening(); | 35 stopListening(); |
| 65 } | 36 } |
| 66 | 37 |
| 67 void PlatformEventDispatcher::notifyControllers() | 38 void PlatformEventDispatcher::notifyControllers() |
| 68 { | 39 { |
| 69 { | 40 { |
| 70 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | 41 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); |
| 71 // Don't notify controllers removed or added during event dispatch. | 42 // m_controllers can be restructured in addController/removeController r un on other threads. |
|
timvolodine
2015/08/20 11:16:17
what other threads? I thought blink was single-thr
peria
2015/08/21 03:33:45
Oops, it comes from my misunderstanding.
Dropped t
| |
| 72 size_t size = m_controllers.size(); | 43 // Thus we take a snapshot and restore into a Vector to access all eleme nts. |
| 73 for (size_t i = 0; i < size; ++i) { | 44 WillBeHeapHashSet<RawPtrWillBeWeakMember<PlatformEventController>> snaps hotSet(m_controllers); |
| 74 if (m_controllers[i]) | 45 HeapVector<PlatformEventController*> snapshotVector; |
| 75 m_controllers[i]->didUpdateData(); | 46 for (PlatformEventController* controller : snapshotSet) { |
| 47 snapshotVector.append(controller); | |
|
timvolodine
2015/08/20 11:16:17
why is it necessary to create both snapshotVector
peria
2015/08/21 03:33:45
snapshotSet was introduced to avoid accessing newl
peria
2015/08/21 03:33:45
We may remove using snapshotSet.
timvolodine
2015/08/21 15:03:32
yeah looks like it's not really needed
| |
| 48 } | |
| 49 for (PlatformEventController* controller : snapshotVector) { | |
| 50 // Skip a controller which is removed during dispatching. | |
| 51 if (m_controllers.contains(controller)) | |
|
timvolodine
2015/08/20 11:16:17
also, how expensive is 'contains' here? the notify
timvolodine
2015/08/20 11:16:17
is it possible to remove and then re-add the same
peria
2015/08/21 03:33:45
I think we can, but am not sure.
peria
2015/08/21 03:33:45
Hmm, unfortunately I don't know the performance re
peria
2015/08/21 05:34:19
It must be more expensive than checking a nullptr,
timvolodine
2015/08/21 15:03:32
we cannot get rid of 'contains' because of re-entr
| |
| 52 controller->didUpdateData(); | |
| 76 } | 53 } |
| 77 } | 54 } |
| 78 | 55 |
| 79 if (m_needsPurge) | 56 if (m_controllers.isEmpty()) |
| 80 purgeControllers(); | 57 stopListening(); |
| 81 } | 58 } |
| 82 | 59 |
| 83 DEFINE_TRACE(PlatformEventDispatcher) | 60 DEFINE_TRACE(PlatformEventDispatcher) |
| 84 { | 61 { |
| 85 #if ENABLE(OILPAN) | 62 #if ENABLE(OILPAN) |
| 86 // Trace the backing store, the weak(&bare) element references won't be. | |
| 87 visitor->trace(m_controllers); | 63 visitor->trace(m_controllers); |
| 88 visitor->template registerWeakMembers<PlatformEventDispatcher, &PlatformEven tDispatcher::clearWeakMembers>(this); | |
| 89 #endif | 64 #endif |
| 90 } | 65 } |
| 91 | 66 |
| 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 | 67 } // namespace blink |
| OLD | NEW |