Skip to content

Commit

Permalink
Bug 1765167 - Part 2: Stop using Cu.import in devtools/. r=mossop
Browse files Browse the repository at this point in the history
  • Loading branch information
arai-a committed May 3, 2022
1 parent 7dfdb15 commit 8c9eb71
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 15 deletions.
8 changes: 6 additions & 2 deletions devtools/client/inspector/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
var isInChrome = window.location.href.includes("chrome:");
if (isInChrome) {
var exports = {};
var { require, loader } = Cu.import("resource://devtools/shared/loader/Loader.jsm");
var { BrowserLoader } = Cu.import("resource://devtools/shared/loader/browser-loader.js");
var { require, loader } = ChromeUtils.import(
"resource://devtools/shared/loader/Loader.jsm"
);
var { BrowserLoader } = ChromeUtils.import(
"resource://devtools/shared/loader/browser-loader.js"
);
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion devtools/docs/contributor/preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Services = require("Services");
In the rare event where you don't have access to the DevTools' require method, you can use

```javascript
const { Services } = Components.utils.import("resource://gre/modules/Services.jsm");
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
```

### Services.pref.get* and Services.pref.set*
Expand Down
2 changes: 1 addition & 1 deletion devtools/docs/user/browser_console/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ One exported symbol from ``Console.jsm`` is ``console``. Below is an example of

.. code-block:: JavaScript
Components.utils.import("resource://gre/modules/Console.jsm");
const { console } = ChromeUtils.import("resource://gre/modules/Console.jsm");
console.log("Hello from Firefox code"); //output messages to the console</pre>
Learn more:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ This page shows how to use the :doc:`Debugger API <../index>` to show how many o
// This defines the 'Debugger' constructor in this
// Scratchpad; it doesn't actually start debugging anything.
Components.utils.import('resource://gre/modules/jsdebugger.jsm');
const { addDebuggerToGlobal } = ChromeUtils.import(
'resource://gre/modules/jsdebugger.jsm'
);
addDebuggerToGlobal(window);
(function () {
Expand Down
8 changes: 6 additions & 2 deletions devtools/docs/user/debugger-api/tutorial-breakpoint/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ This tutorial was tested against Firefox 58 Beta and Nightly. It does not work i

.. code-block:: javascript
Components.utils.import("resource://gre/modules/jsdebugger.jsm");
Components.utils.import("resource://gre/modules/Console.jsm");
const { addDebuggerToGlobal } = ChromeUtils.import(
"resource://gre/modules/jsdebugger.jsm"
);
const { console } = ChromeUtils.import(
"resource://gre/modules/Console.jsm"
);
// This defines 'Debugger' in this Scratchpad;
// it doesn't actually start debugging anything.
Expand Down
11 changes: 8 additions & 3 deletions devtools/server/tests/xpcshell/head_dbg.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,15 @@ function createLongStringFront(conn, form) {
return front;
}

function createTestGlobal(name) {
const sandbox = Cu.Sandbox(
Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
function createTestGlobal(name, options) {
const principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
Ci.nsIPrincipal
);
// NOTE: The Sandbox constructor behaves differently based on the argument
// length.
const sandbox = options
? Cu.Sandbox(principal, options)
: Cu.Sandbox(principal);
sandbox.__name = name;
// Expose a few mocks to better represent a Window object.
// These attributes will be used by DOCUMENT_EVENT resource listener.
Expand Down
8 changes: 6 additions & 2 deletions devtools/server/tests/xpcshell/test_safe-getter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function run_test() {
"resource://gre/modules/jsdebugger.jsm"
);
addDebuggerToGlobal(this);
const g = createTestGlobal("test");
const g = createTestGlobal("test", {
wantGlobalProperties: ["ChromeUtils"],
});
const dbg = new Debugger();
const gw = dbg.addDebuggee(g);

Expand All @@ -20,7 +22,9 @@ function run_test() {
enumerable: true
});
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
// This is a CCW.
XPCOMUtils.defineLazyGetter(this, "foo", function() { return "foo"; });
Expand Down
12 changes: 9 additions & 3 deletions devtools/shared/loader/worker-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,16 @@ var {

// To ensure that the this passed to addDebuggerToGlobal is a global, the
// Debugger object needs to be defined in a sandbox.
const sandbox = Cu.Sandbox(principal, {});
const sandbox = Cu.Sandbox(principal, {
wantGlobalProperties: ["ChromeUtils"],
});
Cu.evalInSandbox(
"Components.utils.import('resource://gre/modules/jsdebugger.jsm');" +
"addDebuggerToGlobal(this);",
`
const { addDebuggerToGlobal } = ChromeUtils.import(
'resource://gre/modules/jsdebugger.jsm'
);
addDebuggerToGlobal(this);
`,
sandbox
);
const Debugger = sandbox.Debugger;
Expand Down

0 comments on commit 8c9eb71

Please sign in to comment.