Home | History | Annotate | Download | only in page
      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 <algorithm>
     36 #include <cmath>
     37 #include "HTMLNames.h"
     38 #include "core/dom/Document.h"
     39 #include "core/dom/Element.h"
     40 #include "core/dom/NodeTraversal.h"
     41 #include "core/html/HTMLHtmlElement.h"
     42 #include "core/page/EventHandler.h"
     43 #include "core/frame/Frame.h"
     44 #include "core/frame/FrameView.h"
     45 #include "core/rendering/HitTestResult.h"
     46 #include "core/rendering/RenderBlock.h"
     47 
     48 using namespace std;
     49 
     50 namespace WebCore {
     51 
     52 static IntRect boundingBoxForEventNodes(Node* eventNode)
     53 {
     54     if (!eventNode->document().view())
     55         return IntRect();
     56 
     57     IntRect result;
     58     Node* node = eventNode;
     59     while (node) {
     60         // Skip the whole sub-tree if the node doesn't propagate events.
     61         if (node != eventNode && node->willRespondToMouseClickEvents()) {
     62             node = NodeTraversal::nextSkippingChildren(*node, eventNode);
     63             continue;
     64         }
     65         result.unite(node->pixelSnappedBoundingBox());
     66         node = NodeTraversal::next(*node, eventNode);
     67     }
     68     return eventNode->document().view()->contentsToWindow(result);
     69 }
     70 
     71 static float scoreTouchTarget(IntPoint touchPoint, int padding, IntRect boundingBox)
     72 {
     73     if (boundingBox.isEmpty())
     74         return 0;
     75 
     76     float reciprocalPadding = 1.f / padding;
     77     float score = 1;
     78 
     79     IntSize distance = boundingBox.differenceToPoint(touchPoint);
     80     score *= max((padding - abs(distance.width())) * reciprocalPadding, 0.f);
     81     score *= max((padding - abs(distance.height())) * reciprocalPadding, 0.f);
     82 
     83     return score;
     84 }
     85 
     86 struct TouchTargetData {
     87     IntRect windowBoundingBox;
     88     float score;
     89 };
     90 
     91 void findGoodTouchTargets(const IntRect& touchBox, Frame* mainFrame, Vector<IntRect>& goodTargets, Vector<Node*>& highlightNodes)
     92 {
     93     goodTargets.clear();
     94 
     95     int touchPointPadding = ceil(max(touchBox.width(), touchBox.height()) * 0.5);
     96 
     97     IntPoint touchPoint = touchBox.center();
     98     IntPoint contentsPoint = mainFrame->view()->windowToContents(touchPoint);
     99 
    100     HitTestResult result = mainFrame->eventHandler().hitTestResultAtPoint(contentsPoint, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::ConfusingAndOftenMisusedDisallowShadowContent, IntSize(touchPointPadding, touchPointPadding));
    101     const ListHashSet<RefPtr<Node> >& hitResults = result.rectBasedTestResult();
    102 
    103     // Blacklist nodes that are container of disambiguated nodes.
    104     // It is not uncommon to have a clickable <div> that contains other clickable objects.
    105     // This heuristic avoids excessive disambiguation in that case.
    106     HashSet<Node*> blackList;
    107     for (ListHashSet<RefPtr<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) {
    108         // Ignore any Nodes that can't be clicked on.
    109         RenderObject* renderer = it->get()->renderer();
    110         if (!renderer || !it->get()->willRespondToMouseClickEvents())
    111             continue;
    112 
    113         // Blacklist all of the Node's containers.
    114         for (RenderBlock* container = renderer->containingBlock(); container; container = container->containingBlock()) {
    115             Node* containerNode = container->node();
    116             if (!containerNode)
    117                 continue;
    118             if (!blackList.add(containerNode).isNewEntry)
    119                 break;
    120         }
    121     }
    122 
    123     HashMap<Node*, TouchTargetData> touchTargets;
    124     float bestScore = 0;
    125     for (ListHashSet<RefPtr<Node> >::const_iterator it = hitResults.begin(); it != hitResults.end(); ++it) {
    126         for (Node* node = it->get(); node; node = node->parentNode()) {
    127             if (blackList.contains(node))
    128                 continue;
    129             if (node->isDocumentNode() || isHTMLHtmlElement(node) || node->hasTagName(HTMLNames::bodyTag))
    130                 break;
    131             if (node->willRespondToMouseClickEvents()) {
    132                 TouchTargetData& targetData = touchTargets.add(node, TouchTargetData()).iterator->value;
    133                 targetData.windowBoundingBox = boundingBoxForEventNodes(node);
    134                 targetData.score = scoreTouchTarget(touchPoint, touchPointPadding, targetData.windowBoundingBox);
    135                 bestScore = max(bestScore, targetData.score);
    136                 break;
    137             }
    138         }
    139     }
    140 
    141     for (HashMap<Node*, TouchTargetData>::iterator it = touchTargets.begin(); it != touchTargets.end(); ++it) {
    142         // Currently the scoring function uses the overlap area with the fat point as the score.
    143         // 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.
    144         if (it->value.score < bestScore * 0.5)
    145             continue;
    146         goodTargets.append(it->value.windowBoundingBox);
    147         highlightNodes.append(it->key);
    148     }
    149 }
    150 
    151 } // namespace WebCore
    152