Bug 1772100 - Part 11: Use plain object for lazy getter in browser/components/pagedata/. r=mossop

Differential Revision: https://phabricator.services.mozilla.com/D148021
This commit is contained in:
Tooru Fujisawa 2022-06-06 22:46:23 +00:00
Родитель 4863993e67
Коммит 9c61c4fd2f
4 изменённых файлов: 44 добавлений и 36 удалений

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

@ -10,7 +10,9 @@ const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
PageDataSchema: "resource:///modules/pagedata/PageDataSchema.jsm",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
});
@ -18,7 +20,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
// We defer any attempt to check for page data for a short time after a page
// loads to allow JS to operate.
XPCOMUtils.defineLazyPreferenceGetter(
this,
lazy,
"READY_DELAY",
"browser.pagedata.readyDelay",
500
@ -39,7 +41,7 @@ class PageDataChild extends JSWindowActorChild {
* Called when the actor is created for a new page.
*/
actorCreated() {
this.#isContentWindowPrivate = PrivateBrowsingUtils.isContentWindowPrivate(
this.#isContentWindowPrivate = lazy.PrivateBrowsingUtils.isContentWindowPrivate(
this.contentWindow
);
}
@ -70,7 +72,7 @@ class PageDataChild extends JSWindowActorChild {
url: this.document.documentURI,
});
},
READY_DELAY,
lazy.READY_DELAY,
Ci.nsITimer.TYPE_ONE_SHOT_LOW_PRIORITY
);
}
@ -98,7 +100,7 @@ class PageDataChild extends JSWindowActorChild {
}
break;
case "PageData:Collect":
return PageDataSchema.collectPageData(this.document);
return lazy.PageDataSchema.collectPageData(this.document);
}
return undefined;

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

