gecko-dev/browser/components/extensions/child/ext-devtools-panels.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

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

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

341 lines
10 KiB
JavaScript

/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
ChromeUtils.defineModuleGetter(this, "ExtensionChildDevToolsUtils",
"resource://gre/modules/ExtensionChildDevToolsUtils.jsm");
var {
promiseDocumentLoaded,
} = ExtensionUtils;
/**
* Represents an addon devtools panel in the child process.
*
* @param {DevtoolsExtensionContext}
* A devtools extension context running in a child process.
* @param {object} panelOptions
* @param {string} panelOptions.id
* The id of the addon devtools panel registered in the main process.
*/
class ChildDevToolsPanel extends ExtensionCommon.EventEmitter {
constructor(context, {id}) {
super();
this.context = context;
this.context.callOnClose(this);
this.id = id;
this._panelContext = null;
this.mm = context.messageManager;
this.mm.addMessageListener("Extension:DevToolsPanelShown", this);
this.mm.addMessageListener("Extension:DevToolsPanelHidden", this);
}
get panelContext() {
if (this._panelContext) {
return this._panelContext;
}
for (let view of this.context.extension.devtoolsViews) {
if (view.viewType === "devtools_panel" &&
view.devtoolsToolboxInfo.toolboxPanelId === this.id) {
this._panelContext = view;
// Reset the cached _panelContext property when the view is closed.
view.callOnClose({
close: () => {
this._panelContext = null;
},
});
return view;
}
}
return null;
}
receiveMessage({name, data}) {
// Filter out any message that is not related to the id of this
// toolbox panel.
if (!data || data.toolboxPanelId !== this.id) {
return;
}
switch (name) {
case "Extension:DevToolsPanelShown":
// Filter out *Shown message received while the panel context do not yet
// exist.
if (!this.panelContext || !this.panelContext.contentWindow) {
return;
}
this.onParentPanelShown();
break;
case "Extension:DevToolsPanelHidden":
this.onParentPanelHidden();
break;
}
}
onParentPanelShown() {
const {document} = this.panelContext.contentWindow;
// Ensure that the onShown event is fired when the panel document has
// been fully loaded.
promiseDocumentLoaded(document).then(() => {
this.emit("shown", this.panelContext.contentWindow);
});
}
onParentPanelHidden() {
this.emit("hidden");
}
api() {
return {
onShown: new EventManager({
context: this.context,
name: "devtoolsPanel.onShown",
register: fire => {
const listener = (eventName, panelContentWindow) => {
fire.asyncWithoutClone(panelContentWindow);
};
this.on("shown", listener);
return () => {
this.off("shown", listener);
};
},
}).api(),
onHidden: new EventManager({
context: this.context,
name: "devtoolsPanel.onHidden",
register: fire => {
const listener = () => {
fire.async();
};
this.on("hidden", listener);
return () => {
this.off("hidden", listener);
};
},
}).api(),
// TODO(rpl): onSearch event and createStatusBarButton method
};
}
close() {
this.mm.removeMessageListener("Extension:DevToolsPanelShown", this);
this.mm.removeMessageListener("Extension:DevToolsPanelHidden", this);
this._panelContext = null;
this.context = null;
}
}
/**
* Represents an addon devtools inspector sidebar in the child process.
*
* @param {DevtoolsExtensionContext}
* A devtools extension context running in a child process.
* @param {object} sidebarOptions
* @param {string} sidebarOptions.id
* The id of the addon devtools sidebar registered in the main process.
*/
class ChildDevToolsInspectorSidebar extends ExtensionCommon.EventEmitter {
constructor(context, {id}) {
super();
this.context = context;
this.context.callOnClose(this);
this.id = id;
this.mm = context.messageManager;
this.mm.addMessageListener("Extension:DevToolsInspectorSidebarShown", this);
this.mm.addMessageListener("Extension:DevToolsInspectorSidebarHidden", this);
}
close() {
this.mm.removeMessageListener("Extension:DevToolsInspectorSidebarShown", this);
this.mm.removeMessageListener("Extension:DevToolsInspectorSidebarHidden", this);
this.content = null;
}
receiveMessage({name, data}) {
// Filter out any message that is not related to the id of this
// toolbox panel.
if (!data || data.inspectorSidebarId !== this.id) {
return;
}
switch (name) {
case "Extension:DevToolsInspectorSidebarShown":
this.onParentSidebarShown();
break;
case "Extension:DevToolsInspectorSidebarHidden":
this.onParentSidebarHidden();
break;
}
}
onParentSidebarShown() {
// TODO: wait and emit sidebar contentWindow once sidebar.setPage is supported.
this.emit("shown");
}
onParentSidebarHidden() {
this.emit("hidden");
}
api() {
const {context, id} = this;
let extensionURL = new URL("/", context.uri.spec);
// This is currently needed by sidebar.setPage because API objects are not automatically wrapped
// by the API Schema validations and so the ExtensionURL type used in the JSON schema
// doesn't have any effect on the parameter received by the setPage API method.
function resolveExtensionURL(url) {
let sidebarPageURL = new URL(url, context.uri.spec);
if (extensionURL.protocol !== sidebarPageURL.protocol ||
extensionURL.host !== sidebarPageURL.host) {
throw new context.cloneScope.Error(
`Invalid sidebar URL: ${sidebarPageURL.href} is not a valid extension URL`);
}
return sidebarPageURL.href;
}
return {
onShown: new EventManager({
context,
name: "devtoolsInspectorSidebar.onShown",
register: fire => {
const listener = (eventName, panelContentWindow) => {
fire.asyncWithoutClone(panelContentWindow);
};
this.on("shown", listener);
return () => {
this.off("shown", listener);
};
},
}).api(),
onHidden: new EventManager({
context,
name: "devtoolsInspectorSidebar.onHidden",
register: fire => {
const listener = () => {
fire.async();
};
this.on("hidden", listener);
return () => {
this.off("hidden", listener);
};
},
}).api(),
setPage(extensionPageURL) {
let resolvedSidebarURL = resolveExtensionURL(extensionPageURL);
return context.childManager.callParentAsyncFunction(
"devtools.panels.elements.Sidebar.setPage",
[id, resolvedSidebarURL]
);
},
setObject(jsonObject, rootTitle) {
return context.cloneScope.Promise.resolve().then(() => {
return context.childManager.callParentAsyncFunction(
"devtools.panels.elements.Sidebar.setObject",
[id, jsonObject, rootTitle]
);
});
},
setExpression(evalExpression, rootTitle) {
return context.cloneScope.Promise.resolve().then(() => {
return context.childManager.callParentAsyncFunction(
"devtools.panels.elements.Sidebar.setExpression",
[id, evalExpression, rootTitle]
);
});
},
};
}
}
this.devtools_panels = class extends ExtensionAPI {
getAPI(context) {
const themeChangeObserver = ExtensionChildDevToolsUtils.getThemeChangeObserver();
return {
devtools: {
panels: {
elements: {
createSidebarPane(title) {
// NOTE: this is needed to be able to return to the caller (the extension)
// a promise object that it had the privileges to use (e.g. by marking this
// method async we will return a promise object which can only be used by
// chrome privileged code).
return context.cloneScope.Promise.resolve().then(async () => {
const sidebarId = await context.childManager.callParentAsyncFunction(
"devtools.panels.elements.createSidebarPane", [title]);
const sidebar = new ChildDevToolsInspectorSidebar(context, {id: sidebarId});
const sidebarAPI = Cu.cloneInto(sidebar.api(),
context.cloneScope,
{cloneFunctions: true});
return sidebarAPI;
});
},
},
create(title, icon, url) {
// NOTE: this is needed to be able to return to the caller (the extension)
// a promise object that it had the privileges to use (e.g. by marking this
// method async we will return a promise object which can only be used by
// chrome privileged code).
return context.cloneScope.Promise.resolve().then(async () => {
const panelId = await context.childManager.callParentAsyncFunction(
"devtools.panels.create", [title, icon, url]);
const devtoolsPanel = new ChildDevToolsPanel(context, {id: panelId});
const devtoolsPanelAPI = Cu.cloneInto(devtoolsPanel.api(),
context.cloneScope,
{cloneFunctions: true});
return devtoolsPanelAPI;
});
},
get themeName() {
return themeChangeObserver.themeName;
},
onThemeChanged: new EventManager({
context,
name: "devtools.panels.onThemeChanged",
register: fire => {
const listener = (eventName, themeName) => {
fire.async(themeName);
};
themeChangeObserver.on("themeChanged", listener);
return () => {
themeChangeObserver.off("themeChanged", listener);
};
},
}).api(),
},
},
};
}
};