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