| 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 #ifndef EventHandlerRegistry_h | 5 #ifndef EventHandlerRegistry_h |
| 6 #define EventHandlerRegistry_h | 6 #define EventHandlerRegistry_h |
| 7 | 7 |
| 8 #include "core/dom/DocumentSupplementable.h" | |
| 9 #include "core/events/Event.h" | 8 #include "core/events/Event.h" |
| 9 #include "core/page/Page.h" |
| 10 #include "wtf/HashCountedSet.h" | 10 #include "wtf/HashCountedSet.h" |
| 11 | 11 |
| 12 namespace WebCore { | 12 namespace WebCore { |
| 13 | 13 |
| 14 typedef HashCountedSet<EventTarget*> EventTargetSet; | 14 typedef HashCountedSet<EventTarget*> EventTargetSet; |
| 15 | 15 |
| 16 // Registry for keeping track of event handlers. Handlers can either be | 16 // Registry for keeping track of event handlers. Note that only handlers on |
| 17 // associated with an EventTarget or be "external" handlers which live outside | 17 // documents that can be rendered or can receive input (i.e., are attached to a |
| 18 // the DOM (e.g., WebViewImpl). | 18 // Page) are registered here. |
| 19 class EventHandlerRegistry FINAL : public NoBaseWillBeGarbageCollectedFinalized<
EventHandlerRegistry>, public DocumentSupplement { | 19 // TODO(skyostil): This class should move to the FrameHost (crbug.com/369082). |
| 20 class EventHandlerRegistry FINAL : public NoBaseWillBeGarbageCollectedFinalized<
EventHandlerRegistry>, public Supplement<Page> { |
| 20 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventHandlerRegistry); | 21 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventHandlerRegistry); |
| 21 public: | 22 public: |
| 22 virtual ~EventHandlerRegistry(); | 23 virtual ~EventHandlerRegistry(); |
| 23 | 24 |
| 24 // Supported event handler classes. Note that each one may correspond to | 25 // Supported event handler classes. Note that each one may correspond to |
| 25 // multiple event types. | 26 // multiple event types. |
| 26 enum EventHandlerClass { | 27 enum EventHandlerClass { |
| 27 ScrollEvent, | 28 ScrollEvent, |
| 29 #if ASSERT_ENABLED |
| 30 // Additional event categories for verifying handler tracking logic. |
| 31 EventsForTesting, |
| 32 #endif |
| 28 EventHandlerClassCount, // Must be the last entry. | 33 EventHandlerClassCount, // Must be the last entry. |
| 29 }; | 34 }; |
| 30 | 35 |
| 31 static const char* supplementName(); | 36 static const char* supplementName(); |
| 32 static EventHandlerRegistry* from(Document&); | 37 static EventHandlerRegistry* from(Page&); |
| 33 | 38 |
| 34 // Returns true if the host Document or any child documents have any | 39 // Returns true if the page has event handlers of the specified class. |
| 35 // registered event handlers of the class. | |
| 36 bool hasEventHandlers(EventHandlerClass) const; | 40 bool hasEventHandlers(EventHandlerClass) const; |
| 37 | 41 |
| 38 // Returns a set of EventTargets which have registered handlers of the | 42 // Returns a set of EventTargets which have registered handlers of the given
class. |
| 39 // given class. Only contains targets directly in this document; all | |
| 40 // handlers in a child Document are collapsed to a single respective | |
| 41 // Document instance in the set. | |
| 42 const EventTargetSet* eventHandlerTargets(EventHandlerClass) const; | 43 const EventTargetSet* eventHandlerTargets(EventHandlerClass) const; |
| 43 | 44 |
| 44 // Registration and management of event handlers attached to EventTargets. | 45 // Registration and management of event handlers attached to EventTargets. |
| 45 void didAddEventHandler(EventTarget&, const AtomicString& eventType); | 46 void didAddEventHandler(EventTarget&, const AtomicString& eventType); |
| 46 void didAddEventHandler(EventTarget&, EventHandlerClass); | 47 void didAddEventHandler(EventTarget&, EventHandlerClass); |
| 47 void didRemoveEventHandler(EventTarget&, const AtomicString& eventType); | 48 void didRemoveEventHandler(EventTarget&, const AtomicString& eventType); |
| 48 void didRemoveEventHandler(EventTarget&, EventHandlerClass); | 49 void didRemoveEventHandler(EventTarget&, EventHandlerClass); |
| 49 void didMoveFromOtherDocument(EventTarget&, Document& oldDocument); | |
| 50 void didRemoveAllEventHandlers(EventTarget&); | 50 void didRemoveAllEventHandlers(EventTarget&); |
| 51 void didMoveIntoPage(EventTarget&); |
| 52 void didMoveOutOfPage(EventTarget&); |
| 53 |
| 54 // Either |documentDetached| or |didMoveOutOfPage| must be called whenever |
| 55 // the Page that is associated with a registered event target changes. This |
| 56 // ensures the registry does not end up with stale references to handlers |
| 57 // that are no longer related to it. |
| 58 void documentDetached(Document&); |
| 51 | 59 |
| 52 virtual void trace(Visitor*) OVERRIDE; | 60 virtual void trace(Visitor*) OVERRIDE; |
| 53 void clearWeakMembers(Visitor*); | 61 void clearWeakMembers(Visitor*); |
| 54 | 62 |
| 55 private: | 63 private: |
| 56 explicit EventHandlerRegistry(Document&); | 64 explicit EventHandlerRegistry(Page&); |
| 57 | 65 |
| 58 enum ChangeOperation { | 66 enum ChangeOperation { |
| 59 Add, // Add a new event handler. | 67 Add, // Add a new event handler. |
| 60 Remove, // Remove an existing event handler. | 68 Remove, // Remove an existing event handler. |
| 61 RemoveAll // Remove any and all existing event handlers for a given targ
et. | 69 RemoveAll // Remove any and all existing event handlers for a given targ
et. |
| 62 }; | 70 }; |
| 63 | 71 |
| 64 // Returns true if |eventType| belongs to a class this registry tracks. | 72 // Returns true if |eventType| belongs to a class this registry tracks. |
| 65 static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClas
s* result); | 73 static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClas
s* result); |
| 66 | 74 |
| 67 // Returns true if the operation actually added a new target or completely | 75 // Returns true if the operation actually added a new target or completely |
| 68 // removed an existing one. | 76 // removed an existing one. |
| 69 bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, EventTarg
et*); | 77 bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, EventTarg
et*); |
| 70 | 78 |
| 71 // Called on the EventHandlerRegistry of the root Document to notify | 79 // Called on the EventHandlerRegistry of the root Document to notify |
| 72 // clients when we have added the first handler or removed the last one for | 80 // clients when we have added the first handler or removed the last one for |
| 73 // a given event class. |hasActiveHandlers| can be used to distinguish | 81 // a given event class. |hasActiveHandlers| can be used to distinguish |
| 74 // between the two cases. | 82 // between the two cases. |
| 75 void notifyHasHandlersChanged(EventHandlerClass, bool hasActiveHandlers); | 83 void notifyHasHandlersChanged(EventHandlerClass, bool hasActiveHandlers); |
| 76 | 84 |
| 77 // Record a change operation to a given event handler class and notify any | 85 // Record a change operation to a given event handler class and notify any |
| 78 // parent registry and other clients accordingly. | 86 // parent registry and other clients accordingly. |
| 79 void updateEventHandlerOfType(ChangeOperation, const AtomicString& eventType
, EventTarget*); | 87 void updateEventHandlerOfType(ChangeOperation, const AtomicString& eventType
, EventTarget*); |
| 80 | 88 |
| 81 void updateEventHandlerInternal(ChangeOperation, EventHandlerClass, EventTar
get*); | 89 void updateEventHandlerInternal(ChangeOperation, EventHandlerClass, EventTar
get*); |
| 82 | 90 |
| 83 struct HandlerState { | 91 void updateAllEventHandlers(ChangeOperation, EventTarget&); |
| 84 HandlerState(); | |
| 85 ~HandlerState(); | |
| 86 | 92 |
| 87 OwnPtr<EventTargetSet> targets; | 93 void checkConsistency() const; |
| 88 }; | |
| 89 | 94 |
| 90 Document& m_document; | 95 Page& m_page; |
| 91 HandlerState m_eventHandlers[EventHandlerClassCount]; | 96 EventTargetSet m_targets[EventHandlerClassCount]; |
| 92 }; | 97 }; |
| 93 | 98 |
| 94 } // namespace WebCore | 99 } // namespace WebCore |
| 95 | 100 |
| 96 #endif // EventHandlerRegistry_h | 101 #endif // EventHandlerRegistry_h |
| OLD | NEW |