diff options
author | Matt A. Tobin <email@mattatobin.com> | 2018-02-10 02:51:36 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2018-02-10 02:51:36 -0500 |
commit | 37d5300335d81cecbecc99812747a657588c63eb (patch) | |
tree | 765efa3b6a56bb715d9813a8697473e120436278 /toolkit/jetpack/sdk/frame | |
parent | b2bdac20c02b12f2057b9ef70b0a946113a00e00 (diff) | |
parent | 4fb11cd5966461bccc3ed1599b808237be6b0de9 (diff) | |
download | UXP-37d5300335d81cecbecc99812747a657588c63eb.tar UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.gz UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.lz UXP-37d5300335d81cecbecc99812747a657588c63eb.tar.xz UXP-37d5300335d81cecbecc99812747a657588c63eb.zip |
Merge branch 'ext-work'
Diffstat (limited to 'toolkit/jetpack/sdk/frame')
-rw-r--r-- | toolkit/jetpack/sdk/frame/hidden-frame.js | 115 | ||||
-rw-r--r-- | toolkit/jetpack/sdk/frame/utils.js | 94 |
2 files changed, 209 insertions, 0 deletions
diff --git a/toolkit/jetpack/sdk/frame/hidden-frame.js b/toolkit/jetpack/sdk/frame/hidden-frame.js new file mode 100644 index 000000000..97e0b7974 --- /dev/null +++ b/toolkit/jetpack/sdk/frame/hidden-frame.js @@ -0,0 +1,115 @@ +/* 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 { Cc, Ci } = require("chrome"); +const { Class } = require("../core/heritage"); +const { List, addListItem, removeListItem } = require("../util/list"); +const { EventTarget } = require("../event/target"); +const { emit } = require("../event/core"); +const { create: makeFrame } = require("./utils"); +const { defer } = require("../core/promise"); +const { when: unload } = require("../system/unload"); +const { validateOptions, getTypeOf } = require("../deprecated/api-utils"); +const { window } = require("../addon/window"); +const { fromIterator } = require("../util/array"); + +// This cache is used to access friend properties between functions +// without exposing them on the public API. +var cache = new Set(); +var elements = new WeakMap(); + +function contentLoaded(target) { + var deferred = defer(); + target.addEventListener("DOMContentLoaded", function DOMContentLoaded(event) { + // "DOMContentLoaded" events from nested frames propagate up to target, + // ignore events unless it's DOMContentLoaded for the given target. + if (event.target === target || event.target === target.contentDocument) { + target.removeEventListener("DOMContentLoaded", DOMContentLoaded, false); + deferred.resolve(target); + } + }, false); + return deferred.promise; +} + +function FrameOptions(options) { + options = options || {} + return validateOptions(options, FrameOptions.validator); +} +FrameOptions.validator = { + onReady: { + is: ["undefined", "function", "array"], + ok: function(v) { + if (getTypeOf(v) === "array") { + // make sure every item is a function + return v.every(item => typeof(item) === "function") + } + return true; + } + }, + onUnload: { + is: ["undefined", "function"] + } +}; + +var HiddenFrame = Class({ + extends: EventTarget, + initialize: function initialize(options) { + options = FrameOptions(options); + EventTarget.prototype.initialize.call(this, options); + }, + get element() { + return elements.get(this); + }, + toString: function toString() { + return "[object Frame]" + } +}); +exports.HiddenFrame = HiddenFrame + +function addHidenFrame(frame) { + if (!(frame instanceof HiddenFrame)) + throw Error("The object to be added must be a HiddenFrame."); + + // This instance was already added. + if (cache.has(frame)) return frame; + else cache.add(frame); + + let element = makeFrame(window.document, { + nodeName: "iframe", + type: "content", + allowJavascript: true, + allowPlugins: true, + allowAuth: true, + }); + elements.set(frame, element); + + contentLoaded(element).then(function onFrameReady(element) { + emit(frame, "ready"); + }, console.exception); + + return frame; +} +exports.add = addHidenFrame + +function removeHiddenFrame(frame) { + if (!(frame instanceof HiddenFrame)) + throw Error("The object to be removed must be a HiddenFrame."); + + if (!cache.has(frame)) return; + + // Remove from cache before calling in order to avoid loop + cache.delete(frame); + emit(frame, "unload") + let element = frame.element + if (element) element.parentNode.removeChild(element) +} +exports.remove = removeHiddenFrame; + +unload(() => fromIterator(cache).forEach(removeHiddenFrame)); diff --git a/toolkit/jetpack/sdk/frame/utils.js b/toolkit/jetpack/sdk/frame/utils.js new file mode 100644 index 000000000..d9fccec4d --- /dev/null +++ b/toolkit/jetpack/sdk/frame/utils.js @@ -0,0 +1,94 @@ +/* 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 } = require("chrome"); +const XUL = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'; + +function eventTarget(frame) { + return getDocShell(frame).chromeEventHandler; +} +exports.eventTarget = eventTarget; + +function getDocShell(frame) { + let { frameLoader } = frame.QueryInterface(Ci.nsIFrameLoaderOwner); + return frameLoader && frameLoader.docShell; +} +exports.getDocShell = getDocShell; + +/** + * Creates a XUL `browser` element in a privileged document. + * @params {nsIDOMDocument} document + * @params {String} options.type + * By default is 'content' for possible values see: + * https://developer.mozilla.org/en/XUL/iframe#a-browser.type + * @params {String} options.uri + * URI of the document to be loaded into created frame. + * @params {Boolean} options.remote + * If `true` separate process will be used for this frame, also in such + * case all the following options are ignored. + * @params {Boolean} options.allowAuth + * Whether to allow auth dialogs. Defaults to `false`. + * @params {Boolean} options.allowJavascript + * Whether to allow Javascript execution. Defaults to `false`. + * @params {Boolean} options.allowPlugins + * Whether to allow plugin execution. Defaults to `false`. + */ +function create(target, options) { + target = target instanceof Ci.nsIDOMDocument ? target.documentElement : + target instanceof Ci.nsIDOMWindow ? target.document.documentElement : + target; + options = options || {}; + let remote = options.remote || false; + let namespaceURI = options.namespaceURI || XUL; + let isXUL = namespaceURI === XUL; + let nodeName = isXUL && options.browser ? 'browser' : 'iframe'; + let document = target.ownerDocument; + + let frame = document.createElementNS(namespaceURI, nodeName); + // Type="content" is mandatory to enable stuff here: + // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1776 + frame.setAttribute('type', options.type || 'content'); + frame.setAttribute('src', options.uri || 'about:blank'); + + // Must set the remote attribute before attaching the frame to the document + if (remote && isXUL) { + // We remove XBL binding to avoid execution of code that is not going to + // work because browser has no docShell attribute in remote mode + // (for example) + frame.setAttribute('style', '-moz-binding: none;'); + frame.setAttribute('remote', 'true'); + } + + target.appendChild(frame); + + // Load in separate process if `options.remote` is `true`. + // http://mxr.mozilla.org/mozilla-central/source/content/base/src/nsFrameLoader.cpp#1347 + if (remote && !isXUL) { + frame.QueryInterface(Ci.nsIMozBrowserFrame); + frame.createRemoteFrameLoader(null); + } + + // If browser is remote it won't have a `docShell`. + if (!remote) { + let docShell = getDocShell(frame); + docShell.allowAuth = options.allowAuth || false; + docShell.allowJavascript = options.allowJavascript || false; + docShell.allowPlugins = options.allowPlugins || false; + docShell.allowWindowControl = options.allowWindowControl || false; + } + + return frame; +} +exports.create = create; + +function swapFrameLoaders(from, to) { + return from.QueryInterface(Ci.nsIFrameLoaderOwner).swapFrameLoaders(to); +} +exports.swapFrameLoaders = swapFrameLoaders; |