• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/dom/FullscreenElementStack.h"
26 #include "core/dom/Touch.h"
27 #include "core/dom/TouchList.h"
28 #include "core/dom/TreeScope.h"
29 #include "core/dom/shadow/InsertionPoint.h"
30 #include "core/dom/shadow/ShadowRoot.h"
31 #include "core/events/EventContext.h"
32 #include "core/events/EventPath.h"
33 #include "core/events/FocusEvent.h"
34 #include "core/events/MouseEvent.h"
35 #include "core/events/TouchEvent.h"
36 #include "wtf/PassRefPtr.h"
37 
38 namespace WebCore {
39 
adjustForMouseEvent(Node * node,MouseEvent & mouseEvent)40 void EventRetargeter::adjustForMouseEvent(Node* node, MouseEvent& mouseEvent)
41 {
42     adjustForRelatedTarget(node, mouseEvent.relatedTarget(), mouseEvent.eventPath());
43 }
44 
adjustForFocusEvent(Node * node,FocusEvent & focusEvent)45 void EventRetargeter::adjustForFocusEvent(Node* node, FocusEvent& focusEvent)
46 {
47     adjustForRelatedTarget(node, focusEvent.relatedTarget(), focusEvent.eventPath());
48 }
49 
adjustForTouchEvent(Node * node,TouchEvent & touchEvent)50 void EventRetargeter::adjustForTouchEvent(Node* node, TouchEvent& touchEvent)
51 {
52     EventPath& eventPath = touchEvent.eventPath();
53     size_t eventPathSize = eventPath.size();
54 
55     EventPathTouchLists eventPathTouches(eventPathSize);
56     EventPathTouchLists eventPathTargetTouches(eventPathSize);
57     EventPathTouchLists eventPathChangedTouches(eventPathSize);
58 
59     for (size_t i = 0; i < eventPathSize; ++i) {
60         TouchEventContext* touchEventContext = eventPath[i].ensureTouchEventContext();
61         eventPathTouches[i] = touchEventContext->touches();
62         eventPathTargetTouches[i] = touchEventContext->targetTouches();
63         eventPathChangedTouches[i] = touchEventContext->changedTouches();
64     }
65 
66     adjustTouchList(node, touchEvent.touches(), eventPath, eventPathTouches);
67     adjustTouchList(node, touchEvent.targetTouches(), eventPath, eventPathTargetTouches);
68     adjustTouchList(node, touchEvent.changedTouches(), eventPath, eventPathChangedTouches);
69 
70 #ifndef NDEBUG
71     for (size_t i = 0; i < eventPathSize; ++i) {
72         checkReachability(node, eventPath[i].touchEventContext()->touches());
73         checkReachability(node, eventPath[i].touchEventContext()->targetTouches());
74         checkReachability(node, eventPath[i].touchEventContext()->changedTouches());
75     }
76 #endif
77 }
78 
79 #ifndef NDEBUG
checkReachability(Node * node,TouchList * touchList)80 void EventRetargeter::checkReachability(Node* node, TouchList* touchList)
81 {
82     for (size_t i = 0; i < touchList->length(); ++i)
83         ASSERT(touchList->item(i)->target()->toNode()->treeScope().isInclusiveAncestorOf(node->treeScope()));
84 }
85 #endif
86 
adjustTouchList(const Node * node,const TouchList * touchList,const EventPath & eventPath,EventPathTouchLists & eventPathTouchLists)87 void EventRetargeter::adjustTouchList(const Node* node, const TouchList* touchList, const EventPath& eventPath, EventPathTouchLists& eventPathTouchLists)
88 {
89     if (!touchList)
90         return;
91     size_t eventPathSize = eventPath.size();
92     ASSERT(eventPathTouchLists.size() == eventPathSize);
93     for (size_t i = 0; i < touchList->length(); ++i) {
94         const Touch& touch = *touchList->item(i);
95         AdjustedTargets adjustedNodes;
96         calculateAdjustedNodes(node, touch.target()->toNode(), DoesNotStopAtBoundary, const_cast<EventPath&>(eventPath), adjustedNodes);
97         ASSERT(adjustedNodes.size() == eventPathSize);
98         for (size_t j = 0; j < eventPathSize; ++j)
99             eventPathTouchLists[j]->append(touch.cloneWithNewTarget(adjustedNodes[j].get()));
100     }
101 }
102 
adjustForRelatedTarget(const Node * node,EventTarget * relatedTarget,EventPath & eventPath)103 void EventRetargeter::adjustForRelatedTarget(const Node* node, EventTarget* relatedTarget, EventPath& eventPath)
104 {
105     if (!node)
106         return;
107     if (!relatedTarget)
108         return;
109     Node* relatedNode = relatedTarget->toNode();
110     if (!relatedNode)
111         return;
112     AdjustedTargets adjustedNodes;
113     calculateAdjustedNodes(node, relatedNode, StopAtBoundaryIfNeeded, eventPath, adjustedNodes);
114     ASSERT(adjustedNodes.size() <= eventPath.size());
115     for (size_t i = 0; i < adjustedNodes.size(); ++i) {
116         eventPath[i].setRelatedTarget(adjustedNodes[i]);
117     }
118 }
119 
calculateAdjustedNodes(const Node * node,const Node * relatedNode,EventWithRelatedTargetDispatchBehavior eventWithRelatedTargetDispatchBehavior,EventPath & eventPath,AdjustedTargets & adjustedTargets)120 void EventRetargeter::calculateAdjustedNodes(const Node* node, const Node* relatedNode, EventWithRelatedTargetDispatchBehavior eventWithRelatedTargetDispatchBehavior, EventPath& eventPath, AdjustedTargets& adjustedTargets)
121 {
122     RelatedTargetMap relatedNodeMap;
123     buildRelatedNodeMap(relatedNode, relatedNodeMap);
124 
125     // Synthetic mouse events can have a relatedTarget which is identical to the target.
126     bool targetIsIdenticalToToRelatedTarget = (node == relatedNode);
127 
128     TreeScope* lastTreeScope = 0;
129     EventTarget* adjustedTarget = 0;
130 
131     for (size_t i = 0; i < eventPath.size(); ++i) {
132         TreeScope* scope = &eventPath[i].node()->treeScope();
133         if (scope == lastTreeScope) {
134             // Re-use the previous adjustedRelatedTarget if treeScope does not change. Just for the performance optimization.
135             adjustedTargets.append(adjustedTarget);
136         } else {
137             adjustedTarget = findRelatedNode(scope, relatedNodeMap);
138             adjustedTargets.append(adjustedTarget);
139         }
140         lastTreeScope = scope;
141         if (eventWithRelatedTargetDispatchBehavior == DoesNotStopAtBoundary)
142             continue;
143         if (targetIsIdenticalToToRelatedTarget) {
144             if (node->treeScope().rootNode() == eventPath[i].node()) {
145                 eventPath.shrink(i + 1);
146                 break;
147             }
148         } else if (eventPath[i].target() == adjustedTarget) {
149             // Event dispatching should be stopped here.
150             eventPath.shrink(i);
151             adjustedTargets.shrink(adjustedTargets.size() - 1);
152             break;
153         }
154     }
155 }
156 
buildRelatedNodeMap(const Node * relatedNode,RelatedTargetMap & relatedTargetMap)157 void EventRetargeter::buildRelatedNodeMap(const Node* relatedNode, RelatedTargetMap& relatedTargetMap)
158 {
159     TreeScope* lastTreeScope = 0;
160     EventPath eventPath(const_cast<Node*>(relatedNode));
161     for (size_t i = 0; i < eventPath.size(); ++i) {
162         TreeScope* treeScope = &eventPath[i].node()->treeScope();
163         if (treeScope != lastTreeScope)
164             relatedTargetMap.add(treeScope, eventPath[i].target());
165         lastTreeScope = treeScope;
166     }
167 }
168 
findRelatedNode(TreeScope * scope,RelatedTargetMap & relatedTargetMap)169 EventTarget* EventRetargeter::findRelatedNode(TreeScope* scope, RelatedTargetMap& relatedTargetMap)
170 {
171     Vector<TreeScope*, 32> parentTreeScopes;
172     EventTarget* relatedNode = 0;
173     while (scope) {
174         parentTreeScopes.append(scope);
175         RelatedTargetMap::const_iterator found = relatedTargetMap.find(scope);
176         if (found != relatedTargetMap.end()) {
177             relatedNode = found->value;
178             break;
179         }
180         scope = scope->parentTreeScope();
181     }
182     for (Vector<TreeScope*, 32>::iterator iter = parentTreeScopes.begin(); iter < parentTreeScopes.end(); ++iter)
183         relatedTargetMap.add(*iter, relatedNode);
184     return relatedNode;
185 }
186 
187 }
188