2017-09-28 18:29:55 +03:00
|
|
|
/* 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";
|
|
|
|
|
2018-10-30 11:08:20 +03:00
|
|
|
const {Ci} = require("chrome");
|
|
|
|
const {rootSpec} = require("devtools/shared/specs/root");
|
2018-12-11 19:32:41 +03:00
|
|
|
const { FrontClassWithSpec, registerFront } = require("devtools/shared/protocol");
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-10-30 11:08:20 +03:00
|
|
|
loader.lazyRequireGetter(this, "getFront", "devtools/shared/protocol", true);
|
2018-11-15 13:22:49 +03:00
|
|
|
loader.lazyRequireGetter(this, "BrowsingContextTargetFront", "devtools/shared/fronts/targets/browsing-context", true);
|
2018-11-13 23:39:58 +03:00
|
|
|
loader.lazyRequireGetter(this, "ContentProcessTargetFront", "devtools/shared/fronts/targets/content-process", true);
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
class RootFront extends FrontClassWithSpec(rootSpec) {
|
|
|
|
constructor(client, form) {
|
2019-01-28 21:42:50 +03:00
|
|
|
super(client);
|
|
|
|
|
|
|
|
// Root Front is a special Front. It is the only one to set its actor ID manually
|
|
|
|
// out of the form object returned by RootActor.sayHello which is called when calling
|
|
|
|
// DebuggerClient.connect().
|
|
|
|
this.actorID = form.from;
|
2018-10-30 11:08:20 +03:00
|
|
|
|
|
|
|
this.applicationType = form.applicationType;
|
|
|
|
this.traits = form.traits;
|
|
|
|
|
|
|
|
// Cache root form as this will always be the same value.
|
|
|
|
Object.defineProperty(this, "rootForm", {
|
|
|
|
get() {
|
|
|
|
delete this.rootForm;
|
|
|
|
this.rootForm = this.getRoot();
|
|
|
|
return this.rootForm;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
});
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-10-30 11:08:20 +03:00
|
|
|
// Cache of already created global scoped fronts
|
|
|
|
// [typeName:string => Front instance]
|
|
|
|
this.fronts = new Map();
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-10-30 11:08:20 +03:00
|
|
|
this._client = client;
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2018-10-11 19:26:45 +03:00
|
|
|
|
2018-04-20 17:27:15 +03:00
|
|
|
/**
|
2018-06-05 01:30:06 +03:00
|
|
|
* Retrieve all service worker registrations as well as workers from the parent and
|
|
|
|
* content processes. Listing service workers involves merging information coming from
|
2018-04-20 17:27:15 +03:00
|
|
|
* registrations and workers, this method will combine this information to present a
|
|
|
|
* unified array of serviceWorkers. If you are only interested in other workers, use
|
|
|
|
* listWorkers.
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* - {Array} service
|
|
|
|
* array of form-like objects for serviceworkers
|
|
|
|
* - {Array} shared
|
2018-06-01 23:59:24 +03:00
|
|
|
* Array of WorkerTargetActor forms, containing shared workers.
|
2018-04-20 17:27:15 +03:00
|
|
|
* - {Array} other
|
2018-06-01 23:59:24 +03:00
|
|
|
* Array of WorkerTargetActor forms, containing other workers.
|
2018-04-20 17:27:15 +03:00
|
|
|
*/
|
2018-12-11 19:32:41 +03:00
|
|
|
async listAllWorkers() {
|
2018-04-27 22:41:10 +03:00
|
|
|
let registrations = [];
|
|
|
|
let workers = [];
|
2018-04-20 17:27:15 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
// List service worker registrations
|
|
|
|
({ registrations } = await this.listServiceWorkerRegistrations());
|
|
|
|
|
|
|
|
// List workers from the Parent process
|
|
|
|
({ workers } = await this.listWorkers());
|
|
|
|
|
|
|
|
// And then from the Child processes
|
2018-06-01 13:36:09 +03:00
|
|
|
const { processes } = await this.listProcesses();
|
|
|
|
for (const process of processes) {
|
2018-04-20 17:27:15 +03:00
|
|
|
// Ignore parent process
|
|
|
|
if (process.parent) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-15 13:22:49 +03:00
|
|
|
const front = await this.getProcess(process.id);
|
|
|
|
const response = await front.listWorkers();
|
2018-04-20 17:27:15 +03:00
|
|
|
workers = workers.concat(response.workers);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Something went wrong, maybe our client is disconnected?
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const result = {
|
2018-04-27 22:41:10 +03:00
|
|
|
service: [],
|
|
|
|
shared: [],
|
2018-10-19 15:55:39 +03:00
|
|
|
other: [],
|
2018-04-27 22:41:10 +03:00
|
|
|
};
|
|
|
|
|
2019-01-10 20:26:35 +03:00
|
|
|
registrations.forEach(front => {
|
|
|
|
// All the information is simply mirrored from the registration front.
|
|
|
|
// However since registering workers will fetch similar information from the worker
|
|
|
|
// target front and will not have a service worker registration front, consumers
|
|
|
|
// should not read meta data directly on the registration front instance.
|
2018-04-27 22:41:10 +03:00
|
|
|
result.service.push({
|
2019-01-10 20:26:35 +03:00
|
|
|
active: front.active,
|
|
|
|
fetch: front.fetch,
|
|
|
|
lastUpdateTime: front.lastUpdateTime,
|
|
|
|
name: front.url,
|
|
|
|
registrationFront: front,
|
|
|
|
scope: front.scope,
|
|
|
|
url: front.url,
|
2018-04-27 22:41:10 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-15 13:23:50 +03:00
|
|
|
workers.forEach(front => {
|
2018-06-01 13:36:09 +03:00
|
|
|
const worker = {
|
2018-11-15 13:23:50 +03:00
|
|
|
name: front.url,
|
|
|
|
url: front.url,
|
|
|
|
workerTargetFront: front,
|
2018-04-27 22:41:10 +03:00
|
|
|
};
|
2018-11-15 13:23:50 +03:00
|
|
|
switch (front.type) {
|
2018-04-27 22:41:10 +03:00
|
|
|
case Ci.nsIWorkerDebugger.TYPE_SERVICE:
|
2018-11-15 13:23:50 +03:00
|
|
|
const registration = result.service.find(r => r.scope === front.scope);
|
2018-04-27 22:41:10 +03:00
|
|
|
if (registration) {
|
|
|
|
// XXX: Race, sometimes a ServiceWorkerRegistrationInfo doesn't
|
|
|
|
// have a scriptSpec, but its associated WorkerDebugger does.
|
|
|
|
if (!registration.url) {
|
2018-11-15 13:23:50 +03:00
|
|
|
registration.name = registration.url = front.url;
|
2018-04-27 22:41:10 +03:00
|
|
|
}
|
2018-11-15 13:23:50 +03:00
|
|
|
registration.workerTargetFront = front;
|
2018-04-27 22:41:10 +03:00
|
|
|
} else {
|
2018-11-15 13:23:50 +03:00
|
|
|
worker.fetch = front.fetch;
|
2018-04-27 22:41:10 +03:00
|
|
|
|
|
|
|
// If a service worker registration could not be found, this means we are in
|
|
|
|
// e10s, and registrations are not forwarded to other processes until they
|
|
|
|
// reach the activated state. Augment the worker as a registration worker to
|
|
|
|
// display it in aboutdebugging.
|
2018-11-15 13:23:50 +03:00
|
|
|
worker.scope = front.scope;
|
2018-04-27 22:41:10 +03:00
|
|
|
worker.active = false;
|
|
|
|
result.service.push(worker);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Ci.nsIWorkerDebugger.TYPE_SHARED:
|
|
|
|
result.shared.push(worker);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result.other.push(worker);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-04-20 17:27:15 +03:00
|
|
|
return result;
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2018-04-20 17:27:15 +03:00
|
|
|
|
2018-11-13 17:07:36 +03:00
|
|
|
/**
|
|
|
|
* Fetch the ParentProcessTargetActor for the main process.
|
|
|
|
*
|
|
|
|
* `getProcess` requests allows to fetch the target actor for any process
|
|
|
|
* and the main process is having the process ID zero.
|
|
|
|
*/
|
|
|
|
getMainProcess() {
|
|
|
|
return this.getProcess(0);
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2018-11-13 17:07:36 +03:00
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
async getProcess(id) {
|
2018-11-15 13:22:49 +03:00
|
|
|
// Do not use specification automatic marshalling as getProcess may return
|
|
|
|
// two different type: ParentProcessTargetActor or ContentProcessTargetActor.
|
|
|
|
// Also, we do want to memoize the fronts and return already existing ones.
|
2018-12-11 19:32:41 +03:00
|
|
|
const { form } = await super.getProcess(id);
|
2018-11-15 13:22:49 +03:00
|
|
|
let front = this.actor(form.actor);
|
|
|
|
if (front) {
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
// getProcess may return a ContentProcessTargetActor or a ParentProcessTargetActor
|
|
|
|
// In most cases getProcess(0) will return the main process target actor,
|
|
|
|
// which is a ParentProcessTargetActor, but not in xpcshell, which uses a
|
|
|
|
// ContentProcessTargetActor. So select the right front based on the actor ID.
|
|
|
|
if (form.actor.includes("contentProcessTarget")) {
|
2019-01-28 21:42:50 +03:00
|
|
|
front = new ContentProcessTargetFront(this._client);
|
2018-11-15 13:22:49 +03:00
|
|
|
} else {
|
|
|
|
// ParentProcessTargetActor doesn't have a specific front, instead it uses
|
|
|
|
// BrowsingContextTargetFront on the client side.
|
2019-01-28 21:42:50 +03:00
|
|
|
front = new BrowsingContextTargetFront(this._client);
|
2018-11-15 13:22:49 +03:00
|
|
|
}
|
2019-01-28 21:42:50 +03:00
|
|
|
// As these fronts aren't instantiated by protocol.js, we have to set their actor ID
|
|
|
|
// manually like that:
|
|
|
|
front.actorID = form.actor;
|
2019-01-28 21:42:48 +03:00
|
|
|
front.form(form);
|
2018-11-15 13:22:49 +03:00
|
|
|
this.manage(front);
|
|
|
|
|
|
|
|
return front;
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2018-11-15 13:22:49 +03:00
|
|
|
|
2018-12-17 14:56:30 +03:00
|
|
|
/**
|
|
|
|
* Override default listTabs request in order to return a list of
|
|
|
|
* BrowsingContextTargetFronts while updating their selected state.
|
|
|
|
*/
|
|
|
|
async listTabs(options) {
|
|
|
|
const { selected, tabs } = await super.listTabs(options);
|
|
|
|
for (const i in tabs) {
|
|
|
|
tabs[i].setIsSelected(i == selected);
|
|
|
|
}
|
|
|
|
return tabs;
|
|
|
|
}
|
|
|
|
|
2017-09-28 18:29:55 +03:00
|
|
|
/**
|
2018-05-26 02:20:28 +03:00
|
|
|
* Fetch the target actor for the currently selected tab, or for a specific
|
2017-09-28 18:29:55 +03:00
|
|
|
* tab given as first parameter.
|
|
|
|
*
|
|
|
|
* @param [optional] object filter
|
|
|
|
* A dictionary object with following optional attributes:
|
|
|
|
* - outerWindowID: used to match tabs in parent process
|
|
|
|
* - tabId: used to match tabs in child processes
|
|
|
|
* - tab: a reference to xul:tab element
|
|
|
|
* If nothing is specified, returns the actor for the currently
|
|
|
|
* selected tab.
|
|
|
|
*/
|
2018-12-11 19:32:41 +03:00
|
|
|
async getTab(filter) {
|
2018-10-30 11:08:20 +03:00
|
|
|
const packet = {};
|
2017-09-28 18:29:55 +03:00
|
|
|
if (filter) {
|
|
|
|
if (typeof (filter.outerWindowID) == "number") {
|
|
|
|
packet.outerWindowID = filter.outerWindowID;
|
|
|
|
} else if (typeof (filter.tabId) == "number") {
|
|
|
|
packet.tabId = filter.tabId;
|
|
|
|
} else if ("tab" in filter) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const browser = filter.tab.linkedBrowser;
|
2017-09-28 18:29:55 +03:00
|
|
|
if (browser.frameLoader.tabParent) {
|
|
|
|
// Tabs in child process
|
|
|
|
packet.tabId = browser.frameLoader.tabParent.tabId;
|
|
|
|
} else if (browser.outerWindowID) {
|
|
|
|
// <xul:browser> tabs in parent process
|
|
|
|
packet.outerWindowID = browser.outerWindowID;
|
|
|
|
} else {
|
|
|
|
// <iframe mozbrowser> tabs in parent process
|
2018-07-25 02:47:41 +03:00
|
|
|
const windowUtils = browser.contentWindow.windowUtils;
|
2017-09-28 18:29:55 +03:00
|
|
|
packet.outerWindowID = windowUtils.outerWindowID;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Throw if a filter object have been passed but without
|
|
|
|
// any clearly idenfified filter.
|
|
|
|
throw new Error("Unsupported argument given to getTab request");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
return super.getTab(packet);
|
|
|
|
}
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-11-27 22:18:06 +03:00
|
|
|
/**
|
|
|
|
* Fetch the target front for a given add-on.
|
|
|
|
* This is just an helper on top of `listAddons` request.
|
|
|
|
*
|
|
|
|
* @param object filter
|
|
|
|
* A dictionary object with following attribute:
|
|
|
|
* - id: used to match the add-on to connect to.
|
|
|
|
*/
|
|
|
|
async getAddon({ id }) {
|
|
|
|
const addons = await this.listAddons();
|
|
|
|
const addonTargetFront = addons.find(addon => addon.id === id);
|
|
|
|
return addonTargetFront;
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2018-11-27 22:18:06 +03:00
|
|
|
|
2017-09-28 18:29:55 +03:00
|
|
|
/**
|
2018-10-30 11:08:20 +03:00
|
|
|
* Test request that returns the object passed as first argument.
|
2017-09-28 18:29:55 +03:00
|
|
|
*
|
2018-10-30 11:08:20 +03:00
|
|
|
* `echo` is special as all the property of the given object have to be passed
|
|
|
|
* on the packet object. That's not something that can be achieve by requester helper.
|
2017-09-28 18:29:55 +03:00
|
|
|
*/
|
|
|
|
|
2018-10-30 11:08:20 +03:00
|
|
|
echo(packet) {
|
|
|
|
packet.type = "echo";
|
2017-09-28 18:29:55 +03:00
|
|
|
return this.request(packet);
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
2017-09-28 18:29:55 +03:00
|
|
|
|
2018-08-21 14:46:46 +03:00
|
|
|
/*
|
|
|
|
* This function returns a protocol.js Front for any root actor.
|
|
|
|
* i.e. the one directly served from RootActor.listTabs or getRoot.
|
2018-08-13 19:38:55 +03:00
|
|
|
*
|
|
|
|
* @param String typeName
|
|
|
|
* The type name used in protocol.js's spec for this actor.
|
2018-08-21 14:46:46 +03:00
|
|
|
*/
|
|
|
|
async getFront(typeName) {
|
|
|
|
let front = this.fronts.get(typeName);
|
|
|
|
if (front) {
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
const rootForm = await this.rootForm;
|
|
|
|
front = getFront(this._client, typeName, rootForm);
|
|
|
|
this.fronts.set(typeName, front);
|
|
|
|
return front;
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-30 11:08:20 +03:00
|
|
|
exports.RootFront = RootFront;
|
2018-12-11 19:32:41 +03:00
|
|
|
registerFront(RootFront);
|