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 1164
Changed: yurys@chromium.org Wed Aug 19 15:35:02 PDT 2020 Changed: yurys@chromium.org Thu Aug 20 10:30:04 PDT 2020

View file

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

View file

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

View file

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