diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index e77338253fde..d9722f3cc5b7 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1004,7 +1004,8 @@ void WebContents::InspectElement(int x, int y) { if (type_ == REMOTE) return; - OpenDevTools(nullptr); + if (!managed_web_contents()->GetDevToolsWebContents()) + OpenDevTools(nullptr); scoped_refptr agent( content::DevToolsAgentHost::GetOrCreateFor(web_contents())); agent->InspectElement(x, y); @@ -1175,7 +1176,7 @@ bool WebContents::IsFocused() const { if (!view) return false; if (GetType() != BACKGROUND_PAGE) { - auto window = web_contents()->GetTopLevelNativeWindow(); + auto window = web_contents()->GetNativeView()->GetToplevelWindow(); if (window && !window->IsVisible()) return false; } diff --git a/atom/browser/api/atom_api_web_contents_mac.mm b/atom/browser/api/atom_api_web_contents_mac.mm index 913951737fe8..c60639f16131 100644 --- a/atom/browser/api/atom_api_web_contents_mac.mm +++ b/atom/browser/api/atom_api_web_contents_mac.mm @@ -4,9 +4,7 @@ #include "atom/browser/api/atom_api_web_contents.h" -@interface NSWindow -- (BOOL)isKeyWindow; -@end +#import namespace atom { @@ -17,7 +15,7 @@ bool WebContents::IsFocused() const { if (!view) return false; if (GetType() != BACKGROUND_PAGE) { - auto window = web_contents()->GetTopLevelNativeWindow(); + auto window = [web_contents()->GetNativeView() window]; // On Mac the render widget host view does not lose focus when the window // loses focus so check if the top level window is the key window. if (window && ![window isKeyWindow]) diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 64a2f4aac390..7e77c39bdb55 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -49,11 +49,9 @@ describe('webContents module', function () { }) describe('getFocusedWebContents() API', function () { - if (isCi) { - return - } - it('returns the focused web contents', function (done) { + if (isCi) return done() + const specWebContents = remote.getCurrentWebContents() assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId()) @@ -69,6 +67,27 @@ describe('webContents module', function () { specWebContents.openDevTools() }) + + it('does not crash when called on a detached dev tools window', function (done) { + const specWebContents = w.webContents + + specWebContents.once('devtools-opened', function () { + assert.doesNotThrow(function () { + webContents.getFocusedWebContents() + }) + specWebContents.closeDevTools() + }) + + specWebContents.once('devtools-closed', function () { + assert.doesNotThrow(function () { + webContents.getFocusedWebContents() + }) + done() + }) + + specWebContents.openDevTools({mode: 'detach'}) + w.inspectElement(100, 100) + }) }) describe('isFocused() API', function () {