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/EventHandlerRegistry.h" | 6 #include "core/frame/EventHandlerRegistry.h" |
7 | 7 |
8 #include "core/events/ThreadLocalEventNames.h" | 8 #include "core/events/ThreadLocalEventNames.h" |
9 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
11 #include "core/html/HTMLFrameOwnerElement.h" | 11 #include "core/html/HTMLFrameOwnerElement.h" |
| 12 #include "core/page/Chrome.h" |
| 13 #include "core/page/ChromeClient.h" |
12 #include "core/page/Page.h" | 14 #include "core/page/Page.h" |
13 #include "core/page/scrolling/ScrollingCoordinator.h" | 15 #include "core/page/scrolling/ScrollingCoordinator.h" |
14 | 16 |
15 namespace blink { | 17 namespace blink { |
16 | 18 |
17 EventHandlerRegistry::EventHandlerRegistry(FrameHost& frameHost) | 19 EventHandlerRegistry::EventHandlerRegistry(FrameHost& frameHost) |
18 : m_frameHost(frameHost) | 20 : m_frameHost(frameHost) |
19 { | 21 { |
20 } | 22 } |
21 | 23 |
22 EventHandlerRegistry::~EventHandlerRegistry() | 24 EventHandlerRegistry::~EventHandlerRegistry() |
23 { | 25 { |
24 checkConsistency(); | 26 checkConsistency(); |
25 } | 27 } |
26 | 28 |
27 bool EventHandlerRegistry::eventTypeToClass(const AtomicString& eventType, Event
HandlerClass* result) | 29 bool EventHandlerRegistry::eventTypeToClass(const AtomicString& eventType, Event
HandlerClass* result) |
28 { | 30 { |
29 if (eventType == EventTypeNames::scroll) { | 31 if (eventType == EventTypeNames::scroll) { |
30 *result = ScrollEvent; | 32 *result = ScrollEvent; |
31 } else if (eventType == EventTypeNames::wheel || eventType == EventTypeNames
::mousewheel) { | 33 } else if (eventType == EventTypeNames::wheel || eventType == EventTypeNames
::mousewheel) { |
32 *result = WheelEvent; | 34 *result = WheelEvent; |
| 35 } else if (isTouchEventType(eventType)) { |
| 36 *result = TouchEvent; |
33 #if ENABLE(ASSERT) | 37 #if ENABLE(ASSERT) |
34 } else if (eventType == EventTypeNames::load || eventType == EventTypeNames:
:mousemove || eventType == EventTypeNames::touchstart) { | 38 } else if (eventType == EventTypeNames::load || eventType == EventTypeNames:
:mousemove || eventType == EventTypeNames::touchstart) { |
35 *result = EventsForTesting; | 39 *result = EventsForTesting; |
36 #endif | 40 #endif |
37 } else { | 41 } else { |
38 return false; | 42 return false; |
39 } | 43 } |
40 return true; | 44 return true; |
41 } | 45 } |
42 | 46 |
43 const EventTargetSet* EventHandlerRegistry::eventHandlerTargets(EventHandlerClas
s handlerClass) const | 47 const EventTargetSet* EventHandlerRegistry::eventHandlerTargets(EventHandlerClas
s handlerClass) const |
44 { | 48 { |
45 checkConsistency(); | 49 checkConsistency(); |
46 return &m_targets[handlerClass]; | 50 return &m_targets[handlerClass]; |
47 } | 51 } |
48 | 52 |
49 bool EventHandlerRegistry::hasEventHandlers(EventHandlerClass handlerClass) cons
t | 53 bool EventHandlerRegistry::hasEventHandlers(EventHandlerClass handlerClass) cons
t |
50 { | 54 { |
| 55 checkConsistency(); |
51 return m_targets[handlerClass].size(); | 56 return m_targets[handlerClass].size(); |
52 } | 57 } |
53 | 58 |
54 bool EventHandlerRegistry::updateEventHandlerTargets(ChangeOperation op, EventHa
ndlerClass handlerClass, EventTarget* target) | 59 bool EventHandlerRegistry::updateEventHandlerTargets(ChangeOperation op, EventHa
ndlerClass handlerClass, EventTarget* target) |
55 { | 60 { |
56 EventTargetSet* targets = &m_targets[handlerClass]; | 61 EventTargetSet* targets = &m_targets[handlerClass]; |
57 if (op == Add) { | 62 if (op == Add) { |
58 if (!targets->add(target).isNewEntry) { | 63 if (!targets->add(target).isNewEntry) { |
59 // Just incremented refcount, no real change. | 64 // Just incremented refcount, no real change. |
60 return false; | 65 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
72 // Just decremented refcount, no real update. | 77 // Just decremented refcount, no real update. |
73 return false; | 78 return false; |
74 } | 79 } |
75 } | 80 } |
76 } | 81 } |
77 return true; | 82 return true; |
78 } | 83 } |
79 | 84 |
80 void EventHandlerRegistry::updateEventHandlerInternal(ChangeOperation op, EventH
andlerClass handlerClass, EventTarget* target) | 85 void EventHandlerRegistry::updateEventHandlerInternal(ChangeOperation op, EventH
andlerClass handlerClass, EventTarget* target) |
81 { | 86 { |
82 bool hadHandlers = hasEventHandlers(handlerClass); | 87 bool hadHandlers = m_targets[handlerClass].size(); |
83 updateEventHandlerTargets(op, handlerClass, target); | 88 bool targetSetChanged = updateEventHandlerTargets(op, handlerClass, target); |
84 bool hasHandlers = hasEventHandlers(handlerClass); | 89 bool hasHandlers = m_targets[handlerClass].size(); |
85 | 90 |
86 if (hadHandlers != hasHandlers) { | 91 if (hadHandlers != hasHandlers) |
87 notifyHasHandlersChanged(handlerClass, hasHandlers); | 92 notifyHasHandlersChanged(handlerClass, hasHandlers); |
88 } | 93 |
89 checkConsistency(); | 94 if (targetSetChanged) |
| 95 notifyDidAddOrRemoveEventHandlerTarget(handlerClass); |
90 } | 96 } |
91 | 97 |
92 void EventHandlerRegistry::updateEventHandlerOfType(ChangeOperation op, const At
omicString& eventType, EventTarget* target) | 98 void EventHandlerRegistry::updateEventHandlerOfType(ChangeOperation op, const At
omicString& eventType, EventTarget* target) |
93 { | 99 { |
94 EventHandlerClass handlerClass; | 100 EventHandlerClass handlerClass; |
95 if (!eventTypeToClass(eventType, &handlerClass)) | 101 if (!eventTypeToClass(eventType, &handlerClass)) |
96 return; | 102 return; |
97 updateEventHandlerInternal(op, handlerClass, target); | 103 updateEventHandlerInternal(op, handlerClass, target); |
98 } | 104 } |
99 | 105 |
(...skipping 12 matching lines...) Expand all Loading... |
112 updateEventHandlerInternal(Add, handlerClass, &target); | 118 updateEventHandlerInternal(Add, handlerClass, &target); |
113 } | 119 } |
114 | 120 |
115 void EventHandlerRegistry::didRemoveEventHandler(EventTarget& target, EventHandl
erClass handlerClass) | 121 void EventHandlerRegistry::didRemoveEventHandler(EventTarget& target, EventHandl
erClass handlerClass) |
116 { | 122 { |
117 updateEventHandlerInternal(Remove, handlerClass, &target); | 123 updateEventHandlerInternal(Remove, handlerClass, &target); |
118 } | 124 } |
119 | 125 |
120 void EventHandlerRegistry::didMoveIntoFrameHost(EventTarget& target) | 126 void EventHandlerRegistry::didMoveIntoFrameHost(EventTarget& target) |
121 { | 127 { |
122 updateAllEventHandlers(Add, target); | 128 if (!target.hasEventListeners()) |
| 129 return; |
| 130 |
| 131 Vector<AtomicString> eventTypes = target.eventTypes(); |
| 132 for (size_t i = 0; i < eventTypes.size(); ++i) { |
| 133 EventHandlerClass handlerClass; |
| 134 if (!eventTypeToClass(eventTypes[i], &handlerClass)) |
| 135 continue; |
| 136 for (unsigned count = target.getEventListeners(eventTypes[i]).size(); co
unt > 0; --count) |
| 137 didAddEventHandler(target, handlerClass); |
| 138 } |
123 } | 139 } |
124 | 140 |
125 void EventHandlerRegistry::didMoveOutOfFrameHost(EventTarget& target) | 141 void EventHandlerRegistry::didMoveOutOfFrameHost(EventTarget& target) |
126 { | 142 { |
127 updateAllEventHandlers(RemoveAll, target); | 143 didRemoveAllEventHandlers(target); |
| 144 } |
| 145 |
| 146 void EventHandlerRegistry::didMoveBetweenFrameHosts(EventTarget& target, FrameHo
st* oldFrameHost, FrameHost* newFrameHost) |
| 147 { |
| 148 ASSERT(newFrameHost != oldFrameHost); |
| 149 for (size_t i = 0; i < EventHandlerClassCount; ++i) { |
| 150 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); |
| 151 const EventTargetSet* targets = &oldFrameHost->eventHandlerRegistry().m_
targets[handlerClass]; |
| 152 for (unsigned count = targets->count(&target); count > 0; --count) |
| 153 newFrameHost->eventHandlerRegistry().didAddEventHandler(target, hand
lerClass); |
| 154 oldFrameHost->eventHandlerRegistry().didRemoveAllEventHandlers(target); |
| 155 } |
128 } | 156 } |
129 | 157 |
130 void EventHandlerRegistry::didRemoveAllEventHandlers(EventTarget& target) | 158 void EventHandlerRegistry::didRemoveAllEventHandlers(EventTarget& target) |
131 { | 159 { |
132 for (size_t i = 0; i < EventHandlerClassCount; ++i) { | 160 for (size_t i = 0; i < EventHandlerClassCount; ++i) { |
133 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); | 161 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); |
134 updateEventHandlerInternal(RemoveAll, handlerClass, &target); | 162 updateEventHandlerInternal(RemoveAll, handlerClass, &target); |
135 } | 163 } |
136 } | 164 } |
137 | 165 |
138 void EventHandlerRegistry::updateAllEventHandlers(ChangeOperation op, EventTarge
t& target) | |
139 { | |
140 if (!target.hasEventListeners()) | |
141 return; | |
142 | |
143 Vector<AtomicString> eventTypes = target.eventTypes(); | |
144 for (size_t i = 0; i < eventTypes.size(); ++i) { | |
145 EventHandlerClass handlerClass; | |
146 if (!eventTypeToClass(eventTypes[i], &handlerClass)) | |
147 continue; | |
148 if (op == RemoveAll) { | |
149 updateEventHandlerInternal(op, handlerClass, &target); | |
150 continue; | |
151 } | |
152 for (unsigned count = target.getEventListeners(eventTypes[i]).size(); co
unt > 0; --count) | |
153 updateEventHandlerInternal(op, handlerClass, &target); | |
154 } | |
155 } | |
156 | |
157 void EventHandlerRegistry::notifyHasHandlersChanged(EventHandlerClass handlerCla
ss, bool hasActiveHandlers) | 166 void EventHandlerRegistry::notifyHasHandlersChanged(EventHandlerClass handlerCla
ss, bool hasActiveHandlers) |
158 { | 167 { |
159 ScrollingCoordinator* scrollingCoordinator = m_frameHost.page().scrollingCoo
rdinator(); | 168 ScrollingCoordinator* scrollingCoordinator = m_frameHost.page().scrollingCoo
rdinator(); |
160 | 169 |
161 switch (handlerClass) { | 170 switch (handlerClass) { |
162 case ScrollEvent: | 171 case ScrollEvent: |
163 if (scrollingCoordinator) | 172 if (scrollingCoordinator) |
164 scrollingCoordinator->updateHaveScrollEventHandlers(); | 173 scrollingCoordinator->updateHaveScrollEventHandlers(); |
165 break; | 174 break; |
166 case WheelEvent: | 175 case WheelEvent: |
167 if (scrollingCoordinator) | 176 if (scrollingCoordinator) |
168 scrollingCoordinator->updateHaveWheelEventHandlers(); | 177 scrollingCoordinator->updateHaveWheelEventHandlers(); |
169 break; | 178 break; |
| 179 case TouchEvent: |
| 180 m_frameHost.chrome().client().needTouchEvents(hasActiveHandlers); |
| 181 break; |
170 #if ENABLE(ASSERT) | 182 #if ENABLE(ASSERT) |
171 case EventsForTesting: | 183 case EventsForTesting: |
172 break; | 184 break; |
173 #endif | 185 #endif |
174 default: | 186 default: |
175 ASSERT_NOT_REACHED(); | 187 ASSERT_NOT_REACHED(); |
176 break; | 188 break; |
177 } | 189 } |
178 } | 190 } |
179 | 191 |
| 192 void EventHandlerRegistry::notifyDidAddOrRemoveEventHandlerTarget(EventHandlerCl
ass handlerClass) |
| 193 { |
| 194 ScrollingCoordinator* scrollingCoordinator = m_frameHost.page().scrollingCoo
rdinator(); |
| 195 if (scrollingCoordinator && handlerClass == TouchEvent) |
| 196 scrollingCoordinator->touchEventTargetRectsDidChange(); |
| 197 } |
| 198 |
180 void EventHandlerRegistry::trace(Visitor* visitor) | 199 void EventHandlerRegistry::trace(Visitor* visitor) |
181 { | 200 { |
182 visitor->registerWeakMembers<EventHandlerRegistry, &EventHandlerRegistry::cl
earWeakMembers>(this); | 201 visitor->registerWeakMembers<EventHandlerRegistry, &EventHandlerRegistry::cl
earWeakMembers>(this); |
183 } | 202 } |
184 | 203 |
185 void EventHandlerRegistry::clearWeakMembers(Visitor* visitor) | 204 void EventHandlerRegistry::clearWeakMembers(Visitor* visitor) |
186 { | 205 { |
187 Vector<EventTarget*> deadTargets; | 206 Vector<EventTarget*> deadTargets; |
188 for (size_t i = 0; i < EventHandlerClassCount; ++i) { | 207 for (size_t i = 0; i < EventHandlerClassCount; ++i) { |
189 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); | 208 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 ASSERT(window->frame()); | 265 ASSERT(window->frame()); |
247 ASSERT(window->frame()->host()); | 266 ASSERT(window->frame()->host()); |
248 ASSERT(window->frame()->host() == &m_frameHost); | 267 ASSERT(window->frame()->host() == &m_frameHost); |
249 } | 268 } |
250 } | 269 } |
251 } | 270 } |
252 #endif // ENABLE(ASSERT) | 271 #endif // ENABLE(ASSERT) |
253 } | 272 } |
254 | 273 |
255 } // namespace blink | 274 } // namespace blink |
OLD | NEW |