1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/lib/sdk/addon/window.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 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 +"use strict"; 1.8 + 1.9 +module.metadata = { 1.10 + "stability": "experimental" 1.11 +}; 1.12 + 1.13 +const { Ci, Cc } = require("chrome"); 1.14 +const { make: makeWindow, getHiddenWindow } = require("../window/utils"); 1.15 +const { create: makeFrame, getDocShell } = require("../frame/utils"); 1.16 +const { defer } = require("../core/promise"); 1.17 +const { when: unload } = require("../system/unload"); 1.18 +const cfxArgs = require("@test/options"); 1.19 + 1.20 +let addonPrincipal = Cc["@mozilla.org/systemprincipal;1"]. 1.21 + createInstance(Ci.nsIPrincipal); 1.22 + 1.23 +let hiddenWindow = getHiddenWindow(); 1.24 + 1.25 +if (cfxArgs.parseable) { 1.26 + console.info("hiddenWindow document.documentURI:" + 1.27 + hiddenWindow.document.documentURI); 1.28 + console.info("hiddenWindow document.readyState:" + 1.29 + hiddenWindow.document.readyState); 1.30 +} 1.31 + 1.32 +// Once Bug 565388 is fixed and shipped we'll be able to make invisible, 1.33 +// permanent docShells. Meanwhile we create hidden top level window and 1.34 +// use it's docShell. 1.35 +let frame = makeFrame(hiddenWindow.document, { 1.36 + nodeName: "iframe", 1.37 + namespaceURI: "http://www.w3.org/1999/xhtml", 1.38 + allowJavascript: true, 1.39 + allowPlugins: true 1.40 +}) 1.41 +let docShell = getDocShell(frame); 1.42 +let eventTarget = docShell.chromeEventHandler; 1.43 + 1.44 +// We need to grant docShell system principals in order to load XUL document 1.45 +// from data URI into it. 1.46 +docShell.createAboutBlankContentViewer(addonPrincipal); 1.47 + 1.48 +// Get a reference to the DOM window of the given docShell and load 1.49 +// such document into that would allow us to create XUL iframes, that 1.50 +// are necessary for hidden frames etc.. 1.51 +let window = docShell.contentViewer.DOMDocument.defaultView; 1.52 +window.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window/>"; 1.53 + 1.54 +// Create a promise that is delivered once add-on window is interactive, 1.55 +// used by add-on runner to defer add-on loading until window is ready. 1.56 +let { promise, resolve } = defer(); 1.57 +eventTarget.addEventListener("DOMContentLoaded", function handler(event) { 1.58 + eventTarget.removeEventListener("DOMContentLoaded", handler, false); 1.59 + resolve(); 1.60 +}, false); 1.61 + 1.62 + 1.63 + 1.64 +exports.ready = promise; 1.65 +exports.window = window; 1.66 + 1.67 +// Still close window on unload to claim memory back early. 1.68 +unload(function() { 1.69 + window.close() 1.70 + frame.parentNode.removeChild(frame); 1.71 +});