cef/tests/cefclient/browser/main_context_impl.cc

283 lines
8.4 KiB
C++
Raw Normal View History

// Copyright (c) 2015 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/cefclient/browser/main_context_impl.h"
#include <algorithm>
#include "include/cef_parser.h"
#include "tests/shared/browser/client_app_browser.h"
#include "tests/shared/common/client_switches.h"
#include "tests/shared/common/string_util.h"
namespace client {
namespace {
// The default URL to load in a browser window.
const char kDefaultUrl[] = "https://www.google.com";
// Returns the ARGB value for |color|.
cef_color_t ParseColor(const std::string& color) {
const std::string& colorToLower = AsciiStrToLower(color);
2023-01-02 23:59:03 +01:00
if (colorToLower == "black") {
return CefColorSetARGB(255, 0, 0, 0);
2023-01-02 23:59:03 +01:00
} else if (colorToLower == "blue") {
return CefColorSetARGB(255, 0, 0, 255);
2023-01-02 23:59:03 +01:00
} else if (colorToLower == "green") {
return CefColorSetARGB(255, 0, 255, 0);
2023-01-02 23:59:03 +01:00
} else if (colorToLower == "red") {
return CefColorSetARGB(255, 255, 0, 0);
2023-01-02 23:59:03 +01:00
} else if (colorToLower == "white") {
return CefColorSetARGB(255, 255, 255, 255);
2023-01-02 23:59:03 +01:00
}
// Use the default color.
return 0;
}
} // namespace
MainContextImpl::MainContextImpl(CefRefPtr<CefCommandLine> command_line,
bool terminate_when_all_windows_closed)
: command_line_(command_line),
terminate_when_all_windows_closed_(terminate_when_all_windows_closed) {
DCHECK(command_line_.get());
// Set the main URL.
2023-01-02 23:59:03 +01:00
if (command_line_->HasSwitch(switches::kUrl)) {
main_url_ = command_line_->GetSwitchValue(switches::kUrl);
2023-01-02 23:59:03 +01:00
}
if (main_url_.empty()) {
main_url_ = kDefaultUrl;
2023-01-02 23:59:03 +01:00
}
Implement Views framework on Windows and Linux (issue #1749). - Add Views header files in a new include/views directory. - Add initial top-level window (CefWindow), control (CefBrowserView, CefLabelButton, CefMenuButton, CefPanel, CefScrollView, CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support. See libcef/browser/views/view_impl.h comments for implementation details. - Add Views example usage in cefclient and cefsimple and Views unit tests in cef_unittests. Pass the `--use-views` command-line flag to cefclient, cefsimple and cef_unittests to run using the Views framework instead of platform APIs. For cefclient and cefsimple this will create the browser window and all related functionality using the Views framework. For cef_unittests this will run all tests (except OSR tests) in a Views-based browser window. Views- specific unit tests (`--gtest_filter=Views*`) will be run even if the the `--use-views` flag is not specified. - Pass the `--hide-frame` command-line flag to cefclient to demo a frameless Views-based browser window. - Pass the `--hide-controls` command-line flag to cefclient to demo a browser window without top controls. This also works in non-Views mode. - Pass the `--enable-high-dpi-support` command-line flag to cef_unittests on Windows to test high-DPI support on a display that supports it. - Add CefImage for reading/writing image file formats. - Add CefBrowser::DownloadImage() for downloading image URLs as a CefImage representation. This is primarily for loading favicons. - Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for creating custom menus. This is primarily for use with CefMenuButton. - Add CefBrowser::TryCloseBrowser() helper for closing a browser. Also improve related documentation in cef_life_span_handler.h. - Rename cef_page_range_t to cef_range_t. It is now also used by CefTextfield. - Remove CefLifeSpanHandler::RunModal() which is never called. - Add draggable regions example to cefclient.
2016-01-19 21:09:01 +01:00
// Whether windowless (off-screen) rendering will be used.
use_windowless_rendering_ =
command_line_->HasSwitch(switches::kOffScreenRenderingEnabled);
if (use_windowless_rendering_ &&
command_line_->HasSwitch(switches::kOffScreenFrameRate)) {
windowless_frame_rate_ =
atoi(command_line_->GetSwitchValue(switches::kOffScreenFrameRate)
.ToString()
.c_str());
}
// Whether transparent painting is used with windowless rendering.
const bool use_transparent_painting =
use_windowless_rendering_ &&
command_line_->HasSwitch(switches::kTransparentPaintingEnabled);
#if defined(OS_WIN)
// Shared texture is only supported on Windows.
shared_texture_enabled_ =
use_windowless_rendering_ &&
command_line_->HasSwitch(switches::kSharedTextureEnabled);
#endif
external_begin_frame_enabled_ =
use_windowless_rendering_ &&
command_line_->HasSwitch(switches::kExternalBeginFrameEnabled);
if (windowless_frame_rate_ <= 0) {
// Choose a reasonable default rate based on the OSR mode.
#if defined(OS_WIN)
windowless_frame_rate_ = shared_texture_enabled_ ? 60 : 30;
#else
windowless_frame_rate_ = 30;
#endif
}
// Enable experimental Chrome runtime. See issue #2969 for details.
use_chrome_runtime_ =
command_line_->HasSwitch(switches::kEnableChromeRuntime);
if (use_windowless_rendering_ && use_chrome_runtime_) {
LOG(ERROR)
<< "Windowless rendering is not supported with the Chrome runtime.";
use_chrome_runtime_ = false;
}
Implement Views framework on Windows and Linux (issue #1749). - Add Views header files in a new include/views directory. - Add initial top-level window (CefWindow), control (CefBrowserView, CefLabelButton, CefMenuButton, CefPanel, CefScrollView, CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support. See libcef/browser/views/view_impl.h comments for implementation details. - Add Views example usage in cefclient and cefsimple and Views unit tests in cef_unittests. Pass the `--use-views` command-line flag to cefclient, cefsimple and cef_unittests to run using the Views framework instead of platform APIs. For cefclient and cefsimple this will create the browser window and all related functionality using the Views framework. For cef_unittests this will run all tests (except OSR tests) in a Views-based browser window. Views- specific unit tests (`--gtest_filter=Views*`) will be run even if the the `--use-views` flag is not specified. - Pass the `--hide-frame` command-line flag to cefclient to demo a frameless Views-based browser window. - Pass the `--hide-controls` command-line flag to cefclient to demo a browser window without top controls. This also works in non-Views mode. - Pass the `--enable-high-dpi-support` command-line flag to cef_unittests on Windows to test high-DPI support on a display that supports it. - Add CefImage for reading/writing image file formats. - Add CefBrowser::DownloadImage() for downloading image URLs as a CefImage representation. This is primarily for loading favicons. - Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for creating custom menus. This is primarily for use with CefMenuButton. - Add CefBrowser::TryCloseBrowser() helper for closing a browser. Also improve related documentation in cef_life_span_handler.h. - Rename cef_page_range_t to cef_range_t. It is now also used by CefTextfield. - Remove CefLifeSpanHandler::RunModal() which is never called. - Add draggable regions example to cefclient.
2016-01-19 21:09:01 +01:00
// Whether the Views framework will be used.
use_views_ = command_line_->HasSwitch(switches::kUseViews);
if (use_windowless_rendering_ && use_views_) {
LOG(ERROR)
<< "Windowless rendering is not supported by the Views framework.";
Implement Views framework on Windows and Linux (issue #1749). - Add Views header files in a new include/views directory. - Add initial top-level window (CefWindow), control (CefBrowserView, CefLabelButton, CefMenuButton, CefPanel, CefScrollView, CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support. See libcef/browser/views/view_impl.h comments for implementation details. - Add Views example usage in cefclient and cefsimple and Views unit tests in cef_unittests. Pass the `--use-views` command-line flag to cefclient, cefsimple and cef_unittests to run using the Views framework instead of platform APIs. For cefclient and cefsimple this will create the browser window and all related functionality using the Views framework. For cef_unittests this will run all tests (except OSR tests) in a Views-based browser window. Views- specific unit tests (`--gtest_filter=Views*`) will be run even if the the `--use-views` flag is not specified. - Pass the `--hide-frame` command-line flag to cefclient to demo a frameless Views-based browser window. - Pass the `--hide-controls` command-line flag to cefclient to demo a browser window without top controls. This also works in non-Views mode. - Pass the `--enable-high-dpi-support` command-line flag to cef_unittests on Windows to test high-DPI support on a display that supports it. - Add CefImage for reading/writing image file formats. - Add CefBrowser::DownloadImage() for downloading image URLs as a CefImage representation. This is primarily for loading favicons. - Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for creating custom menus. This is primarily for use with CefMenuButton. - Add CefBrowser::TryCloseBrowser() helper for closing a browser. Also improve related documentation in cef_life_span_handler.h. - Rename cef_page_range_t to cef_range_t. It is now also used by CefTextfield. - Remove CefLifeSpanHandler::RunModal() which is never called. - Add draggable regions example to cefclient.
2016-01-19 21:09:01 +01:00
use_views_ = false;
}
chrome: win/linux: Add support for browser with native parent (see issue #3294) This change adds Chrome runtime support on Windows and Linux for creating a browser parented to a native window supplied by the client application. Expected API usage and window behavior is similar to what already exists with the Alloy runtime. The parent window handle should be specified by using CefWindowInfo::SetAsChild in combination with the CefBrowserHost::CreateBrowser and CefLifeSpanHandler::OnBeforePopup callbacks. The previously existing behavior of creating a fully-featured Chrome browser window when empty CefWindowInfo is used with CreateBrowser remains unchanged and Views is still the preferred API for creating top-level Chrome windows with custom styling (e.g. title bar only, frameless, etc). The cefclient Popup Window test with a native parent window continues to crash on Linux with both the Alloy and Chrome runtimes (see issue #3165). Also adds Chrome runtime support for CefDisplayHandler::OnCursorChange. To test: - Run `cefclient --enable-chrome-runtime [--use-views]` for the default (and previously existing) Views-based behavior. - Run `cefclient --enable-chrome-runtime --use-native` for the new native parent window behavior. - Run `cefclient --enable-chrome-runtime --use-native --no-activate` and the window will not be activated (take input focus) on launch (Windows only). - Run `cefclient --enable-chrome-runtime [--use-views|--use-native] --mouse-cursor-change-disabled` and the mouse cursor will not change on mouseover of DOM elements.
2022-04-08 22:48:56 +02:00
#if defined(OS_WIN) || defined(OS_LINUX)
if (use_chrome_runtime_ && !use_views_ &&
!command_line->HasSwitch(switches::kUseNative)) {
LOG(WARNING) << "Chrome runtime defaults to the Views framework.";
use_views_ = true;
}
#else // !(defined(OS_WIN) || defined(OS_LINUX))
if (use_chrome_runtime_ && !use_views_) {
// TODO(chrome): Add support for this runtime configuration (e.g. a fully
// styled Chrome window with cefclient menu customizations). In the mean
// time this can be demo'd with "cefsimple --enable-chrome-runtime".
LOG(WARNING) << "Chrome runtime requires the Views framework.";
use_views_ = true;
}
chrome: win/linux: Add support for browser with native parent (see issue #3294) This change adds Chrome runtime support on Windows and Linux for creating a browser parented to a native window supplied by the client application. Expected API usage and window behavior is similar to what already exists with the Alloy runtime. The parent window handle should be specified by using CefWindowInfo::SetAsChild in combination with the CefBrowserHost::CreateBrowser and CefLifeSpanHandler::OnBeforePopup callbacks. The previously existing behavior of creating a fully-featured Chrome browser window when empty CefWindowInfo is used with CreateBrowser remains unchanged and Views is still the preferred API for creating top-level Chrome windows with custom styling (e.g. title bar only, frameless, etc). The cefclient Popup Window test with a native parent window continues to crash on Linux with both the Alloy and Chrome runtimes (see issue #3165). Also adds Chrome runtime support for CefDisplayHandler::OnCursorChange. To test: - Run `cefclient --enable-chrome-runtime [--use-views]` for the default (and previously existing) Views-based behavior. - Run `cefclient --enable-chrome-runtime --use-native` for the new native parent window behavior. - Run `cefclient --enable-chrome-runtime --use-native --no-activate` and the window will not be activated (take input focus) on launch (Windows only). - Run `cefclient --enable-chrome-runtime [--use-views|--use-native] --mouse-cursor-change-disabled` and the mouse cursor will not change on mouseover of DOM elements.
2022-04-08 22:48:56 +02:00
#endif // !(defined(OS_WIN) || defined(OS_LINUX))
Implement Views framework on Windows and Linux (issue #1749). - Add Views header files in a new include/views directory. - Add initial top-level window (CefWindow), control (CefBrowserView, CefLabelButton, CefMenuButton, CefPanel, CefScrollView, CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support. See libcef/browser/views/view_impl.h comments for implementation details. - Add Views example usage in cefclient and cefsimple and Views unit tests in cef_unittests. Pass the `--use-views` command-line flag to cefclient, cefsimple and cef_unittests to run using the Views framework instead of platform APIs. For cefclient and cefsimple this will create the browser window and all related functionality using the Views framework. For cef_unittests this will run all tests (except OSR tests) in a Views-based browser window. Views- specific unit tests (`--gtest_filter=Views*`) will be run even if the the `--use-views` flag is not specified. - Pass the `--hide-frame` command-line flag to cefclient to demo a frameless Views-based browser window. - Pass the `--hide-controls` command-line flag to cefclient to demo a browser window without top controls. This also works in non-Views mode. - Pass the `--enable-high-dpi-support` command-line flag to cef_unittests on Windows to test high-DPI support on a display that supports it. - Add CefImage for reading/writing image file formats. - Add CefBrowser::DownloadImage() for downloading image URLs as a CefImage representation. This is primarily for loading favicons. - Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for creating custom menus. This is primarily for use with CefMenuButton. - Add CefBrowser::TryCloseBrowser() helper for closing a browser. Also improve related documentation in cef_life_span_handler.h. - Rename cef_page_range_t to cef_range_t. It is now also used by CefTextfield. - Remove CefLifeSpanHandler::RunModal() which is never called. - Add draggable regions example to cefclient.
2016-01-19 21:09:01 +01:00
if (use_views_ && command_line->HasSwitch(switches::kHideFrame) &&
!command_line_->HasSwitch(switches::kUrl)) {
// Use the draggable regions test as the default URL for frameless windows.
main_url_ = "http://tests/draggable";
}
if (command_line_->HasSwitch(switches::kBackgroundColor)) {
// Parse the background color value.
background_color_ =
ParseColor(command_line_->GetSwitchValue(switches::kBackgroundColor));
}
if (background_color_ == 0 && !use_views_) {
// Set an explicit background color.
background_color_ = CefColorSetARGB(255, 255, 255, 255);
}
// |browser_background_color_| should remain 0 to enable transparent painting.
if (!use_transparent_painting) {
browser_background_color_ = background_color_;
}
}
MainContextImpl::~MainContextImpl() {
// The context must either not have been initialized, or it must have also
// been shut down.
DCHECK(!initialized_ || shutdown_);
}
std::string MainContextImpl::GetConsoleLogPath() {
return GetAppWorkingDirectory() + "console.log";
}
std::string MainContextImpl::GetMainURL() {
return main_url_;
}
cef_color_t MainContextImpl::GetBackgroundColor() {
return background_color_;
}
bool MainContextImpl::UseChromeRuntime() {
return use_chrome_runtime_;
}
Implement Views framework on Windows and Linux (issue #1749). - Add Views header files in a new include/views directory. - Add initial top-level window (CefWindow), control (CefBrowserView, CefLabelButton, CefMenuButton, CefPanel, CefScrollView, CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support. See libcef/browser/views/view_impl.h comments for implementation details. - Add Views example usage in cefclient and cefsimple and Views unit tests in cef_unittests. Pass the `--use-views` command-line flag to cefclient, cefsimple and cef_unittests to run using the Views framework instead of platform APIs. For cefclient and cefsimple this will create the browser window and all related functionality using the Views framework. For cef_unittests this will run all tests (except OSR tests) in a Views-based browser window. Views- specific unit tests (`--gtest_filter=Views*`) will be run even if the the `--use-views` flag is not specified. - Pass the `--hide-frame` command-line flag to cefclient to demo a frameless Views-based browser window. - Pass the `--hide-controls` command-line flag to cefclient to demo a browser window without top controls. This also works in non-Views mode. - Pass the `--enable-high-dpi-support` command-line flag to cef_unittests on Windows to test high-DPI support on a display that supports it. - Add CefImage for reading/writing image file formats. - Add CefBrowser::DownloadImage() for downloading image URLs as a CefImage representation. This is primarily for loading favicons. - Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for creating custom menus. This is primarily for use with CefMenuButton. - Add CefBrowser::TryCloseBrowser() helper for closing a browser. Also improve related documentation in cef_life_span_handler.h. - Rename cef_page_range_t to cef_range_t. It is now also used by CefTextfield. - Remove CefLifeSpanHandler::RunModal() which is never called. - Add draggable regions example to cefclient.
2016-01-19 21:09:01 +01:00
bool MainContextImpl::UseViews() {
return use_views_;
}
bool MainContextImpl::UseWindowlessRendering() {
return use_windowless_rendering_;
}
bool MainContextImpl::TouchEventsEnabled() {
return command_line_->GetSwitchValue("touch-events") == "enabled";
}
bool MainContextImpl::UseDefaultPopup() {
return !use_windowless_rendering_ &&
command_line_->HasSwitch(switches::kUseDefaultPopup);
}
void MainContextImpl::PopulateSettings(CefSettings* settings) {
client::ClientAppBrowser::PopulateSettings(command_line_, *settings);
2023-01-02 23:59:03 +01:00
if (use_chrome_runtime_) {
settings->chrome_runtime = true;
2023-01-02 23:59:03 +01:00
}
CefString(&settings->cache_path) =
command_line_->GetSwitchValue(switches::kCachePath);
2023-01-02 23:59:03 +01:00
if (use_windowless_rendering_) {
settings->windowless_rendering_enabled = true;
2023-01-02 23:59:03 +01:00
}
2023-01-02 23:59:03 +01:00
if (browser_background_color_ != 0) {
settings->background_color = browser_background_color_;
2023-01-02 23:59:03 +01:00
}
if (command_line_->HasSwitch("lang")) {
// Use the same locale for the Accept-Language HTTP request header.
CefString(&settings->accept_language_list) =
command_line_->GetSwitchValue("lang");
}
}
void MainContextImpl::PopulateBrowserSettings(CefBrowserSettings* settings) {
settings->windowless_frame_rate = windowless_frame_rate_;
2023-01-02 23:59:03 +01:00
if (browser_background_color_ != 0) {
settings->background_color = browser_background_color_;
2023-01-02 23:59:03 +01:00
}
if (use_chrome_runtime_ &&
command_line_->HasSwitch(switches::kHideChromeStatusBubble)) {
settings->chrome_status_bubble = STATE_DISABLED;
}
}
void MainContextImpl::PopulateOsrSettings(OsrRendererSettings* settings) {
settings->show_update_rect =
command_line_->HasSwitch(switches::kShowUpdateRect);
#if defined(OS_WIN)
settings->shared_texture_enabled = shared_texture_enabled_;
#endif
settings->external_begin_frame_enabled = external_begin_frame_enabled_;
settings->begin_frame_rate = windowless_frame_rate_;
2023-01-02 23:59:03 +01:00
if (browser_background_color_ != 0) {
settings->background_color = browser_background_color_;
2023-01-02 23:59:03 +01:00
}
}
RootWindowManager* MainContextImpl::GetRootWindowManager() {
DCHECK(InValidState());
return root_window_manager_.get();
}
bool MainContextImpl::Initialize(const CefMainArgs& args,
const CefSettings& settings,
CefRefPtr<CefApp> application,
void* windows_sandbox_info) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!initialized_);
DCHECK(!shutdown_);
2023-01-02 23:59:03 +01:00
if (!CefInitialize(args, settings, application, windows_sandbox_info)) {
return false;
2023-01-02 23:59:03 +01:00
}
// Need to create the RootWindowManager after calling CefInitialize because
// TempWindowX11 uses cef_get_xdisplay().
root_window_manager_.reset(
new RootWindowManager(terminate_when_all_windows_closed_));
initialized_ = true;
return true;
}
void MainContextImpl::Shutdown() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(initialized_);
DCHECK(!shutdown_);
root_window_manager_.reset();
CefShutdown();
shutdown_ = true;
}
} // namespace client