symbiont.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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": "deprecated"
  7. };
  8. const { Worker } = require('./traits-worker');
  9. const { Loader } = require('../content/loader');
  10. const hiddenFrames = require('../frame/hidden-frame');
  11. const { on, off } = require('../system/events');
  12. const unload = require('../system/unload');
  13. const { getDocShell } = require("../frame/utils");
  14. const { ignoreWindow } = require('../private-browsing/utils');
  15. // Everything coming from add-on's xpi considered an asset.
  16. const assetsURI = require('../self').data.url().replace(/data\/$/, "");
  17. /**
  18. * This trait is layered on top of `Worker` and in contrast to symbiont
  19. * Worker constructor requires `content` option that represents content
  20. * that will be loaded in the provided frame, if frame is not provided
  21. * Worker will create hidden one.
  22. */
  23. const Symbiont = Worker.resolve({
  24. constructor: '_initWorker',
  25. destroy: '_workerDestroy'
  26. }).compose(Loader, {
  27. /**
  28. * The constructor requires all the options that are required by
  29. * `require('content').Worker` with the difference that the `frame` option
  30. * is optional. If `frame` is not provided, `contentURL` is expected.
  31. * @param {Object} options
  32. * @param {String} options.contentURL
  33. * URL of a content to load into `this._frame` and create worker for.
  34. * @param {Element} [options.frame]
  35. * iframe element that is used to load `options.contentURL` into.
  36. * if frame is not provided hidden iframe will be created.
  37. */
  38. constructor: function Symbiont(options) {
  39. options = options || {};
  40. if ('contentURL' in options)
  41. this.contentURL = options.contentURL;
  42. if ('contentScriptWhen' in options)
  43. this.contentScriptWhen = options.contentScriptWhen;
  44. if ('contentScriptOptions' in options)
  45. this.contentScriptOptions = options.contentScriptOptions;
  46. if ('contentScriptFile' in options)
  47. this.contentScriptFile = options.contentScriptFile;
  48. if ('contentScript' in options)
  49. this.contentScript = options.contentScript;
  50. if ('allow' in options)
  51. this.allow = options.allow;
  52. if ('onError' in options)
  53. this.on('error', options.onError);
  54. if ('onMessage' in options)
  55. this.on('message', options.onMessage);
  56. if ('frame' in options) {
  57. this._initFrame(options.frame);
  58. }
  59. else {
  60. let self = this;
  61. this._hiddenFrame = hiddenFrames.HiddenFrame({
  62. onReady: function onFrame() {
  63. self._initFrame(this.element);
  64. },
  65. onUnload: function onUnload() {
  66. // Bug 751211: Remove reference to _frame when hidden frame is
  67. // automatically removed on unload, otherwise we are going to face
  68. // "dead object" exception
  69. self.destroy();
  70. }
  71. });
  72. hiddenFrames.add(this._hiddenFrame);
  73. }
  74. unload.ensure(this._public, "destroy");
  75. },
  76. destroy: function destroy() {
  77. this._workerDestroy();
  78. this._unregisterListener();
  79. this._frame = null;
  80. if (this._hiddenFrame) {
  81. hiddenFrames.remove(this._hiddenFrame);
  82. this._hiddenFrame = null;
  83. }
  84. },
  85. /**
  86. * XUL iframe or browser elements with attribute `type` being `content`.
  87. * Used to create `ContentSymbiont` from.
  88. * @type {nsIFrame|nsIBrowser}
  89. */
  90. _frame: null,
  91. /**
  92. * Listener to the `'frameReady"` event (emitted when `iframe` is ready).
  93. * Removes listener, sets right permissions to the frame and loads content.
  94. */
  95. _initFrame: function _initFrame(frame) {
  96. if (this._loadListener)
  97. this._unregisterListener();
  98. this._frame = frame;
  99. if (getDocShell(frame)) {
  100. this._reallyInitFrame(frame);
  101. }
  102. else {
  103. if (this._waitForFrame) {
  104. off('content-document-global-created', this._waitForFrame);
  105. }
  106. this._waitForFrame = this.__waitForFrame.bind(this, frame);
  107. on('content-document-global-created', this._waitForFrame);
  108. }
  109. },
  110. __waitForFrame: function _waitForFrame(frame, { subject: win }) {
  111. if (frame.contentWindow == win) {
  112. off('content-document-global-created', this._waitForFrame);
  113. delete this._waitForFrame;
  114. this._reallyInitFrame(frame);
  115. }
  116. },
  117. _reallyInitFrame: function _reallyInitFrame(frame) {
  118. getDocShell(frame).allowJavascript = this.allow.script;
  119. frame.setAttribute("src", this._contentURL);
  120. // Inject `addon` object in document if we load a document from
  121. // one of our addon folder and if no content script are defined. bug 612726
  122. let isDataResource =
  123. typeof this._contentURL == "string" &&
  124. this._contentURL.indexOf(assetsURI) == 0;
  125. let hasContentScript =
  126. (Array.isArray(this.contentScript) ? this.contentScript.length > 0
  127. : !!this.contentScript) ||
  128. (Array.isArray(this.contentScriptFile) ? this.contentScriptFile.length > 0
  129. : !!this.contentScriptFile);
  130. // If we have to inject `addon` we have to do it before document
  131. // script execution, so during `start`:
  132. this._injectInDocument = isDataResource && !hasContentScript;
  133. if (this._injectInDocument)
  134. this.contentScriptWhen = "start";
  135. if ((frame.contentDocument.readyState == "complete" ||
  136. (frame.contentDocument.readyState == "interactive" &&
  137. this.contentScriptWhen != 'end' )) &&
  138. frame.contentDocument.location == this._contentURL) {
  139. // In some cases src doesn't change and document is already ready
  140. // (for ex: when the user moves a widget while customizing toolbars.)
  141. this._onInit();
  142. return;
  143. }
  144. let self = this;
  145. if ('start' == this.contentScriptWhen) {
  146. this._loadEvent = 'start';
  147. on('document-element-inserted',
  148. this._loadListener = function onStart({ subject: doc }) {
  149. let window = doc.defaultView;
  150. if (ignoreWindow(window)) {
  151. return;
  152. }
  153. if (window && window == frame.contentWindow) {
  154. self._unregisterListener();
  155. self._onInit();
  156. }
  157. });
  158. return;
  159. }
  160. let eventName = 'end' == this.contentScriptWhen ? 'load' : 'DOMContentLoaded';
  161. let self = this;
  162. this._loadEvent = eventName;
  163. frame.addEventListener(eventName,
  164. this._loadListener = function _onReady(event) {
  165. if (event.target != frame.contentDocument)
  166. return;
  167. self._unregisterListener();
  168. self._onInit();
  169. }, true);
  170. },
  171. /**
  172. * Unregister listener that watchs for document being ready to be injected.
  173. * This listener is registered in `Symbiont._initFrame`.
  174. */
  175. _unregisterListener: function _unregisterListener() {
  176. if (this._waitForFrame) {
  177. off('content-document-global-created', this._waitForFrame);
  178. delete this._waitForFrame;
  179. }
  180. if (!this._loadListener)
  181. return;
  182. if (this._loadEvent == "start") {
  183. off('document-element-inserted', this._loadListener);
  184. }
  185. else {
  186. this._frame.removeEventListener(this._loadEvent, this._loadListener,
  187. true);
  188. }
  189. this._loadListener = null;
  190. },
  191. /**
  192. * Called by Symbiont itself when the frame is ready to load
  193. * content scripts according to contentScriptWhen. Overloaded by Panel.
  194. */
  195. _onInit: function () {
  196. this._initWorker({ window: this._frame.contentWindow });
  197. }
  198. });
  199. exports.Symbiont = Symbiont;