Browse Source

Merge pull request #6944 from electron/check-window-from-native-view

Access native window through native view
Cheng Zhao 8 years ago
parent
commit
7bdce52290

+ 3 - 2
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<content::DevToolsAgentHost> 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;
   }

+ 2 - 4
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 <Cocoa/Cocoa.h>
 
 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])

+ 23 - 4
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 () {