EmbedTouchListener.cpp 14.7 KB
Newer Older
Tatiana Meshkova's avatar
Tatiana Meshkova committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "EmbedTouchListener.h"

#include "nsServiceManagerUtils.h"
#include "nsIObserverService.h"
#include "mozilla/embedlite/EmbedLog.h"

#include "nsStringGlue.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDOMWindow.h"
16 17 18 19
#include "nsIDOMDocument.h"
#include "nsIDOMElement.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsIDocument.h"
Tatiana Meshkova's avatar
Tatiana Meshkova committed
20 21 22 23 24 25 26 27 28 29 30 31
#include "nsIDOMEventTarget.h"
#include "nsIDOMEvent.h"
#include "nsPIDOMWindow.h"
#include "nsIEmbedLiteJSON.h"
#include "nsComponentManagerUtils.h"
#include "nsIVariant.h"
#include "nsHashPropertyBag.h"
#include "nsIDOMWindowUtils.h"
#include "nsIDOMHTMLLinkElement.h"
#include "nsIFocusManager.h"
#include "nsIDocShellTreeItem.h"
#include "nsIWebNavigation.h"
32 33 34 35 36 37 38 39 40 41
#include "nsIDOMHTMLIFrameElement.h"
#include "nsIDOMHTMLFrameElement.h"
#include "nsIDOMClientRect.h"
#include "nsIDOMHTMLLIElement.h"
#include "nsIDOMCSSStyleDeclaration.h"
#include "nsIWidget.h"
#include "nsIBaseWindow.h"
#include "nsIDOMHTMLTextAreaElement.h"
#include "nsIDOMHTMLBodyElement.h"
#include "nsIDOMHTMLInputElement.h"
42 43 44
#include "nsIDOMHTMLAnchorElement.h"
#include "nsIDOMHTMLAreaElement.h"
#include "nsIDOMHTMLImageElement.h"
45 46

using namespace mozilla;
Tatiana Meshkova's avatar
Tatiana Meshkova committed
47 48

EmbedTouchListener::EmbedTouchListener(nsIDOMWindow* aWin)
49 50
  : DOMWindow(aWin)
  , mGotViewPortUpdate(false)
Tatiana Meshkova's avatar
Tatiana Meshkova committed
51 52 53 54
{
    if (!mService) {
        mService = do_GetService("@mozilla.org/embedlite-app-service;1");
    }
55
    mService->GetIDByWindow(aWin, &mTopWinid);
Tatiana Meshkova's avatar
Tatiana Meshkova committed
56 57 58 59 60 61
}

EmbedTouchListener::~EmbedTouchListener()
{
}

62
NS_IMPL_ISUPPORTS(EmbedTouchListener, nsIDOMEventListener)
Tatiana Meshkova's avatar
Tatiana Meshkova committed
63

64
void EmbedTouchListener::HandleSingleTap(const CSSPoint& aPoint, int32_t, const mozilla::layers::ScrollableLayerGuid&)
65
{
66
    // SingleTap handler of JavaScript (embedhelper.js) is taken care of input zooming.
67 68
}

69
void EmbedTouchListener::HandleLongTap(const CSSPoint& aPoint, int32_t, const mozilla::layers::ScrollableLayerGuid&, uint64_t)
70
{
71
    LOGT("pt[%f,%f]", aPoint.x, aPoint.y);
72 73
}

74
void EmbedTouchListener::HandleLongTapUp(const CSSPoint& aPoint, int32_t, const mozilla::layers::ScrollableLayerGuid&)
75
{
76
    LOGT("pt[%f,%f]", aPoint.x, aPoint.y);
77 78
}

79
void EmbedTouchListener::SendAsyncScrollDOMEvent(bool aIsRoot,
80
                                                 const mozilla::CSSRect& aRect,
81
                                                 const mozilla::CSSSize& aSize)
82 83 84
{
    // LOGT("r[%g,%g,%g,%g], size[%g,%g]", aRect.x, aRect.y, aRect.width, aRect.height, aSize.width, aSize.height);
}
Tatiana Meshkova's avatar
Tatiana Meshkova committed
85 86 87 88 89 90 91 92 93 94 95 96

