diff --git a/filenames.gni b/filenames.gni index ea1637c99030..45fafbdb52eb 100644 --- a/filenames.gni +++ b/filenames.gni @@ -125,6 +125,7 @@ filenames = { "shell/browser/animation_util.h", "shell/browser/animation_util_mac.mm", "shell/browser/api/electron_api_app_mac.mm", + "shell/browser/api/electron_api_base_window_mac.mm", "shell/browser/api/electron_api_menu_mac.h", "shell/browser/api/electron_api_menu_mac.mm", "shell/browser/api/electron_api_native_theme_mac.mm", diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index 848cc6cc2c9f..a7983dd39c8b 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -70,6 +70,7 @@ namespace electron::api { namespace { +#if !BUILDFLAG(IS_MAC) // Converts binary data to Buffer. v8::Local ToBuffer(v8::Isolate* isolate, void* val, int size) { auto buffer = node::Buffer::Copy(isolate, static_cast(val), size); @@ -78,6 +79,7 @@ v8::Local ToBuffer(v8::Isolate* isolate, void* val, int size) { else return buffer.ToLocalChecked(); } +#endif [[nodiscard]] constexpr std::array ToArray(const gfx::Size size) { return {size.width(), size.height()}; @@ -778,6 +780,7 @@ std::string BaseWindow::GetMediaSourceId() const { return window_->GetDesktopMediaID().ToString(); } +#if !BUILDFLAG(IS_MAC) v8::Local BaseWindow::GetNativeWindowHandle() { // TODO(MarshallOfSound): Replace once // https://chromium-review.googlesource.com/c/chromium/src/+/1253094/ has @@ -785,6 +788,7 @@ v8::Local BaseWindow::GetNativeWindowHandle() { NativeWindowHandle handle = window_->GetNativeWindowHandle(); return ToBuffer(isolate(), &handle, sizeof(handle)); } +#endif void BaseWindow::SetProgressBar(double progress, gin_helper::Arguments* args) { gin_helper::Dictionary options; diff --git a/shell/browser/api/electron_api_base_window_mac.mm b/shell/browser/api/electron_api_base_window_mac.mm new file mode 100644 index 000000000000..148ec216283f --- /dev/null +++ b/shell/browser/api/electron_api_base_window_mac.mm @@ -0,0 +1,32 @@ +// Copyright (c) 2025 Microsoft, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/browser/api/electron_api_base_window.h" + +#include "electron/buildflags/buildflags.h" +#include "shell/browser/api/electron_api_view.h" +#include "shell/browser/native_window.h" +#include "shell/common/node_includes.h" + +namespace { + +// Converts binary data to Buffer. +v8::Local ToBuffer(v8::Isolate* isolate, void* val, int size) { + auto buffer = node::Buffer::Copy(isolate, static_cast(val), size); + if (buffer.IsEmpty()) + return v8::Null(isolate); + else + return buffer.ToLocalChecked(); +} + +} // namespace + +namespace electron::api { + +v8::Local BaseWindow::GetNativeWindowHandle() { + NSView* handle = window_->GetNativeWindowHandle().GetNativeNSView(); + return ToBuffer(isolate(), &handle, sizeof(handle)); +} + +} // namespace electron::api diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 0779c4919b29..bf5f6ec4b44b 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3743,15 +3743,17 @@ void WebContents::SetDevToolsWebContents(const WebContents* devtools) { inspectable_web_contents_->SetDevToolsWebContents(devtools->web_contents()); } +#if !BUILDFLAG(IS_MAC) v8::Local WebContents::GetNativeView(v8::Isolate* isolate) const { gfx::NativeView ptr = web_contents()->GetNativeView(); - auto buffer = node::Buffer::Copy(isolate, reinterpret_cast(&ptr), - sizeof(gfx::NativeView)); + auto buffer = + node::Buffer::Copy(isolate, reinterpret_cast(&ptr), sizeof(ptr)); if (buffer.IsEmpty()) return v8::Null(isolate); else return buffer.ToLocalChecked(); } +#endif v8::Local WebContents::DevToolsWebContents(v8::Isolate* isolate) { if (devtools_web_contents_.IsEmpty()) diff --git a/shell/browser/api/electron_api_web_contents_mac.mm b/shell/browser/api/electron_api_web_contents_mac.mm index 52c436283961..1358b3dde95d 100644 --- a/shell/browser/api/electron_api_web_contents_mac.mm +++ b/shell/browser/api/electron_api_web_contents_mac.mm @@ -6,6 +6,7 @@ #include "shell/browser/api/electron_api_web_contents.h" #include "shell/browser/ui/cocoa/event_dispatching_window.h" #include "shell/browser/web_contents_preferences.h" +#include "shell/common/node_includes.h" #include "ui/base/cocoa/command_dispatcher.h" #include "ui/base/cocoa/nsmenu_additions.h" #include "ui/base/cocoa/nsmenuitem_additions.h" @@ -92,4 +93,22 @@ bool WebContents::PlatformHandleKeyboardEvent( return false; } +namespace { + +// Converts binary data to Buffer. +v8::Local ToBuffer(v8::Isolate* isolate, void* val, int size) { + auto buffer = node::Buffer::Copy(isolate, static_cast(val), size); + if (buffer.IsEmpty()) + return v8::Null(isolate); + else + return buffer.ToLocalChecked(); +} + +} // namespace + +v8::Local WebContents::GetNativeView(v8::Isolate* isolate) const { + NSView* handle = web_contents()->GetNativeView().GetNativeNSView(); + return ToBuffer(isolate, &handle, sizeof(handle)); +} + } // namespace electron::api diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index cc4d150ca72b..831cf8947aa8 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -58,9 +58,9 @@ class BrowserView; } #if BUILDFLAG(IS_MAC) -typedef gfx::NativeView NativeWindowHandle; +using NativeWindowHandle = gfx::NativeView; #else -typedef gfx::AcceleratedWidget NativeWindowHandle; +using NativeWindowHandle = gfx::AcceleratedWidget; #endif class NativeWindow : public base::SupportsUserData,