Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: Source/core/dom/EventHandlerRegistry.h

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

Powered by Google App Engine
This is Rietveld 408576698