NS_IMETHODIMP
EmbedTouchListener::HandleEvent(nsIDOMEvent* aEvent)
{
    nsString type;
    if (aEvent) {
        aEvent->GetType(type);
    }
    LOGT("Event:'%s'", NS_ConvertUTF16toUTF8(type).get());

    return NS_OK;
}
97 98 99

void EmbedTouchListener::RequestContentRepaint(const mozilla::layers::FrameMetrics& aMetrics)
{
Tatiana Meshkova's avatar
Tatiana Meshkova committed
100 101
//    LOGT("Metr off[%g,%g], vp[%g,%g,%g,%g], scrRe[%g,%g,%g,%g], res[%g,%g]",
//         aMetrics.mScrollOffset.x, aMetrics.mScrollOffset.y,
102
//         aMetrics.GetViewport().x, aMetrics.GetViewport().y, aMetrics.GetViewport().width, aMetrics.GetViewport().height,
Tatiana Meshkova's avatar
Tatiana Meshkova committed
103 104
//         aMetrics.mScrollableRect.x, aMetrics.mScrollableRect.y, aMetrics.mScrollableRect.width, aMetrics.mScrollableRect.height,
//         aMetrics.mResolution.width, aMetrics.mResolution.height);
105
    mGotViewPortUpdate = true;
106
    mViewport = gfx::Rect(aMetrics.GetScrollOffset().x, aMetrics.GetScrollOffset().y,
107
                          aMetrics.GetViewport().width, aMetrics.GetViewport().height);
108
    mCssCompositedRect = gfx::Rect(aMetrics.GetScrollOffset().x, aMetrics.GetScrollOffset().y,
109 110 111 112
                                   0, 0);
    float x, y;
    mService->GetCompositedRectInCSS(aMetrics, &x, &y, &mCssCompositedRect.width, &mCssCompositedRect.height);

113 114
    mCssPageRect = gfx::Rect(aMetrics.GetScrollableRect().x, aMetrics.GetScrollableRect().y,
                             aMetrics.GetScrollableRect().width, aMetrics.GetScrollableRect().height);
115

116 117 118
//    LOGT("EmbedTouchListener::RequestContentRepaint mCssPageRect %g %g %g %g", mCssPageRect.x, mCssPageRect.y, mCssPageRect.width, mCssPageRect.height);
//    LOGT("EmbedTouchListener::RequestContentRepaint Viewport %g %g %g %g", mViewport.x, mViewport.y, mViewport.width, mViewport.height);
//    LOGT("EmbedTouchListener::RequestContentRepaint mCssCompositedRect %g %g %g %g", mCssCompositedRect.x, mCssCompositedRect.y, mCssCompositedRect.width, mCssCompositedRect.height);
119 120
}

121
void EmbedTouchListener::HandleDoubleTap(const CSSPoint& aPoint, int32_t, const mozilla::layers::ScrollableLayerGuid&)
122
{
123
    LOGT("pt[%f,%f]", aPoint.x, aPoint.y);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    // We haven't received a metrics update yet; don't do anything.
    if (!mGotViewPortUpdate) {
        return;
    }

    nsCOMPtr<nsIDOMElement> element;
    gfxRect retRect(0,0,0,0);
    AnyElementFromPoint(DOMWindow, aPoint.x, aPoint.y, getter_AddRefs(element));
    if (!element) {
        mService->ZoomToRect(mTopWinid, 0, 0, 0, 0);
        return;
    }

    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(element);
    NS_ENSURE_TRUE(node, );
    nsCOMPtr<nsIDOMElement> elementtest = element;
    while (elementtest && !ShouldZoomToElement(elementtest)) {
        node->GetParentNode(getter_AddRefs(node));
        elementtest = do_QueryInterface(node);
        if (elementtest) {
            element = elementtest;
        }
    }
147

148 149 150 151 152 153 154 155 156
    // Don't zoom in or out when in full screen
    nsCOMPtr<nsIDOMDocument> document;
    NS_ENSURE_SUCCESS(node->GetOwnerDocument(getter_AddRefs(document)), );
    bool isFullScreen(false);
    document->GetMozFullScreen(&isFullScreen);
    if (isFullScreen) {
        return;
    }

157 158 159 160 161 162 163 164 165 166
    if (!element) {
        mService->ZoomToRect(mTopWinid, 0, 0, 0, 0);
    } else {
        ZoomToElement(element, aPoint.y, true, true);
    }
}

