Files
tubestation/browser/components/shopping/ReviewCheckerParent.sys.mjs
Fred Chasen 4a6d9ffaf8 Bug 1933539 - Split ReviewCheckerChild from the ShoppingSidebarChild actor. r=shopping-reviewers,kpatenio
This separates `ReviewCheckerChild` from `ShoppingSidebarChild` as a precursor to adding caching to the `ReviewCheckerChild` in Bug 1927956.

* The new cache functionality in the Review Checker won't apply to the previous sidebar and the complex `updateContent` method make it difficult to implement.
* Splitting up `updateContent` into smaller methods makes this a large change from the current child, but will enable implementing product caching easily in the `getProductForURI()` method.
* As both children communicate with the same `<shopping-container>` element there is a good amount of repetition in the event handlers and a few confusingly named events for the short term but this will be replacing the previous actors eventually.

Differential Revision: https://phabricator.services.mozilla.com/D232287
2025-01-16 06:12:23 +00:00

112 lines
3.2 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, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});
const ABOUT_SHOPPING_SIDEBAR = "about:shoppingsidebar";
/**
* 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";
static CLOSE_SIDEBAR = "CloseReviewCheckerSidebar";
actorCreated() {
this.topBrowserWindow = this.browsingContext.topChromeWindow;
this.topBrowserWindow.gBrowser.addProgressListener(this);
}
didDestroy() {
if (!this.topBrowserWindow) {
return;
}
this.topBrowserWindow.gBrowser.removeProgressListener(this);
this.topBrowserWindow = undefined;
}
updateCurrentURL(uri, flags) {
// about:shoppingsidebar is only used for testing with fake data.
if (!uri || uri.spec == ABOUT_SHOPPING_SIDEBAR) {
return;
}
this.sendAsyncMessage("ReviewChecker:UpdateCurrentURL", {
url: uri.spec,
isReload: !!(flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_RELOAD),
});
}
getCurrentURL() {
let { selectedBrowser } = this.topBrowserWindow.gBrowser;
let uri = selectedBrowser.currentURI;
// about:shoppingsidebar is only used for testing with fake data.
if (!uri || uri.spec == ABOUT_SHOPPING_SIDEBAR) {
return null;
}
return uri.spec;
}
async receiveMessage(message) {
if (this.browsingContext.usePrivateBrowsing) {
throw new Error("We should never be invoked in PBM.");
}
switch (message.name) {
case "GetCurrentURL":
return this.getCurrentURL();
case "DisableShopping":
Services.prefs.setIntPref(
ReviewCheckerParent.SHOPPING_OPTED_IN_PREF,
2
);
break;
case "CloseShoppingSidebar":
this.closeSidebarPanel();
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;
}
this.updateCurrentURL(aLocationURI, aFlags);
}
closeSidebarPanel() {
let closeEvent = new CustomEvent(ReviewCheckerParent.CLOSE_SIDEBAR, {
bubbles: true,
composed: true,
});
this.topBrowserWindow.dispatchEvent(closeEvent);
}
}