window.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "experimental"
  7. };
  8. const { Ci, Cc } = require("chrome");
  9. const { make: makeWindow, getHiddenWindow } = require("../window/utils");
  10. const { create: makeFrame, getDocShell } = require("../frame/utils");
  11. const { defer } = require("../core/promise");
  12. const { when: unload } = require("../system/unload");
  13. const cfxArgs = require("@test/options");
  14. let addonPrincipal = Cc["@mozilla.org/systemprincipal;1"].
  15. createInstance(Ci.nsIPrincipal);
  16. let hiddenWindow = getHiddenWindow();
  17. if (cfxArgs.parseable) {
  18. console.info("hiddenWindow document.documentURI:" +
  19. hiddenWindow.document.documentURI);
  20. console.info("hiddenWindow document.readyState:" +
  21. hiddenWindow.document.readyState);
  22. }
  23. // Once Bug 565388 is fixed and shipped we'll be able to make invisible,
  24. // permanent docShells. Meanwhile we create hidden top level window and
  25. // use it's docShell.
  26. let frame = makeFrame(hiddenWindow.document, {
  27. nodeName: "iframe",
  28. namespaceURI: "http://www.w3.org/1999/xhtml",
  29. allowJavascript: true,
  30. allowPlugins: true
  31. })
  32. let docShell = getDocShell(frame);
  33. let eventTarget = docShell.chromeEventHandler;
  34. // We need to grant docShell system principals in order to load XUL document
  35. // from data URI into it.
  36. docShell.createAboutBlankContentViewer(addonPrincipal);
  37. // Get a reference to the DOM window of the given docShell and load
  38. // such document into that would allow us to create XUL iframes, that
  39. // are necessary for hidden frames etc..
  40. let window = docShell.contentViewer.DOMDocument.defaultView;
  41. window.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window/>";
  42. // Create a promise that is delivered once add-on window is interactive,
  43. // used by add-on runner to defer add-on loading until window is ready.
  44. let { promise, resolve } = defer();
  45. eventTarget.addEventListener("DOMContentLoaded", function handler(event) {
  46. eventTarget.removeEventListener("DOMContentLoaded", handler, false);
  47. resolve();
  48. }, false);
  49. exports.ready = promise;
  50. exports.window = window;
  51. // Still close window on unload to claim memory back early.
  52. unload(function() {
  53. window.close()
  54. frame.parentNode.removeChild(frame);
  55. });