From a825b294215b2c4fd51a62aea93b20d0af660ae7 Mon Sep 17 00:00:00 2001 From: Nicolas Chevobbe Date: Thu, 10 Aug 2023 14:36:04 +0000 Subject: [PATCH] Bug 1771113 - [devtools] Notify about removed stylesheet resources. r=devtools-reviewers,ochameau. Differential Revision: https://phabricator.services.mozilla.com/D185790 --- .../server/actors/resources/stylesheets.js | 25 +++++- .../actors/utils/stylesheets-manager.js | 49 ++++++++++- .../tests/browser_resources_stylesheets.js | 82 ++++++++++++++++++- 3 files changed, 149 insertions(+), 7 deletions(-) diff --git a/devtools/server/actors/resources/stylesheets.js b/devtools/server/actors/resources/stylesheets.js index a9826791079b..64289d94a918 100644 --- a/devtools/server/actors/resources/stylesheets.js +++ b/devtools/server/actors/resources/stylesheets.js @@ -19,6 +19,7 @@ class StyleSheetWatcher { this._onApplicableStylesheetAdded = this._onApplicableStylesheetAdded.bind(this); this._onStylesheetUpdated = this._onStylesheetUpdated.bind(this); + this._onStylesheetRemoved = this._onStylesheetRemoved.bind(this); } /** @@ -31,10 +32,11 @@ class StyleSheetWatcher { * - onAvailable: mandatory function * This will be called for each resource. */ - async watch(targetActor, { onAvailable, onUpdated }) { + async watch(targetActor, { onAvailable, onUpdated, onDestroyed }) { this._targetActor = targetActor; this._onAvailable = onAvailable; this._onUpdated = onUpdated; + this._onDestroyed = onDestroyed; this._styleSheetsManager = targetActor.getStyleSheetsManager(); @@ -47,6 +49,10 @@ class StyleSheetWatcher { "stylesheet-updated", this._onStylesheetUpdated ); + this._styleSheetsManager.on( + "applicable-stylesheet-removed", + this._onStylesheetRemoved + ); // startWatching will emit applicable-stylesheet-added for already existing stylesheet await this._styleSheetsManager.startWatching(); @@ -60,6 +66,10 @@ class StyleSheetWatcher { this._notifyResourceUpdated(resourceId, updateKind, updates); } + _onStylesheetRemoved({ resourceId }) { + return this._notifyResourcesDestroyed(resourceId); + } + async _toResource( styleSheet, { isCreatedByDevTools = false, fileName = null, resourceId } = {} @@ -124,6 +134,15 @@ class StyleSheetWatcher { ]); } + _notifyResourcesDestroyed(resourceId) { + this._onDestroyed([ + { + resourceType: STYLESHEET, + resourceId, + }, + ]); + } + destroy() { this._styleSheetsManager.off( "applicable-stylesheet-added", @@ -133,6 +152,10 @@ class StyleSheetWatcher { "stylesheet-updated", this._onStylesheetUpdated ); + this._styleSheetsManager.off( + "applicable-stylesheet-removed", + this._onStylesheetRemoved + ); } } diff --git a/devtools/server/actors/utils/stylesheets-manager.js b/devtools/server/actors/utils/stylesheets-manager.js index b25757ac8cc1..937944744414 100644 --- a/devtools/server/actors/utils/stylesheets-manager.js +++ b/devtools/server/actors/utils/stylesheets-manager.js @@ -90,6 +90,7 @@ class StyleSheetsManager extends EventEmitter { this._targetActor = targetActor; this._onApplicableStateChanged = this._onApplicableStateChanged.bind(this); + this._onStylesheetRemoved = this._onStylesheetRemoved.bind(this); this._onTargetActorWindowReady = this._onTargetActorWindowReady.bind(this); } @@ -112,6 +113,11 @@ class StyleSheetsManager extends EventEmitter { this._onApplicableStateChanged, true ); + this._targetActor.chromeEventHandler.addEventListener( + "StyleSheetRemoved", + this._onStylesheetRemoved, + true + ); this._watchStyleSheetChangeEvents(); this._targetActor.on("window-ready", this._onTargetActorWindowReady); @@ -145,7 +151,7 @@ class StyleSheetsManager extends EventEmitter { _watchStyleSheetChangeEventsForWindow(window) { // We have to set this flag in order to get the - // StyleSheetApplicableStateChanged events. See Document.webidl. + // StyleSheetApplicableStateChanged and StyleSheetRemoved events. See Document.webidl. window.document.styleSheetChangeEventsEnabled = true; } @@ -758,10 +764,11 @@ class StyleSheetsManager extends EventEmitter { * When appending ,