Browse Source

fix: return pointer instead of pointer's content (#16641)

Cheng Zhao 6 years ago
parent
commit
8ab1309215

+ 2 - 2
atom/browser/api/atom_api_top_level_window.cc

@@ -667,8 +667,8 @@ v8::Local<v8::Value> TopLevelWindow::GetNativeWindowHandle() {
   // TODO(MarshallOfSound): Replace once
   // https://chromium-review.googlesource.com/c/chromium/src/+/1253094/ has
   // landed
-  auto handle = window_->GetNativeWindowHandlePointer();
-  return ToBuffer(isolate(), std::get<0>(handle), std::get<1>(handle));
+  NativeWindowHandle handle = window_->GetNativeWindowHandle();
+  return ToBuffer(isolate(), &handle, sizeof(handle));
 }
 
 void TopLevelWindow::SetProgressBar(double progress, mate::Arguments* args) {

+ 7 - 1
atom/browser/native_window.h

@@ -45,6 +45,12 @@ class NativeBrowserView;
 
 struct DraggableRegion;
 
+#if defined(OS_MACOSX)
+typedef NSView* NativeWindowHandle;
+#else
+typedef gfx::AcceleratedWidget NativeWindowHandle;
+#endif
+
 class NativeWindow : public base::SupportsUserData,
                      public views::WidgetDelegate {
  public:
@@ -151,7 +157,7 @@ class NativeWindow : public base::SupportsUserData,
   virtual gfx::NativeView GetNativeView() const = 0;
   virtual gfx::NativeWindow GetNativeWindow() const = 0;
   virtual gfx::AcceleratedWidget GetAcceleratedWidget() const = 0;
-  virtual std::tuple<void*, int> GetNativeWindowHandlePointer() const = 0;
+  virtual NativeWindowHandle GetNativeWindowHandle() const = 0;
 
   // Taskbar/Dock APIs.
   enum ProgressState {

+ 1 - 1
atom/browser/native_window_mac.h

@@ -104,7 +104,7 @@ class NativeWindowMac : public NativeWindow {
   gfx::NativeView GetNativeView() const override;
   gfx::NativeWindow GetNativeWindow() const override;
   gfx::AcceleratedWidget GetAcceleratedWidget() const override;
-  std::tuple<void*, int> GetNativeWindowHandlePointer() const override;
+  NativeWindowHandle GetNativeWindowHandle() const override;
   void SetProgressBar(double progress, const ProgressState state) override;
   void SetOverlayIcon(const gfx::Image& overlay,
                       const std::string& description) override;

+ 2 - 3
atom/browser/native_window_mac.mm

@@ -1081,9 +1081,8 @@ gfx::AcceleratedWidget NativeWindowMac::GetAcceleratedWidget() const {
   return gfx::kNullAcceleratedWidget;
 }
 
-std::tuple<void*, int> NativeWindowMac::GetNativeWindowHandlePointer() const {
-  NSView* view = [window_ contentView];
-  return std::make_tuple(static_cast<void*>(view), sizeof(view));
+NativeWindowHandle NativeWindowMac::GetNativeWindowHandle() const {
+  return [window_ contentView];
 }
 
 void NativeWindowMac::SetProgressBar(double progress,

+ 2 - 3
atom/browser/native_window_views.cc

@@ -1047,9 +1047,8 @@ gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() const {
   return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
 }
 
-std::tuple<void*, int> NativeWindowViews::GetNativeWindowHandlePointer() const {
-  gfx::AcceleratedWidget handle = GetAcceleratedWidget();
-  return std::make_tuple(static_cast<void*>(&handle), sizeof(handle));
+NativeWindowHandle NativeWindowViews::GetNativeWindowHandle() const {
+  return GetAcceleratedWidget();
 }
 
 gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(

+ 1 - 1
atom/browser/native_window_views.h

@@ -124,7 +124,7 @@ class NativeWindowViews : public NativeWindow,
   bool IsVisibleOnAllWorkspaces() override;
 
   gfx::AcceleratedWidget GetAcceleratedWidget() const override;
-  std::tuple<void*, int> GetNativeWindowHandlePointer() const override;
+  NativeWindowHandle GetNativeWindowHandle() const override;
 
   gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& bounds) const override;
   gfx::Rect WindowBoundsToContentBounds(const gfx::Rect& bounds) const override;

+ 2 - 0
spec/.hash

@@ -0,0 +1,2 @@
+24d8222313d02ffe2a2181b9c82fbfa5ab8372aec6442839302622fbf45cbe47
+null

+ 13 - 0
spec/api-browser-window-spec.js

@@ -2790,6 +2790,19 @@ describe('BrowserWindow module', () => {
     })
   })
 
+  describe('window.getNativeWindowHandle()', () => {
+    if (!nativeModulesEnabled) {
+      this.skip()
+    }
+
+    it('returns valid handle', () => {
+      // The module's source code is hosted at
+      // https://github.com/electron/node-is-valid-window
+      const isValidWindow = remote.require('is-valid-window')
+      assert.ok(isValidWindow(w.getNativeWindowHandle()))
+    })
+  })
+
   describe('extensions and dev tools extensions', () => {
     let showPanelTimeoutId
 

File diff suppressed because it is too large
+ 249 - 258
spec/package-lock.json


+ 1 - 0
spec/package.json

@@ -15,6 +15,7 @@
     "dbus-native": "^0.2.5",
     "dirty-chai": "^2.0.1",
     "graceful-fs": "^4.1.11",
+    "is-valid-window": "^0.0.3",
     "mkdirp": "^0.5.1",
     "mocha": "^5.2.0",
     "mocha-junit-reporter": "^1.17.0",

Some files were not shown because too many files changed in this diff