From 17077fd9017fd519873e757aec673b76f9e9b7f0 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 25 Aug 2020 14:50:40 -0700 Subject: [PATCH] browser(firefox): introduce browser level screencastFinished event (#3625) --- browser_patches/firefox/BUILD_NUMBER | 4 ++-- .../firefox/juggler/TargetRegistry.js | 15 ++++---------- .../juggler/protocol/BrowserHandler.js | 20 ++++++++++--------- .../firefox/juggler/protocol/Dispatcher.js | 10 ++++------ .../firefox/juggler/protocol/PageHandler.js | 5 +---- .../firefox/juggler/protocol/Protocol.js | 8 ++++---- 6 files changed, 26 insertions(+), 36 deletions(-) diff --git a/browser_patches/firefox/BUILD_NUMBER b/browser_patches/firefox/BUILD_NUMBER index 41338445571ef..48294aca41d56 100644 --- a/browser_patches/firefox/BUILD_NUMBER +++ b/browser_patches/firefox/BUILD_NUMBER @@ -1,2 +1,2 @@ -1166 -Changed: yurys@chromium.org Thu Aug 20 13:55:50 PDT 2020 +1167 +Changed: yurys@chromium.org Tue Aug 25 14:46:17 PDT 2020 diff --git a/browser_patches/firefox/juggler/TargetRegistry.js b/browser_patches/firefox/juggler/TargetRegistry.js index 8f9c2b79d934d..620da981c6e8d 100644 --- a/browser_patches/firefox/juggler/TargetRegistry.js +++ b/browser_patches/firefox/juggler/TargetRegistry.js @@ -134,8 +134,7 @@ class TargetRegistry { if (!target) return; target.emit('crashed'); - if (target) - target.dispose().catch(e => dump(`Failed to destroy target: ${e}`)); + target.dispose(); } }, 'oop-frameloader-crashed'); @@ -204,7 +203,7 @@ class TargetRegistry { const linkedBrowser = tab.linkedBrowser; const target = this._browserToTarget.get(linkedBrowser); if (target) - target.dispose().catch(e => dump(`Failed to destroy target: ${e}`)); + target.dispose(); }; Services.wm.addListener({ @@ -460,17 +459,12 @@ class PageTarget { return await this._channel.connect('').send('hasFailedToOverrideTimezone').catch(e => true); } - async dispose() { + dispose() { this._disposed = true; + this._browserContext.pages.delete(this); this._registry._browserToTarget.delete(this._linkedBrowser); this._registry._browserBrowsingContextToTarget.delete(this._linkedBrowser.browsingContext); helper.removeListeners(this._eventListeners); - - const event = { pendingActivity: [], target: this }; - this._registry.emit(TargetRegistry.Events.TargetWillBeDestroyed, event); - await Promise.all(event.pendingActivity); - - this._browserContext.pages.delete(this); this._registry.emit(TargetRegistry.Events.TargetDestroyed, this); } } @@ -740,7 +734,6 @@ function setViewportSizeForBrowser(viewportSize, browser, window) { TargetRegistry.Events = { TargetCreated: Symbol('TargetRegistry.Events.TargetCreated'), - TargetWillBeDestroyed: Symbol('TargetRegistry.Events.TargetWillBeDestroyed'), TargetDestroyed: Symbol('TargetRegistry.Events.TargetDestroyed'), DownloadCreated: Symbol('TargetRegistry.Events.DownloadCreated'), DownloadFinished: Symbol('TargetRegistry.Events.DownloadFinished'), diff --git a/browser_patches/firefox/juggler/protocol/BrowserHandler.js b/browser_patches/firefox/juggler/protocol/BrowserHandler.js index f5d0e087eb6d7..9bed75397db54 100644 --- a/browser_patches/firefox/juggler/protocol/BrowserHandler.js +++ b/browser_patches/firefox/juggler/protocol/BrowserHandler.js @@ -44,10 +44,16 @@ class BrowserHandler { this._eventListeners = [ helper.on(this._targetRegistry, TargetRegistry.Events.TargetCreated, this._onTargetCreated.bind(this)), - helper.on(this._targetRegistry, TargetRegistry.Events.TargetWillBeDestroyed, this._onTargetWillBeDestroyed.bind(this)), + helper.on(this._targetRegistry, TargetRegistry.Events.TargetDestroyed, this._onTargetDestroyed.bind(this)), helper.on(this._targetRegistry, TargetRegistry.Events.DownloadCreated, this._onDownloadCreated.bind(this)), helper.on(this._targetRegistry, TargetRegistry.Events.DownloadFinished, this._onDownloadFinished.bind(this)), ]; + + const onScreencastStopped = (subject, topic, data) => { + this._session.emitEvent('Browser.screencastFinished', {screencastId: '' + data}); + }; + Services.obs.addObserver(onScreencastStopped, 'juggler-screencast-stopped'); + this._eventListeners.push(() => Services.obs.removeObserver(onScreencastStopped, 'juggler-screencast-stopped')); } async createBrowserContext({removeOnDetach}) { @@ -65,11 +71,11 @@ class BrowserHandler { this._createdBrowserContextIds.delete(browserContextId); } - async dispose() { + dispose() { helper.removeListeners(this._eventListeners); for (const [target, session] of this._attachedSessions) { target.disconnectSession(session); - await this._dispatcher.destroySession(session); + this._dispatcher.destroySession(session); } this._attachedSessions.clear(); for (const browserContextId of this._createdBrowserContextIds) { @@ -101,16 +107,12 @@ class BrowserHandler { sessions.push(session); } - _onTargetWillBeDestroyed({target, pendingActivity}) { - pendingActivity.push(this._detachFromTarget(target)); - } - - async _detachFromTarget(target) { + _onTargetDestroyed(target) { const session = this._attachedSessions.get(target); if (!session) return; this._attachedSessions.delete(target); - await this._dispatcher.destroySession(session); + this._dispatcher.destroySession(session); this._session.emitEvent('Browser.detachedFromTarget', { sessionId: session.sessionId(), targetId: target.id(), diff --git a/browser_patches/firefox/juggler/protocol/Dispatcher.js b/browser_patches/firefox/juggler/protocol/Dispatcher.js index 242d7cb8a0c8d..1c882dda331a1 100644 --- a/browser_patches/firefox/juggler/protocol/Dispatcher.js +++ b/browser_patches/firefox/juggler/protocol/Dispatcher.js @@ -29,9 +29,9 @@ class Dispatcher { return session; } - async destroySession(session) { + destroySession(session) { this._sessions.delete(session.sessionId()); - await session._dispose(); + session._dispose(); } _dispose() { @@ -108,15 +108,13 @@ class ProtocolSession { this._handlers.set(domainName, handler); } - async _dispose() { - const promises = []; + _dispose() { for (const [domainName, handler] of this._handlers) { if (typeof handler.dispose !== 'function') throw new Error(`Handler for "${domainName}" domain does not define |dispose| method!`); - promises.push(handler.dispose()); + handler.dispose(); } this._handlers.clear(); - await Promise.all(promises); this._dispatcher = null; } diff --git a/browser_patches/firefox/juggler/protocol/PageHandler.js b/browser_patches/firefox/juggler/protocol/PageHandler.js index 523f12d41e13d..78d0a057b9c8f 100644 --- a/browser_patches/firefox/juggler/protocol/PageHandler.js +++ b/browser_patches/firefox/juggler/protocol/PageHandler.js @@ -311,7 +311,7 @@ class PageHandler { const rect = this._pageTarget.linkedBrowser().getBoundingClientRect(); const devicePixelRatio = this._pageTarget._window.devicePixelRatio; this._videoSessionId = screencast.startVideoRecording(docShell, file, width, height, scale || 0, devicePixelRatio * rect.top); - this._session.emitEvent('Page.screencastStarted', {uid: '' + this._videoSessionId, file}); + this._session.emitEvent('Page.screencastStarted', {screencastId: '' + this._videoSessionId, file}); } async stopVideoRecording() { @@ -320,7 +320,6 @@ class PageHandler { const videoSessionId = this._videoSessionId; this._videoSessionId = -1; const screencast = Cc['@mozilla.org/juggler/screencast;1'].getService(Ci.nsIScreencastService); - const session = this._session; const result = new Promise(resolve => Services.obs.addObserver(function onStopped(subject, topic, data) { if (videoSessionId != data) @@ -328,8 +327,6 @@ class PageHandler { Services.obs.removeObserver(onStopped, 'juggler-screencast-stopped'); resolve(); - - session.emitEvent('Page.screencastStopped', {uid: '' + videoSessionId}); }, 'juggler-screencast-stopped') ); screencast.stopVideoRecording(videoSessionId); diff --git a/browser_patches/firefox/juggler/protocol/Protocol.js b/browser_patches/firefox/juggler/protocol/Protocol.js index f47b73e4496be..c9794d3701824 100644 --- a/browser_patches/firefox/juggler/protocol/Protocol.js +++ b/browser_patches/firefox/juggler/protocol/Protocol.js @@ -216,6 +216,9 @@ const Browser = { canceled: t.Optional(t.Boolean), error: t.Optional(t.String), }, + 'screencastFinished': { + screencastId: t.String, + }, }, methods: { @@ -650,12 +653,9 @@ const Page = { message: t.String, }, 'screencastStarted': { - uid: t.String, + screencastId: t.String, file: t.String, }, - 'screencastStopped': { - uid: t.String, - }, }, methods: {