Bug 1146603: Use messaging to set up the parent and child workers for tab.attach. r=jsantell

This commit is contained in:
Dave Townsend 2015-08-27 13:57:44 -07:00
parent 330049fd9e
commit 80328dc9dd
5 changed files with 61 additions and 24 deletions

View File

@ -5,6 +5,7 @@
const system = require('sdk/system/events');
const { frames } = require('sdk/remote/child');
const { WorkerChild } = require('sdk/content/worker-child');
// map observer topics to tab event names
const EVENTS = {
@ -34,3 +35,8 @@ function eventListener({target, type, persisted}) {
frame.port.emit('sdk/tab/event', type, persisted);
}
frames.addEventListener('pageshow', eventListener, true);
frames.port.on('sdk/tab/attach', (frame, options) => {
options.window = frame.content;
new WorkerChild(options);
});

View File

@ -12,6 +12,7 @@ let { data } = require('../self');
let assetsURI = data.url();
let isArray = Array.isArray;
let method = require('../../method/core');
let { uuid } = require('../util/uuid');
const isAddonContent = ({ contentURL }) =>
contentURL && data.url(contentURL).startsWith(assetsURI);
@ -84,3 +85,21 @@ function WorkerHost (workerFor) {
}
}
exports.WorkerHost = WorkerHost;
function makeChildOptions(options) {
function makeStringArray(arrayOrValue) {
if (!arrayOrValue)
return [];
return [String(v) for (v of [].concat(arrayOrValue))];
}
return {
id: String(uuid()),
contentScript: makeStringArray(options.contentScript),
contentScriptFile: makeStringArray(options.contentScriptFile),
contentScriptOptions: options.contentScriptOptions ?
JSON.stringify(options.contentScriptOptions) :
null,
}
}
exports.makeChildOptions = makeChildOptions;

View File

@ -15,10 +15,9 @@ const { getInnerId } = require('../window/utils');
const { EventTarget } = require('../event/target');
const { isPrivate } = require('../private-browsing/utils');
const { getTabForBrowser, getTabForContentWindow, getBrowserForTab } = require('../tabs/utils');
const { attach, connect, detach, destroy } = require('./utils');
const { attach, connect, detach, destroy, makeChildOptions } = require('./utils');
const { ensure } = require('../system/unload');
const { on: observe } = require('../system/events');
const { uuid } = require('../util/uuid');
const { Ci } = require('chrome');
const { modelFor: tabFor } = require('sdk/model/core');
const { remoteRequire, processes, frames } = require('../remote/parent');
@ -128,26 +127,12 @@ attach.define(Worker, function(worker, window) {
if (tab)
frame = frames.getFrameForBrowser(getBrowserForTab(tab));
function makeStringArray(arrayOrValue) {
if (!arrayOrValue)
return [];
return [String(v) for (v of [].concat(arrayOrValue))];
}
let id = String(uuid());
let childOptions = {
id,
windowId: getInnerId(window),
contentScript: makeStringArray(model.options.contentScript),
contentScriptFile: makeStringArray(model.options.contentScriptFile),
contentScriptOptions: model.options.contentScriptOptions ?
JSON.stringify(model.options.contentScriptOptions) :
null,
}
let childOptions = makeChildOptions(model.options);
childOptions.windowId = getInnerId(window);
processes.port.emit('sdk/worker/create', childOptions);
connect(worker, frame, { id, url: String(window.location) });
connect(worker, frame, { id: childOptions.id, url: String(window.location) });
})
connect.define(Worker, function(worker, frame, { id, url }) {

View File

@ -239,7 +239,7 @@ const FrameList = Class({
return frame;
}
return null;
}
},
});
let frames = exports.frames = new FrameList();

View File

@ -191,10 +191,37 @@ const Tab = Class({
if (isDestroyed(this))
return;
// BUG 792946 https://bugzilla.mozilla.org/show_bug.cgi?id=792946
// TODO: fix this circular dependency
let { Worker } = require('./worker');
return Worker(options, browser(this).contentWindow);
let { Worker } = require('../content/worker');
let { connect, makeChildOptions } = require('../content/utils');
let worker = Worker(options);
worker.once("detach", () => {
worker.destroy();
});
let attach = frame => {
let childOptions = makeChildOptions(options);
frame.port.emit("sdk/tab/attach", childOptions);
connect(worker, frame, { id: childOptions.id, url: this.url });
};
// Do this synchronously if possible
let frame = frames.getFrameForBrowser(browser(this));
if (frame) {
attach(frame);
}
else {
let listener = (frame) => {
if (frame.frameElement != browser(this))
return;
listener.off("attach", listener);
attach(frame);
};
frames.on("attach", listener);
}
return worker;
},
destroy: function() {