cef/libcef/browser/chrome/views/chrome_browser_view.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

102 lines
3.3 KiB
C++

// Copyright 2021 The Chromium Embedded Framework 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/browser/chrome/views/chrome_browser_view.h"
#include "libcef/browser/chrome/views/chrome_browser_frame.h"
#include "libcef/browser/views/browser_view_impl.h"
ChromeBrowserView::ChromeBrowserView(CefBrowserViewImpl* cef_browser_view)
: ParentClass(cef_browser_view->delegate()),
cef_browser_view_(cef_browser_view) {}
void ChromeBrowserView::InitBrowser(std::unique_ptr<Browser> browser) {
DCHECK(!web_view_);
// Initialize the BrowserFrame and BrowserView.
auto chrome_widget = static_cast<ChromeBrowserFrame*>(GetWidget());
chrome_widget->Init(this, std::move(browser));
// Retrieve the views::WebView that was created by the above initializations.
web_view_ = cef_browser_view_->web_view();
DCHECK(web_view_);
ParentClass::AddedToWidget();
}
void ChromeBrowserView::Destroyed() {
DCHECK(!destroyed_);
destroyed_ = true;
web_view_ = nullptr;
}
void ChromeBrowserView::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
ParentClass::ViewHierarchyChanged(details);
if (details.is_add && details.child == this) {
gfx::Size size = GetPreferredSize();
if (size.IsEmpty()) {
// No size was provided for this View. Size it to the parent by default
// or, depending on the Layout, the browser may be initially 0x0 size and
// will not display until the parent is next resized (resulting in a call
// to WebView::OnBoundsChanged). For example, this can happen when adding
// this View to a CefWindow with FillLayout and then calling
// CefWindow::Show() without first resizing the CefWindow.
size = details.parent->GetPreferredSize();
if (!size.IsEmpty()) {
SetSize(size);
}
}
}
}
void ChromeBrowserView::AddedToWidget() {
// Create the Browser and ChromeBrowserHostImpl.
// Results in a call to InitBrowser which calls ParentClass::AddedToWidget.
cef_browser_view_->AddedToWidget();
}
void ChromeBrowserView::RemovedFromWidget() {
ParentClass::RemovedFromWidget();
cef_browser_view_->RemovedFromWidget();
}
void ChromeBrowserView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
ParentClass::OnBoundsChanged(previous_bounds);
cef_browser_view_->OnBoundsChanged();
}
void ChromeBrowserView::OnGestureEvent(ui::GestureEvent* event) {
if (cef_browser_view_->OnGestureEvent(event)) {
return;
}
ParentClass::OnGestureEvent(event);
}
ToolbarView* ChromeBrowserView::OverrideCreateToolbar() {
if (cef_delegate()) {
auto toolbar_type = cef_delegate()->GetChromeToolbarType(cef_browser_view_);
std::optional<ToolbarView::DisplayMode> display_mode;
switch (toolbar_type) {
case CEF_CTT_NORMAL:
display_mode = ToolbarView::DisplayMode::NORMAL;
break;
case CEF_CTT_LOCATION:
display_mode = ToolbarView::DisplayMode::LOCATION;
break;
default:
break;
}
if (display_mode) {
cef_toolbar_ =
CefToolbarViewImpl::Create(nullptr, browser(), this, display_mode);
// Ownership will be taken by BrowserView.
view_util::PassOwnership(cef_toolbar_).release();
return cef_toolbar_->root_view();
}
}
return nullptr;
}