Files
tubestation/browser/components/shopping/ReviewCheckerParent.sys.mjs
Fred Chasen 2a40aed615 Bug 1916547 - Part 1: Render Review Checker in the new sidebar. r=shopping-reviewers,firefox-desktop-core-reviewers ,sidebar-reviewers,sclements,mossop,kpatenio
- Adds a remote content browser to contain `about:shoppingsidebar` in `review-checker.xhtml` and update the Review Checker sidebar to use that wrapper document.
- Adds ReviewChecker actors for the sidebar Review Checker panel.
- ReviewCheckerParent is a simplified version of the ShoppingSidebarParent as it does not need to handle the state of the sidebar opening or closing.
- ReviewCheckerChild is just extends ShoppingSidebarChild for now, though it will need to be separate in the future.
- Adds a browser.shopping.experience2023.shoppingSidebar boolean pref to disable ShoppingSidebar actors when they are not needed.
- Fixes `AbortError` error handling when the outer sidebar browser is removed.

Note that any auto-opening or closing behavior will need to be added afterwards, as these actors are not around to check product urls when the sidebar is closed.

Differential Revision: https://phabricator.services.mozilla.com/D222840
2024-12-12 01:24:20 +00:00

98 lines
3.0 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
isProductURL: "chrome://global/content/shopping/ShoppingProduct.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
ShoppingUtils: "resource:///modules/ShoppingUtils.sys.mjs",
});
/**
* When a Review Checker sidebar panel is open ReviewCheckerParent
* handles listening for location changes and determining if the
* location or the current URI is a product or not.
*
* The ReviewCheckerChild will use that info to update the sidebar UI.
*
* This is a simplified version of the `ShoppingSidebarParent` for
* using the shopping components in the main sidebar instead of the
* custom shopping sidebar.
*/
export class ReviewCheckerParent extends JSWindowActorParent {
static SHOPPING_OPTED_IN_PREF = "browser.shopping.experience2023.optedIn";
actorCreated() {
this.topBrowserWindow = this.browsingContext.topChromeWindow;
this.topBrowserWindow.gBrowser.addProgressListener(this);
}
didDestroy() {
if (this.topBrowserWindow) {
this.topBrowserWindow.gBrowser.removeProgressListener(this);
this.topBrowserWindow = undefined;
}
}
updateProductURL(uri, flags) {
this.sendAsyncMessage("ShoppingSidebar:UpdateProductURL", {
url: uri?.spec ?? null,
isReload: !!(flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_RELOAD),
});
}
currentProductUrl() {
let window = this.browsingContext.topChromeWindow;
let { selectedBrowser } = window.gBrowser;
let uri = selectedBrowser.currentURI;
let isProduct = lazy.isProductURL(uri);
return isProduct ? uri.spec : null;
}
async receiveMessage(message) {
if (this.browsingContext.usePrivateBrowsing) {
throw new Error("We should never be invoked in PBM.");
}
switch (message.name) {
case "GetProductURL":
return this.currentProductUrl();
case "DisableShopping":
Services.prefs.setIntPref(
ReviewCheckerParent.SHOPPING_OPTED_IN_PREF,
2
);
break;
}
return null;
}
/**
* Called by TabsProgressListener whenever any browser navigates from one
* URL to another.
* Note that this includes hash changes / pushState navigations, because
* those can be significant for us.
*/
onLocationChange(aWebProgress, _aRequest, aLocationURI, aFlags) {
if (aWebProgress && !aWebProgress.isTopLevel) {
return;
}
let isPBM = lazy.PrivateBrowsingUtils.isWindowPrivate(
this.topBrowserWindow
);
if (isPBM) {
return;
}
lazy.ShoppingUtils.onLocationChange(aLocationURI, aFlags);
let isProduct = lazy.isProductURL(aLocationURI);
if (isProduct) {
this.updateProductURL(aLocationURI, aFlags);
} else {
this.updateProductURL(null);
}
}
}