Bug 1503628 - Merge TabTarget and WorkerTarget. r=yulia

MozReview-Commit-ID: 58iL6HyXjM4

Depends on D11013

Differential Revision: https://phabricator.services.mozilla.com/D11014

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2018-11-08 10:09:10 +00:00
Родитель 1e489b18b6
Коммит 42da350649
2 изменённых файлов: 43 добавлений и 11 удалений

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

@ -126,7 +126,23 @@ const TargetFactory = exports.TargetFactory = {
forWorker: function(workerTargetFront) { forWorker: function(workerTargetFront) {
let target = targets.get(workerTargetFront); let target = targets.get(workerTargetFront);
if (target == null) { if (target == null) {
target = new WorkerTarget(workerTargetFront); target = new TabTarget({
client: workerTargetFront.client,
// Fake a form attribute until all TabTarget is merged with the Front itself
// and will receive form attribute natively.
get form() {
return {
actor: workerTargetFront.actorID,
traits: {},
// /!\ This depends on WorkerTargetFront.attach being called before this.
// It happens that WorkerTargetFront is instantiated from attachWorker,
// which instiate this class *and* calls `attach`.
consoleActor: workerTargetFront.consoleActor,
};
},
activeTab: workerTargetFront,
chrome: false,
});
targets.set(workerTargetFront, target); targets.set(workerTargetFront, target);
} }
return target; return target;
@ -190,15 +206,18 @@ const TargetFactory = exports.TargetFactory = {
* @param {Boolean} chrome * @param {Boolean} chrome
* True, if we allow to see privileged resources like JSM, xpcom, * True, if we allow to see privileged resources like JSM, xpcom,
* frame scripts... * frame scripts...
* @param {Front} activeTab (optional)
* If we already have a front for this target, pass it here.
* @param {xul:tab} tab (optional) * @param {xul:tab} tab (optional)
* If the target is a local Firefox tab, a reference to the firefox * If the target is a local Firefox tab, a reference to the firefox
* frontend tab object. * frontend tab object.
*/ */
function TabTarget({ form, client, chrome, tab = null }) { function TabTarget({ form, client, chrome, activeTab = null, tab = null }) {
EventEmitter.decorate(this); EventEmitter.decorate(this);
this.destroy = this.destroy.bind(this); this.destroy = this.destroy.bind(this);
this._onTabNavigated = this._onTabNavigated.bind(this); this._onTabNavigated = this._onTabNavigated.bind(this);
this.activeTab = this.activeConsole = null; this.activeConsole = null;
this.activeTab = activeTab;
this._form = form; this._form = form;
this._url = form.url; this._url = form.url;
@ -231,7 +250,7 @@ function TabTarget({ form, client, chrome, tab = null }) {
if (this._form.traits && ("isBrowsingContext" in this._form.traits)) { if (this._form.traits && ("isBrowsingContext" in this._form.traits)) {
this._isBrowsingContext = this._form.traits.isBrowsingContext; this._isBrowsingContext = this._form.traits.isBrowsingContext;
} else { } else {
this._isBrowsingContext = !this.isLegacyAddon && !this.isContentProcess; this._isBrowsingContext = !this.isLegacyAddon && !this.isContentProcess && !this.isWorkerTarget;
} }
// Cache of already created targed-scoped fronts // Cache of already created targed-scoped fronts
@ -416,6 +435,10 @@ TabTarget.prototype = {
return this.isLegacyAddon || this.isWebExtension; return this.isLegacyAddon || this.isWebExtension;
}, },
get isWorkerTarget() {
return this.activeTab && this.activeTab.typeName === "workerTarget";
},
get isLegacyAddon() { get isLegacyAddon() {
return !!(this._form && this._form.actor && return !!(this._form && this._form.actor &&
this._form.actor.match(/conn\d+\.addon(Target)?\d+/)); this._form.actor.match(/conn\d+\.addon(Target)?\d+/));
@ -550,6 +573,9 @@ TabTarget.prototype = {
} else if (this.isLegacyAddon) { } else if (this.isLegacyAddon) {
const [, addonTargetFront] = await this._client.attachAddon(this._form); const [, addonTargetFront] = await this._client.attachAddon(this._form);
this.activeTab = addonTargetFront; this.activeTab = addonTargetFront;
} else if (this.isWorkerTarget) {
// Worker target is the first target to have its front already instantiated.
// The plan is to have all targets to have its front passed as constructor argument.
} else { } else {
throw new Error(`Unsupported type of target. Expected target of one of the` + throw new Error(`Unsupported type of target. Expected target of one of the` +
` following types: BrowsingContext, ContentProcess, or Addon (legacy).`); ` following types: BrowsingContext, ContentProcess, or Addon (legacy).`);

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

@ -47,12 +47,21 @@ const WorkerTargetFront = protocol.FrontClassWithSpec(workerTargetSpec, {
this.url = response.url; this.url = response.url;
// Immediately call `connect` in other to fetch console and thread actors
// that will be later used by Target.
const connectResponse = await this.connect({});
this.consoleActor = connectResponse.consoleActor;
this.threadActor = connectResponse.threadActor;
return response; return response;
}, { }, {
impl: "_attach", impl: "_attach",
}), }),
detach: custom(async function() { detach: custom(async function() {
if (this.isClosed) {
return {};
}
let response; let response;
try { try {
response = await this._detach(); response = await this._detach();
@ -81,18 +90,15 @@ const WorkerTargetFront = protocol.FrontClassWithSpec(workerTargetSpec, {
return response; return response;
} }
// The connect call on server doesn't attach the thread as of version 44. const attachResponse = await this.client.request({
const connectResponse = await this.connect(options); to: this.threadActor,
await this.client.request({
to: connectResponse.threadActor,
type: "attach", type: "attach",
options, options,
}); });
this.thread = new ThreadClient(this, connectResponse.threadActor); this.thread = new ThreadClient(this, this.threadActor);
this.consoleActor = connectResponse.consoleActor;
this.client.registerClient(this.thread); this.client.registerClient(this.thread);
return [connectResponse, this.thread]; return [attachResponse, this.thread];
}, },
}); });