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 #ifndef EventHandlerRegistry_h |
| 5 #define EventHandlerRegistry_h |
| 6 |
| 7 #include "core/dom/DocumentLifecycleObserver.h" |
| 8 #include "core/dom/DocumentSupplementable.h" |
| 9 #include "core/events/Event.h" |
| 10 #include "core/frame/DOMWindowLifecycleObserver.h" |
| 11 #include "wtf/HashCountedSet.h" |
| 12 |
| 13 namespace WebCore { |
| 14 |
| 15 typedef HashCountedSet<EventTarget*> EventTargetSet; |
| 16 |
| 17 // Registry for keeping track of event handlers. Handlers can either be |
| 18 // associated with an EventTarget or be "external" handlers which live outside |
| 19 // the DOM (e.g., WebViewImpl). |
| 20 class EventHandlerRegistry FINAL : public DocumentSupplement { |
| 21 public: |
| 22 virtual ~EventHandlerRegistry(); |
| 23 |
| 24 // Supported event handler classes. Note that each one may correspond to |
| 25 // multiple event types. |
| 26 enum EventHandlerClass { |
| 27 WheelEvent, |
| 28 ScrollEvent, |
| 29 TouchEvent, |
| 30 EventHandlerClassCount, // Must be the last entry. |
| 31 }; |
| 32 |
| 33 static const char* supplementName(); |
| 34 static EventHandlerRegistry* from(Document&); |
| 35 |
| 36 // Returns true if the host Document or any child documents have any |
| 37 // registered event handlers of the class. |
| 38 bool hasEventHandlers(EventHandlerClass) const; |
| 39 |
| 40 // Returns a set of EventTargets which have registered handlers of the |
| 41 // given class. Only contains targets directly in this document; all |
| 42 // handlers in a child Document are collapsed to a single respective |
| 43 // Document instance in the set. |
| 44 const EventTargetSet* eventHandlerTargets(EventHandlerClass) const; |
| 45 |
| 46 // Returns the number of external event handlers for a given class. |
| 47 // External handlers are not included in |eventHandlerTargets|. |
| 48 unsigned externalEventHandlerCount(EventHandlerClass) const; |
| 49 |
| 50 // Registration and management of event handlers attached to EventTargets. |
| 51 void didAddEventHandler(EventTarget&, const AtomicString& eventType); |
| 52 void didAddEventHandler(EventTarget&, EventHandlerClass); |
| 53 void didRemoveEventHandler(EventTarget&, const AtomicString& eventType); |
| 54 void didRemoveEventHandler(EventTarget&, EventHandlerClass); |
| 55 void didMoveFromOtherDocument(EventTarget&, Document& oldDocument); |
| 56 void didRemoveAllEventHandlers(EventTarget&); |
| 57 |
| 58 // Registration of event handlers attached to non-DOM objects. |
| 59 void didAddExternalEventHandler(const AtomicString& eventType); |
| 60 void didRemoveExternalEventHandler(const AtomicString& eventType); |
| 61 private: |
| 62 explicit EventHandlerRegistry(Document&); |
| 63 |
| 64 enum ChangeOperation { |
| 65 Add, // Add a new event handler. |
| 66 Remove, // Remove an existing event handler. |
| 67 RemoveAll // Remove any and all existing event handlers for a given targ
et. |
| 68 }; |
| 69 |
| 70 // Returns true if |eventType| belongs to a class this registry tracks. |
| 71 static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClas
s* result); |
| 72 |
| 73 // Returns true if the operation actually added a new target or completely |
| 74 // removed an existing one. |
| 75 bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, EventTarg
et*); |
| 76 |
| 77 // Called to notify clients whenever a single event handler target is |
| 78 // registered or unregistered. If several handlers are registered for the |
| 79 // same target, only the first registration will trigger this notification. |
| 80 void notifyDidAddOrRemoveEventHandlerTarget(EventHandlerClass); |
| 81 |
| 82 // Called on the EventHandlerRegistry of the root Document to notify |
| 83 // clients when we have added the first handler or removed the last one for |
| 84 // a given event class. |hasActiveHandlers| can be used to distinguish |
| 85 // between the two cases. |
| 86 void notifyHasHandlersChanged(EventHandlerClass, bool hasActiveHandlers); |
| 87 |
| 88 // Record a change operation to a given event handler class and notify any |
| 89 // parent registry and other clients accordingly. |
| 90 void updateEventHandlerOfType(ChangeOperation, const AtomicString& eventType
, EventTarget*); |
| 91 |
| 92 void updateExternalHandlerCount(ChangeOperation, EventHandlerClass); |
| 93 void updateEventHandlerInternal(ChangeOperation, EventHandlerClass, EventTar
get*); |
| 94 |
| 95 class DocumentObserver: public DocumentLifecycleObserver { |
| 96 public: |
| 97 explicit DocumentObserver(Document&); |
| 98 ~DocumentObserver(); |
| 99 |
| 100 // Inherited from DocumentLifecycleObserver |
| 101 virtual void documentWasAttached() OVERRIDE; |
| 102 virtual void willDetachDocument() OVERRIDE; |
| 103 }; |
| 104 |
| 105 class WindowObserver: public DOMWindowLifecycleObserver { |
| 106 public: |
| 107 WindowObserver(EventHandlerRegistry&, DOMWindow&); |
| 108 ~WindowObserver(); |
| 109 |
| 110 // Inherited from DOMWindowLifecycleObserver |
| 111 virtual void didAddEventListener(DOMWindow*, const AtomicString&) OVERRI
DE; |
| 112 virtual void didRemoveEventListener(DOMWindow*, const AtomicString&) OVE
RRIDE; |
| 113 virtual void didRemoveAllEventListeners(DOMWindow*) OVERRIDE; |
| 114 private: |
| 115 EventHandlerRegistry& m_registry; |
| 116 }; |
| 117 |
| 118 struct HandlerState { |
| 119 HandlerState(); |
| 120 ~HandlerState(); |
| 121 |
| 122 OwnPtr<EventTargetSet> targets; |
| 123 // External handlers are not included in |targets|. |
| 124 unsigned externalHandlerCount; |
| 125 }; |
| 126 |
| 127 Document& m_document; |
| 128 |
| 129 DocumentObserver m_documentObserver; |
| 130 OwnPtr<WindowObserver> m_windowObserver; |
| 131 |
| 132 HandlerState m_eventHandlers[EventHandlerClassCount]; |
| 133 }; |
| 134 |
| 135 } // namespace WebCore |
| 136 |
| 137 #endif // EventHandlerRegistry_h |
OLD | NEW |