summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/content/page-worker.js
blob: e9e741120b6b4b96ade13de7f9c641d9fa14accd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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";

const { frames } = require("../remote/child");
const { Class } = require("../core/heritage");
const { Disposable } = require('../core/disposable');
const { data } = require("../self");
const { once } = require("../dom/events");
const { getAttachEventType } = require("./utils");
const { Rules } = require('../util/rules');
const { uuid } = require('../util/uuid');
const { WorkerChild } = require("./worker-child");
const { Cc, Ci, Cu } = require("chrome");
const { observe } = require("../event/chrome");
const { on } = require("../event/core");

const appShell = Cc["@mozilla.org/appshell/appShellService;1"].getService(Ci.nsIAppShellService);

const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");

const pages = new Map();

const DOC_INSERTED = "document-element-inserted";

function isValidURL(page, url) {
  return !page.rules || page.rules.matchesAny(url);
}

const ChildPage = Class({
  implements: [ Disposable ],
  setup: function(frame, id, options) {
    this.id = id;
    this.frame = frame;
    this.options = options;

    this.webNav = appShell.createWindowlessBrowser(false);
    this.docShell.allowJavascript = this.options.allow.script;

    // Accessing the browser's window forces the initial about:blank document to
    // be created before we start listening for notifications
    this.contentWindow;

    this.webProgress.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_LOCATION);

    pages.set(this.id, this);

    this.contentURL = options.contentURL;

    if (options.include) {
      this.rules = Rules();
      this.rules.add.apply(this.rules, [].concat(options.include));
    }
  },

  dispose: function() {
    pages.delete(this.id);
    this.webProgress.removeProgressListener(this);
    this.webNav.close();
    this.webNav = null;
  },

  attachWorker: function() {
    if (!isValidURL(this, this.contentWindow.location.href))
      return;

    this.options.id = uuid().toString();
    this.options.window = this.contentWindow;
    this.frame.port.emit("sdk/frame/connect", this.id, {
      id: this.options.id,
      url: this.contentWindow.document.documentURIObject.spec
    });
    new WorkerChild(this.options);
  },

  get docShell() {
    return this.webNav.QueryInterface(Ci.nsIInterfaceRequestor)
                      .getInterface(Ci.nsIDocShell);
  },

  get webProgress() {
    return this.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIWebProgress);
  },

  get contentWindow() {
    return this.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIDOMWindow);
  },

  get contentURL() {
    return this.options.contentURL;
  },
  set contentURL(url) {
    this.options.contentURL = url;

    url = this.options.contentURL ? data.url(this.options.contentURL) : "about:blank";
    this.webNav.loadURI(url, Ci.nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
  },

  onLocationChange: function(progress, request, location, flags) {
    // Ignore inner-frame events
    if (progress != this.webProgress)
      return;
    // Ignore events that don't change the document
    if (flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT)
      return;

    let event = getAttachEventType(this.options);
    // Attaching at the start of the load is handled by the
    // document-element-inserted listener.
    if (event == DOC_INSERTED)
      return;

    once(this.contentWindow, event, () => {
      this.attachWorker();
    }, false);
  },

  QueryInterface: XPCOMUtils.generateQI(["nsIWebProgressListener", "nsISupportsWeakReference"])
});

on(observe(DOC_INSERTED), "data", ({ target }) => {
  let page = Array.from(pages.values()).find(p => p.contentWindow.document === target);
  if (!page)
    return;

  if (getAttachEventType(page.options) == DOC_INSERTED)
    page.attachWorker();
});

frames.port.on("sdk/frame/create", (frame, id, options) => {
  new ChildPage(frame, id, options);
});

frames.port.on("sdk/frame/set", (frame, id, params) => {
  let page = pages.get(id);
  if (!page)
    return;

  if ("allowScript" in params)
    page.docShell.allowJavascript = params.allowScript;
  if ("contentURL" in params)
    page.contentURL = params.contentURL;
});

frames.port.on("sdk/frame/destroy", (frame, id) => {
  let page = pages.get(id);
  if (!page)
    return;

  page.destroy();
});