views: Fix style calculation for popup BrowserViews (fixes #3499)

Don't depend on a CefBrowserViewDelegate for popup BrowserView style.
Popup style must always match the opener style.
This commit is contained in:
Marshall Greenblatt
2024-10-08 14:17:18 -04:00
parent 0a627230ac
commit e23bf31bf4
3 changed files with 56 additions and 30 deletions

View File

@@ -195,8 +195,9 @@ void CefBrowserPlatformDelegate::PopupWebContentsCreated(
} }
CefRefPtr<CefBrowserViewDelegate> new_delegate; CefRefPtr<CefBrowserViewDelegate> new_delegate;
CefRefPtr<CefBrowserViewDelegate> opener_delegate; CefRefPtr<CefBrowserViewDelegate> opener_delegate;
cef_runtime_style_t opener_runtime_style = CEF_RUNTIME_STYLE_DEFAULT;
auto browser_view = GetBrowserView(); auto browser_view = GetBrowserView();
if (browser_view) { if (browser_view) {
// When |this| (the popup opener) is Views-hosted use the current delegate. // When |this| (the popup opener) is Views-hosted use the current delegate.
@@ -212,9 +213,16 @@ void CefBrowserPlatformDelegate::PopupWebContentsCreated(
browser_view, settings, client, is_devtools); browser_view, settings, client, is_devtools);
} }
if (browser_view) {
opener_runtime_style = browser_view->GetRuntimeStyle();
} else if (opener_delegate) {
opener_runtime_style = opener_delegate->GetBrowserRuntimeStyle();
}
// Create a new BrowserView for the popup. // Create a new BrowserView for the popup.
CefRefPtr<CefBrowserViewImpl> new_browser_view = CefRefPtr<CefBrowserViewImpl> new_browser_view =
CefBrowserViewImpl::CreateForPopup(settings, new_delegate, is_devtools); CefBrowserViewImpl::CreateForPopup(settings, new_delegate, is_devtools,
opener_runtime_style);
// Associate the PlatformDelegate with the new BrowserView. // Associate the PlatformDelegate with the new BrowserView.
new_platform_delegate->SetBrowserView(new_browser_view); new_platform_delegate->SetBrowserView(new_browser_view);

View File

