frame.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. const { Ci } = require("chrome");
  6. const { InputPort } = require("./system");
  7. const { getFrameElement, getOuterId,
  8. getOwnerBrowserWindow } = require("../window/utils");
  9. const { isnt } = require("../lang/functional");
  10. const { foldp, lift, merges, keepIf } = require("../event/utils");
  11. const { object } = require("../util/sequence");
  12. const { compose } = require("../lang/functional");
  13. const { LastClosed } = require("./browser");
  14. const { patch } = require("diffpatcher/index");
  15. const Document = Ci.nsIDOMDocument;
  16. const isntNull = isnt(null);
  17. const frameID = frame => frame.id;
  18. const browserID = compose(getOuterId, getOwnerBrowserWindow);
  19. const isInnerFrame = frame =>
  20. frame && frame.hasAttribute("data-is-sdk-inner-frame");
  21. // Utility function that given content window loaded in our frame views returns
  22. // an actual frame. This basically takes care of fact that actual frame document
  23. // is loaded in the nested iframe. If content window is not loaded in the nested
  24. // frame of the frame view it returs null.
  25. const getFrame = document =>
  26. document && document.defaultView && getFrameElement(document.defaultView);
  27. const FrameInput = function(options) {
  28. const input = keepIf(isInnerFrame, null,
  29. lift(getFrame, new InputPort(options)));
  30. return lift(frame => {
  31. if (!frame) return frame;
  32. const [id, owner] = [frameID(frame), browserID(frame)];
  33. return object([id, {owners: object([owner, options.update])}]);
  34. }, input);
  35. };
  36. const LastLoading = new FrameInput({topic: "document-element-inserted",
  37. update: {readyState: "loading"}});
  38. exports.LastLoading = LastLoading;
  39. const LastInteractive = new FrameInput({topic: "content-document-interactive",
  40. update: {readyState: "interactive"}});
  41. exports.LastInteractive = LastInteractive;
  42. const LastLoaded = new FrameInput({topic: "content-document-loaded",
  43. update: {readyState: "complete"}});
  44. exports.LastLoaded = LastLoaded;
  45. const LastUnloaded = new FrameInput({topic: "content-page-hidden",
  46. update: null});
  47. exports.LastUnloaded = LastUnloaded;
  48. // Represents state of SDK frames in form of data structure:
  49. // {"frame#1": {"id": "frame#1",
  50. // "inbox": {"data": "ping",
  51. // "target": {"id": "frame#1", "owner": "outerWindowID#2"},
  52. // "source": {"id": "frame#1"}}
  53. // "url": "resource://addon-1/data/index.html",
  54. // "owners": {"outerWindowID#1": {"readyState": "loading"},
  55. // "outerWindowID#2": {"readyState": "complete"}}
  56. //
  57. //
  58. // frame#2: {"id": "frame#2",
  59. // "url": "resource://addon-1/data/main.html",
  60. // "outbox": {"data": "pong",
  61. // "source": {"id": "frame#2", "owner": "outerWindowID#1"}
  62. // "target": {"id": "frame#2"}}
  63. // "owners": {outerWindowID#1: {readyState: "interacitve"}}}}
  64. const Frames = foldp(patch, {}, merges([
  65. LastLoading,
  66. LastInteractive,
  67. LastLoaded,
  68. LastUnloaded,
  69. new InputPort({ id: "frame-mailbox" }),
  70. new InputPort({ id: "frame-change" }),
  71. new InputPort({ id: "frame-changed" })
  72. ]));
  73. exports.Frames = Frames;