Skip to content

Commit

Permalink
browser(firefox): introduce browser level screencastFinished event (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Aug 25, 2020
1 parent a0bd8de commit 17077fd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
4 changes: 2 additions & 2 deletions browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1166
Changed: [email protected] Thu Aug 20 13:55:50 PDT 2020
1167
Changed: [email protected] Tue Aug 25 14:46:17 PDT 2020
15 changes: 4 additions & 11 deletions browser_patches/firefox/juggler/TargetRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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'),
Expand Down
20 changes: 11 additions & 9 deletions browser_patches/firefox/juggler/protocol/BrowserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 4 additions & 6 deletions browser_patches/firefox/juggler/protocol/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Dispatcher {
return session;
}

async destroySession(session) {
destroySession(session) {
this._sessions.delete(session.sessionId());
await session._dispose();
session._dispose();
}

_dispose() {
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 1 addition & 4 deletions browser_patches/firefox/juggler/protocol/PageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -320,16 +320,13 @@ 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)
return;

Services.obs.removeObserver(onStopped, 'juggler-screencast-stopped');
resolve();

session.emitEvent('Page.screencastStopped', {uid: '' + videoSessionId});
}, 'juggler-screencast-stopped')
);
screencast.stopVideoRecording(videoSessionId);
Expand Down
8 changes: 4 additions & 4 deletions browser_patches/firefox/juggler/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ const Browser = {
canceled: t.Optional(t.Boolean),
error: t.Optional(t.String),
},
'screencastFinished': {
screencastId: t.String,
},
},

methods: {
Expand Down Expand Up @@ -650,12 +653,9 @@ const Page = {
message: t.String,
},
'screencastStarted': {
uid: t.String,
screencastId: t.String,
file: t.String,
},
'screencastStopped': {
uid: t.String,
},
},

methods: {
Expand Down

0 comments on commit 17077fd

Please sign in to comment.