From a6b9f9d8e56d2ea9bdcd37c1326b3a3890f92777 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Thu, 3 Sep 2020 23:57:29 -0700 Subject: [PATCH] fix: only focus a webContents if the window was not initially hidden (#25292) --- lib/browser/api/browser-window.ts | 8 +++++--- shell/browser/api/electron_api_web_contents.cc | 11 ++++++++--- shell/browser/api/electron_api_web_contents.h | 3 +++ typings/internal-electron.d.ts | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index fe0c8f6ac010..8a1bfe42fc3b 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -28,9 +28,11 @@ Object.setPrototypeOf(BrowserWindow.prototype, BaseWindow.prototype); // 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' as any, function (this: WebContents) { - this.focus(); - }); + if (this.webContents._initiallyShown) { + this.webContents.once('load-url' as any, function (this: WebContents) { + this.focus(); + }); + } // Redirect focus/blur event to app instance too. this.on('blur', (event: Event) => { diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index a7c2b9ac1709..4c7c7e6b1441 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -474,8 +474,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; @@ -531,7 +531,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); } @@ -2732,6 +2732,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(), @@ -2925,6 +2929,7 @@ v8::Local WebContents::FillObjectTemplate( .SetProperty("hostWebContents", &WebContents::HostWebContents) .SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents) .SetProperty("debugger", &WebContents::Debugger) + .SetProperty("_initiallyShown", &WebContents::WasInitiallyShown) .Build(); } diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index f24b9a566fd0..0dcfbb9b556b 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -393,6 +393,7 @@ class WebContents : public gin::Wrappable, content::WebContents* HostWebContents() const; v8::Local DevToolsWebContents(v8::Isolate* isolate); v8::Local Debugger(v8::Isolate* isolate); + bool WasInitiallyShown(); WebContentsZoomController* GetZoomController() { return zoom_controller_; } @@ -683,6 +684,8 @@ class WebContents : public gin::Wrappable, // 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 d7aae3092d57..8fab594dc4b2 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -56,6 +56,7 @@ declare namespace Electron { getLastWebPreferences(): Electron.WebPreferences; _getPreloadPaths(): string[]; equal(other: WebContents): boolean; + _initiallyShown: boolean; } interface WebPreferences {