OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/dom/EventHandlerRegistry.h" | 6 #include "core/dom/EventHandlerRegistry.h" |
7 | 7 |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/events/ThreadLocalEventNames.h" | 9 #include "core/events/ThreadLocalEventNames.h" |
10 #include "core/events/WheelEvent.h" | 10 #include "core/events/WheelEvent.h" |
11 #include "core/frame/FrameHost.h" | 11 #include "core/frame/FrameHost.h" |
12 #include "core/frame/LocalFrame.h" | 12 #include "core/frame/LocalFrame.h" |
13 #include "core/page/Chrome.h" | 13 #include "core/page/Chrome.h" |
14 #include "core/page/ChromeClient.h" | 14 #include "core/page/ChromeClient.h" |
15 #include "core/page/Page.h" | 15 #include "core/page/Page.h" |
16 #include "core/page/scrolling/ScrollingCoordinator.h" | 16 #include "core/page/scrolling/ScrollingCoordinator.h" |
17 | 17 |
18 namespace WebCore { | 18 namespace WebCore { |
19 | 19 |
| 20 EventHandlerRegistry::DocumentObserver::DocumentObserver(Document& document) |
| 21 : ActiveDOMObject(&document) |
| 22 { |
| 23 } |
| 24 |
| 25 EventHandlerRegistry::DocumentObserver::~DocumentObserver() |
| 26 { |
| 27 } |
| 28 |
| 29 void EventHandlerRegistry::DocumentObserver::stop() |
| 30 { |
| 31 Document* document = static_cast<Document*>(lifecycleContext()); |
| 32 Document* parentDocument = document->parentDocument(); |
| 33 if (!parentDocument) |
| 34 return; |
| 35 EventHandlerRegistry* parentRegistry = EventHandlerRegistry::from(*parentDoc
ument); |
| 36 parentRegistry->didRemoveAllEventHandlers(*document); |
| 37 } |
| 38 |
20 EventHandlerRegistry::HandlerState::HandlerState() | 39 EventHandlerRegistry::HandlerState::HandlerState() |
21 { | 40 { |
22 } | 41 } |
23 | 42 |
24 EventHandlerRegistry::HandlerState::~HandlerState() | 43 EventHandlerRegistry::HandlerState::~HandlerState() |
25 { | 44 { |
26 } | 45 } |
27 | 46 |
28 EventHandlerRegistry::EventHandlerRegistry(Document& document) | 47 EventHandlerRegistry::EventHandlerRegistry(Document& document) |
29 : m_document(document) | 48 : m_document(document) |
| 49 , m_documentObserver(DocumentObserver(document)) |
30 { | 50 { |
| 51 m_documentObserver.suspendIfNeeded(); |
31 } | 52 } |
32 | 53 |
33 EventHandlerRegistry::~EventHandlerRegistry() | 54 EventHandlerRegistry::~EventHandlerRegistry() |
34 { | 55 { |
35 } | 56 } |
36 | 57 |
37 const char* EventHandlerRegistry::supplementName() | 58 const char* EventHandlerRegistry::supplementName() |
38 { | 59 { |
39 return "EventHandlerRegistry"; | 60 return "EventHandlerRegistry"; |
40 } | 61 } |
(...skipping 27 matching lines...) Expand all Loading... |
68 { | 89 { |
69 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); | 90 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); |
70 return targets && targets->size(); | 91 return targets && targets->size(); |
71 } | 92 } |
72 | 93 |
73 bool EventHandlerRegistry::updateEventHandlerTargets(ChangeOperation op, EventHa
ndlerClass handlerClass, EventTarget* target) | 94 bool EventHandlerRegistry::updateEventHandlerTargets(ChangeOperation op, EventHa
ndlerClass handlerClass, EventTarget* target) |
74 { | 95 { |
75 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); | 96 EventTargetSet* targets = m_eventHandlers[handlerClass].targets.get(); |
76 if (op == Add) { | 97 if (op == Add) { |
77 #if ASSERT_ENABLED | 98 #if ASSERT_ENABLED |
78 if (Node* node = target->toNode()) | 99 if (Node* node = target->toNode()) { |
79 ASSERT(&node->document() == &m_document); | 100 // The node should either be in the document, or be the Document nod
e of a child |
| 101 // of the document. |
| 102 ASSERT(&node->document() == &m_document |
| 103 || (node->isDocumentNode() && toDocument(node)->parentDocument()
== &m_document)); |
| 104 } |
80 #endif // ASSERT_ENABLED | 105 #endif // ASSERT_ENABLED |
81 | 106 |
82 if (!targets) { | 107 if (!targets) { |
83 m_eventHandlers[handlerClass].targets = adoptPtr(new EventTargetSet)
; | 108 m_eventHandlers[handlerClass].targets = adoptPtr(new EventTargetSet)
; |
84 targets = m_eventHandlers[handlerClass].targets.get(); | 109 targets = m_eventHandlers[handlerClass].targets.get(); |
85 } | 110 } |
86 | 111 |
87 if (!targets->add(target).isNewEntry) { | 112 if (!targets->add(target).isNewEntry) { |
88 // Just incremented refcount, no real change. | 113 // Just incremented refcount, no real change. |
| 114 #if ASSERT_ENABLED |
| 115 // If this is a child document node, then the count should never go
above 1. |
| 116 if (Node* node = target->toNode()) |
| 117 ASSERT(!node->isDocumentNode() || &node->document() == &m_docume
nt); |
| 118 #endif // ASSERT_ENABLED |
89 return false; | 119 return false; |
90 } | 120 } |
91 } else { | 121 } else { |
92 // Note that we can't assert that |target| is in this document because | 122 // Note that we can't assert that |target| is in this document because |
93 // it might be in the process of moving out of it. | 123 // it might be in the process of moving out of it. |
94 ASSERT(op == Remove || op == RemoveAll); | 124 ASSERT(op == Remove || op == RemoveAll); |
95 ASSERT(op == RemoveAll || targets->contains(target)); | 125 ASSERT(op == RemoveAll || targets->contains(target)); |
96 if (!targets) | 126 if (!targets) |
97 return false; | 127 return false; |
98 | 128 |
99 if (op == RemoveAll) { | 129 if (op == RemoveAll) { |
100 if (!targets->contains(target)) | 130 if (!targets->contains(target)) |
101 return false; | 131 return false; |
102 targets->removeAll(target); | 132 targets->removeAll(target); |
103 } else { | 133 } else { |
104 if (!targets->remove(target)) { | 134 if (!targets->remove(target)) { |
105 // Just decremented refcount, no real update. | 135 // Just decremented refcount, no real update. |
106 return false; | 136 return false; |
107 } | 137 } |
108 } | 138 } |
109 } | 139 } |
110 return true; | 140 return true; |
111 } | 141 } |
112 | 142 |
113 void EventHandlerRegistry::updateEventHandlerInternal(ChangeOperation op, EventH
andlerClass handlerClass, EventTarget* target) | 143 void EventHandlerRegistry::updateEventHandlerInternal(ChangeOperation op, EventH
andlerClass handlerClass, EventTarget* target) |
114 { | 144 { |
115 // After the document has stopped, all updates become no-ops. | 145 // Notify our parent registry if we added the first or removed the last hand
ler. |
116 if (!m_document.isActive()) { | 146 for (Document* document = &m_document; document; document = document->parent
Document()) { |
117 return; | 147 // After the document has stopped, all updates become no-ops. |
118 } | 148 if (!document->isActive()) { |
| 149 return; |
| 150 } |
| 151 EventHandlerRegistry* registry = EventHandlerRegistry::from(*document); |
119 | 152 |
120 bool hadHandlers = hasEventHandlers(handlerClass); | 153 bool hadHandlers = registry->hasEventHandlers(handlerClass); |
121 updateEventHandlerTargets(op, handlerClass, target); | 154 registry->updateEventHandlerTargets(op, handlerClass, target); |
122 bool hasHandlers = hasEventHandlers(handlerClass); | 155 bool hasHandlers = registry->hasEventHandlers(handlerClass); |
123 | 156 |
124 // Notify the parent document's registry if we added the first or removed | 157 if (hadHandlers == hasHandlers) { |
125 // the last handler. | 158 break; |
126 if (hadHandlers != hasHandlers && !m_document.parentDocument()) { | 159 } |
127 // This is the root registry; notify clients accordingly. | 160 if (!document->parentDocument()) { |
128 notifyHasHandlersChanged(handlerClass, hasHandlers); | 161 // This is the root registry; notify clients accordingly. |
| 162 registry->notifyHasHandlersChanged(handlerClass, hasHandlers); |
| 163 } else { |
| 164 // Report change to parent with our Document as the target. |
| 165 target = document; |
| 166 } |
129 } | 167 } |
130 } | 168 } |
131 | 169 |
132 void EventHandlerRegistry::updateEventHandlerOfType(ChangeOperation op, const At
omicString& eventType, EventTarget* target) | 170 void EventHandlerRegistry::updateEventHandlerOfType(ChangeOperation op, const At
omicString& eventType, EventTarget* target) |
133 { | 171 { |
134 EventHandlerClass handlerClass; | 172 EventHandlerClass handlerClass; |
135 if (!eventTypeToClass(eventType, &handlerClass)) | 173 if (!eventTypeToClass(eventType, &handlerClass)) |
136 return; | 174 return; |
137 updateEventHandlerInternal(op, handlerClass, target); | 175 updateEventHandlerInternal(op, handlerClass, target); |
138 } | 176 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (scrollingCoordinator) | 231 if (scrollingCoordinator) |
194 scrollingCoordinator->updateHaveScrollEventHandlers(); | 232 scrollingCoordinator->updateHaveScrollEventHandlers(); |
195 break; | 233 break; |
196 default: | 234 default: |
197 ASSERT_NOT_REACHED(); | 235 ASSERT_NOT_REACHED(); |
198 break; | 236 break; |
199 } | 237 } |
200 } | 238 } |
201 | 239 |
202 } // namespace WebCore | 240 } // namespace WebCore |
OLD | NEW |