Bug 1722276 - Move DATA_TYPE from Snapshots.jsm to PageDataCollector.jsm. r=mossop

This avoids needing to load Snapshots.jsm into the content process.

Differential Revision: https://phabricator.services.mozilla.com/D121168
This commit is contained in:
Mark Banner 2021-07-29 13:40:54 +00:00
Родитель f51af42cfe
Коммит 9846c1cbf9
9 изменённых файлов: 32 добавлений и 27 удалений

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

@ -18,6 +18,15 @@ const { EventEmitter } = ChromeUtils.import(
* the available data changes.
*/
class PageDataCollector extends EventEmitter {
/**
* Supported data types.
*/
static get DATA_TYPE() {
return {
PRODUCT: 1,
};
}
/**
* Internal, should generally not need to be overriden by child classes.
*

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

@ -31,7 +31,7 @@ const ALLOWED_SCHEMES = ["http", "https", "data", "blob"];
* @typedef {object} Data
* An individual piece of data about a page.
* @property {number} type
* The type of data, see Snapshots.DATA_TYPE.*
* The type of data, see PageDataCollector.DATA_TYPE.*
* @property {object} data
* The data in a format specific to the type of data.
*

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

@ -162,9 +162,10 @@ class SchemaOrgPageData extends PageDataCollector {
for (let scope of scopes) {
switch (scope.getAttribute("itemtype")) {
case "https://schema.org/Product":
// TODO: The `1` should be Snapshots.DATA_TYPE.PRODUCT, but we want
// to move the type definition into `PageDataCollector.jsm`.
insert(1, this.#collectProduct(scope));
insert(
PageDataCollector.DATA_TYPE.PRODUCT,
this.#collectProduct(scope)
);
break;
}
}

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

@ -31,6 +31,6 @@ When page data has been found a `page-data` event is emitted. The event's argume
## Supported Types of page data
The following types of page data (`Snapshots.DATA_TYPE`) are currently supported:
The following types of page data (`PageDataCollector.DATA_TYPE`) are currently supported:
- [`PRODUCT`](./schema-org.html#Product)

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

@ -24,7 +24,7 @@ add_task(async function test_single_product_data() {
pageData.data,
[
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: [
{
gtin: "13572468",
@ -55,7 +55,7 @@ add_task(async function test_single_multiple_data() {
pageData.data,
[
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: [
{
gtin: "13572468",

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

@ -7,6 +7,7 @@ const { XPCOMUtils } = ChromeUtils.import(
);
XPCOMUtils.defineLazyModuleGetters(this, {
PageDataCollector: "resource:///modules/pagedata/PageDataCollector.jsm",
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
Snapshots: "resource:///modules/Snapshots.jsm",
});

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

@ -11,8 +11,9 @@ const { XPCOMUtils } = ChromeUtils.import(
);
XPCOMUtils.defineLazyModuleGetters(this, {
Services: "resource://gre/modules/Services.jsm",
PageDataCollector: "resource:///modules/pagedata/PageDataCollector.jsm",
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
Services: "resource://gre/modules/Services.jsm",
Snapshots: "resource:///modules/Snapshots.jsm",
});
@ -29,7 +30,7 @@ add_task(async function test_pageDataDiscoverd_notifies() {
PageDataService.pageDataDiscovered(url, [
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: {
price: 276,
},
@ -46,7 +47,7 @@ add_task(async function test_pageDataDiscoverd_notifies() {
pageData.data,
[
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: {
price: 276,
},

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

@ -13,9 +13,10 @@ const { XPCOMUtils } = ChromeUtils.import(
const VERSION_PREF = "browser.places.snapshots.version";
XPCOMUtils.defineLazyModuleGetters(this, {
PageDataCollector: "resource:///modules/pagedata/PageDataCollector.jsm",
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
Services: "resource://gre/modules/Services.jsm",
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
});
/**
@ -116,15 +117,6 @@ const Snapshots = new (class Snapshots {
// PageDataService.on("page-data", this.#onPageData);
}
/**
* Supported data types.
*/
get DATA_TYPE() {
return {
PRODUCT: 1,
};
}
#notify(topic, urls) {
Services.obs.notifyObservers(null, topic, JSON.stringify(urls));
}
@ -141,7 +133,7 @@ const Snapshots = new (class Snapshots {
let pageData = PageDataService.getCached(url);
if (pageData?.data.length) {
for (let data of pageData.data) {
if (Object.values(this.DATA_TYPE).includes(data.type)) {
if (Object.values(PageDataCollector.DATA_TYPE).includes(data.type)) {
bindings[`id${index}`] = placeId;
bindings[`type${index}`] = data.type;
// We store the whole data object that also includes type because

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

@ -5,6 +5,7 @@
* Tests that adding a snapshot also adds related page data.
*/
XPCOMUtils.defineLazyModuleGetters(this, {
PageDataCollector: "resource:///modules/pagedata/PageDataCollector.jsm",
PageDataService: "resource:///modules/pagedata/PageDataService.jsm",
});
@ -16,7 +17,7 @@ add_task(async function pagedata() {
// Register some page data.
PageDataService.pageDataDiscovered(TEST_URL1, [
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: {
price: 276,
},
@ -32,7 +33,7 @@ add_task(async function pagedata() {
PageDataService.pageDataDiscovered(TEST_URL2, [
{
type: Snapshots.DATA_TYPE.PRODUCT,
type: PageDataCollector.DATA_TYPE.PRODUCT,
data: {
price: 384,
},
@ -88,7 +89,7 @@ add_task(async function pagedata() {
let snap = await Snapshots.get(TEST_URL1);
Assert.equal(snap.pageData.size, 1, "Should have some page data.");
Assert.equal(
snap.pageData.get(Snapshots.DATA_TYPE.PRODUCT).price,
snap.pageData.get(PageDataCollector.DATA_TYPE.PRODUCT).price,
276,
"Should have the right price."
);
@ -97,7 +98,7 @@ add_task(async function pagedata() {
snap = await Snapshots.get(TEST_URL2);
Assert.equal(snap.pageData.size, 1, "Should have some page data.");
Assert.equal(
snap.pageData.get(Snapshots.DATA_TYPE.PRODUCT).price,
snap.pageData.get(PageDataCollector.DATA_TYPE.PRODUCT).price,
384,
"Should have the right price."
);
@ -119,7 +120,7 @@ add_task(async function pagedata() {
documentType: Interactions.DOCUMENT_TYPE.GENERIC,
},
],
{ type: Snapshots.DATA_TYPE.PRODUCT }
{ type: PageDataCollector.DATA_TYPE.PRODUCT }
);
info("Ensure that removing a snapshot removes pagedata for it");
@ -141,7 +142,7 @@ add_task(async function pagedata() {
snap = await Snapshots.get(TEST_URL1);
Assert.equal(snap.pageData.size, 1, "Should have some page data.");
Assert.equal(
snap.pageData.get(Snapshots.DATA_TYPE.PRODUCT).price,
snap.pageData.get(PageDataCollector.DATA_TYPE.PRODUCT).price,
276,
"Should have the right price."
);