browser(firefox): make context close wait for sessions to finish (#3550)

This commit is contained in:
Yury Semikhatsky 2020-08-20 11:04:57 -07:00 committed by GitHub
parent 0d03cc0f9a
commit 854d755db5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 15 deletions

View File

@ -1,2 +1,2 @@
1163
Changed: yurys@chromium.org Wed Aug 19 15:35:02 PDT 2020
1164
Changed: yurys@chromium.org Thu Aug 20 10:30:04 PDT 2020

View File

@ -134,8 +134,7 @@ class TargetRegistry {
if (!target)
return;
target.emit('crashed');
target.dispose();
this.emit(TargetRegistry.Events.TargetDestroyed, target);
this._destroyTarget(target).catch(e => dump(`Failed to destroy target: ${e}`));
}
}, 'oop-frameloader-crashed');
@ -182,7 +181,6 @@ class TargetRegistry {
const sessions = [];
const readyData = { sessions, target };
this.emit(TargetRegistry.Events.TargetCreated, readyData);
sessions.forEach(session => target._initSession(session));
return {
scriptsToEvaluateOnNewDocument: browserContext ? browserContext.scriptsToEvaluateOnNewDocument : [],
bindings: browserContext ? browserContext.bindings : [],
@ -204,10 +202,7 @@ class TargetRegistry {
const tab = event.target;
const linkedBrowser = tab.linkedBrowser;
const target = this._browserToTarget.get(linkedBrowser);
if (target) {
target.dispose();
this.emit(TargetRegistry.Events.TargetDestroyed, target);
}
this._destroyTarget(target).catch(e => dump(`Failed to destroy target: ${e}`));
};
Services.wm.addListener({
@ -245,6 +240,16 @@ class TargetRegistry {
extHelperAppSvc.setDownloadInterceptor(new DownloadInterceptor(this));
}
async _destroyTarget(target) {
if (!target)
return;
const event = { pendingActivity: [], target };
this.emit(TargetRegistry.Events.TargetWillBeDestroyed, event);
await Promise.all(event.pendingActivity);
target.dispose();
this.emit(TargetRegistry.Events.TargetDestroyed, target);
}
setBrowserProxy(proxy) {
this._browserProxy = proxy;
}
@ -400,7 +405,6 @@ class PageTarget {
}
connectSession(session) {
this._initSession(session);
this._channel.connect('').send('attach', { sessionId: session.sessionId() });
}
@ -415,7 +419,7 @@ class PageTarget {
});
}
_initSession(session) {
initSession(session) {
const pageHandler = new PageHandler(this, session, this._channel);
const networkHandler = new NetworkHandler(this, session, this._channel);
session.registerHandler('Page', pageHandler);
@ -738,6 +742,7 @@ 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'),

View File

@ -33,6 +33,7 @@ class BrowserHandler {
if (!this._shouldAttachToTarget(target))
continue;
const session = this._dispatcher.createSession();
target.initSession(session);
target.connectSession(session);
this._attachedSessions.set(target, session);
this._session.emitEvent('Browser.attachedToTarget', {
@ -43,7 +44,7 @@ class BrowserHandler {
this._eventListeners = [
helper.on(this._targetRegistry, TargetRegistry.Events.TargetCreated, this._onTargetCreated.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.TargetDestroyed, this._onTargetDestroyed.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.TargetWillBeDestroyed, this._onTargetWillBeDestroyed.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.DownloadCreated, this._onDownloadCreated.bind(this)),
helper.on(this._targetRegistry, TargetRegistry.Events.DownloadFinished, this._onDownloadFinished.bind(this)),
];
@ -91,6 +92,7 @@ class BrowserHandler {
if (!this._shouldAttachToTarget(target))
return;
const session = this._dispatcher.createSession();
target.initSession(session);
this._attachedSessions.set(target, session);
this._session.emitEvent('Browser.attachedToTarget', {
sessionId: session.sessionId(),
@ -99,7 +101,11 @@ class BrowserHandler {
sessions.push(session);
}
async _onTargetDestroyed(target) {
_onTargetWillBeDestroyed({target, pendingActivity}) {
pendingActivity.push(this._detachFromTarget(target));
}
async _detachFromTarget(target) {
const session = this._attachedSessions.get(target);
if (!session)
return;

View File

@ -31,7 +31,7 @@ class Dispatcher {
async destroySession(session) {
this._sessions.delete(session.sessionId());
await session.dispose();
await session._dispose();
}
_dispose() {
@ -108,7 +108,7 @@ class ProtocolSession {
this._handlers.set(domainName, handler);
}
async dispose() {
async _dispose() {
const promises = [];
for (const [domainName, handler] of this._handlers) {
if (typeof handler.dispose !== 'function')