cef/libcef/renderer/chrome/chrome_content_renderer_client_cef.cc
Marshall Greenblatt dca0435d2f chrome: Add support for Alloy style browsers and windows (see #3681)
Split the Alloy runtime into bootstrap and style components. Support
creation of Alloy style browsers and windows with the Chrome runtime.
Chrome runtime (`--enable-chrome-runtime`) + Alloy style
(`--use-alloy-style`) supports Views (`--use-views`), native parent
(`--use-native`) and windowless rendering
(`--off-screen-rendering-enabled`).

Print preview is supported in all cases except with windowless rendering
on all platforms and native parent on MacOS. It is disabled by default
with Alloy style for legacy compatibility. Where supported it can be
enabled or disabled globally using `--[enable|disable]-print-preview` or
configured on a per-RequestContext basis using the
`printing.print_preview_disabled` preference. It also behaves as
expected when triggered via the PDF viewer print button.

Chrome runtime + Alloy style behavior differs from Alloy runtime in the
following significant ways:

- Supports Chrome error pages by default.
- DevTools popups are Chrome style only (cannot be windowless).
- The Alloy extension API will not supported.

Chrome runtime + Alloy style passes all expected Alloy ceftests except
the following:

- `DisplayTest.AutoResize` (Alloy extension API not supported)
- `DownloadTest.*` (Download API not yet supported)
- `ExtensionTest.*` (Alloy extension API not supported)

This change also adds Chrome runtime support for
CefContextMenuHandler::RunContextMenu (see #3293).

This change also explicitly blocks (and doesn't retry) FrameAttached
requests from PDF viewer and print preview excluded frames (see #3664).

Known issues specific to Chrome runtime + Alloy style:
- DevTools popup with windowless rendering doesn't load successfully.
  Use windowed rendering or remote debugging as a workaround.
- Chrome style Window with Alloy style BrowserView (`--use-alloy-style
  --use-chrome-style-window`) does not show Chrome theme changes.

To test:
- Run `ceftests --enable-chrome-runtime --use-alloy-style
       [--use-chrome-style-window] [--use-views|--use-native]
       --gtest_filter=...`
- Run `cefclient --enable-chrome-runtime --use-alloy-style
       [--use-chrome-style-window]
       [--use-views|--use-native|--off-screen-rendering-enabled]`
- Run `cefsimple --enable-chrome-runtime --use-alloy-style [--use-views]`
2024-04-22 14:57:37 -04:00

132 lines
4.6 KiB
C++

// Copyright 2020 The Chromium Embedded Framework Authors.
// Portions copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "libcef/renderer/chrome/chrome_content_renderer_client_cef.h"
#include "libcef/renderer/render_frame_observer.h"
#include "libcef/renderer/render_manager.h"
#include "libcef/renderer/thread_util.h"
#include "chrome/renderer/printing/chrome_print_render_frame_helper_delegate.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "third_party/blink/public/web/web_view.h"
ChromeContentRendererClientCef::ChromeContentRendererClientCef()
: render_manager_(new CefRenderManager) {}
ChromeContentRendererClientCef::~ChromeContentRendererClientCef() = default;
scoped_refptr<base::SingleThreadTaskRunner>
ChromeContentRendererClientCef::GetCurrentTaskRunner() {
// Check if currently on the render thread.
if (CEF_CURRENTLY_ON_RT()) {
return render_task_runner_;
}
return nullptr;
}
void ChromeContentRendererClientCef::RenderThreadStarted() {
ChromeContentRendererClient::RenderThreadStarted();
render_task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
}
void ChromeContentRendererClientCef::RenderThreadConnected() {
ChromeContentRendererClient::RenderThreadConnected();
render_manager_->RenderThreadConnected();
}
void ChromeContentRendererClientCef::RenderFrameCreated(
content::RenderFrame* render_frame) {
// Will delete itself when no longer needed.
CefRenderFrameObserver* render_frame_observer =
new CefRenderFrameObserver(render_frame);
bool browser_created;
std::optional<bool> is_windowless;
std::optional<bool> print_preview_enabled;
render_manager_->RenderFrameCreated(render_frame, render_frame_observer,
browser_created, is_windowless,
print_preview_enabled);
if (browser_created) {
OnBrowserCreated(render_frame->GetWebView(), is_windowless);
}
if (print_preview_enabled.has_value()) {
// This value will be used when the when ChromeContentRendererClient
// creates the new ChromePrintRenderFrameHelperDelegate below.
ChromePrintRenderFrameHelperDelegate::SetNextPrintPreviewEnabled(
*print_preview_enabled);
}
ChromeContentRendererClient::RenderFrameCreated(render_frame);
}
void ChromeContentRendererClientCef::WebViewCreated(
blink::WebView* web_view,
bool was_created_by_renderer,
const url::Origin* outermost_origin) {
ChromeContentRendererClient::WebViewCreated(web_view, was_created_by_renderer,
outermost_origin);
bool browser_created;
std::optional<bool> is_windowless;
std::optional<bool> print_preview_enabled;
render_manager_->WebViewCreated(web_view, browser_created, is_windowless,
print_preview_enabled);
if (browser_created) {
OnBrowserCreated(web_view, is_windowless);
}
}
void ChromeContentRendererClientCef::DevToolsAgentAttached() {
// WebWorkers may be creating agents on a different thread.
if (!render_task_runner_->BelongsToCurrentThread()) {
render_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ChromeContentRendererClientCef::DevToolsAgentAttached,
base::Unretained(this)));
return;
}
render_manager_->DevToolsAgentAttached();
}
void ChromeContentRendererClientCef::DevToolsAgentDetached() {
// WebWorkers may be creating agents on a different thread.
if (!render_task_runner_->BelongsToCurrentThread()) {
render_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ChromeContentRendererClientCef::DevToolsAgentDetached,
base::Unretained(this)));
return;
}
render_manager_->DevToolsAgentDetached();
}
void ChromeContentRendererClientCef::ExposeInterfacesToBrowser(
mojo::BinderMap* binders) {
ChromeContentRendererClient::ExposeInterfacesToBrowser(binders);
render_manager_->ExposeInterfacesToBrowser(binders);
}
void ChromeContentRendererClientCef::OnBrowserCreated(
blink::WebView* web_view,
std::optional<bool> is_windowless) {
#if BUILDFLAG(IS_MAC)
const bool windowless = is_windowless.has_value() && *is_windowless;
// FIXME: It would be better if this API would be a callback from the
// WebKit layer, or if it would be exposed as an WebView instance method; the
// current implementation uses a static variable, and WebKit needs to be
// patched in order to make it work for each WebView instance
web_view->SetUseExternalPopupMenusThisInstance(!windowless);
#endif
}