hidden-frame.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 { Cc, Ci } = require("chrome");
  9. const errors = require("../deprecated/errors");
  10. const { Class } = require("../core/heritage");
  11. const { List, addListItem, removeListItem } = require("../util/list");
  12. const { EventTarget } = require("../event/target");
  13. const { emit } = require("../event/core");
  14. const { create: makeFrame } = require("./utils");
  15. const { defer } = require("../core/promise");
  16. const { when: unload } = require("../system/unload");
  17. const { validateOptions, getTypeOf } = require("../deprecated/api-utils");
  18. const { window } = require("../addon/window");
  19. const { fromIterator } = require("../util/array");
  20. // This cache is used to access friend properties between functions
  21. // without exposing them on the public API.
  22. let cache = new Set();
  23. let elements = new WeakMap();
  24. function contentLoaded(target) {
  25. var deferred = defer();
  26. target.addEventListener("DOMContentLoaded", function DOMContentLoaded(event) {
  27. // "DOMContentLoaded" events from nested frames propagate up to target,
  28. // ignore events unless it's DOMContentLoaded for the given target.
  29. if (event.target === target || event.target === target.contentDocument) {
  30. target.removeEventListener("DOMContentLoaded", DOMContentLoaded, false);
  31. deferred.resolve(target);
  32. }
  33. }, false);
  34. return deferred.promise;
  35. }
  36. function FrameOptions(options) {
  37. options = options || {}
  38. return validateOptions(options, FrameOptions.validator);
  39. }
  40. FrameOptions.validator = {
  41. onReady: {
  42. is: ["undefined", "function", "array"],
  43. ok: function(v) {
  44. if (getTypeOf(v) === "array") {
  45. // make sure every item is a function
  46. return v.every(function (item) typeof(item) === "function")
  47. }
  48. return true;
  49. }
  50. },
  51. onUnload: {
  52. is: ["undefined", "function"]
  53. }
  54. };
  55. var HiddenFrame = Class({
  56. extends: EventTarget,
  57. initialize: function initialize(options) {
  58. options = FrameOptions(options);
  59. EventTarget.prototype.initialize.call(this, options);
  60. },
  61. get element() {
  62. return elements.get(this);
  63. },
  64. toString: function toString() {
  65. return "[object Frame]"
  66. }
  67. });
  68. exports.HiddenFrame = HiddenFrame
  69. function addHidenFrame(frame) {
  70. if (!(frame instanceof HiddenFrame))
  71. throw Error("The object to be added must be a HiddenFrame.");
  72. // This instance was already added.
  73. if (cache.has(frame)) return frame;
  74. else cache.add(frame);
  75. let element = makeFrame(window.document, {
  76. nodeName: "iframe",
  77. type: "content",
  78. allowJavascript: true,
  79. allowPlugins: true,
  80. allowAuth: true,
  81. });
  82. elements.set(frame, element);
  83. contentLoaded(element).then(function onFrameReady(element) {
  84. emit(frame, "ready");
  85. }, console.exception);
  86. return frame;
  87. }
  88. exports.add = addHidenFrame
  89. function removeHiddenFrame(frame) {
  90. if (!(frame instanceof HiddenFrame))
  91. throw Error("The object to be removed must be a HiddenFrame.");
  92. if (!cache.has(frame)) return;
  93. // Remove from cache before calling in order to avoid loop
  94. cache.delete(frame);
  95. emit(frame, "unload")
  96. let element = frame.element
  97. if (element) element.parentNode.removeChild(element)
  98. }
  99. exports.remove = removeHiddenFrame;
  100. unload(function() fromIterator(cache).forEach(removeHiddenFrame));