utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 } = require("chrome");
  9. const XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
  10. function eventTarget(frame) {
  11. return getDocShell(frame).chromeEventHandler;
  12. }
  13. exports.eventTarget = eventTarget;
  14. function getDocShell(frame) {
  15. let { frameLoader } = frame.QueryInterface(Ci.nsIFrameLoaderOwner);
  16. return frameLoader && frameLoader.docShell;
  17. }
  18. exports.getDocShell = getDocShell;
  19. /**
  20. * Creates a XUL `browser` element in a privileged document.
  21. * @params {nsIDOMDocument} document
  22. * @params {String} options.type
  23. * By default is 'content' for possible values see:
  24. * https://developer.mozilla.org/en/XUL/iframe#a-browser.type
  25. * @params {String} options.uri
  26. * URI of the document to be loaded into created frame.
  27. * @params {Boolean} options.remote
  28. * If `true` separate process will be used for this frame, also in such
  29. * case all the following options are ignored.
  30. * @params {Boolean} options.allowAuth
  31. * Whether to allow auth dialogs. Defaults to `false`.
  32. * @params {Boolean} options.allowJavascript
  33. * Whether to allow Javascript execution. Defaults to `false`.
  34. * @params {Boolean} options.allowPlugins
  35. * Whether to allow plugin execution. Defaults to `false`.
  36. */
  37. function create(target, options) {
  38. target = target instanceof Ci.nsIDOMDocument ? target.documentElement :
  39. target instanceof Ci.nsIDOMWindow ? target.document.documentElement :
  40. target;
  41. options = options || {};
  42. let remote = options.remote || false;
  43. let namespaceURI = options.namespaceURI || XUL;
  44. let isXUL = namespaceURI === XUL;
  45. let nodeName = isXUL && options.browser ? 'browser' : 'iframe';
  46. let document = target.ownerDocument;
  47. let frame = document.createElementNS(namespaceURI, nodeName);
  48. // Type="content" is mandatory to enable stuff here:
  49. // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1776
  50. frame.setAttribute('type', options.type || 'content');
  51. frame.setAttribute('src', options.uri || 'about:blank');
  52. target.appendChild(frame);
  53. // Load in separate process if `options.remote` is `true`.
  54. // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1347
  55. if (remote) {
  56. if (isXUL) {
  57. // We remove XBL binding to avoid execution of code that is not going to
  58. // work because browser has no docShell attribute in remote mode
  59. // (for example)
  60. frame.setAttribute('style', '-moz-binding: none;');
  61. frame.setAttribute('remote', 'true');
  62. }
  63. else {
  64. frame.QueryInterface(Ci.nsIMozBrowserFrame);
  65. frame.createRemoteFrameLoader(null);
  66. }
  67. }
  68. // If browser is remote it won't have a `docShell`.
  69. if (!remote) {
  70. let docShell = getDocShell(frame);
  71. docShell.allowAuth = options.allowAuth || false;
  72. docShell.allowJavascript = options.allowJavascript || false;
  73. docShell.allowPlugins = options.allowPlugins || false;
  74. // Control whether the document can move/resize the window. Requires
  75. // recently added platform capability, so we test to avoid exceptions
  76. // in cases where capability is not present yet.
  77. if ("allowWindowControl" in docShell && "allowWindowControl" in options)
  78. docShell.allowWindowControl = !!options.allowWindowControl;
  79. }
  80. return frame;
  81. }
  82. exports.create = create;
  83. function swapFrameLoaders(from, to)
  84. from.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(to)
  85. exports.swapFrameLoaders = swapFrameLoaders;