layout/ipc/test-ipcbrowser-chrome.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/ipc/test-ipcbrowser-chrome.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function init() {
     1.9 +    enableAsyncScrolling();
    1.10 +    messageManager.loadFrameScript(
    1.11 +        "chrome://global/content/test-ipcbrowser-content.js", true
    1.12 +    );
    1.13 +}
    1.14 +
    1.15 +function browser() {
    1.16 +    return document.getElementById("content");
    1.17 +}
    1.18 +
    1.19 +function frameLoader() {
    1.20 +    return browser().QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader;
    1.21 +}
    1.22 +
    1.23 +function viewManager() {
    1.24 +    return frameLoader().QueryInterface(Components.interfaces.nsIContentViewManager);
    1.25 +}
    1.26 +
    1.27 +function rootView() {
    1.28 +    return viewManager().rootContentView;
    1.29 +}
    1.30 +
    1.31 +function enableAsyncScrolling() {
    1.32 +    frameLoader().renderMode = Components.interfaces.nsIFrameLoader.RENDER_MODE_ASYNC_SCROLL;
    1.33 +}
    1.34 +
    1.35 +// Functions affecting the content window.
    1.36 +
    1.37 +function loadURL(url) {
    1.38 +    browser().setAttribute('src', url);
    1.39 +}
    1.40 +
    1.41 +function scrollContentBy(dx, dy) {
    1.42 +    messageManager.broadcastAsyncMessage("scrollBy",
    1.43 +                                         { dx: dx, dy: dy });
    1.44 +
    1.45 +}
    1.46 +
    1.47 +function scrollContentTo(x, y) {
    1.48 +    messageManager.broadcastAsyncMessage("scrollTo",
    1.49 +                                         { x: x, y: y });
    1.50 +}
    1.51 +
    1.52 +function setContentViewport(w, h) {
    1.53 +    messageManager.broadcastAsyncMessage("setViewport",
    1.54 +                                         { w: w, h: h });
    1.55 +}
    1.56 +
    1.57 +function setContentDisplayPort(x, y, w, h) {
    1.58 +    messageManager.broadcastAsyncMessage("setDisplayPort",
    1.59 +                                         { x: x, y: y, w: w, h: h });
    1.60 +}
    1.61 +
    1.62 +function setContentResolution(xres, yres) {
    1.63 +    messageManager.broadcastAsyncMessage("setResolution",
    1.64 +                                         { xres: xres, yres: yres });
    1.65 +}
    1.66 +
    1.67 +// Functions affecting <browser>.
    1.68 +
    1.69 +function scrollViewportBy(dx, dy) {
    1.70 +    rootView().scrollBy(dx, dy);
    1.71 +}
    1.72 +
    1.73 +function scrollViewportTo(x, y) {
    1.74 +    rootView().scrollTo(x, y);
    1.75 +}
    1.76 +
    1.77 +function setViewportScale(xs, ys) {
    1.78 +    rootView().setScale(xs, ys);
    1.79 +}
    1.80 +
    1.81 +var kDelayMs = 100;
    1.82 +var kDurationMs = 250;
    1.83 +var scrolling = false;
    1.84 +function startAnimatedScrollBy(dx, dy) {
    1.85 +    if (scrolling)
    1.86 +        throw "don't interrupt me!";
    1.87 +
    1.88 +    scrolling = true;
    1.89 +
    1.90 +    var start = mozAnimationStartTime;
    1.91 +    var end = start + kDurationMs;
    1.92 +    // |- k| so that we do something in first invocation of nudge()
    1.93 +    var prevNow = start - 20;
    1.94 +    var accumDx = 0, accumDy = 0;
    1.95 +
    1.96 +    var sentScrollBy = false;
    1.97 +    function nudgeScroll(now) {
    1.98 +	if (!scrolling) {
    1.99 +	    // we've been canceled
   1.100 +	    return;
   1.101 +	}
   1.102 +        var ddx = dx * (now - prevNow) / kDurationMs;
   1.103 +        var ddy = dy * (now - prevNow) / kDurationMs;
   1.104 +
   1.105 +        ddx = Math.min(dx - accumDx, ddx);
   1.106 +        ddy = Math.min(dy - accumDy, ddy);
   1.107 +        accumDx += ddx;
   1.108 +        accumDy += ddy;
   1.109 +
   1.110 +        rootView().scrollBy(ddx, ddy);
   1.111 +
   1.112 +        if (!sentScrollBy && 100 <= (now - start)) {
   1.113 +            messageManager.broadcastAsyncMessage("scrollBy",
   1.114 +                                                 { dx: dx, dy: dy });
   1.115 +            sentScrollBy = true;
   1.116 +        }
   1.117 +
   1.118 +        if (now >= end || (accumDx >= dx && accumDy >= dy)) {
   1.119 +            var fixupDx = Math.max(dx - accumDx, 0);
   1.120 +            var fixupDy = Math.max(dy - accumDy, 0);
   1.121 +            rootView().scrollBy(fixupDx, fixupDy);
   1.122 +
   1.123 +            scrolling = false;
   1.124 +        }
   1.125 +        else {
   1.126 +            mozRequestAnimationFrame(nudgeScroll);
   1.127 +        }
   1.128 +
   1.129 +        prevNow = now;
   1.130 +    }
   1.131 +
   1.132 +    nudgeScroll(start);
   1.133 +    mozRequestAnimationFrame(nudgeScroll);
   1.134 +}

mercurial