summaryrefslogtreecommitdiff
path: root/components/jetpack/sdk/deprecated/sync-worker.js
blob: 71cadac364d7e4ca8035628e380727b7f29f48e8 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* 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/. */

/**
 *
 * `deprecated/sync-worker` was previously `content/worker`, that was
 * incompatible with e10s. we are in the process of switching to the new
 * asynchronous `Worker`, which behaves slightly differently in some edge
 * cases, so we are keeping this one around for a short period.
 * try to switch to the new one as soon as possible..
 *
 */

"use strict";

module.metadata = {
  "stability": "unstable"
};

const { Class } = require('../core/heritage');
const { EventTarget } = require('../event/target');
const { on, off, emit, setListeners } = require('../event/core');
const {
  attach, detach, destroy
} = require('../content/utils');
const { method } = require('../lang/functional');
const { Ci, Cu, Cc } = require('chrome');
const unload = require('../system/unload');
const events = require('../system/events');
const { getInnerId } = require("../window/utils");
const { WorkerSandbox } = require('../content/sandbox');
const { isPrivate } = require('../private-browsing/utils');

// A weak map of workers to hold private attributes that
// should not be exposed
const workers = new WeakMap();

var modelFor = (worker) => workers.get(worker);

const ERR_DESTROYED =
  "Couldn't find the worker to receive this message. " +
  "The script may not be initialized yet, or may already have been unloaded.";

const ERR_FROZEN = "The page is currently hidden and can no longer be used " +
                   "until it is visible again.";

/**
 * Message-passing facility for communication between code running
 * in the content and add-on process.
 * @see https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/content_worker
 */
const Worker = Class({
  implements: [EventTarget],
  initialize: function WorkerConstructor (options) {
    // Save model in weak map to not expose properties
    let model = createModel();
    workers.set(this, model);

    options = options || {};

    if ('contentScriptFile' in options)
      this.contentScriptFile = options.contentScriptFile;
    if ('contentScriptOptions' in options)
      this.contentScriptOptions = options.contentScriptOptions;
    if ('contentScript' in options)
      this.contentScript = options.contentScript;
    if ('injectInDocument' in options)
      this.injectInDocument = !!options.injectInDocument;

    setListeners(this, options);

    unload.ensure(this, "destroy");

    // Ensure that worker.port is initialized for contentWorker to be able
    // to send events during worker initialization.
    this.port = createPort(this);

    model.documentUnload = documentUnload.bind(this);
    model.pageShow = pageShow.bind(this);
    model.pageHide = pageHide.bind(this);

    if ('window' in options)
      attach(this, options.window);
  },

  /**
   * Sends a message to the worker's global scope. Method takes single
   * argument, which represents data to be sent to the worker. The data may
   * be any primitive type value or `JSON`. Call of this method asynchronously
   * emits `message` event with data value in the global scope of this
   * worker.
   *
   * `message` event listeners can be set either by calling
   * `self.on` with a first argument string `"message"` or by
   * implementing `onMessage` function in the global scope of this worker.
   * @param {Number|String|JSON} data
   */
  postMessage: function (...data) {
    let model = modelFor(this);
    let args = ['message'].concat(data);
    if (!model.inited) {
      model.earlyEvents.push(args);
      return;
    }
    processMessage.apply(null, [this].concat(args));
  },

  get url () {
    let model = modelFor(this);
    // model.window will be null after detach
    return model.window ? model.window.document.location.href : null;
  },

  get contentURL () {
    let model = modelFor(this);
    return model.window ? model.window.document.URL : null;
  },

  // Implemented to provide some of the previous features of exposing sandbox
  // so that Worker can be extended
  getSandbox: function () {
    return modelFor(this).contentWorker;
  },

  toString: function () { return '[object Worker]'; },
  attach: method(attach),
  detach: method(detach),
  destroy: method(destroy)
});
exports.Worker = Worker;

