michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function init() { michael@0: enableAsyncScrolling(); michael@0: messageManager.loadFrameScript( michael@0: "chrome://global/content/test-ipcbrowser-content.js", true michael@0: ); michael@0: } michael@0: michael@0: function browser() { michael@0: return document.getElementById("content"); michael@0: } michael@0: michael@0: function frameLoader() { michael@0: return browser().QueryInterface(Components.interfaces.nsIFrameLoaderOwner).frameLoader; michael@0: } michael@0: michael@0: function viewManager() { michael@0: return frameLoader().QueryInterface(Components.interfaces.nsIContentViewManager); michael@0: } michael@0: michael@0: function rootView() { michael@0: return viewManager().rootContentView; michael@0: } michael@0: michael@0: function enableAsyncScrolling() { michael@0: frameLoader().renderMode = Components.interfaces.nsIFrameLoader.RENDER_MODE_ASYNC_SCROLL; michael@0: } michael@0: michael@0: // Functions affecting the content window. michael@0: michael@0: function loadURL(url) { michael@0: browser().setAttribute('src', url); michael@0: } michael@0: michael@0: function scrollContentBy(dx, dy) { michael@0: messageManager.broadcastAsyncMessage("scrollBy", michael@0: { dx: dx, dy: dy }); michael@0: michael@0: } michael@0: michael@0: function scrollContentTo(x, y) { michael@0: messageManager.broadcastAsyncMessage("scrollTo", michael@0: { x: x, y: y }); michael@0: } michael@0: michael@0: function setContentViewport(w, h) { michael@0: messageManager.broadcastAsyncMessage("setViewport", michael@0: { w: w, h: h }); michael@0: } michael@0: michael@0: function setContentDisplayPort(x, y, w, h) { michael@0: messageManager.broadcastAsyncMessage("setDisplayPort", michael@0: { x: x, y: y, w: w, h: h }); michael@0: } michael@0: michael@0: function setContentResolution(xres, yres) { michael@0: messageManager.broadcastAsyncMessage("setResolution", michael@0: { xres: xres, yres: yres }); michael@0: } michael@0: michael@0: // Functions affecting . michael@0: michael@0: function scrollViewportBy(dx, dy) { michael@0: rootView().scrollBy(dx, dy); michael@0: } michael@0: michael@0: function scrollViewportTo(x, y) { michael@0: rootView().scrollTo(x, y); michael@0: } michael@0: michael@0: function setViewportScale(xs, ys) { michael@0: rootView().setScale(xs, ys); michael@0: } michael@0: michael@0: var kDelayMs = 100; michael@0: var kDurationMs = 250; michael@0: var scrolling = false; michael@0: function startAnimatedScrollBy(dx, dy) { michael@0: if (scrolling) michael@0: throw "don't interrupt me!"; michael@0: michael@0: scrolling = true; michael@0: michael@0: var start = mozAnimationStartTime; michael@0: var end = start + kDurationMs; michael@0: // |- k| so that we do something in first invocation of nudge() michael@0: var prevNow = start - 20; michael@0: var accumDx = 0, accumDy = 0; michael@0: michael@0: var sentScrollBy = false; michael@0: function nudgeScroll(now) { michael@0: if (!scrolling) { michael@0: // we've been canceled michael@0: return; michael@0: } michael@0: var ddx = dx * (now - prevNow) / kDurationMs; michael@0: var ddy = dy * (now - prevNow) / kDurationMs; michael@0: michael@0: ddx = Math.min(dx - accumDx, ddx); michael@0: ddy = Math.min(dy - accumDy, ddy); michael@0: accumDx += ddx; michael@0: accumDy += ddy; michael@0: michael@0: rootView().scrollBy(ddx, ddy); michael@0: michael@0: if (!sentScrollBy && 100 <= (now - start)) { michael@0: messageManager.broadcastAsyncMessage("scrollBy", michael@0: { dx: dx, dy: dy }); michael@0: sentScrollBy = true; michael@0: } michael@0: michael@0: if (now >= end || (accumDx >= dx && accumDy >= dy)) { michael@0: var fixupDx = Math.max(dx - accumDx, 0); michael@0: var fixupDy = Math.max(dy - accumDy, 0); michael@0: rootView().scrollBy(fixupDx, fixupDy); michael@0: michael@0: scrolling = false; michael@0: } michael@0: else { michael@0: mozRequestAnimationFrame(nudgeScroll); michael@0: } michael@0: michael@0: prevNow = now; michael@0: } michael@0: michael@0: nudgeScroll(start); michael@0: mozRequestAnimationFrame(nudgeScroll); michael@0: }