From fa3652a14f3f22acf7046e72c5922ecb4986f2ec Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 8 Sep 2020 00:55:46 -0700 Subject: [PATCH] fix: only focus a webContents if the window was not initially hidden (#25323) (#25330) Co-authored-by: Samuel Attard Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> --- lib/browser/api/browser-window.js | 8 +++++--- shell/browser/api/electron_api_web_contents.cc | 13 +++++++++---- shell/browser/api/electron_api_web_contents.h | 3 +++ typings/internal-electron.d.ts | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 7c3e8579831f2..bf0cfe2a9face 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -30,9 +30,11 @@ BrowserWindow.prototype._init = function () { // Though this hack is only needed on macOS when the app is launched from // Finder, we still do it on all platforms in case of other bugs we don't // know. - this.webContents.once('load-url', function () { - this.focus(); - }); + if (this.webContents._initiallyShown) { + this.webContents.once('load-url', function () { + this.focus(); + }); + } // Redirect focus/blur event to app instance too. this.on('blur', (event) => { diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index ff92b6ed15cd8..22c1c4af5f8c2 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -453,8 +453,8 @@ WebContents::WebContents(v8::Isolate* isolate, // BrowserViews are not attached to a window initially so they should start // off as hidden. This is also important for compositor recycling. See: // https://github.com/electron/electron/pull/21372 - bool initially_shown = type_ != Type::BROWSER_VIEW; - options.Get(options::kShow, &initially_shown); + initially_shown_ = type_ != Type::BROWSER_VIEW; + options.Get(options::kShow, &initially_shown_); // Obtain the session. std::string partition; @@ -510,7 +510,7 @@ WebContents::WebContents(v8::Isolate* isolate, #endif } else { content::WebContents::CreateParams params(session->browser_context()); - params.initially_hidden = !initially_shown; + params.initially_hidden = !initially_shown_; web_contents = content::WebContents::Create(params); } @@ -2678,6 +2678,10 @@ v8::Local WebContents::Debugger(v8::Isolate* isolate) { return v8::Local::New(isolate, debugger_); } +bool WebContents::WasInitiallyShown() { + return initially_shown_; +} + void WebContents::GrantOriginAccess(const GURL& url) { content::ChildProcessSecurityPolicy::GetInstance()->GrantCommitOrigin( web_contents()->GetMainFrame()->GetProcess()->GetID(), @@ -2852,7 +2856,8 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetProperty("session", &WebContents::Session) .SetProperty("hostWebContents", &WebContents::HostWebContents) .SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents) - .SetProperty("debugger", &WebContents::Debugger); + .SetProperty("debugger", &WebContents::Debugger) + .SetProperty("_initiallyShown", &WebContents::WasInitiallyShown); } ElectronBrowserContext* WebContents::GetBrowserContext() const { diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 6e88384e4dc6c..8f5756369ca81 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -376,6 +376,7 @@ class WebContents : public gin_helper::TrackableObject, content::WebContents* HostWebContents() const; v8::Local DevToolsWebContents(v8::Isolate* isolate); v8::Local Debugger(v8::Isolate* isolate); + bool WasInitiallyShown(); WebContentsZoomController* GetZoomController() { return zoom_controller_; } @@ -650,6 +651,8 @@ class WebContents : public gin_helper::TrackableObject, // Observers of this WebContents. base::ObserverList observers_; + bool initially_shown_ = true; + // The ID of the process of the currently committed RenderViewHost. // -1 means no speculative RVH has been committed yet. int currently_committed_process_id_ = -1; diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index f485a4348ba21..23ea1a8cc753f 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -32,6 +32,7 @@ declare namespace Electron { interface WebContents { _getURL(): string; getOwnerBrowserWindow(): Electron.BrowserWindow; + _initiallyShown: boolean; } interface SerializedError {