From b446e32dd9c94de075dd2c8a6613fa6398a93eac Mon Sep 17 00:00:00 2001 From: Alexandre Poirot Date: Wed, 31 Jul 2019 08:12:16 +0000 Subject: [PATCH] Bug 1510690 - Do focus the content window from the actor when the toolbox closes. r=nchevobbe No RDP request should be done when the toolbox closes as there no guarantee that the request will complete. Instead, such cleanup should be done by the actors. Differential Revision: https://phabricator.services.mozilla.com/D38309 --- devtools/client/framework/toolbox.js | 9 +++++++++ .../mochitest/browser_webconsole_document_focus.js | 10 +++++++++- devtools/client/webconsole/webconsole.js | 8 -------- devtools/server/actors/targets/browsing-context.js | 6 ++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/devtools/client/framework/toolbox.js b/devtools/client/framework/toolbox.js index e2414a6a674c..37c1e0f97d82 100644 --- a/devtools/client/framework/toolbox.js +++ b/devtools/client/framework/toolbox.js @@ -763,6 +763,15 @@ Toolbox.prototype = { await promise.all([splitConsolePromise, framesPromise]); + // Request the actor to restore the focus to the content page once the + // target is detached. This typically happens when the console closes. + // We restore the focus as it may have been stolen by the console input. + await this.target.reconfigure({ + options: { + restoreFocus: true, + }, + }); + // Lazily connect to the profiler here and don't wait for it to complete, // used to intercept console.profile calls before the performance tools are open. const performanceFrontConnection = this.initPerformance(); diff --git a/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js b/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js index 7a2d0a607897..5c9a1ef690bd 100644 --- a/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js +++ b/devtools/client/webconsole/test/mochitest/browser_webconsole_document_focus.js @@ -13,12 +13,20 @@ add_task(async function() { info("Focus after console is opened"); ok(isInputFocused(hud), "input node is focused after console is opened"); + await ContentTask.spawn(gBrowser.selectedBrowser, {}, function() { + this.onFocus = new Promise(resolve => { + content.addEventListener("focus", resolve, { once: true }); + }); + }); + info("Closing console"); await closeConsole(); + const isFocused = await ContentTask.spawn( gBrowser.selectedBrowser, {}, - function() { + async function() { + await this.onFocus; return Services.focus.focusedWindow == content; } ); diff --git a/devtools/client/webconsole/webconsole.js b/devtools/client/webconsole/webconsole.js index 4910d9310c2c..6612bfd67f13 100644 --- a/devtools/client/webconsole/webconsole.js +++ b/devtools/client/webconsole/webconsole.js @@ -386,14 +386,6 @@ class WebConsole { this.ui.destroy(); } - if (!this.isBrowserConsole) { - try { - await this.target.focus(); - } catch (ex) { - // Tab focus can fail if the tab or target is closed. - } - } - if (this._parserService) { this._parserService.stop(); this._parserService = null; diff --git a/devtools/server/actors/targets/browsing-context.js b/devtools/server/actors/targets/browsing-context.js index efa9a943c771..783af6e20037 100644 --- a/devtools/server/actors/targets/browsing-context.js +++ b/devtools/server/actors/targets/browsing-context.js @@ -1135,6 +1135,9 @@ const browsingContextTargetPrototype = { options.serviceWorkersTestingEnabled ); } + if (typeof options.restoreFocus == "boolean") { + this._restoreFocus = options.restoreFocus; + } // Reload if: // - there's an explicit `performReload` flag and it's true @@ -1157,6 +1160,9 @@ const browsingContextTargetPrototype = { this._setCacheDisabled(false); this._setServiceWorkersTestingEnabled(false); this._setPaintFlashingEnabled(false); + if (this._restoreFocus) { + this.window.focus(); + } }, /**