OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/dom/EventHandlerRegistry.h" |
| 7 |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/events/ThreadLocalEventNames.h" |
| 10 #include "core/events/WheelEvent.h" |
| 11 #include "core/frame/FrameHost.h" |
| 12 #include "core/frame/LocalFrame.h" |
| 13 #include "core/page/Chrome.h" |
| 14 #include "core/page/ChromeClient.h" |
| 15 #include "core/page/Page.h" |
| 16 #include "core/page/scrolling/ScrollingCoordinator.h" |
| 17 |
| 18 namespace WebCore { |
| 19 |
| 20 EventHandlerRegistry::HandlerState::HandlerState() |
| 21 { |
| 22 } |
| 23 |
| 24 EventHandlerRegistry::HandlerState::~HandlerState() |
| 25 { |
| 26 } |
| 27 |
| 28 EventHandlerRegistry::EventHandlerRegistry(Document& document) |
| 29 : m_document(document) |
| 30 { |
| 31 } |
| 32 |
| 33 EventHandlerRegistry::~EventHandlerRegistry() |
| 34 { |
| 35 } |
| 36 |
| 37 const char* EventHandlerRegistry::supplementName() |
| 38 { |
| 39 return "EventHandlerRegistry"; |
| 40 } |
| 41 |
| 42 EventHandlerRegistry* EventHandlerRegistry::from(Document& document) |
| 43 { |
| 44 EventHandlerRegistry* registry = static_cast<EventHandlerRegistry*>(Document
Supplement::from(document, supplementName())); |
| 45 if (!registry) { |
| 46 registry = new EventHandlerRegistry(document); |
| 47 DocumentSupplement::provideTo(document, supplementName(), adoptPtr(regis
try)); |
| 48 } |
| 49 return registry; |
| 50 } |
| 51 |
| 52 bool EventHandlerRegistry::eventTypeToClass(const AtomicString& eventType, Event
HandlerClass* result) |
| 53 { |
| 54 if (eventType == EventTypeNames::scroll) { |
| 55 *result = ScrollEvent; |
| 56 } else { |
| 57 return false; |
| 58 } |
| 59 return true; |
| 60 } |
| 61 |
| 62 const EventTargetSet* EventHandlerRegistry::eventHandlerTargets(EventHandlerClas
s handlerClass) const |
| 63 { |
| 64 return m_eventHandlers[handlerClass].targets.get(); |
| 65 } |
| 66 |
| 67 bool EventHandlerRegistry::hasEventHandlers(EventHandlerClass handlerClass) cons
t |
| 68 { |
| 69 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); |
| 70 return targets && targets->size(); |
| 71 } |
| 72 |
| 73 bool EventHandlerRegistry::updateEventHandlerTargets(ChangeOperation op, EventHa
ndlerClass handlerClass, EventTarget* target) |
| 74 { |
| 75 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); |
| 76 if (op == Add) { |
| 77 #if ASSERT_ENABLED |
| 78 if (Node* node = target->toNode()) |
| 79 ASSERT(&node->document() == &m_document); |
| 80 #endif // ASSERT_ENABLED |
| 81 |
| 82 if (!targets) { |
| 83 m_eventHandlers[handlerClass].targets = adoptPtr(new EventTargetSet)
; |
| 84 targets = m_eventHandlers[handlerClass].targets.get(); |
| 85 } |
| 86 |
| 87 if (!targets->add(target).isNewEntry) { |
| 88 // Just incremented refcount, no real change. |
| 89 return false; |
| 90 } |
| 91 } else { |
| 92 // Note that we can't assert that |target| is in this document because |
| 93 // it might be in the process of moving out of it. |
| 94 ASSERT(op == Remove || op == RemoveAll); |
| 95 ASSERT(op == RemoveAll || targets->contains(target)); |
| 96 if (!targets) |
| 97 return false; |
| 98 |
| 99 if (op == RemoveAll) { |
| 100 if (!targets->contains(target)) |
| 101 return false; |
| 102 targets->removeAll(target); |
| 103 } else { |
| 104 if (!targets->remove(target)) { |
| 105 // Just decremented refcount, no real update. |
| 106 return false; |
| 107 } |
| 108 } |
| 109 } |
| 110 return true; |
| 111 } |
| 112 |
| 113 void EventHandlerRegistry::updateEventHandlerInternal(ChangeOperation op, EventH
andlerClass handlerClass, EventTarget* target) |
| 114 { |
| 115 // After the document has stopped, all updates become no-ops. |
| 116 if (!m_document.isActive()) { |
| 117 return; |
| 118 } |
| 119 |
| 120 bool hadHandlers = hasEventHandlers(handlerClass); |
| 121 updateEventHandlerTargets(op, handlerClass, target); |
| 122 bool hasHandlers = hasEventHandlers(handlerClass); |
| 123 |
| 124 // Notify the parent document's registry if we added the first or removed |
| 125 // the last handler. |
| 126 if (hadHandlers != hasHandlers && !m_document.parentDocument()) { |
| 127 // This is the root registry; notify clients accordingly. |
| 128 notifyHasHandlersChanged(handlerClass, hasHandlers); |
| 129 } |
| 130 } |
| 131 |
| 132 void EventHandlerRegistry::updateEventHandlerOfType(ChangeOperation op, const At
omicString& eventType, EventTarget* target) |
| 133 { |
| 134 EventHandlerClass handlerClass; |
| 135 if (!eventTypeToClass(eventType, &handlerClass)) |
| 136 return; |
| 137 updateEventHandlerInternal(op, handlerClass, target); |
| 138 } |
| 139 |
| 140 void EventHandlerRegistry::didAddEventHandler(EventTarget& target, const AtomicS
tring& eventType) |
| 141 { |
| 142 updateEventHandlerOfType(Add, eventType, &target); |
| 143 } |
| 144 |
| 145 void EventHandlerRegistry::didRemoveEventHandler(EventTarget& target, const Atom
icString& eventType) |
| 146 { |
| 147 updateEventHandlerOfType(Remove, eventType, &target); |
| 148 } |
| 149 |
| 150 void EventHandlerRegistry::didAddEventHandler(EventTarget& target, EventHandlerC
lass handlerClass) |
| 151 { |
| 152 updateEventHandlerInternal(Add, handlerClass, &target); |
| 153 } |
| 154 |
| 155 void EventHandlerRegistry::didRemoveEventHandler(EventTarget& target, EventHandl
erClass handlerClass) |
| 156 { |
| 157 updateEventHandlerInternal(Remove, handlerClass, &target); |
| 158 } |
| 159 |
| 160 void EventHandlerRegistry::didMoveFromOtherDocument(EventTarget& target, Documen
t& oldDocument) |
| 161 { |
| 162 EventHandlerRegistry* oldRegistry = EventHandlerRegistry::from(oldDocument); |
| 163 for (size_t i = 0; i < EventHandlerClassCount; ++i) { |
| 164 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); |
| 165 const EventTargetSet* targets = oldRegistry->eventHandlerTargets(handler
Class); |
| 166 if (!targets) |
| 167 continue; |
| 168 for (unsigned count = targets->count(&target); count > 0; --count) { |
| 169 oldRegistry->updateEventHandlerInternal(Remove, handlerClass, &targe
t); |
| 170 updateEventHandlerInternal(Add, handlerClass, &target); |
| 171 } |
| 172 } |
| 173 } |
| 174 |
| 175 void EventHandlerRegistry::didRemoveAllEventHandlers(EventTarget& target) |
| 176 { |
| 177 for (size_t i = 0; i < EventHandlerClassCount; ++i) { |
| 178 EventHandlerClass handlerClass = static_cast<EventHandlerClass>(i); |
| 179 const EventTargetSet* targets = eventHandlerTargets(handlerClass); |
| 180 if (!targets) |
| 181 continue; |
| 182 updateEventHandlerInternal(RemoveAll, handlerClass, &target); |
| 183 } |
| 184 } |
| 185 |
| 186 void EventHandlerRegistry::notifyHasHandlersChanged(EventHandlerClass handlerCla
ss, bool hasActiveHandlers) |
| 187 { |
| 188 Page* page = m_document.page(); |
| 189 ScrollingCoordinator* scrollingCoordinator = page ? page->scrollingCoordinat
or() : 0; |
| 190 |
| 191 switch (handlerClass) { |
| 192 case ScrollEvent: |
| 193 if (scrollingCoordinator) |
| 194 scrollingCoordinator->updateHaveScrollEventHandlers(); |
| 195 break; |
| 196 default: |
| 197 ASSERT_NOT_REACHED(); |
| 198 break; |
| 199 } |
| 200 } |
| 201 |
| 202 } // namespace WebCore |
OLD | NEW |