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

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: Tests now seem to pass. Created 6 years, 9 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 #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 class DOMWindow;
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, public DOMWindowLi fecycleObserver, public DocumentLifecycleObserver {
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 the number of registered event handlers for the given class on
39 // the host Document as well as all child Documents. For efficiency each
Rick Byers 2014/03/27 16:43:31 I find this wording a little contradictory. How a
Sami 2014/04/02 19:58:05 Thanks for putting it into simpler words :) Note t
40 // child Document only reports none (0) or any (1) handlers instead of the
41 // actual count.
42 unsigned eventHandlerCount(EventHandlerClass) const;
43
44 // Returns a set of EventTargets which have registered handlers of the
45 // given class. Only contains targets directly in this document; all
46 // handlers in a child Document are collapsed to a single respective
47 // Document instance in the set.
48 const EventTargetSet* eventHandlerTargets(EventHandlerClass) const;
49
50 // Registration and management of event handlers attached to EventTargets.
51 void didAddEventHandler(EventTarget&, const AtomicString&);
Rick Byers 2014/03/27 16:43:31 document (perhaps just with a good variable name)
Sami 2014/04/02 19:58:05 Done.
52 void didRemoveEventHandler(EventTarget&, const AtomicString&);
53 void didMoveToNewDocument(EventTarget&, Document& oldDocument);
54 void didRemoveAllEventHandlers(EventTarget&);
55
56 // Registration of event handlers attached to non-DOM objects.
57 void didAddExternalEventHandler(Document&, const AtomicString&);
Rick Byers 2014/03/27 16:43:31 each EventHandlerRegistry is coupled to a specific
Sami 2014/04/02 19:58:05 Excellent point. Even though the call sites might
58 void didRemoveExternalEventHandler(Document&, const AtomicString&);
59
60 // Inherited from DOMWindowLifecycleObserver
Rick Byers 2014/03/27 16:43:31 It may be confusing to clients of this class wheth
Sami 2014/04/02 19:58:05 Great suggestion, done.
61 virtual void didAddEventListener(DOMWindow*, const AtomicString&) OVERRIDE;
62 virtual void didRemoveEventListener(DOMWindow*, const AtomicString&) OVERRID E;
63 virtual void didRemoveAllEventListeners(DOMWindow*) OVERRIDE;
64
65 // Inherited from DocumentLifecycleObserver
66 virtual void documentWasAttached() OVERRIDE;
67 virtual void willDetachDocument() OVERRIDE;
68
69 private:
70 explicit EventHandlerRegistry(Document&);
71
72 enum ChangeOperation {
73 Add, // Add a new event handler.
74 Remove, // Remove an existing event handler.
75 RemoveAll // Remove any and all existing event handlers for a given targ et.
76 };
77
78 // Returns true if |eventType| belongs to a class this registry tracks.
79 static bool eventTypeToClass(const AtomicString& eventType, EventHandlerClas s* result);
80
81 // Returns true if the operation actually added a new target or completely
82 // removed an existing one.
83 bool updateEventHandlerTargets(ChangeOperation, EventHandlerClass, Document& , EventTarget*);
84
85 // Called to notify clients whenever a single event handler target is
86 // registered or unregistered. If several handlers are registered for the
87 // same target, only the first registration will trigger this notification.
88 void notifyDidAddOrRemoveEventHandlerTarget(EventHandlerClass, Document&);
89
90 // Called on the EventHandlerRegistry of the root Document to notify
91 // clients when we have added the first handler or removed the last one for
92 // a given event class. |hasActiveHandlers| can be used to distinguish
93 // between the two cases.
94 void notifyDidAddFirstOrRemoveLastEventHandler(Document&, EventHandlerClass, bool hasActiveHandlers);
Rick Byers 2014/03/27 16:43:31 this method name is a mouthfull. How about notify
Sami 2014/04/02 19:58:05 Couldn't have put it better myself :)
95
96 // Record a change operation to a given event handler class and notify any
97 // parent registry and other clients accordingly.
98 void updateEventHandlerOfClass(ChangeOperation, EventHandlerClass, EventTarg et&);
99 void updateEventHandlerOfType(ChangeOperation, const AtomicString&, EventTar get&);
100
101 void updateExternalHandlerCount(ChangeOperation, EventHandlerClass);
102 void updateEventHandlerInternal(ChangeOperation, Document&, EventHandlerClas s, EventTarget*);
103 void checkEventHandlerConsistency(EventHandlerClass) const;
104
105 // Perform a bulk update of all registered event handlers on a given
106 // EventTarget. The updates will be applied to |EventHandlerRegistry|
107 // instead of |this|.
108 void updateAllEventHandlersForTarget(ChangeOperation, EventTarget&, EventHan dlerRegistry&) const;
109
110 struct HandlerState {
111 HandlerState();
112 ~HandlerState();
113
114 OwnPtr<EventTargetSet> targets;
115 // Count of all handler targets in |targets|, including repeated ones.
116 unsigned handlerCount;
117 // External handlers are not included in |targets|.
118 unsigned externalHandlerCount;
119 };
120
121 HandlerState m_eventHandlers[EventHandlerClassCount];
122 };
123
124 } // namespace WebCore
125
126 #endif // EventHandlerRegistry_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698