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