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

--HG--
extra : commitid : 1eKD6egU5SB
extra : rebase_source : 3d62cc6ea611d057397d7e38f1c9a32a6c0dc062
This commit is contained in:
Dave Townsend 2015-08-27 13:57:44 -07:00
Родитель 1ec66fe1ab
Коммит a176d489a0
4 изменённых файлов: 44 добавлений и 23 удалений

Просмотреть файл

@ -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);
});

Просмотреть файл

@ -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;

Просмотреть файл

@ -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 }) {

Просмотреть файл

@ -191,10 +191,21 @@ 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 frame = frames.getFrameForBrowser(browser(this));
let childOptions = makeChildOptions(options);
frame.port.emit('sdk/tab/attach', childOptions);
let worker = Worker(options);
worker.once("detach", () => {
worker.destroy();
});
connect(worker, frame, { id: childOptions.id, url: this.url });
return worker;
},
destroy: function() {