2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "core/page/TouchDisambiguation.h"
35 #include "core/HTMLNames.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/Element.h"
38 #include "core/dom/NodeTraversal.h"
39 #include "core/frame/FrameView.h"
40 #include "core/frame/LocalFrame.h"
41 #include "core/html/HTMLHtmlElement.h"
42 #include "core/page/EventHandler.h"
43 #include "core/rendering/HitTestResult.h"
44 #include "core/rendering/RenderBlock.h"
50 static IntRect boundingBoxForEventNodes(Node* eventNode)
52 if (!eventNode->document().view())
56 Node* node = eventNode;
58 // Skip the whole sub-tree if the node doesn't propagate events.
59 if (node != eventNode && node->willRespondToMouseClickEvents()) {
60 node = NodeTraversal::nextSkippingChildren(*node, eventNode);
63 result.unite(node->pixelSnappedBoundingBox());
64 node = NodeTraversal::next(*node, eventNode);
66 return eventNode->document().view()->contentsToWindow(result);
69 static float scoreTouchTarget(IntPoint touchPoint, int padding, IntRect boundingBox)
71 if (boundingBox.isEmpty())
74 float reciprocalPadding = 1.f / padding;
77 IntSize distance = boundingBox.differenceToPoint(touchPoint);
78 score *= std::max((padding - abs(distance.width())) * reciprocalPadding, 0.f);
79 score *= std::max((padding - abs(distance.height())) * reciprocalPadding, 0.f);
84 struct TouchTargetData {
85 IntRect windowBoundingBox;
89 void findGoodTouchTargets(const IntRect& touchBox, LocalFrame* mainFrame, Vector<IntRect>& goodTargets, WillBeHeapVector<RawPtrWillBeMember<Node>>& highlightNodes)
93 int touchPointPadding = ceil(std::max(touchBox.width(), touchBox.height()) * 0.5);
95 IntPoint touchPoint = touchBox.center();
96 IntPoint contentsPoint = mainFrame->view()->windowToContents(touchPoint);
98 HitTestResult result = mainFrame->eventHandler().hitTestResultAtPoint(contentsPoint, HitTestRequest::ReadOnly | HitTestRequest::Active, IntSize(touchPointPadding, touchPointPadding));
99 const WillBeHeapListHashSet<RefPtrWillBeMember<Node>>& hitResults = result.rectBasedTestResult();
101 // Blacklist nodes that are container of disambiguated nodes.
102 // It is not uncommon to have a clickable <div> that contains other clickable objects.
103 // This heuristic avoids excessive disambiguation in that case.
104 WillBeHeapHashSet<RawPtrWillBeMember<Node>> blackList;
105 for (const auto& hitResult : hitResults) {
106 // Ignore any Nodes that can't be clicked on.
107 RenderObject* renderer = hitResult.get()->renderer();
108 if (!renderer || !hitResult.get()->willRespondToMouseClickEvents())
111 // Blacklist all of the Node's containers.
112 for (RenderBlock* container = renderer->containingBlock(); container; container = container->containingBlock()) {
113 Node* containerNode = container->node();
116 if (!blackList.add(containerNode).isNewEntry)
121 WillBeHeapHashMap<RawPtrWillBeMember<Node>, TouchTargetData> touchTargets;
123 for (const auto& hitResult : hitResults) {
124 for (Node* node = hitResult.get(); node; node = node->parentNode()) {
125 if (blackList.contains(node))
127 if (node->isDocumentNode() || isHTMLHtmlElement(*node) || isHTMLBodyElement(*node))
129 if (node->willRespondToMouseClickEvents()) {
130 TouchTargetData& targetData = touchTargets.add(node, TouchTargetData()).storedValue->value;
131 targetData.windowBoundingBox = boundingBoxForEventNodes(node);
132 targetData.score = scoreTouchTarget(touchPoint, touchPointPadding, targetData.windowBoundingBox);
133 bestScore = std::max(bestScore, targetData.score);
139 for (const auto& touchTarget : touchTargets) {
140 // Currently the scoring function uses the overlap area with the fat point as the score.
141 // We ignore the candidates that has less than 1/2 overlap (we consider not really ambiguous enough) than the best candidate to avoid excessive popups.
142 if (touchTarget.value.score < bestScore * 0.5)
144 goodTargets.append(touchTarget.value.windowBoundingBox);
145 highlightNodes.append(touchTarget.key);