@@ -35,30 +35,39 @@ std::optional<cef_gesture_command_t> GetGestureCommand(
return std::nullopt; return std::nullopt;
} }
bool ComputeAlloyStyle(CefBrowserViewDelegate* cef_delegate, bool ComputeAlloyStyle(
bool is_devtools_popup) { CefBrowserViewDelegate* cef_delegate,
// Alloy style is not supported with Chrome DevTools popups. bool is_devtools_popup,
const bool supports_alloy_style = !is_devtools_popup; std::optional<cef_runtime_style_t> opener_runtime_style) {
const auto default_style = CEF_RUNTIME_STYLE_CHROME; if (is_devtools_popup) {
// Alloy style is not supported with Chrome DevTools popups.
auto result_style = default_style; if (cef_delegate &&
cef_delegate->GetBrowserRuntimeStyle() == CEF_RUNTIME_STYLE_ALLOY) {
if (cef_delegate) { LOG(ERROR) << "GetBrowserRuntimeStyle() requested Alloy style; only "
auto requested_style = cef_delegate->GetBrowserRuntimeStyle(); "Chrome style is supported for DevTools popups";
if (requested_style == CEF_RUNTIME_STYLE_ALLOY) {
if (supports_alloy_style) {
result_style = requested_style;
} else {
LOG(ERROR) << "GetBrowserRuntimeStyle() requested Alloy style; only "
"Chrome style is supported";
}
} else if (requested_style == CEF_RUNTIME_STYLE_CHROME) {
// Chrome style is always supported.
result_style = requested_style;
} }
return false;
} }
return result_style == CEF_RUNTIME_STYLE_ALLOY; if (opener_runtime_style) {
// Popup style must match the opener style.
const bool opener_alloy_style =
*opener_runtime_style == CEF_RUNTIME_STYLE_ALLOY;
if (cef_delegate) {
const auto requested_style = cef_delegate->GetBrowserRuntimeStyle();
if (requested_style != CEF_RUNTIME_STYLE_DEFAULT &&
requested_style != (opener_alloy_style ? CEF_RUNTIME_STYLE_ALLOY
: CEF_RUNTIME_STYLE_CHROME)) {
LOG(ERROR)
<< "GetBrowserRuntimeStyle() for popups must match opener style";
}
}
return opener_alloy_style;
}
// Chrome style is the default unless Alloy is specifically requested.
return cef_delegate &&
cef_delegate->GetBrowserRuntimeStyle() == CEF_RUNTIME_STYLE_ALLOY;
} }
} // namespace } // namespace
@@ -111,7 +120,8 @@ CefRefPtr<CefBrowserViewImpl> CefBrowserViewImpl::Create(
} }
CefRefPtr<CefBrowserViewImpl> browser_view = CefRefPtr<CefBrowserViewImpl> browser_view =
new CefBrowserViewImpl(delegate, /*is_devtools_popup=*/false); new CefBrowserViewImpl(delegate, /*is_devtools_popup=*/false,
/*opener_runtime_style=*/std::nullopt);
browser_view->SetPendingBrowserCreateParams( browser_view->SetPendingBrowserCreateParams(
window_info, client, url, settings, extra_info, request_context); window_info, client, url, settings, extra_info, request_context);
browser_view->Initialize(); browser_view->Initialize();
@@ -123,11 +133,12 @@ CefRefPtr<CefBrowserViewImpl> CefBrowserViewImpl::Create(
CefRefPtr<CefBrowserViewImpl> CefBrowserViewImpl::CreateForPopup( CefRefPtr<CefBrowserViewImpl> CefBrowserViewImpl::CreateForPopup(
const CefBrowserSettings& settings, const CefBrowserSettings& settings,
CefRefPtr<CefBrowserViewDelegate> delegate, CefRefPtr<CefBrowserViewDelegate> delegate,
bool is_devtools) { bool is_devtools,
cef_runtime_style_t opener_runtime_style) {
CEF_REQUIRE_UIT_RETURN(nullptr); CEF_REQUIRE_UIT_RETURN(nullptr);
CefRefPtr<CefBrowserViewImpl> browser_view = CefRefPtr<CefBrowserViewImpl> browser_view =
new CefBrowserViewImpl(delegate, is_devtools); new CefBrowserViewImpl(delegate, is_devtools, opener_runtime_style);
browser_view->Initialize(); browser_view->Initialize();
browser_view->SetDefaults(settings); browser_view->SetDefaults(settings);
return browser_view; return browser_view;
@@ -337,9 +348,12 @@ bool CefBrowserViewImpl::OnGestureEvent(ui::GestureEvent* event) {
CefBrowserViewImpl::CefBrowserViewImpl( CefBrowserViewImpl::CefBrowserViewImpl(
CefRefPtr<CefBrowserViewDelegate> delegate, CefRefPtr<CefBrowserViewDelegate> delegate,
bool is_devtools_popup) bool is_devtools_popup,
std::optional<cef_runtime_style_t> opener_runtime_style)
: ParentClass(delegate), : ParentClass(delegate),
is_alloy_style_(ComputeAlloyStyle(delegate.get(), is_devtools_popup)), is_alloy_style_(ComputeAlloyStyle(delegate.get(),
is_devtools_popup,
opener_runtime_style)),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
void CefBrowserViewImpl::SetPendingBrowserCreateParams( void CefBrowserViewImpl::SetPendingBrowserCreateParams(

View File

@@ -6,6 +6,8 @@
#define CEF_LIBCEF_BROWSER_VIEWS_BROWSER_VIEW_IMPL_H_ #define CEF_LIBCEF_BROWSER_VIEWS_BROWSER_VIEW_IMPL_H_
#pragma once #pragma once
#include <optional>
#include "base/functional/callback_forward.h" #include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
@@ -49,7 +51,8 @@ class CefBrowserViewImpl
static CefRefPtr<CefBrowserViewImpl> CreateForPopup( static CefRefPtr<CefBrowserViewImpl> CreateForPopup(
const CefBrowserSettings& settings, const CefBrowserSettings& settings,
CefRefPtr<CefBrowserViewDelegate> delegate, CefRefPtr<CefBrowserViewDelegate> delegate,
bool is_devtools); bool is_devtools,
cef_runtime_style_t opener_runtime_style);
// Called from CefBrowserPlatformDelegate[Chrome]Views. // Called from CefBrowserPlatformDelegate[Chrome]Views.
void WebContentsCreated(content::WebContents* web_contents); void WebContentsCreated(content::WebContents* web_contents);
@@ -101,7 +104,8 @@ class CefBrowserViewImpl
// Always call Initialize() after creation. // Always call Initialize() after creation.
// |delegate| may be nullptr. // |delegate| may be nullptr.
CefBrowserViewImpl(CefRefPtr<CefBrowserViewDelegate> delegate, CefBrowserViewImpl(CefRefPtr<CefBrowserViewDelegate> delegate,
bool is_devtools_popup); bool is_devtools_popup,
std::optional<cef_runtime_style_t> opener_runtime_style);
void SetPendingBrowserCreateParams( void SetPendingBrowserCreateParams(
const CefWindowInfo& window_info, const CefWindowInfo& window_info,