1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #include "config.h" 32 33 #include "core/page/TouchDisambiguation.h" 34 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" 45 #include <algorithm> 46 #include <cmath> 47 48 namespace WebCore { 49 50 static IntRect boundingBoxForEventNodes(Node* eventNode) 51 { 52 if (!eventNode->document().view()) 53 return IntRect(); 54 55 IntRect result; 56 Node* node = eventNode; 57 while (node) { 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); 61 continue; 62 } 63 result.unite(node->pixelSnappedBoundingBox()); 64 node = NodeTraversal::next(*node, eventNode); 65 } 66 return eventNode->document().view()->contentsToWindow(result); 67 } 68 69 static float scoreTouchTarget(IntPoint touchPoint, int padding, IntRect boundingBox) 70 { 71 if (boundingBox.isEmpty()) 72 return 0; 73 74 float reciprocalPadding = 1.f / padding; 75 float score = 1; 76 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); 80 81 return score; 82 } 83 84 struct TouchTargetData { 85 IntRect windowBoundingBox; 86 float score; 87 }; 88 89 void findGoodTouchTargets(const IntRect& touchBox, LocalFrame* mainFrame, Vector<IntRect>& goodTargets, WillBeHeapVector<RawPtrWillBeMember<Node> >& highlightNodes) 90 { 91 goodTargets.clear(); 92 93 int touchPointPadding = ceil(std::max(touchBox.width(), touchBox.height()) * 0.5); 94 95 IntPoint touchPoint = touchBox.center(); 96 IntPoint contentsPoint = mainFrame->view()->windowToContents(touchPoint); 97 98 HitTestResult result = mainFrame->eventHandler().hitTestResultAtPoint(contentsPoint, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent, IntSize(touchPointPadding, touchPointPadding)); 99 const WillBeHeapListHashSet<RefPtrWillBeMember<Node> >& hitResults = result.rectBasedTestResult(); 100 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 (WillBeHeapListHashSet<RefPtrWillBeMember<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) { 106 // Ignore any Nodes that can't be clicked on. 107 RenderObject* renderer = it->get()->renderer(); 108 if (!renderer || !it->get()->willRespondToMouseClickEvents()) 109 continue; 110 111 // Blacklist all of the Node's containers. 112 for (RenderBlock* container = renderer->containingBlock(); container; container = container->containingBlock()) { 113 Node* containerNode = container->node(); 114 if (!containerNode) 115 continue; 116 if (!blackList.add(containerNode).isNewEntry) 117 break; 118 } 119 } 120 121 WillBeHeapHashMap<RawPtrWillBeMember<Node>, TouchTargetData> touchTargets; 122 float bestScore = 0; 123 for (WillBeHeapListHashSet<RefPtrWillBeMember<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) { 124 for (Node* node = it->get(); node; node = node->parentNode()) { 125 if (blackList.contains(node)) 126 continue; 127 if (node->isDocumentNode() || isHTMLHtmlElement(*node) || isHTMLBodyElement(*node)) 128 break; 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); 134 break; 135 } 136 } 137 } 138 139 for (WillBeHeapHashMap<RawPtrWillBeMember<Node>, TouchTargetData>::iterator it = touchTargets.begin(); it != touchTargets.end(); ++it) { 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 (it->value.score < bestScore * 0.5) 143 continue; 144 goodTargets.append(it->value.windowBoundingBox); 145 highlightNodes.append(it->key); 146 } 147 } 148 149 } // namespace WebCore 150