2015-01-20 08:57:00 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
"use strict";
|
2015-01-20 08:57:00 +03:00
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
/* global content, docShell, addEventListener, addMessageListener,
|
2016-03-01 22:58:54 +03:00
|
|
|
removeEventListener, removeMessageListener, sendAsyncMessage, Services */
|
2015-01-20 08:57:00 +03:00
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
var global = this;
|
2015-01-20 08:57:00 +03:00
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
// Guard against loading this frame script mutiple times
|
2016-05-17 21:25:54 +03:00
|
|
|
(function () {
|
2016-01-22 06:40:14 +03:00
|
|
|
if (global.responsiveFrameScriptLoaded) {
|
2015-01-20 08:57:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
var Ci = Components.interfaces;
|
|
|
|
const gDeviceSizeWasPageSize = docShell.deviceSizeIsPageSize;
|
|
|
|
const gFloatingScrollbarsStylesheet = Services.io.newURI("chrome://devtools/skin/floating-scrollbars-responsive-design.css", null, null);
|
|
|
|
var gRequiresFloatingScrollbars;
|
|
|
|
|
|
|
|
var active = false;
|
|
|
|
var resizeNotifications = false;
|
|
|
|
|
|
|
|
addMessageListener("ResponsiveMode:Start", startResponsiveMode);
|
|
|
|
addMessageListener("ResponsiveMode:Stop", stopResponsiveMode);
|
2016-04-19 02:20:22 +03:00
|
|
|
addMessageListener("ResponsiveMode:IsActive", isActive);
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
function debug(msg) {
|
|
|
|
// dump(`RDM CHILD: ${msg}\n`);
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
|
|
|
|
2016-04-19 02:20:22 +03:00
|
|
|
/**
|
|
|
|
* Used by tests to verify the state of responsive mode.
|
|
|
|
*/
|
|
|
|
function isActive() {
|
|
|
|
sendAsyncMessage("ResponsiveMode:IsActive:Done", { active });
|
|
|
|
}
|
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
function startResponsiveMode({data:data}) {
|
|
|
|
debug("START");
|
|
|
|
if (active) {
|
2016-04-19 02:20:22 +03:00
|
|
|
debug("ALREADY STARTED, ABORT");
|
2016-01-22 06:40:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
addMessageListener("ResponsiveMode:RequestScreenshot", screenshot);
|
|
|
|
let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebProgress);
|
|
|
|
webProgress.addProgressListener(WebProgressListener, Ci.nsIWebProgress.NOTIFY_ALL);
|
|
|
|
docShell.deviceSizeIsPageSize = true;
|
|
|
|
gRequiresFloatingScrollbars = data.requiresFloatingScrollbars;
|
|
|
|
if (data.notifyOnResize) {
|
|
|
|
startOnResize();
|
|
|
|
}
|
2015-01-20 08:57:00 +03:00
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
// At this point, a content viewer might not be loaded for this
|
|
|
|
// docshell. makeScrollbarsFloating will be triggered by onLocationChange.
|
|
|
|
if (docShell.contentViewer) {
|
|
|
|
makeScrollbarsFloating();
|
|
|
|
}
|
|
|
|
active = true;
|
|
|
|
sendAsyncMessage("ResponsiveMode:Start:Done");
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
function onResize() {
|
|
|
|
let { width, height } = content.screen;
|
|
|
|
debug(`EMIT RESIZE: ${width} x ${height}`);
|
|
|
|
sendAsyncMessage("ResponsiveMode:OnContentResize", {
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
});
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
function bindOnResize() {
|
|
|
|
content.addEventListener("resize", onResize, false);
|
|
|
|
}
|
2015-01-20 08:57:00 +03:00
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
function startOnResize() {
|
|
|
|
debug("START ON RESIZE");
|
|
|
|
if (resizeNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resizeNotifications = true;
|
|
|
|
bindOnResize();
|
|
|
|
addEventListener("DOMWindowCreated", bindOnResize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopOnResize() {
|
|
|
|
debug("STOP ON RESIZE");
|
|
|
|
if (!resizeNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resizeNotifications = false;
|
|
|
|
content.removeEventListener("resize", onResize, false);
|
|
|
|
removeEventListener("DOMWindowCreated", bindOnResize, false);
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
function stopResponsiveMode() {
|
|
|
|
debug("STOP");
|
|
|
|
if (!active) {
|
2016-04-19 02:20:22 +03:00
|
|
|
debug("ALREADY STOPPED, ABORT");
|
2016-01-22 06:40:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
active = false;
|
|
|
|
removeMessageListener("ResponsiveMode:RequestScreenshot", screenshot);
|
|
|
|
let webProgress = docShell.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebProgress);
|
|
|
|
webProgress.removeProgressListener(WebProgressListener);
|
|
|
|
docShell.deviceSizeIsPageSize = gDeviceSizeWasPageSize;
|
|
|
|
restoreScrollbars();
|
|
|
|
stopOnResize();
|
|
|
|
sendAsyncMessage("ResponsiveMode:Stop:Done");
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
function makeScrollbarsFloating() {
|
|
|
|
if (!gRequiresFloatingScrollbars) {
|
2015-08-12 00:26:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
let allDocShells = [docShell];
|
|
|
|
|
|
|
|
for (let i = 0; i < docShell.childCount; i++) {
|
|
|
|
let child = docShell.getChildAt(i).QueryInterface(Ci.nsIDocShell);
|
|
|
|
allDocShells.push(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let d of allDocShells) {
|
|
|
|
let win = d.contentViewer.DOMDocument.defaultView;
|
|
|
|
let winUtils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
try {
|
|
|
|
winUtils.loadSheet(gFloatingScrollbarsStylesheet, win.AGENT_SHEET);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (e) { }
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
2016-01-22 06:40:14 +03:00
|
|
|
|
|
|
|
flushStyle();
|
2015-01-20 08:57:00 +03:00
|
|
|
}
|
|
|
|
|
2016-01-22 06:40:14 +03:00
|
|
|
function restoreScrollbars() {
|
|
|
|
let allDocShells = [docShell];
|
|
|
|
for (let i = 0; i < docShell.childCount; i++) {
|
|
|
|
allDocShells.push(docShell.getChildAt(i).QueryInterface(Ci.nsIDocShell));
|
|
|
|
}
|
|
|
|
for (let d of allDocShells) {
|
|
|
|
let win = d.contentViewer.DOMDocument.defaultView;
|
|
|
|
let winUtils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
try {
|
|
|
|
winUtils.removeSheet(gFloatingScrollbarsStylesheet, win.AGENT_SHEET);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (e) { }
|
2016-01-22 06:40:14 +03:00
|
|
|
}
|
|
|
|
flushStyle();
|
|
|
|
}
|
|
|
|
|
|
|
|
function flushStyle() {
|
|
|
|
// Force presContext destruction
|
|
|
|
let isSticky = docShell.contentViewer.sticky;
|
|
|
|
docShell.contentViewer.sticky = false;
|
|
|
|
docShell.contentViewer.hide();
|
|
|
|
docShell.contentViewer.show();
|
|
|
|
docShell.contentViewer.sticky = isSticky;
|
|
|
|
}
|
|
|
|
|
|
|
|
function screenshot() {
|
|
|
|
let canvas = content.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
|
2016-04-19 01:18:26 +03:00
|
|
|
let ratio = content.devicePixelRatio;
|
|
|
|
let width = content.innerWidth * ratio;
|
|
|
|
let height = content.innerHeight * ratio;
|
2016-01-22 06:40:14 +03:00
|
|
|
canvas.mozOpaque = true;
|
|
|
|
canvas.width = width;
|
|
|
|
canvas.height = height;
|
|
|
|
let ctx = canvas.getContext("2d");
|
2016-04-19 01:18:26 +03:00
|
|
|
ctx.scale(ratio, ratio);
|
2016-01-22 06:40:14 +03:00
|
|
|
ctx.drawWindow(content, content.scrollX, content.scrollY, width, height, "#fff");
|
|
|
|
sendAsyncMessage("ResponsiveMode:RequestScreenshot:Done", canvas.toDataURL());
|
|
|
|
}
|
|
|
|
|
|
|
|
var WebProgressListener = {
|
|
|
|
onLocationChange(webProgress, request, URI, flags) {
|
|
|
|
if (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
makeScrollbarsFloating();
|
|
|
|
},
|
|
|
|
QueryInterface: function QueryInterface(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsIWebProgressListener) ||
|
|
|
|
aIID.equals(Ci.nsISupportsWeakReference) ||
|
|
|
|
aIID.equals(Ci.nsISupports)) {
|
2016-05-17 21:25:54 +03:00
|
|
|
return this;
|
2016-01-22 06:40:14 +03:00
|
|
|
}
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
global.responsiveFrameScriptLoaded = true;
|
2015-01-20 08:57:00 +03:00
|
|
|
sendAsyncMessage("ResponsiveMode:ChildScriptReady");
|