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) | |
15 , m_isDispatching(false) | |
16 { | 14 { |
17 } | 15 } |
18 | 16 |
19 void PlatformEventDispatcher::addController(PlatformEventController* controller) | 17 void PlatformEventDispatcher::addController(PlatformEventController* controller) |
20 { | 18 { |
19 ASSERT(controller); | |
20 | |
21 bool wasEmpty = m_controllers.isEmpty(); | 21 bool wasEmpty = m_controllers.isEmpty(); |
22 if (!m_controllers.contains(controller)) | 22 m_controllers.add(controller); |
23 m_controllers.append(controller); | |
24 if (wasEmpty) | 23 if (wasEmpty) |
25 startListening(); | 24 startListening(); |
26 } | 25 } |
27 | 26 |
28 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) | 27 void PlatformEventDispatcher::removeController(PlatformEventController* controll er) |
29 { | 28 { |
30 // Do not actually remove the controller from the vector, instead zero them out. | 29 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. | |
haraken
2015/08/19 11:12:51
This comment explains why we have to use a Vector
peria
2015/08/20 09:35:50
Ah, I understand that the main focus of this comme
timvolodine
2015/08/20 11:16:17
yes, the main concern here was that we can execute
| |
36 size_t index = m_controllers.find(controller); | |
37 if (index == kNotFound) | |
38 return; | 30 return; |
39 | 31 |
40 m_controllers[index] = nullptr; | 32 m_controllers.remove(controller); |
41 m_needsPurge = true; | 33 purgeControllers(); |
42 | |
43 if (!m_isDispatching) | |
44 purgeControllers(); | |
45 } | 34 } |
46 | 35 |
47 void PlatformEventDispatcher::purgeControllers() | 36 void PlatformEventDispatcher::purgeControllers() |
48 { | 37 { |
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()) | 38 if (m_controllers.isEmpty()) |
64 stopListening(); | 39 stopListening(); |
65 } | 40 } |
66 | 41 |
67 void PlatformEventDispatcher::notifyControllers() | 42 void PlatformEventDispatcher::notifyControllers() |
68 { | 43 { |
69 { | 44 // didUpdateData() may delete the object itself, and it can lead to restruct ure m_controllers. |
70 TemporaryChange<bool> changeIsDispatching(m_isDispatching, true); | 45 // Thus we restore elements of m_controllers into a Vector to access all ele ments. |
71 // Don't notify controllers removed or added during event dispatch. | 46 HeapVector<PlatformEventController*> snapshotVector; |
72 size_t size = m_controllers.size(); | 47 for (PlatformEventController* controller : m_controllers) |
73 for (size_t i = 0; i < size; ++i) { | 48 snapshotVector.append(controller); |
74 if (m_controllers[i]) | 49 for (PlatformEventController* controller : snapshotVector) |
75 m_controllers[i]->didUpdateData(); | 50 controller->didUpdateData(); |
76 } | |
77 } | |
78 | 51 |
79 if (m_needsPurge) | 52 purgeControllers(); |
80 purgeControllers(); | |
81 } | 53 } |
82 | 54 |
83 DEFINE_TRACE(PlatformEventDispatcher) | 55 DEFINE_TRACE(PlatformEventDispatcher) |
84 { | 56 { |
85 #if ENABLE(OILPAN) | 57 #if ENABLE(OILPAN) |
86 // Trace the backing store, the weak(&bare) element references won't be. | |
87 visitor->trace(m_controllers); | 58 visitor->trace(m_controllers); |
88 visitor->template registerWeakMembers<PlatformEventDispatcher, &PlatformEven tDispatcher::clearWeakMembers>(this); | |
89 #endif | 59 #endif |
90 } | 60 } |
91 | 61 |
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 | 62 } // namespace blink |
OLD | NEW |