attach.define(Worker, function (worker, window) {
  let model = modelFor(worker);
  model.window = window;
  // Track document unload to destroy this worker.
  // We can't watch for unload event on page's window object as it
  // prevents bfcache from working:
  // https://developer.mozilla.org/En/Working_with_BFCache
  model.windowID = getInnerId(model.window);
  events.on("inner-window-destroyed", model.documentUnload);

  // will set model.contentWorker pointing to the private API:
  model.contentWorker = WorkerSandbox(worker, model.window);

  // Listen to pagehide event in order to freeze the content script
  // while the document is frozen in bfcache:
  model.window.addEventListener("pageshow", model.pageShow, true);
  model.window.addEventListener("pagehide", model.pageHide, true);

  // Mainly enable worker.port.emit to send event to the content worker
  model.inited = true;
  model.frozen = false;

  // Fire off `attach` event
  emit(worker, 'attach', window);

  // Process all events and messages that were fired before the
  // worker was initialized.
  model.earlyEvents.forEach(args => processMessage.apply(null, [worker].concat(args)));
});

/**
 * Remove all internal references to the attached document
 * Tells _port to unload itself and removes all the references from itself.
 */
detach.define(Worker, function (worker, reason) {
  let model = modelFor(worker);

  // maybe unloaded before content side is created
  if (model.contentWorker) {
    model.contentWorker.destroy(reason);
  }

  model.contentWorker = null;
  if (model.window) {
    model.window.removeEventListener("pageshow", model.pageShow, true);
    model.window.removeEventListener("pagehide", model.pageHide, true);
  }
  model.window = null;
  // This method may be called multiple times,
  // avoid dispatching `detach` event more than once
  if (model.windowID) {
    model.windowID = null;
    events.off("inner-window-destroyed", model.documentUnload);
    model.earlyEvents.length = 0;
    emit(worker, 'detach');
  }
  model.inited = false;
});

isPrivate.define(Worker, ({ tab }) => isPrivate(tab));

/**
 * Tells content worker to unload itself and
 * removes all the references from itself.
 */
destroy.define(Worker, function (worker, reason) {
  detach(worker, reason);
  modelFor(worker).inited = true;
  // Specifying no type or listener removes all listeners
  // from target
  off(worker);
  off(worker.port);
});

/**
 * Events fired by workers
 */
function documentUnload ({ subject, data }) {
  let model = modelFor(this);
  let innerWinID = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
  if (innerWinID != model.windowID) return false;
  detach(this);
  return true;
}

function pageShow () {
  let model = modelFor(this);
  model.contentWorker.emitSync('pageshow');
  emit(this, 'pageshow');
  model.frozen = false;
}

function pageHide () {
  let model = modelFor(this);
  model.contentWorker.emitSync('pagehide');
  emit(this, 'pagehide');
  model.frozen = true;
}

/**
 * Fired from postMessage and emitEventToContent, or from the earlyMessage
 * queue when fired before the content is loaded. Sends arguments to
 * contentWorker if able
 */

function processMessage (worker, ...args) {
  let model = modelFor(worker) || {};
  if (!model.contentWorker)
    throw new Error(ERR_DESTROYED);
  if (model.frozen)
    throw new Error(ERR_FROZEN);
  model.contentWorker.emit.apply(null, args);
}

function createModel () {
  return {
    // List of messages fired before worker is initialized
    earlyEvents: [],
    // Is worker connected to the content worker sandbox ?
    inited: false,
    // Is worker being frozen? i.e related document is frozen in bfcache.
    // Content script should not be reachable if frozen.
    frozen: true,
    /**
     * Reference to the content side of the worker.
     * @type {WorkerGlobalScope}
     */
    contentWorker: null,
    /**
     * Reference to the window that is accessible from
     * the content scripts.
     * @type {Object}
     */
    window: null
  };
}

function createPort (worker) {
  let port = EventTarget();
  port.emit = emitEventToContent.bind(null, worker);
  return port;
}

/**
 * Emit a custom event to the content script,
 * i.e. emit this event on `self.port`
 */
function emitEventToContent (worker, ...eventArgs) {
  let model = modelFor(worker);
  let args = ['event'].concat(eventArgs);
  if (!model.inited) {
    model.earlyEvents.push(args);
    return;
  }
  processMessage.apply(null, [worker].concat(args));
}