chrome: Change popup default behavior with native parent (see issue #3294)

When using a Views-hosted browser window the client receives Views-related
callbacks for popups and can thereby customize the Views-hosted popup behavior.
When using an external parent window no Views-related callbacks are delivered
and customization options are restricted to providing a new parent handle via
OnBeforePopup. Consequently, we should default to a normal browser window in
the external parent case instead of the very minimial Views-hosted default.

To test (A):
1. Run `cefclient --use-default-popup --enable-chrome-runtime --use-native`
2. Select Tests > Popup Window
3. Get a normal Chrome browser window

To test (B):
1. Run `cefclient --use-default-popup [--enable-chrome-runtime] [--use-views]`
2. Select Tests > Popup Window
3. Get a native or Views-hosted browser window with title bar only
This commit is contained in:
Marshall Greenblatt 2022-04-11 17:29:53 -04:00
parent 75ca552a4e
commit a0a7a35fe2
8 changed files with 41 additions and 23 deletions

View File

@ -25,7 +25,6 @@
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "extensions/browser/process_manager.h"
#include "pdf/pdf_features.h"
#include "printing/mojom/print.mojom.h"
@ -162,14 +161,6 @@ bool CefBrowserPlatformDelegateAlloy::
return true;
}
void CefBrowserPlatformDelegateAlloy::RenderViewCreated(
content::RenderViewHost* render_view_host) {
// Indicate that the view has an external parent (namely us). This setting is
// required for proper focus handling on Windows and Linux.
if (!IsViewsHosted() && render_view_host->GetWidget()->GetView())
render_view_host->GetWidget()->GetView()->SetHasExternalParent(true);
}
void CefBrowserPlatformDelegateAlloy::RenderViewReady() {
ConfigureAutoResize();
}

View File

@ -35,7 +35,6 @@ class CefBrowserPlatformDelegateAlloy : public CefBrowserPlatformDelegate {
bool* was_blocked) override;
bool ShouldAllowRendererInitiatedCrossProcessNavigation(
bool is_main_frame_navigation) override;
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void RenderViewReady() override;
void BrowserCreated(CefBrowserHostBase* browser) override;
void CreateExtensionHost(const extensions::Extension* extension,

View File

@ -179,7 +179,16 @@ bool CefBrowserInfoManager::CanCreateWindow(
if (allow) {
CefBrowserCreateParams create_params;
create_params.MaybeSetWindowInfo(window_info);
create_params.popup_with_views_hosted_opener = browser->HasView();
// In most cases, Views-hosted browsers should create Views-hosted popups
// and native browsers should use default popup handling. The one exception
// is with the Chrome runtime where a Views-hosted browser may have an
// external parent. In that case we want to use default popup handling even
// though the parent is (technically) Views-hosted.
create_params.popup_with_views_hosted_opener =
browser->HasView() &&
!browser->platform_delegate()->HasExternalParent();
create_params.settings = pending_popup->settings;
create_params.client = pending_popup->client;
create_params.extra_info = pending_popup->extra_info;

View File

@ -7,6 +7,9 @@
#include "libcef/browser/alloy/alloy_browser_host_impl.h"
#include "base/logging.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
CefBrowserPlatformDelegate::CefBrowserPlatformDelegate() = default;
@ -61,7 +64,12 @@ bool CefBrowserPlatformDelegate::
}
void CefBrowserPlatformDelegate::RenderViewCreated(
content::RenderViewHost* render_view_host) {}
content::RenderViewHost* render_view_host) {
// Indicate that the view has an external parent (namely us). This setting is
// required for proper focus handling on Windows and Linux.
if (HasExternalParent() && render_view_host->GetWidget()->GetView())
render_view_host->GetWidget()->GetView()->SetHasExternalParent(true);
}
void CefBrowserPlatformDelegate::RenderViewReady() {}
@ -242,6 +250,12 @@ bool CefBrowserPlatformDelegate::IsViewsHosted() const {
return false;
}
bool CefBrowserPlatformDelegate::HasExternalParent() const {
// In the majority of cases a Views-hosted browser will not have an external
// parent, and visa-versa.
return !IsViewsHosted();
}
void CefBrowserPlatformDelegate::WasHidden(bool hidden) {
NOTREACHED();
}

View File

@ -288,6 +288,10 @@ class CefBrowserPlatformDelegate {
// be called on multiple threads.
virtual bool IsViewsHosted() const;
// Returns true if this delegate implements a browser with external
// (client-provided) parent window. May be called on multiple threads.
virtual bool HasExternalParent() const;
// Notify the browser that it was hidden. Only used with windowless rendering.
virtual void WasHidden(bool hidden);

View File

@ -14,15 +14,6 @@ CefBrowserPlatformDelegateChromeChildWindow::
: CefBrowserPlatformDelegateChromeViews(std::move(native_delegate),
browser_view) {}
void CefBrowserPlatformDelegateChromeChildWindow::RenderViewCreated(
content::RenderViewHost* render_view_host) {
CefBrowserPlatformDelegateChromeViews::RenderViewCreated(render_view_host);
// Calls SetHasExternalParent(true). This setting is required for proper
// focus handling on Windows and Linux.
native_delegate_->RenderViewCreated(render_view_host);
}
void CefBrowserPlatformDelegateChromeChildWindow::CloseHostWindow() {
native_delegate_->CloseHostWindow();
}

View File

@ -16,13 +16,14 @@ class CefBrowserPlatformDelegateChromeChildWindow
CefRefPtr<CefBrowserViewImpl> browser_view);
// CefBrowserPlatformDelegate overrides.
void RenderViewCreated(content::RenderViewHost* render_view_host) override;
void CloseHostWindow() override;
void SetFocus(bool focus) override;
#if BUILDFLAG(IS_WIN) || (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC))
void NotifyMoveOrResizeStarted() override;
#endif
bool HasExternalParent() const override { return true; }
};
#endif // CEF_LIBCEF_BROWSER_CHROME_VIEWS_BROWSER_PLATFORM_DELEGATE_CHROME_CHILD_WINDOW_H_

View File

@ -117,7 +117,11 @@ void CefBrowserPlatformDelegateChromeViews::PopupWebContentsCreated(
content::WebContents* new_web_contents,
CefBrowserPlatformDelegate* new_platform_delegate,
bool is_devtools) {
DCHECK(new_platform_delegate->IsViewsHosted());
// Default popup handling may not be Views-hosted.
if (!new_platform_delegate->IsViewsHosted()) {
return;
}
auto* new_platform_delegate_impl =
static_cast<CefBrowserPlatformDelegateChromeViews*>(
new_platform_delegate);
@ -139,6 +143,11 @@ void CefBrowserPlatformDelegateChromeViews::PopupWebContentsCreated(
void CefBrowserPlatformDelegateChromeViews::PopupBrowserCreated(
CefBrowserHostBase* new_browser,
bool is_devtools) {
// Default popup handling may not be Views-hosted.
if (!new_browser->HasView()) {
return;
}
CefRefPtr<CefBrowserView> new_browser_view =
CefBrowserView::GetForBrowser(new_browser);
DCHECK(new_browser_view);