Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | "stability": "experimental" |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | const { Cc, Ci } = require("chrome"); |
michael@0 | 12 | const errors = require("../deprecated/errors"); |
michael@0 | 13 | const { Class } = require("../core/heritage"); |
michael@0 | 14 | const { List, addListItem, removeListItem } = require("../util/list"); |
michael@0 | 15 | const { EventTarget } = require("../event/target"); |
michael@0 | 16 | const { emit } = require("../event/core"); |
michael@0 | 17 | const { create: makeFrame } = require("./utils"); |
michael@0 | 18 | const { defer } = require("../core/promise"); |
michael@0 | 19 | const { when: unload } = require("../system/unload"); |
michael@0 | 20 | const { validateOptions, getTypeOf } = require("../deprecated/api-utils"); |
michael@0 | 21 | const { window } = require("../addon/window"); |
michael@0 | 22 | const { fromIterator } = require("../util/array"); |
michael@0 | 23 | |
michael@0 | 24 | // This cache is used to access friend properties between functions |
michael@0 | 25 | // without exposing them on the public API. |
michael@0 | 26 | let cache = new Set(); |
michael@0 | 27 | let elements = new WeakMap(); |
michael@0 | 28 | |
michael@0 | 29 | function contentLoaded(target) { |
michael@0 | 30 | var deferred = defer(); |
michael@0 | 31 | target.addEventListener("DOMContentLoaded", function DOMContentLoaded(event) { |
michael@0 | 32 | // "DOMContentLoaded" events from nested frames propagate up to target, |
michael@0 | 33 | // ignore events unless it's DOMContentLoaded for the given target. |
michael@0 | 34 | if (event.target === target || event.target === target.contentDocument) { |
michael@0 | 35 | target.removeEventListener("DOMContentLoaded", DOMContentLoaded, false); |
michael@0 | 36 | deferred.resolve(target); |
michael@0 | 37 | } |
michael@0 | 38 | }, false); |
michael@0 | 39 | return deferred.promise; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function FrameOptions(options) { |
michael@0 | 43 | options = options || {} |
michael@0 | 44 | return validateOptions(options, FrameOptions.validator); |
michael@0 | 45 | } |
michael@0 | 46 | FrameOptions.validator = { |
michael@0 | 47 | onReady: { |
michael@0 | 48 | is: ["undefined", "function", "array"], |
michael@0 | 49 | ok: function(v) { |
michael@0 | 50 | if (getTypeOf(v) === "array") { |
michael@0 | 51 | // make sure every item is a function |
michael@0 | 52 | return v.every(function (item) typeof(item) === "function") |
michael@0 | 53 | } |
michael@0 | 54 | return true; |
michael@0 | 55 | } |
michael@0 | 56 | }, |
michael@0 | 57 | onUnload: { |
michael@0 | 58 | is: ["undefined", "function"] |
michael@0 | 59 | } |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | var HiddenFrame = Class({ |
michael@0 | 63 | extends: EventTarget, |
michael@0 | 64 | initialize: function initialize(options) { |
michael@0 | 65 | options = FrameOptions(options); |
michael@0 | 66 | EventTarget.prototype.initialize.call(this, options); |
michael@0 | 67 | }, |
michael@0 | 68 | get element() { |
michael@0 | 69 | return elements.get(this); |
michael@0 | 70 | }, |
michael@0 | 71 | toString: function toString() { |
michael@0 | 72 | return "[object Frame]" |
michael@0 | 73 | } |
michael@0 | 74 | }); |
michael@0 | 75 | exports.HiddenFrame = HiddenFrame |
michael@0 | 76 | |
michael@0 | 77 | function addHidenFrame(frame) { |
michael@0 | 78 | if (!(frame instanceof HiddenFrame)) |
michael@0 | 79 | throw Error("The object to be added must be a HiddenFrame."); |
michael@0 | 80 | |
michael@0 | 81 | // This instance was already added. |
michael@0 | 82 | if (cache.has(frame)) return frame; |
michael@0 | 83 | else cache.add(frame); |
michael@0 | 84 | |
michael@0 | 85 | let element = makeFrame(window.document, { |
michael@0 | 86 | nodeName: "iframe", |
michael@0 | 87 | type: "content", |
michael@0 | 88 | allowJavascript: true, |
michael@0 | 89 | allowPlugins: true, |
michael@0 | 90 | allowAuth: true, |
michael@0 | 91 | }); |
michael@0 | 92 | elements.set(frame, element); |
michael@0 | 93 | |
michael@0 | 94 | contentLoaded(element).then(function onFrameReady(element) { |
michael@0 | 95 | emit(frame, "ready"); |
michael@0 | 96 | }, console.exception); |
michael@0 | 97 | |
michael@0 | 98 | return frame; |
michael@0 | 99 | } |
michael@0 | 100 | exports.add = addHidenFrame |
michael@0 | 101 | |
michael@0 | 102 | function removeHiddenFrame(frame) { |
michael@0 | 103 | if (!(frame instanceof HiddenFrame)) |
michael@0 | 104 | throw Error("The object to be removed must be a HiddenFrame."); |
michael@0 | 105 | |
michael@0 | 106 | if (!cache.has(frame)) return; |
michael@0 | 107 | |
michael@0 | 108 | // Remove from cache before calling in order to avoid loop |
michael@0 | 109 | cache.delete(frame); |
michael@0 | 110 | emit(frame, "unload") |
michael@0 | 111 | let element = frame.element |
michael@0 | 112 | if (element) element.parentNode.removeChild(element) |
michael@0 | 113 | } |
michael@0 | 114 | exports.remove = removeHiddenFrame; |
michael@0 | 115 | |
michael@0 | 116 | unload(function() fromIterator(cache).forEach(removeHiddenFrame)); |