Bug 1667455 - Part 14: Stop importing Services.jsm in documents. r=kmag,necko-reviewers,dragana

Differential Revision: https://phabricator.services.mozilla.com/D150903
This commit is contained in:
Tooru Fujisawa 2022-07-11 12:41:54 +00:00
parent a04d47efb9
commit c26c3f5551
10 changed files with 21 additions and 30 deletions

View File

@ -50,11 +50,7 @@ To require Services and use Services.pref, you can normally use the following sn
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 } = ChromeUtils.import("resource://gre/modules/Services.jsm");
```
In the rare event where you don't have access to the DevTools' `require` method, if it's chrome-privileged environment, the `Services` object should already be available in the global variable.
### Services.pref.get* and Services.pref.set*

View File

@ -11,7 +11,7 @@ Examples of incorrect code for this rule:
const obj = {};
ChromeUtils.defineESModuleGetters(obj, {
Services: “resource://gre/modules/Services.sys.mjs”,
AppConstants: “resource://gre/modules/AppConstants.sys.mjs”,
});
Examples of correct code for this rule:
@ -21,5 +21,5 @@ Examples of correct code for this rule:
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
Services: “resource://gre/modules/Services.sys.mjs”,
AppConstants: “resource://gre/modules/AppConstants.sys.mjs”,
});

View File

@ -10,13 +10,13 @@ Examples of incorrect code for this rule:
.. code-block:: js
ChromeUtils.import("resource://gre/modules/Services.jsm", this);
ChromeUtils.import("resource://gre/modules/Services.jsm", null);
ChromeUtils.import("resource://gre/modules/Services.jsm", {});
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", this);
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", null);
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", {});
Examples of correct code for this rule:
---------------------------------------
.. code-block:: js
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { AppConstants } = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");

View File

@ -10,7 +10,7 @@ Examples of incorrect code for this rule:
.. code-block:: js
ChromeUtils.defineESModuleGetters(lazy, {
Services: "resource://gre/modules/Services.sys.mjs",
AppConstants: "resource://gre/modules/AppConstants.sys.mjs",
});
XPCOMUtils.defineLazyModuleGetters(lazy, {
XPCOMUtils: "resource://gre/modules/XPCOMUtils.jsm",
@ -26,7 +26,7 @@ Examples of correct code for this rule:
.. code-block:: js
import { Services } from "resource://gre/modules/Services.sys.mjs";
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);

View File

@ -15,5 +15,5 @@ Examples of incorrect code for this rule:
globalThis.foo = 10;
Object.defineProperty(globalThis, "bar", { value: 20});
XPCOMUtils.defineLazyModuleGetters(globalThis, {
Services: "resource://gre/modules/Services.jsm",
AppConstants: "resource://gre/modules/AppConstants.jsm",
});

View File

@ -16,7 +16,7 @@ Inside a non-system module:
.. code-block:: js
import { Services } from "resource://gre/modules/Services.sys.mjs";
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
Examples of correct code for this rule:
---------------------------------------
@ -25,12 +25,12 @@ Inside a non-system module:
.. code-block:: js
const { Services } = ChromeUtils.importESM(
"resource://gre/modules/Services.sys.mjs"
const { AppConstants } = ChromeUtils.importESM(
"resource://gre/modules/AppConstants.sys.mjs"
);
Inside a system module:
.. code-block:: js
import { Services } from "resource://gre/modules/Services.sys.mjs";
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";

View File

@ -10,15 +10,15 @@ Examples of incorrect code for this rule:
.. code-block:: js
Components.utils.import("resource://gre/modules/Services.jsm", this);
XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/AppConstants.jsm", this);
XPCOMUtils.defineLazyModuleGetter(this, "AppConstants", "resource://gre/modules/AppConstants.jsm");
Examples of correct code for this rule:
---------------------------------------
.. code-block:: js
ChromeUtils.import("resource://gre/modules/Services.jsm", this);
ChromeUtils.defineModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", this);
ChromeUtils.defineModuleGetter(this, "AppConstants", "resource://gre/modules/AppConstants.jsm");
// 4 argument version of defineLazyModuleGetter is allowed.
XPCOMUtils.defineLazyModuleGetter(this, "Services","resource://gre/modules/Service.jsm","Foo");
XPCOMUtils.defineLazyModuleGetter(this, "AppConstants","resource://gre/modules/AppConstants.jsm","Foo");

View File

@ -1,8 +1,8 @@
use-services
============
Requires the use of ``Services.jsm`` rather than ``Cc[].getService()`` where a
service is already defined in ``Services.jsm``.
Requires the use of ``Services`` rather than ``Cc[].getService()`` where a
service is already defined in ``Services``.
Examples of incorrect code for this rule:
-----------------------------------------

View File

@ -46,7 +46,6 @@ Attaching a listener for a request is pretty simple::
}
}
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(observer, "http-on-modify-request", false);
See nsIObserverService_ for the details.
@ -85,7 +84,6 @@ The response: http-on-examine-response
Firefox fires a "http-on-examine-response" observer notification after parsing the HTTP response status and headers, but **before** reading the response body. Attaching a listener for this phase is also very easy::
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
Services.obs.addObserver(observer, "http-on-examine-response", false);
If you use the same observer for "http-on-modify-request" and "http-on-examine-response", make sure you check the topic argument before interacting with the channel.

View File

@ -9,9 +9,6 @@ data within the Gecko application.
From JavaScript, the service can be accessed via::
const { Services } = ChromeUtils.import(
"resource://gre/modules/Services.jsm"
);
let crashManager = Services.crashmanager;
That will give you an instance of ``CrashManager`` from ``CrashManager.jsm``.