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 |
haraken
2015/08/26 00:28:19
Add ASSERT(!m_controllers.contains(controller)).
peria
2015/08/26 04:36:14
Done.
| |
23 m_controllers.append(controller); | 23 m_controllers.add(controller); |
24 if (wasEmpty) | 24 if (!m_isListening) { |
25 startListening(); | 25 startListening(); |
26 m_isListening = true; | |
27 } | |
26 } | 28 } |
27 | 29 |
28 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) | 30 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) |
29 { | 31 { |
haraken
2015/08/26 00:28:19
Add ASSERT(m_controllers.contains(controller)).
peria
2015/08/26 04:36:13
Done.
| |
30 // Do not actually remove the controller from the vector, instead zero them out. | 32 m_controllers.remove(controller); |
31 // The zeros are removed in these two cases: | 33 if (!m_isDispatching && m_controllers.isEmpty()) { |
32 // 1. either immediately if we are not dispatching any events, | 34 stopListening(); |
33 // 2. or after events to all controllers have dispatched (see notifyControll ers()). | 35 m_isListening = false; |
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 | |
40 m_controllers[index] = nullptr; | |
41 m_needsPurge = true; | |
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 } | 36 } |
60 | |
61 m_needsPurge = false; | |
62 | |
63 if (m_controllers.isEmpty()) | |
64 stopListening(); | |
65 } | 37 } |
66 | 38 |
67 void PlatformEventDispatcher::notifyControllers() | 39 void PlatformEventDispatcher::notifyControllers() |
68 { | 40 { |
41 if (m_controllers.isEmpty()) | |
42 return; | |
43 | |
69 { | 44 { |
70 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | 45 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); |
71 // Don't notify controllers removed or added during event dispatch. | 46 // HashSet m_controllers can be updated during an iteration, and it stop s the iteration. |
72 size_t size = m_controllers.size(); | 47 // Thus we store it into a Vector to access all elements. |
73 for (size_t i = 0; i < size; ++i) { | 48 HeapVector<PlatformEventController*> snapshotVector; |
haraken
2015/08/26 00:28:19
WillBeHeapVector<RawPtrWillBeMember<PlatformEventC
peria
2015/08/26 04:36:13
Done.
| |
74 if (m_controllers[i]) | 49 snapshotVector.reserveCapacity(m_controllers.size()); |
75 m_controllers[i]->didUpdateData(); | 50 for (PlatformEventController* controller : m_controllers) |
51 snapshotVector.append(controller); | |
haraken
2015/08/26 00:28:19
Can we use copyToVector?
peria
2015/08/26 04:36:13
Done.
| |
52 for (PlatformEventController* controller : snapshotVector) { | |
53 if (m_controllers.contains(controller)) | |
54 controller->didUpdateData(); | |
76 } | 55 } |
77 } | 56 } |
78 | 57 |
79 if (m_needsPurge) | 58 if (m_controllers.isEmpty()) { |
80 purgeControllers(); | 59 stopListening(); |
60 m_isListening = false; | |
61 } | |
81 } | 62 } |
82 | 63 |
83 DEFINE_TRACE(PlatformEventDispatcher) | 64 DEFINE_TRACE(PlatformEventDispatcher) |
84 { | 65 { |
85 #if ENABLE(OILPAN) | 66 #if ENABLE(OILPAN) |
86 // Trace the backing store, the weak(&bare) element references won't be. | |
87 visitor->trace(m_controllers); | 67 visitor->trace(m_controllers); |
88 visitor->template registerWeakMembers<PlatformEventDispatcher, &PlatformEven tDispatcher::clearWeakMembers>(this); | |
89 #endif | 68 #endif |
90 } | 69 } |
91 | 70 |
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 | 71 } // namespace blink |
OLD | NEW |