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.
5 #ifndef EventHandlerRegistry_h
6 #define EventHandlerRegistry_h
8 #include "core/events/Event.h"
9 #include "core/frame/FrameHost.h"
10 #include "wtf/HashCountedSet.h"
14 typedef HashCountedSet<EventTarget*> EventTargetSet;
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> {
21 explicit EventHandlerRegistry(FrameHost&);
22 virtual ~EventHandlerRegistry();
24 // Supported event handler classes. Note that each one may correspond to
25 // multiple event types.
26 enum EventHandlerClass {
29 // Additional event categories for verifying handler tracking logic.
32 EventHandlerClassCount, // Must be the last entry.
35 // Returns true if the FrameHost has event handlers of the specified class.
36 bool hasEventHandlers(EventHandlerClass) const;
38 // Returns a set of EventTargets which have registered handlers of the given class.
39 const EventTargetSet* eventHandlerTargets(EventHandlerClass) const;
41 // Registration and management of event handlers attached to EventTargets.
42 void didAddEventHandler(EventTarget&, const AtomicString& eventType);
43 void didAddEventHandler(EventTarget&, EventHandlerClass);
44 void didRemoveEventHandler(EventTarget&, const AtomicString& eventType);
45 void didRemoveEventHandler(EventTarget&, EventHandlerClass);
46 void didRemoveAllEventHandlers(EventTarget&);
47 void didMoveIntoFrameHost(EventTarget&);
48 void didMoveOutOfFrameHost(EventTarget&);
50 // Either |documentDetached| or |didMoveOutOfFrameHost| must be called
51 // whenever the FrameHost that is associated with a registered event target
52 // changes. This ensures the registry does not end up with stale references
53 // to handlers that are no longer related to it.
54 void documentDetached(Document&);
57 void clearWeakMembers(Visitor*);
60 enum ChangeOperation {
61 Add, // Add a new event handler.
62 Remove, // Remove an existing event handler.
63 RemoveAll // Remove any and all existing event handlers for a given target.
66 // Returns true if |eventType| belongs to a class this registry tracks.
67 static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClass* result);
69 // Returns true if the operation actually added a new target or completely
70 // removed an existing one.
71 bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, EventTarget*);
73 // Called on the EventHandlerRegistry of the root Document to notify
74 // clients when we have added the first handler or removed the last one for
75 // a given event class. |hasActiveHandlers| can be used to distinguish
76 // between the two cases.
77 void notifyHasHandlersChanged(EventHandlerClass, bool hasActiveHandlers);
79 // Record a change operation to a given event handler class and notify any
80 // parent registry and other clients accordingly.
81 void updateEventHandlerOfType(ChangeOperation, const AtomicString& eventType, EventTarget*);
83 void updateEventHandlerInternal(ChangeOperation, EventHandlerClass, EventTarget*);
85 void updateAllEventHandlers(ChangeOperation, EventTarget&);
87 void checkConsistency() const;
89 FrameHost& m_frameHost;
90 EventTargetSet m_targets[EventHandlerClassCount];
93 } // namespace WebCore
95 #endif // EventHandlerRegistry_h