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

Side by Side Diff: Source/core/events/EventRetargeter.cpp

Issue 26558004: Revert 158219 "Move dom/*Event* to events to match the DOM vs. E..." (Closed) Base URL: svn://svn.chromium.org/blink/branches/chromium/1651/
Patch Set: Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/events/EventRetargeter.h ('k') | Source/core/events/EventSender.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "core/events/EventRetargeter.h"
22
23 #include "RuntimeEnabledFeatures.h"
24 #include "core/dom/ContainerNode.h"
25 #include "core/events/EventContext.h"
26 #include "core/events/EventPathWalker.h"
27 #include "core/events/FocusEvent.h"
28 #include "core/dom/FullscreenElementStack.h"
29 #include "core/events/MouseEvent.h"
30 #include "core/dom/Touch.h"
31 #include "core/events/TouchEvent.h"
32 #include "core/dom/TouchList.h"
33 #include "core/dom/TreeScope.h"
34 #include "core/dom/shadow/ShadowRoot.h"
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/Vector.h"
38
39 namespace WebCore {
40
41 static inline bool inTheSameScope(ShadowRoot* shadowRoot, EventTarget* target)
42 {
43 return target->toNode() && target->toNode()->treeScope().rootNode() == shado wRoot;
44 }
45
46 static inline EventDispatchBehavior determineDispatchBehavior(Event* event, Shad owRoot* shadowRoot, EventTarget* target)
47 {
48 // Video-only full screen is a mode where we use the shadow DOM as an implem entation
49 // detail that should not be detectable by the web content.
50 if (Element* element = FullscreenElementStack::currentFullScreenElementFrom( &target->toNode()->document())) {
51 // FIXME: We assume that if the full screen element is a media element t hat it's
52 // the video-only full screen. Both here and elsewhere. But that is prob ably wrong.
53 if (element->isMediaElement() && shadowRoot && shadowRoot->host() == ele ment)
54 return StayInsideShadowDOM;
55 }
56
57 // WebKit never allowed selectstart event to cross the the shadow DOM bounda ry.
58 // Changing this breaks existing sites.
59 // See https://bugs.webkit.org/show_bug.cgi?id=52195 for details.
60 const AtomicString eventType = event->type();
61 if (inTheSameScope(shadowRoot, target)
62 && (eventType == eventNames().abortEvent
63 || eventType == eventNames().changeEvent
64 || eventType == eventNames().errorEvent
65 || eventType == eventNames().loadEvent
66 || eventType == eventNames().resetEvent
67 || eventType == eventNames().resizeEvent
68 || eventType == eventNames().scrollEvent
69 || eventType == eventNames().selectEvent
70 || eventType == eventNames().selectstartEvent))
71 return StayInsideShadowDOM;
72
73 return RetargetEvent;
74 }
75
76 void EventRetargeter::ensureEventPath(Node* node, Event* event)
77 {
78 calculateEventPath(node, event);
79 calculateAdjustedEventPathForEachNode(event->eventPath());
80 }
81
82 void EventRetargeter::calculateEventPath(Node* node, Event* event)
83 {
84 EventPath& eventPath = event->eventPath();
85 eventPath.clear();
86 bool inDocument = node->inDocument();
87 bool isSVGElement = node->isSVGElement();
88 bool isMouseOrFocusEvent = event->isMouseEvent() || event->isFocusEvent();
89 bool isTouchEvent = event->isTouchEvent();
90 Vector<EventTarget*, 32> targetStack;
91 for (EventPathWalker walker(node); walker.node(); walker.moveToParent()) {
92 Node* node = walker.node();
93 if (targetStack.isEmpty())
94 targetStack.append(eventTargetRespectingTargetRules(node));
95 else if (walker.isVisitingInsertionPointInReprojection())
96 targetStack.append(targetStack.last());
97 if (isMouseOrFocusEvent)
98 eventPath.append(adoptPtr(new MouseOrFocusEventContext(node, eventTa rgetRespectingTargetRules(node), targetStack.last())));
99 else if (isTouchEvent)
100 eventPath.append(adoptPtr(new TouchEventContext(node, eventTargetRes pectingTargetRules(node), targetStack.last())));
101 else
102 eventPath.append(adoptPtr(new EventContext(node, eventTargetRespecti ngTargetRules(node), targetStack.last())));
103 if (!inDocument)
104 break;
105 if (!node->isShadowRoot())
106 continue;
107 if (determineDispatchBehavior(event, toShadowRoot(node), targetStack.las t()) == StayInsideShadowDOM)
108 break;
109 if (!isSVGElement) {
110 ASSERT(!targetStack.isEmpty());
111 targetStack.removeLast();
112 }
113 }
114 }
115
116 void EventRetargeter::calculateAdjustedEventPathForEachNode(EventPath& eventPath )
117 {
118 if (!RuntimeEnabledFeatures::shadowDOMEnabled())
119 return;
120 TreeScope* lastScope = 0;
121 size_t eventPathSize = eventPath.size();
122 for (size_t i = 0; i < eventPathSize; ++i) {
123 TreeScope* currentScope = &eventPath[i]->node()->treeScope();
124 if (currentScope == lastScope) {
125 // Fast path.
126 eventPath[i]->setEventPath(eventPath[i - 1]->eventPath());
127 continue;
128 }
129 lastScope = currentScope;
130 Vector<RefPtr<Node> > nodes;
131 for (size_t j = 0; j < eventPathSize; ++j) {
132 Node* node = eventPath[j]->node();
133 if (node->treeScope().isInclusiveAncestorOf(*currentScope))
134 nodes.append(node);
135 }
136 eventPath[i]->adoptEventPath(nodes);
137 }
138 }
139
140 void EventRetargeter::adjustForMouseEvent(Node* node, MouseEvent& mouseEvent)
141 {
142 adjustForRelatedTarget(node, mouseEvent.relatedTarget(), mouseEvent.eventPat h());
143 }
144
145 void EventRetargeter::adjustForFocusEvent(Node* node, FocusEvent& focusEvent)
146 {
147 adjustForRelatedTarget(node, focusEvent.relatedTarget(), focusEvent.eventPat h());
148 }
149
150 void EventRetargeter::adjustForTouchEvent(Node* node, TouchEvent& touchEvent)
151 {
152 EventPath& eventPath = touchEvent.eventPath();
153 size_t eventPathSize = eventPath.size();
154
155 EventPathTouchLists eventPathTouches(eventPathSize);
156 EventPathTouchLists eventPathTargetTouches(eventPathSize);
157 EventPathTouchLists eventPathChangedTouches(eventPathSize);
158
159 for (size_t i = 0; i < eventPathSize; ++i) {
160 ASSERT(eventPath[i]->isTouchEventContext());
161 TouchEventContext* touchEventContext = toTouchEventContext(eventPath[i]. get());
162 eventPathTouches[i] = touchEventContext->touches();
163 eventPathTargetTouches[i] = touchEventContext->targetTouches();
164 eventPathChangedTouches[i] = touchEventContext->changedTouches();
165 }
166
167 adjustTouchList(node, touchEvent.touches(), eventPath, eventPathTouches);
168 adjustTouchList(node, touchEvent.targetTouches(), eventPath, eventPathTarget Touches);
169 adjustTouchList(node, touchEvent.changedTouches(), eventPath, eventPathChang edTouches);
170 }
171
172 void EventRetargeter::adjustTouchList(const Node* node, const TouchList* touchLi st, const EventPath& eventPath, EventPathTouchLists& eventPathTouchLists)
173 {
174 if (!touchList)
175 return;
176 size_t eventPathSize = eventPath.size();
177 ASSERT(eventPathTouchLists.size() == eventPathSize);
178 for (size_t i = 0; i < touchList->length(); ++i) {
179 const Touch& touch = *touchList->item(i);
180 AdjustedNodes adjustedNodes;
181 calculateAdjustedNodes(node, touch.target()->toNode(), DoesNotStopAtBoun dary, const_cast<EventPath&>(eventPath), adjustedNodes);
182 ASSERT(adjustedNodes.size() == eventPathSize);
183 for (size_t j = 0; j < eventPathSize; ++j)
184 eventPathTouchLists[j]->append(touch.cloneWithNewTarget(adjustedNode s[j].get()));
185 }
186 }
187
188 void EventRetargeter::adjustForRelatedTarget(const Node* node, EventTarget* rela tedTarget, EventPath& eventPath)
189 {
190 if (!node)
191 return;
192 if (!relatedTarget)
193 return;
194 Node* relatedNode = relatedTarget->toNode();
195 if (!relatedNode)
196 return;
197 AdjustedNodes adjustedNodes;
198 calculateAdjustedNodes(node, relatedNode, StopAtBoundaryIfNeeded, eventPath, adjustedNodes);
199 ASSERT(adjustedNodes.size() <= eventPath.size());
200 for (size_t i = 0; i < adjustedNodes.size(); ++i) {
201 ASSERT(eventPath[i]->isMouseOrFocusEventContext());
202 MouseOrFocusEventContext* mouseOrFocusEventContext = static_cast<MouseOr FocusEventContext*>(eventPath[i].get());
203 mouseOrFocusEventContext->setRelatedTarget(adjustedNodes[i]);
204 }
205 }
206
207 void EventRetargeter::calculateAdjustedNodes(const Node* node, const Node* relat edNode, EventWithRelatedTargetDispatchBehavior eventWithRelatedTargetDispatchBeh avior, EventPath& eventPath, AdjustedNodes& adjustedNodes)
208 {
209 RelatedNodeMap relatedNodeMap;
210 buildRelatedNodeMap(relatedNode, relatedNodeMap);
211
212 // Synthetic mouse events can have a relatedTarget which is identical to the target.
213 bool targetIsIdenticalToToRelatedTarget = (node == relatedNode);
214
215 TreeScope* lastTreeScope = 0;
216 Node* adjustedNode = 0;
217 for (EventPath::const_iterator iter = eventPath.begin(); iter < eventPath.en d(); ++iter) {
218 TreeScope* scope = &(*iter)->node()->treeScope();
219 if (scope == lastTreeScope) {
220 // Re-use the previous adjustedRelatedTarget if treeScope does not c hange. Just for the performance optimization.
221 adjustedNodes.append(adjustedNode);
222 } else {
223 adjustedNode = findRelatedNode(scope, relatedNodeMap);
224 adjustedNodes.append(adjustedNode);
225 }
226 lastTreeScope = scope;
227 if (eventWithRelatedTargetDispatchBehavior == DoesNotStopAtBoundary)
228 continue;
229 if (targetIsIdenticalToToRelatedTarget) {
230 if (node->treeScope().rootNode() == (*iter)->node()) {
231 eventPath.shrink(iter + 1 - eventPath.begin());
232 break;
233 }
234 } else if ((*iter)->target() == adjustedNode) {
235 // Event dispatching should be stopped here.
236 eventPath.shrink(iter - eventPath.begin());
237 adjustedNodes.shrink(adjustedNodes.size() - 1);
238 break;
239 }
240 }
241 }
242
243 void EventRetargeter::buildRelatedNodeMap(const Node* relatedNode, RelatedNodeMa p& relatedNodeMap)
244 {
245 Vector<Node*, 32> relatedNodeStack;
246 TreeScope* lastTreeScope = 0;
247 for (EventPathWalker walker(relatedNode); walker.node(); walker.moveToParent ()) {
248 Node* node = walker.node();
249 if (relatedNodeStack.isEmpty())
250 relatedNodeStack.append(node);
251 else if (walker.isVisitingInsertionPointInReprojection())
252 relatedNodeStack.append(relatedNodeStack.last());
253 TreeScope* scope = &node->treeScope();
254 // Skips adding a node to the map if treeScope does not change. Just for the performance optimization.
255 if (scope != lastTreeScope)
256 relatedNodeMap.add(scope, relatedNodeStack.last());
257 lastTreeScope = scope;
258 if (node->isShadowRoot()) {
259 ASSERT(!relatedNodeStack.isEmpty());
260 relatedNodeStack.removeLast();
261 }
262 }
263 }
264
265 Node* EventRetargeter::findRelatedNode(TreeScope* scope, RelatedNodeMap& related NodeMap)
266 {
267 Vector<TreeScope*, 32> parentTreeScopes;
268 Node* relatedNode = 0;
269 while (scope) {
270 parentTreeScopes.append(scope);
271 RelatedNodeMap::const_iterator found = relatedNodeMap.find(scope);
272 if (found != relatedNodeMap.end()) {
273 relatedNode = found->value;
274 break;
275 }
276 scope = scope->parentTreeScope();
277 }
278 for (Vector<TreeScope*, 32>::iterator iter = parentTreeScopes.begin(); iter < parentTreeScopes.end(); ++iter)
279 relatedNodeMap.add(*iter, relatedNode);
280 return relatedNode;
281 }
282
283 }
OLDNEW
« no previous file with comments | « Source/core/events/EventRetargeter.h ('k') | Source/core/events/EventSender.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698