summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/sdk/addon/window.js
blob: 93ed1d8dc6dcb184d5e36ea6eadf290f431dbdae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 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/. */
"use strict";

module.metadata = {
  "stability": "experimental"
};

const { Ci, Cc } = require("chrome");
const { make: makeWindow, getHiddenWindow } = require("../window/utils");
const { create: makeFrame, getDocShell } = require("../frame/utils");
const { defer } = require("../core/promise");
const { when: unload } = require("../system/unload");
const cfxArgs = require("../test/options");

var addonPrincipal = Cc["@mozilla.org/systemprincipal;1"].
                     createInstance(Ci.nsIPrincipal);

var hiddenWindow = getHiddenWindow();

if (cfxArgs.parseable) {
  console.info("hiddenWindow document.documentURI:" +
    hiddenWindow.document.documentURI);
  console.info("hiddenWindow document.readyState:" +
    hiddenWindow.document.readyState);
}

// Once Bug 565388 is fixed and shipped we'll be able to make invisible,
// permanent docShells. Meanwhile we create hidden top level window and
// use it's docShell.
var frame = makeFrame(hiddenWindow.document, {
  nodeName: "iframe",
  namespaceURI: "http://www.w3.org/1999/xhtml",
  allowJavascript: true,
  allowPlugins: true
})
var docShell = getDocShell(frame);
var eventTarget = docShell.chromeEventHandler;

// We need to grant docShell system principals in order to load XUL document
// from data URI into it.
docShell.createAboutBlankContentViewer(addonPrincipal);

// Get a reference to the DOM window of the given docShell and load
// such document into that would allow us to create XUL iframes, that
// are necessary for hidden frames etc..
var window = docShell.contentViewer.DOMDocument.defaultView;
window.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window/>";

// Create a promise that is delivered once add-on window is interactive,
// used by add-on runner to defer add-on loading until window is ready.
var { promise, resolve } = defer();
eventTarget.addEventListener("DOMContentLoaded", function handler(event) {
  eventTarget.removeEventListener("DOMContentLoaded", handler, false);
  resolve();
}, false);

exports.ready = promise;
exports.window = window;

// Still close window on unload to claim memory back early.
unload(function() {
  window.close()
  frame.parentNode.removeChild(frame);
});