void
EmbedTouchListener::AnyElementFromPoint(nsIDOMWindow* aWindow, double aX, double aY, nsIDOMElement* *aElem)
{
167
    mService->EnterSecureJSContext();
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    nsCOMPtr<nsIDOMWindowUtils> utils = do_GetInterface(aWindow);
    nsCOMPtr<nsIDOMElement> elem;
    NS_ENSURE_SUCCESS(utils->ElementFromPoint(aX, aY, true, true, getter_AddRefs(elem)), );

    nsCOMPtr<nsIDOMHTMLIFrameElement> elAsIFrame = do_QueryInterface(elem);
    nsCOMPtr<nsIDOMHTMLFrameElement> elAsFrame = do_QueryInterface(elem);
    while (elem && (elAsIFrame || elAsFrame)) {
        nsCOMPtr<nsIDOMClientRect> rect;
        elem->GetBoundingClientRect(getter_AddRefs(rect));
        float left, top;
        rect->GetLeft(&left);
        rect->GetTop(&top);
        aX -= left;
        aY -= top;
        nsCOMPtr<nsIDOMDocument> contentDocument;
        if (!elAsIFrame || NS_FAILED(elAsIFrame->GetContentDocument(getter_AddRefs(contentDocument)))) {
            if (!elAsFrame || NS_FAILED(elAsFrame->GetContentDocument(getter_AddRefs(contentDocument)))) {
                break;
            }
        }
        nsCOMPtr<nsIDOMWindow> newWin;
        contentDocument->GetDefaultView(getter_AddRefs(newWin));
        utils = do_GetInterface(newWin);
        if (NS_FAILED(utils->ElementFromPoint(aX, aY, true, true, getter_AddRefs(elem)))) {
            elem = nullptr;
        } else {
            elAsIFrame = do_QueryInterface(elem);
            elAsFrame = do_QueryInterface(elem);
        }
    }
    if (elem) {
        NS_ADDREF(*aElem = elem);
    }
201
    mService->LeaveSecureJSContext();
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

    return;
}

bool
EmbedTouchListener::ShouldZoomToElement(nsIDOMElement* aElement)
{
    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
    NS_ENSURE_TRUE(node, false);

    nsCOMPtr<nsIDOMDocument> document;
    NS_ENSURE_SUCCESS(node->GetOwnerDocument(getter_AddRefs(document)), false);

    nsCOMPtr<nsIDOMWindow> win;
    NS_ENSURE_SUCCESS(document->GetDefaultView(getter_AddRefs(win)), false);

    nsCOMPtr<nsIDOMCSSStyleDeclaration> bW;
    NS_ENSURE_SUCCESS(win->GetComputedStyle(aElement, nsString(), getter_AddRefs(bW)), false);

    nsString display;
    if (NS_SUCCEEDED(bW->GetPropertyValue(NS_LITERAL_STRING("display"), display))) {
        if (display.EqualsLiteral("inline")) {
            return false;
        }
    }
    nsCOMPtr<nsIDOMHTMLLIElement> liel = do_QueryInterface(aElement);
    nsCOMPtr<nsIDOMHTMLLIElement> qoteel = do_QueryInterface(aElement);
    if (liel || qoteel)
        return false;

    return true;
}

/* Zoom to an element, optionally keeping a particular part of it
 * in view if it is really tall.
 */
