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 |
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 { |
30 // Do not actually remove the controller from the vector, instead zero them out. | 32 if (!m_controllers.contains(controller)) |
timvolodine
2015/08/21 15:03:32
I think you can drop this check. m_controllers.rem
peria
2015/08/24 02:46:03
Done.
| |
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; | 33 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 // PlatformEventController::didUpdateData() can add/remove elements of m _controllers. |
timvolodine
2015/08/21 15:03:32
this is basically re-entrancy on the JavaScript si
peria
2015/08/24 02:46:02
Thank you for the explanation.
I got it.
| |
72 size_t size = m_controllers.size(); | 50 // It can change the structure of m_controllers and we cannot iterate it . |
73 for (size_t i = 0; i < size; ++i) { | 51 // Thus we take a snapshot and restore into a Vector to access all eleme nts. |
timvolodine
2015/08/21 15:03:32
"restore" -> "store" ?
peria
2015/08/24 02:46:03
Done.
| |
74 if (m_controllers[i]) | 52 HeapVector<PlatformEventController*> snapshotVector; |
timvolodine
2015/08/21 15:03:32
you could supply an initial capacity to the vector
peria
2015/08/24 02:46:03
Done.
| |
75 m_controllers[i]->didUpdateData(); | 53 for (PlatformEventController* controller : m_controllers) |
54 snapshotVector.append(controller); | |
55 for (PlatformEventController* controller : snapshotVector) { | |
56 if (m_controllers.contains(controller)) | |
57 controller->didUpdateData(); | |
76 } | 58 } |
77 } | 59 } |
78 | 60 |
79 if (m_needsPurge) | 61 if (m_controllers.isEmpty()) { |
80 purgeControllers(); | 62 stopListening(); |
63 m_isListening = false; | |
64 } | |
81 } | 65 } |
82 | 66 |
83 DEFINE_TRACE(PlatformEventDispatcher) | 67 DEFINE_TRACE(PlatformEventDispatcher) |
84 { | 68 { |
85 #if ENABLE(OILPAN) | 69 #if ENABLE(OILPAN) |
86 // Trace the backing store, the weak(&bare) element references won't be. | |
87 visitor->trace(m_controllers); | 70 visitor->trace(m_controllers); |
88 visitor->template registerWeakMembers<PlatformEventDispatcher, &PlatformEven tDispatcher::clearWeakMembers>(this); | |
89 #endif | 71 #endif |
90 } | 72 } |
91 | 73 |
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 | 74 } // namespace blink |
OLD | NEW |