From c04895b222633e6f49a3b76f0799bf28eabdd8b2 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Tue, 19 Apr 2022 10:45:03 -0700 Subject: [PATCH] chrome: Implement GetWindowHandle() for --multi-threaded-message-loop (see issue #3294) --- .../chrome/browser_platform_delegate_chrome.cc | 17 +++++++++++++---- .../chrome/browser_platform_delegate_chrome.h | 3 +++ .../browser/chrome/chrome_browser_host_impl.cc | 5 +++-- .../browser/chrome/chrome_browser_host_impl.h | 1 + libcef/browser/views/view_util.h | 4 ++++ libcef/browser/views/view_util_aura.cc | 12 +++++++++--- libcef/browser/views/view_util_mac.mm | 12 ++++++++++++ 7 files changed, 45 insertions(+), 9 deletions(-) diff --git a/libcef/browser/chrome/browser_platform_delegate_chrome.cc b/libcef/browser/chrome/browser_platform_delegate_chrome.cc index 854914d31..b94c06ea8 100644 --- a/libcef/browser/chrome/browser_platform_delegate_chrome.cc +++ b/libcef/browser/chrome/browser_platform_delegate_chrome.cc @@ -4,6 +4,8 @@ #include "libcef/browser/chrome/browser_platform_delegate_chrome.h" +#include "libcef/browser/views/view_util.h" + #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_window.h" #include "ui/display/screen.h" @@ -40,6 +42,10 @@ void CefBrowserPlatformDelegateChrome::BrowserDestroyed( native_delegate_->BrowserDestroyed(browser); } +CefWindowHandle CefBrowserPlatformDelegateChrome::GetHostWindowHandle() const { + return view_util::GetWindowHandle(GetNativeWindow()); +} + SkColor CefBrowserPlatformDelegateChrome::GetBackgroundColor() const { return native_delegate_->GetBackgroundColor(); } @@ -73,12 +79,9 @@ gfx::Point CefBrowserPlatformDelegateChrome::GetScreenPoint( const gfx::Point& view) const { auto screen = display::Screen::GetScreen(); - gfx::NativeWindow native_window = - chrome_browser_ ? chrome_browser_->window()->GetNativeWindow() : nullptr; - // Returns screen pixel coordinates. auto screen_rect = screen->DIPToScreenRectInWindow( - native_window, gfx::Rect(view, gfx::Size(0, 0))); + GetNativeWindow(), gfx::Rect(view, gfx::Size(0, 0))); return screen_rect.origin(); } @@ -104,3 +107,9 @@ gfx::Point CefBrowserPlatformDelegateChrome::GetParentScreenPoint( void CefBrowserPlatformDelegateChrome::set_chrome_browser(Browser* browser) { chrome_browser_ = browser; } + +gfx::NativeWindow CefBrowserPlatformDelegateChrome::GetNativeWindow() const { + if (chrome_browser_ && chrome_browser_->window()) + return chrome_browser_->window()->GetNativeWindow(); + return gfx::NativeWindow(); +} diff --git a/libcef/browser/chrome/browser_platform_delegate_chrome.h b/libcef/browser/chrome/browser_platform_delegate_chrome.h index 1eefeb25b..c1e839f16 100644 --- a/libcef/browser/chrome/browser_platform_delegate_chrome.h +++ b/libcef/browser/chrome/browser_platform_delegate_chrome.h @@ -24,6 +24,7 @@ class CefBrowserPlatformDelegateChrome void WebContentsDestroyed(content::WebContents* web_contents) override; void BrowserCreated(CefBrowserHostBase* browser) override; void BrowserDestroyed(CefBrowserHostBase* browser) override; + CefWindowHandle GetHostWindowHandle() const override; SkColor GetBackgroundColor() const override; void SendKeyEvent(const CefKeyEvent& event) override; void SendMouseClickEvent(const CefMouseEvent& event, @@ -50,6 +51,8 @@ class CefBrowserPlatformDelegateChrome } protected: + gfx::NativeWindow GetNativeWindow() const; + std::unique_ptr native_delegate_; Browser* chrome_browser_ = nullptr; diff --git a/libcef/browser/chrome/chrome_browser_host_impl.cc b/libcef/browser/chrome/chrome_browser_host_impl.cc index 05d26de63..256131364 100644 --- a/libcef/browser/chrome/chrome_browser_host_impl.cc +++ b/libcef/browser/chrome/chrome_browser_host_impl.cc @@ -168,8 +168,7 @@ CefWindowHandle ChromeBrowserHostImpl::GetWindowHandle() { if (platform_delegate_) return platform_delegate_->GetHostWindowHandle(); } - NOTIMPLEMENTED(); - return kNullWindowHandle; + return host_window_handle_; } CefWindowHandle ChromeBrowserHostImpl::GetOpenerWindowHandle() { @@ -524,6 +523,8 @@ void ChromeBrowserHostImpl::SetBrowser(Browser* browser) { browser_ = browser; static_cast(platform_delegate_.get()) ->set_chrome_browser(browser); + if (browser_) + host_window_handle_ = platform_delegate_->GetHostWindowHandle(); } void ChromeBrowserHostImpl::WindowDestroyed() { diff --git a/libcef/browser/chrome/chrome_browser_host_impl.h b/libcef/browser/chrome/chrome_browser_host_impl.h index faabce1a4..c532d89d6 100644 --- a/libcef/browser/chrome/chrome_browser_host_impl.h +++ b/libcef/browser/chrome/chrome_browser_host_impl.h @@ -159,6 +159,7 @@ class ChromeBrowserHostImpl : public CefBrowserHostBase { int GetCurrentTabIndex() const; Browser* browser_ = nullptr; + CefWindowHandle host_window_handle_ = kNullWindowHandle; }; #endif // CEF_LIBCEF_BROWSER_CHROME_CHROME_BROWSER_HOST_IMPL_H_ diff --git a/libcef/browser/views/view_util.h b/libcef/browser/views/view_util.h index 5502d4ee7..e49ae5285 100644 --- a/libcef/browser/views/view_util.h +++ b/libcef/browser/views/view_util.h @@ -9,6 +9,7 @@ #include "include/views/cef_view.h" #include "include/views/cef_window.h" +#include "ui/gfx/native_widget_types.h" #include "ui/views/view.h" namespace display { @@ -123,6 +124,9 @@ gfx::NativeView GetNativeView(views::Widget* widget); // Returns the platform window handle for |widget|. May return nullptr. CefWindowHandle GetWindowHandle(views::Widget* widget); +// Returns the platform window handle for |window|. May return nullptr. +CefWindowHandle GetWindowHandle(gfx::NativeWindow window); + } // namespace view_util #endif // CEF_LIBCEF_BROWSER_VIEWS_VIEW_UTIL_H_ diff --git a/libcef/browser/views/view_util_aura.cc b/libcef/browser/views/view_util_aura.cc index 8488b5119..8a144eb33 100644 --- a/libcef/browser/views/view_util_aura.cc +++ b/libcef/browser/views/view_util_aura.cc @@ -26,9 +26,15 @@ gfx::NativeView GetNativeView(views::Widget* widget) { CefWindowHandle GetWindowHandle(views::Widget* widget) { // Same implementation as views::HWNDForView() but cross-platform. if (widget) { - aura::Window* window = widget->GetNativeWindow(); - if (window && window->GetRootWindow()) - return window->GetHost()->GetAcceleratedWidget(); + return GetWindowHandle(widget->GetNativeWindow()); + } + return kNullWindowHandle; +} + +CefWindowHandle GetWindowHandle(gfx::NativeWindow window) { + // |window| is an aura::Window*. + if (window && window->GetRootWindow()) { + return window->GetHost()->GetAcceleratedWidget(); } return kNullWindowHandle; } diff --git a/libcef/browser/views/view_util_mac.mm b/libcef/browser/views/view_util_mac.mm index c352dfb1c..16983e018 100644 --- a/libcef/browser/views/view_util_mac.mm +++ b/libcef/browser/views/view_util_mac.mm @@ -4,6 +4,8 @@ #include "libcef/browser/views/view_util.h" +#import + #include "include/internal/cef_types_mac.h" #include "ui/views/widget/widget.h" @@ -23,10 +25,20 @@ gfx::NativeView GetNativeView(views::Widget* widget) { } CefWindowHandle GetWindowHandle(views::Widget* widget) { + // |view| is a wrapper type from native_widget_types.h. auto view = GetNativeView(widget); if (view) return CAST_NSVIEW_TO_CEF_WINDOW_HANDLE(view.GetNativeNSView()); return kNullWindowHandle; } +CefWindowHandle GetWindowHandle(gfx::NativeWindow window) { + // |window| is a wrapper type from native_widget_types.h. + if (window) { + NSWindow* nswindow = window.GetNativeNSWindow(); + return CAST_NSVIEW_TO_CEF_WINDOW_HANDLE([nswindow contentView]); + } + return kNullWindowHandle; +} + } // namespace view_util