ported from custom branch of UXP: [Basilisk] port PM's ghostbuster (d1fde5c4)

This commit is contained in:
2024-09-22 21:41:35 +08:00
parent 502e778308
commit 7a704927ed
3 changed files with 48 additions and 1 deletions
@@ -477,6 +477,9 @@ pref("javascript.options.showInConsole", true);
pref("general.warnOnAboutConfig", false);
#endif
// Enable unlinking of ghost windows so they can be garbage collected.
pref("browser.ghostbuster.enabled", true);
// This is the pref to control the location bar, change this to true to
// force this - this makes the origin of popup windows more obvious to avoid
// spoofing. We would rather not do it by default because it affects UE for web
@@ -2736,12 +2736,24 @@
}
// We're going to remove the tab and the browser now.
// Using the "STOP_ALL" flag should halt all animations, fetches, network
// activity, etc. to come to a clean state for removal and unlinking (if enabled).
var browser = this.getBrowserForTab(aTab);
browser.webNavigation.stop(nsIWebNavigation.STOP_ALL);
this._tabFilters.delete(aTab);
this._tabListeners.delete(aTab);
var browser = this.getBrowserForTab(aTab);
this._outerWindowIDBrowserMap.delete(browser.outerWindowID);
if (Services.prefs.getBoolPref("browser.ghostbuster.enabled", true)) {
Cu.unlinkGhostWindows();
#ifdef DEBUG
dump("Unlinking ghost windows has run on tab close.\n");
#endif
}
// Because of the way XBL works (fields just set JS
// properties on the element) and the code we have in place
// to preserve the JS objects for any elements that have
@@ -107,6 +107,10 @@ const BOOKMARKS_BACKUP_MIN_INTERVAL_DAYS = 1;
// days we will try to create a new one more aggressively.
const BOOKMARKS_BACKUP_MAX_INTERVAL_DAYS = 3;
// Use users' idle time to unlink ghost windows and clean up memory.
// Trigger this by default every 5 minutes.
const GHOSTBUSTER_INTERVAL = 5 * 60;
// Factory object
const BrowserGlueServiceFactory = {
_instance: null,
@@ -125,6 +129,10 @@ function BrowserGlue() {
"@mozilla.org/widget/idleservice;1",
"nsIIdleService");
XPCOMUtils.defineLazyServiceGetter(this, "_ghostBusterService",
"@mozilla.org/widget/idleservice;1",
"nsIIdleService");
XPCOMUtils.defineLazyGetter(this, "_distributionCustomizer", function() {
Cu.import("resource:///modules/distribution.js");
return new DistributionCustomizer();
@@ -157,6 +165,7 @@ BrowserGlue.prototype = {
_isPlacesShutdownObserver: false,
_isPlacesDatabaseLocked: false,
_migrationImportsDefaultBookmarks: false,
_isGhostBusterObserver: false,
_setPrefToSaveSession: function BG__setPrefToSaveSession(aForce) {
if (!this._saveSession && !aForce)
@@ -276,6 +285,15 @@ BrowserGlue.prototype = {
break;
case "idle":
this._backupBookmarks();
if (this._ghostBusterService.idleTime > GHOSTBUSTER_INTERVAL * 1000) {
if (Services.prefs.getBoolPref("browser.ghostbuster.enabled", true)) {
Cu.unlinkGhostWindows();
Cu.forceGC();
#ifdef DEBUG
dump("Unlinking ghost windows + GC has run on idle.\n");
#endif
}
}
break;
case "distribution-customization-complete":
Services.obs.removeObserver(this, "distribution-customization-complete");
@@ -919,6 +937,13 @@ BrowserGlue.prototype = {
this._trackSlowStartup();
// Initialize ghost window idle observer.
if (!this._isGhostBusterObserver) {
this._ghostBusterService.addIdleObserver(this, GHOSTBUSTER_INTERVAL);
// Prevent re-entry.
this._isGhostBusterObserver = true;
}
// Offer to reset a user's profile if it hasn't been used for 60 days.
const OFFER_PROFILE_RESET_INTERVAL_MS = 60 * 24 * 60 * 60 * 1000;
let lastUse = Services.appinfo.replacedLockTime;
@@ -998,6 +1023,13 @@ BrowserGlue.prototype = {
#ifdef NIGHTLY_BUILD
AddonWatcher.uninit();
#endif
// Shut down ghost window idle observer.
if (this._isGhostBusterObserver) {
this._ghostBusterService.removeIdleObserver(this, GHOSTBUSTER_INTERVAL);
this._isGhostBusterObserver = false;
}
// Do one final unlink to combat shutdown issues.
Cu.unlinkGhostWindows();
},
_initServiceDiscovery() {