Bug 1952104 - Don't submit captcha detection ping if profile is about to be closed or closed. r=tjr

Differential Revision: https://phabricator.services.mozilla.com/D240860
This commit is contained in:
Fatih Kilic
2025-03-11 12:15:12 +00:00
parent c89735b508
commit a3bdc6fe8c

View File

@@ -13,6 +13,10 @@ ChromeUtils.defineLazyGetter(lazy, "console", () => {
});
});
ChromeUtils.defineESModuleGetters(lazy, {
AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs",
});
const HAS_UNSUBMITTED_DATA_PREF = "captchadetection.hasUnsubmittedData";
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
@@ -89,6 +93,13 @@ export class CaptchaDetectionPingUtils {
return;
}
if (!CaptchaDetectionPingUtils.profileIsOpen) {
lazy.console.debug(
"Not submitting ping because profile is closing or already closed."
);
return;
}
lazy.console.debug("Flushing ping.");
GleanPings.captchaDetection.submit();
@@ -177,6 +188,9 @@ export class CaptchaDetectionPingUtils {
name: "privacy.resistFingerprinting.pbmode",
},
};
static profileIsOpen = true;
static init() {
if (CaptchaDetectionPingUtils.hasPrefObservers) {
return;
@@ -197,6 +211,22 @@ export class CaptchaDetectionPingUtils {
}
this.hasPrefObservers = true;
try {
lazy.AsyncShutdown.profileBeforeChange.addBlocker(
"CaptchaDetectionPingUtils: Don't submit pings after shutdown",
async () => {
this.profileIsOpen = false;
}
);
} catch (e) {
// This is not a critical error, so we just log it.
// According to https://searchfox.org/mozilla-central/rev/d5baa11e35e0186c3c867f4948010f0742198467/toolkit/components/asyncshutdown/nsIAsyncShutdown.idl#82-103
// this error can happen if it is too late to add a blocker.
lazy.console.error(
"Failed to add blocker for profileBeforeChange: " + e.message
);
this.profileIsOpen = false;
}
}
}
@@ -204,4 +234,5 @@ export class CaptchaDetectionPingUtils {
* @typedef lazy
* @type {object}
* @property {ConsoleInstance} console - console instance.
* @property {AsyncShutdown} AsyncShutdown - AsyncShutdown module.
*/