void
EmbedTouchListener::ZoomToElement(nsIDOMElement* aElement, int aClickY, bool aCanZoomOut, bool aCanZoomIn)
{
    const int margin = 15;
    gfx::Rect clrect = GetBoundingContentRect(aElement);
243 244 245 246
    clrect.x = std::max(float(0.0), clrect.x - margin);
    clrect.y = std::max(float(0.0), clrect.y - margin);
    clrect.width = std::min(clrect.width + 2*margin, mCssCompositedRect.width);
    clrect.height = std::min(clrect.height + 2*margin, mCssCompositedRect.height);
247 248 249 250
    float elementAspectRatio = clrect.width / clrect.height;
    float viewportAspectRatio = mCssCompositedRect.width / mCssCompositedRect.height;
    bool zoomed = false;
    if (IsRectZoomedIn(clrect, mCssCompositedRect)) {
251 252 253 254 255
        if (aCanZoomOut) {
            mService->ZoomToRect(mTopWinid, 0, 0, 0, 0);
        }
        return;
    }
256 257 258 259 260 261
    if (elementAspectRatio > viewportAspectRatio) {
        if ((clrect.width < mCssCompositedRect.width && aCanZoomIn) ||
            (clrect.width > mCssCompositedRect.width && aCanZoomOut) ) {
            mService->ZoomToRect(mTopWinid, clrect.x, clrect.y, clrect.width, clrect.height);
            zoomed = true;
        }
262
    }
263
    else if (elementAspectRatio < viewportAspectRatio && viewportAspectRatio < 1) {
264 265 266 267
        if ((clrect.height < mCssCompositedRect.height && aCanZoomIn) ||
            (clrect.height > mCssCompositedRect.height && aCanZoomOut) ) {
            mService->ZoomToRect(mTopWinid, clrect.x, clrect.y, clrect.width, clrect.height);
            zoomed = true;
268 269
        }
    }
270

271 272 273
    if (!zoomed) {
        mService->ZoomToRect(mTopWinid, clrect.x, clrect.y, mCssCompositedRect.width, mCssCompositedRect.height);
    }
274 275
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289
static bool HasFrameElement(nsIDOMDocument* aDocument, nsIDOMElement* *aFrameElement = nullptr)
{
    if (!aDocument) {
        return false;
    }
    nsCOMPtr<nsIDOMWindow> newWin;
    if (NS_FAILED(aDocument->GetDefaultView(getter_AddRefs(newWin))) || !newWin) {
        return false;
    }
    nsCOMPtr<nsIDOMElement> frameElement;
    if (NS_FAILED(newWin->GetFrameElement(getter_AddRefs(frameElement))) || !frameElement) {
        return false;
    }
    if (aFrameElement) {
290
        *aFrameElement = frameElement.forget().take();
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    }
    return true;
}

static void GetDefViewFrameElemOwnerDocument(nsIDOMDocument* aDocument, nsIDOMDocument* *aOutDocument)
{
    nsCOMPtr<nsIDOMElement> frameElement;
    if (HasFrameElement(aDocument, getter_AddRefs(frameElement)) && frameElement) {
        nsCOMPtr<nsIDOMNode> node = do_QueryInterface(frameElement);
        if (node) {
            node->GetOwnerDocument(aOutDocument);
        }
    }
}

static bool _HasFrameElement(nsIDOMWindow* aWindow)
{
    if (!aWindow) {
        return false;
    }
    nsCOMPtr<nsIDOMElement> frameElement;
    if (NS_FAILED(aWindow->GetFrameElement(getter_AddRefs(frameElement))) || !frameElement) {
        return false;
    }
    return true;
}

static void GetParentFrame(nsIDOMWindow* aWindow, nsIDOMWindow** outWindow)
{
    nsCOMPtr<nsIDOMWindow> newWin;
    if (aWindow) {
      aWindow->GetParent(getter_AddRefs(newWin));
323
      *outWindow = newWin.forget().take();
324 325 326
    }
}

327 328 329 330 331 332 333 334 335
gfx::Rect
EmbedTouchListener::GetBoundingContentRect(nsIDOMElement* aElement)
{
    gfx::Rect retRect(0, 0, 0, 0);
    if (!aElement)
      return retRect;

    nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
    NS_ENSURE_TRUE(node, retRect);
336

337
    nsCOMPtr<nsIDOMDocument> origDocument;
338 339
    nsCOMPtr<nsIDOMDocument> document;
    NS_ENSURE_SUCCESS(node->GetOwnerDocument(getter_AddRefs(document)), retRect);
340 341 342 343 344 345 346
    origDocument = document;
    while (HasFrameElement(document)) {
        nsCOMPtr<nsIDOMDocument> newDocument;
        GetDefViewFrameElemOwnerDocument(document, getter_AddRefs(newDocument));
        if (newDocument)
            document = newDocument;
        else
347
            break;
348 349 350 351 352
    }

    nsCOMPtr<nsIDOMWindow> newWin;
    if (NS_FAILED(document->GetDefaultView(getter_AddRefs(newWin))) || !newWin) {
        return retRect;
353 354
    }

355
    mService->EnterSecureJSContext();
356 357 358 359 360 361
    nsCOMPtr<nsIDOMWindowUtils> utils = do_GetInterface(newWin);
    int32_t scrollX = 0, scrollY = 0;
    NS_ENSURE_SUCCESS(utils->GetScrollXY(false, &scrollX, &scrollY), retRect);
    nsCOMPtr<nsIDOMClientRect> r;
    aElement->GetBoundingClientRect(getter_AddRefs(r));

362 363 364 365 366
    nsCOMPtr<nsIDOMWindow> defView;
    origDocument->GetDefaultView(getter_AddRefs(defView));
    for (nsCOMPtr<nsIDOMWindow> frame = defView; _HasFrameElement(frame) && frame != DOMWindow; GetParentFrame(frame, getter_AddRefs(frame))) {
        nsCOMPtr<nsIDOMElement> frElement;
        frame->GetFrameElement(getter_AddRefs(frElement));
367
        nsCOMPtr<nsIDOMClientRect> gr;
368
        frElement->GetBoundingClientRect(getter_AddRefs(gr));
369 370 371 372
        float grleft, grtop;
        gr->GetLeft(&grleft);
        gr->GetTop(&grtop);
        nsCOMPtr<nsIDOMCSSStyleDeclaration> bW;
373
        frame->GetComputedStyle(frElement, NS_LITERAL_STRING(""), getter_AddRefs(bW));
374 375 376 377 378 379 380 381 382 383 384 385
        nsString blw, btw;
        bW->GetPropertyValue(NS_LITERAL_STRING("border-left-width"), blw);
        bW->GetPropertyValue(NS_LITERAL_STRING("border-top-width"), btw);
        scrollX += grleft + atoi(NS_ConvertUTF16toUTF8(blw).get());
        scrollY += grtop + atoi(NS_ConvertUTF16toUTF8(btw).get());
    }

    float rleft = 0, rtop = 0, rwidth = 0, rheight = 0;
    r->GetLeft(&rleft);
    r->GetTop(&rtop);
    r->GetWidth(&rwidth);
    r->GetHeight(&rheight);
386
    mService->LeaveSecureJSContext();
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

    return gfx::Rect(rleft + scrollX,
                     rtop + scrollY,
                     rwidth, rheight);
}

bool
EmbedTouchListener::IsRectZoomedIn(gfx::Rect aRect, gfx::Rect aViewport)
{
    // This function checks to see if the area of the rect visible in the
    // viewport (i.e. the "overlapArea" variable below) is approximately 
    // the max area of the rect we can show.
    gfx::Rect vRect(aViewport);
    gfx::Rect overlap = vRect.Intersect(aRect);
    float overlapArea = overlap.width * overlap.height;
    float availHeight = std::min(aRect.width * vRect.height / vRect.width, aRect.height);
    float showing = overlapArea / (aRect.width * availHeight);
    float ratioW = (aRect.width / vRect.width);
    float ratioH = (aRect.height / vRect.height);

    return (showing > 0.9 && (ratioW > 0.9 || ratioH > 0.9)); 
}

410
void EmbedTouchListener::ScrollUpdate(const mozilla::CSSPoint&, float)
Tatiana Meshkova's avatar
Tatiana Meshkova committed
411 412
{
}