@ -10,7 +10,9 @@ const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
});
@ -30,7 +32,7 @@ class PageDataParent extends JSWindowActorParent {
*/
collectPageData() {
if (!this.#deferredCollection) {
this.#deferredCollection = PromiseUtils.defer();
this.#deferredCollection = lazy.PromiseUtils.defer();
this.sendQuery("PageData:Collect").then(
this.#deferredCollection.resolve,
this.#deferredCollection.reject
@ -56,7 +58,7 @@ class PageDataParent extends JSWindowActorParent {
receiveMessage(msg) {
switch (msg.name) {
case "PageData:DocumentReady":
PageDataService.pageLoaded(this, msg.data.url);
lazy.PageDataService.pageLoaded(this, msg.data.url);
break;
}
}

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

@ -13,7 +13,9 @@ const { XPCOMUtils } = ChromeUtils.import(
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetters(this, {
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
JsonSchemaValidator:
"resource://gre/modules/components-utils/JsonSchemaValidator.jsm",
OpenGraphPageData: "resource:///modules/pagedata/OpenGraphPageData.jsm",
@ -21,7 +23,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
TwitterPageData: "resource:///modules/pagedata/TwitterPageData.jsm",
});
XPCOMUtils.defineLazyGetter(this, "logConsole", function() {
XPCOMUtils.defineLazyGetter(lazy, "logConsole", function() {
return console.createInstance({
prefix: "PageData",
maxLogLevel: Services.prefs.getBoolPref("browser.pagedata.log", false)
@ -42,8 +44,8 @@ XPCOMUtils.defineLazyGetter(this, "logConsole", function() {
* The data returned need not be valid, collectors should return whatever they
* can and then we drop anything that is invalid once all data is joined.
*/
XPCOMUtils.defineLazyGetter(this, "DATA_COLLECTORS", function() {
return [SchemaOrgPageData, OpenGraphPageData, TwitterPageData];
XPCOMUtils.defineLazyGetter(lazy, "DATA_COLLECTORS", function() {
return [lazy.SchemaOrgPageData, lazy.OpenGraphPageData, lazy.TwitterPageData];
});
let SCHEMAS = new Map();
@ -81,7 +83,7 @@ async function loadSchema(schemaName) {
async function validateData(schemaName, data) {
let schema = await loadSchema(schemaName.toLocaleLowerCase());
let result = JsonSchemaValidator.validate(data, schema, {
let result = lazy.JsonSchemaValidator.validate(data, schema, {
allowExplicitUndefinedProperties: true,
// Allowed for future expansion of the schema.
allowExtraProperties: true,
@ -230,13 +232,13 @@ const PageDataSchema = {
* error.
*/
async collectPageData(document) {
logConsole.debug("Starting collection", document.documentURI);
lazy.logConsole.debug("Starting collection", document.documentURI);
let pending = DATA_COLLECTORS.map(async collector => {
let pending = lazy.DATA_COLLECTORS.map(async collector => {
try {
return await collector.collect(document);
} catch (e) {
logConsole.error("Error collecting page data", e);
lazy.logConsole.error("Error collecting page data", e);
return null;
}
});
@ -251,7 +253,7 @@ const PageDataSchema = {
try {
return this.validatePageData(pageData);
} catch (e) {
logConsole.error("Failed to collect valid page data", e);
lazy.logConsole.error("Failed to collect valid page data", e);
return null;
}
},

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

@ -11,7 +11,9 @@ const { XPCOMUtils } = ChromeUtils.import(
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetters(this, {
const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
E10SUtils: "resource://gre/modules/E10SUtils.jsm",
EventEmitter: "resource://gre/modules/EventEmitter.jsm",
@ -19,7 +21,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
});
XPCOMUtils.defineLazyGetter(this, "logConsole", function() {
XPCOMUtils.defineLazyGetter(lazy, "logConsole", function() {
return console.createInstance({
prefix: "PageData",
maxLogLevel: Services.prefs.getBoolPref("browser.pagedata.log", false)
@ -28,12 +30,12 @@ XPCOMUtils.defineLazyGetter(this, "logConsole", function() {
});
});
XPCOMUtils.defineLazyServiceGetters(this, {
XPCOMUtils.defineLazyServiceGetters(lazy, {
idleService: ["@mozilla.org/widget/useridleservice;1", "nsIUserIdleService"],
});
XPCOMUtils.defineLazyPreferenceGetter(
this,
lazy,
"fetchIdleTime",
"browser.pagedata.fetchIdleTime",
300
@ -88,7 +90,7 @@ class HiddenBrowserManager {
async #acquireBrowser() {
this.#browsers++;
if (!this.#frame) {
this.#frame = new HiddenFrame();
this.#frame = new lazy.HiddenFrame();
}
let frame = await this.#frame.get();
@ -268,7 +270,7 @@ class PageDataCache {
* the format defined by the schemas at `browser/components/pagedata/schemas`.
*/
const PageDataService = new (class PageDataService extends EventEmitter {
const PageDataService = new (class PageDataService extends lazy.EventEmitter {
/**
* Caches page data discovered from browsers.
*
@ -353,9 +355,9 @@ const PageDataService = new (class PageDataService extends EventEmitter {
},
});
logConsole.debug("Service started");
lazy.logConsole.debug("Service started");
for (let win of BrowserWindowTracker.orderedWindows) {
for (let win of lazy.BrowserWindowTracker.orderedWindows) {
if (!win.closed) {
// Ask any existing tabs to report
for (let tab of win.gBrowser.tabs) {
@ -368,7 +370,7 @@ const PageDataService = new (class PageDataService extends EventEmitter {
}
}
idleService.addIdleObserver(this, fetchIdleTime);
lazy.idleService.addIdleObserver(this, lazy.fetchIdleTime);
}
/**
@ -376,7 +378,7 @@ const PageDataService = new (class PageDataService extends EventEmitter {
* don't really need to do much cleanup.
*/
uninit() {
logConsole.debug("Service stopped");
lazy.logConsole.debug("Service stopped");
}
/**
@ -486,7 +488,7 @@ const PageDataService = new (class PageDataService extends EventEmitter {
this.pageDataDiscovered(data);
}
} catch (e) {
logConsole.error(e);
lazy.logConsole.error(e);
}
}
@ -498,7 +500,7 @@ const PageDataService = new (class PageDataService extends EventEmitter {
* The set of data discovered.
*/
pageDataDiscovered(pageData) {
logConsole.debug("Discovered page data", pageData);
lazy.logConsole.debug("Discovered page data", pageData);
this.#pageDataCache.set(pageData.url, {
...pageData,
@ -536,20 +538,20 @@ const PageDataService = new (class PageDataService extends EventEmitter {
async fetchPageData(url) {
return this.#browserManager.withHiddenBrowser(async browser => {
try {
let { promise, resolve } = PromiseUtils.defer();
let { promise, resolve } = lazy.PromiseUtils.defer();
this.#backgroundBrowsers.set(browser, resolve);
let principal = Services.scriptSecurityManager.getSystemPrincipal();
let oa = E10SUtils.predictOriginAttributes({
let oa = lazy.E10SUtils.predictOriginAttributes({
browser,
});
let loadURIOptions = {
triggeringPrincipal: principal,
remoteType: E10SUtils.getRemoteTypeForURI(
remoteType: lazy.E10SUtils.getRemoteTypeForURI(
url,
true,
false,
E10SUtils.DEFAULT_REMOTE_TYPE,
lazy.E10SUtils.DEFAULT_REMOTE_TYPE,
null,
oa
),
@ -577,12 +579,12 @@ const PageDataService = new (class PageDataService extends EventEmitter {
observe(subject, topic, data) {
switch (topic) {
case "idle":
logConsole.debug("User went idle");
lazy.logConsole.debug("User went idle");
this.#userIsIdle = true;
this.#startBackgroundWorkers();
break;
case "active":
logConsole.debug("User became active");
lazy.logConsole.debug("User became active");
this.#userIsIdle = false;
break;
}
@ -627,7 +629,7 @@ const PageDataService = new (class PageDataService extends EventEmitter {
this.emit("page-data", pageData);
}
} catch (e) {
logConsole.error(e);
lazy.logConsole.error(e);
}
// Check whether the user became active or the worker limit changed