mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2024-12-26 17:42:35 +01:00
dca0435d2f
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]`
180 lines
4.9 KiB
C++
180 lines
4.9 KiB
C++
// Copyright (c) 2013 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 "tests/cefsimple/simple_handler.h"
|
|
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
#include "include/base/cef_callback.h"
|
|
#include "include/cef_app.h"
|
|
#include "include/cef_parser.h"
|
|
#include "include/views/cef_browser_view.h"
|
|
#include "include/views/cef_window.h"
|
|
#include "include/wrapper/cef_closure_task.h"
|
|
#include "include/wrapper/cef_helpers.h"
|
|
|
|
namespace {
|
|
|
|
SimpleHandler* g_instance = nullptr;
|
|
|
|
// Returns a data: URI with the specified contents.
|
|
std::string GetDataURI(const std::string& data, const std::string& mime_type) {
|
|
return "data:" + mime_type + ";base64," +
|
|
CefURIEncode(CefBase64Encode(data.data(), data.size()), false)
|
|
.ToString();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
SimpleHandler::SimpleHandler(bool is_alloy_style)
|
|
: is_alloy_style_(is_alloy_style) {
|
|
DCHECK(!g_instance);
|
|
g_instance = this;
|
|
}
|
|
|
|
SimpleHandler::~SimpleHandler() {
|
|
g_instance = nullptr;
|
|
}
|
|
|
|
// static
|
|
SimpleHandler* SimpleHandler::GetInstance() {
|
|
return g_instance;
|
|
}
|
|
|
|
void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
|
|
const CefString& title) {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
if (auto browser_view = CefBrowserView::GetForBrowser(browser)) {
|
|
// Set the title of the window using the Views framework.
|
|
CefRefPtr<CefWindow> window = browser_view->GetWindow();
|
|
if (window) {
|
|
window->SetTitle(title);
|
|
}
|
|
} else if (is_alloy_style_) {
|
|
// Set the title of the window using platform APIs.
|
|
PlatformTitleChange(browser, title);
|
|
}
|
|
}
|
|
|
|
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
// Sanity-check the configured runtime style.
|
|
CHECK_EQ(is_alloy_style_ ? CEF_RUNTIME_STYLE_ALLOY : CEF_RUNTIME_STYLE_CHROME,
|
|
browser->GetHost()->GetRuntimeStyle());
|
|
|
|
// Add to the list of existing browsers.
|
|
browser_list_.push_back(browser);
|
|
}
|
|
|
|
bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser) {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
// Closing the main window requires special handling. See the DoClose()
|
|
// documentation in the CEF header for a detailed destription of this
|
|
// process.
|
|
if (browser_list_.size() == 1) {
|
|
// Set a flag to indicate that the window close should be allowed.
|
|
is_closing_ = true;
|
|
}
|
|
|
|
// Allow the close. For windowed browsers this will result in the OS close
|
|
// event being sent.
|
|
return false;
|
|
}
|
|
|
|
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
// Remove from the list of existing browsers.
|
|
BrowserList::iterator bit = browser_list_.begin();
|
|
for (; bit != browser_list_.end(); ++bit) {
|
|
if ((*bit)->IsSame(browser)) {
|
|
browser_list_.erase(bit);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (browser_list_.empty()) {
|
|
// All browser windows have closed. Quit the application message loop.
|
|
CefQuitMessageLoop();
|
|
}
|
|
}
|
|
|
|
void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
|
|
CefRefPtr<CefFrame> frame,
|
|
ErrorCode errorCode,
|
|
const CefString& errorText,
|
|
const CefString& failedUrl) {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
// Allow Chrome to show the error page.
|
|
if (!is_alloy_style_) {
|
|
return;
|
|
}
|
|
|
|
// Don't display an error for downloaded files.
|
|
if (errorCode == ERR_ABORTED) {
|
|
return;
|
|
}
|
|
|
|
// Display a load error message using a data: URI.
|
|
std::stringstream ss;
|
|
ss << "<html><body bgcolor=\"white\">"
|
|
"<h2>Failed to load URL "
|
|
<< std::string(failedUrl) << " with error " << std::string(errorText)
|
|
<< " (" << errorCode << ").</h2></body></html>";
|
|
|
|
frame->LoadURL(GetDataURI(ss.str(), "text/html"));
|
|
}
|
|
|
|
void SimpleHandler::ShowMainWindow() {
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
// Execute on the UI thread.
|
|
CefPostTask(TID_UI, base::BindOnce(&SimpleHandler::ShowMainWindow, this));
|
|
return;
|
|
}
|
|
|
|
if (browser_list_.empty()) {
|
|
return;
|
|
}
|
|
|
|
auto main_browser = browser_list_.front();
|
|
|
|
if (auto browser_view = CefBrowserView::GetForBrowser(main_browser)) {
|
|
// Show the window using the Views framework.
|
|
if (auto window = browser_view->GetWindow()) {
|
|
window->Show();
|
|
}
|
|
} else if (is_alloy_style_) {
|
|
PlatformShowWindow(main_browser);
|
|
}
|
|
}
|
|
|
|
void SimpleHandler::CloseAllBrowsers(bool force_close) {
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
// Execute on the UI thread.
|
|
CefPostTask(TID_UI, base::BindOnce(&SimpleHandler::CloseAllBrowsers, this,
|
|
force_close));
|
|
return;
|
|
}
|
|
|
|
if (browser_list_.empty()) {
|
|
return;
|
|
}
|
|
|
|
BrowserList::const_iterator it = browser_list_.begin();
|
|
for (; it != browser_list_.end(); ++it) {
|
|
(*it)->GetHost()->CloseBrowser(force_close);
|
|
}
|
|
}
|
|
|
|
#if !defined(OS_MAC)
|
|
void SimpleHandler::PlatformShowWindow(CefRefPtr<CefBrowser> browser) {
|
|
NOTIMPLEMENTED();
|
|
}
|
|
#endif
|