Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / EventHandlerRegistry.h
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 #if ASSERT_ENABLED
29         // Additional event categories for verifying handler tracking logic.
30         EventsForTesting,
31 #endif
32         EventHandlerClassCount, // Must be the last entry.
33     };
34
35     // Returns true if the FrameHost has event handlers of the specified class.
36     bool hasEventHandlers(EventHandlerClass) const;
37
38     // Returns a set of EventTargets which have registered handlers of the given class.
39     const EventTargetSet* eventHandlerTargets(EventHandlerClass) const;
40
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&);
49
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&);
55
56     void trace(Visitor*);
57     void clearWeakMembers(Visitor*);
58
59 private:
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.
64     };
65
66     // Returns true if |eventType| belongs to a class this registry tracks.
67     static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClass* result);
68
69     // Returns true if the operation actually added a new target or completely
70     // removed an existing one.
71     bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, EventTarget*);
72
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);
78
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*);
82
83     void updateEventHandlerInternal(ChangeOperation, EventHandlerClass, EventTarget*);
84
85     void updateAllEventHandlers(ChangeOperation, EventTarget&);
86
87     void checkConsistency() const;
88
89     FrameHost& m_frameHost;
90     EventTargetSet m_targets[EventHandlerClassCount];
91 };
92
93 } // namespace WebCore
94
95 #endif // EventHandlerRegistry_h