2015-01-23 00:11:30 +01:00
|
|
|
// 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.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/main_context_impl.h"
|
2015-01-23 00:11:30 +01:00
|
|
|
|
2021-06-20 18:08:10 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
#include "include/cef_parser.h"
|
2021-04-09 20:34:45 +02:00
|
|
|
#include "tests/shared/browser/client_app_browser.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/common/client_switches.h"
|
2022-08-02 22:30:37 +02:00
|
|
|
#include "tests/shared/common/string_util.h"
|
2015-01-23 00:11:30 +01:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The default URL to load in a browser window.
|
2023-01-03 19:53:40 +01:00
|
|
|
const char kDefaultUrl[] = "https://www.google.com";
|
2015-01-23 00:11:30 +01:00
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
// Returns the ARGB value for |color|.
|
|
|
|
cef_color_t ParseColor(const std::string& color) {
|
2022-08-02 22:30:37 +02:00
|
|
|
const std::string& colorToLower = AsciiStrToLower(color);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (colorToLower == "black") {
|
2016-02-05 01:49:19 +01:00
|
|
|
return CefColorSetARGB(255, 0, 0, 0);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (colorToLower == "blue") {
|
2016-02-05 01:49:19 +01:00
|
|
|
return CefColorSetARGB(255, 0, 0, 255);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (colorToLower == "green") {
|
2016-02-05 01:49:19 +01:00
|
|
|
return CefColorSetARGB(255, 0, 255, 0);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (colorToLower == "red") {
|
2016-02-05 01:49:19 +01:00
|
|
|
return CefColorSetARGB(255, 255, 0, 0);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (colorToLower == "white") {
|
2016-02-05 01:49:19 +01:00
|
|
|
return CefColorSetARGB(255, 255, 255, 255);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-02-05 01:49:19 +01:00
|
|
|
|
|
|
|
// Use the default color.
|
2017-02-25 06:03:31 +01:00
|
|
|
return 0;
|
2016-02-05 01:49:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
} // namespace
|
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
MainContextImpl::MainContextImpl(CefRefPtr<CefCommandLine> command_line,
|
2015-01-29 21:53:53 +01:00
|
|
|
bool terminate_when_all_windows_closed)
|
2015-01-31 05:41:36 +01:00
|
|
|
: command_line_(command_line),
|
2022-03-21 22:22:07 +01:00
|
|
|
terminate_when_all_windows_closed_(terminate_when_all_windows_closed) {
|
2015-01-31 05:41:36 +01:00
|
|
|
DCHECK(command_line_.get());
|
2015-01-23 00:11:30 +01:00
|
|
|
|
|
|
|
// Set the main URL.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (command_line_->HasSwitch(switches::kUrl)) {
|
2015-01-23 20:09:34 +01:00
|
|
|
main_url_ = command_line_->GetSwitchValue(switches::kUrl);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (main_url_.empty()) {
|
2015-01-23 00:11:30 +01:00
|
|
|
main_url_ = kDefaultUrl;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-09 16:59:34 +02:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
// Whether windowless (off-screen) rendering will be used.
|
|
|
|
use_windowless_rendering_ =
|
|
|
|
command_line_->HasSwitch(switches::kOffScreenRenderingEnabled);
|
|
|
|
|
2018-09-26 13:25:58 +02:00
|
|
|
if (use_windowless_rendering_ &&
|
|
|
|
command_line_->HasSwitch(switches::kOffScreenFrameRate)) {
|
|
|
|
windowless_frame_rate_ =
|
|
|
|
atoi(command_line_->GetSwitchValue(switches::kOffScreenFrameRate)
|
|
|
|
.ToString()
|
|
|
|
.c_str());
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
// Whether transparent painting is used with windowless rendering.
|
|
|
|
const bool use_transparent_painting =
|
|
|
|
use_windowless_rendering_ &&
|
|
|
|
command_line_->HasSwitch(switches::kTransparentPaintingEnabled);
|
|
|
|
|
2018-09-26 13:25:58 +02:00
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:58:25 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
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_) {
|
2017-05-17 11:29:28 +02:00
|
|
|
LOG(ERROR)
|
|
|
|
<< "Windowless rendering is not supported by the Views framework.";
|
2016-01-19 21:09:01 +01:00
|
|
|
use_views_ = false;
|
|
|
|
}
|
|
|
|
|
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))
|
2021-02-18 02:58:25 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-04-08 22:48:56 +02:00
|
|
|
#endif // !(defined(OS_WIN) || defined(OS_LINUX))
|
2021-02-18 02:58:25 +01:00
|
|
|
|
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";
|
|
|
|
}
|
2016-10-14 17:56:41 +02:00
|
|
|
|
2017-02-25 06:03:31 +01:00
|
|
|
if (command_line_->HasSwitch(switches::kBackgroundColor)) {
|
|
|
|
// Parse the background color value.
|
|
|
|
background_color_ =
|
|
|
|
ParseColor(command_line_->GetSwitchValue(switches::kBackgroundColor));
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
if (background_color_ == 0 && !use_views_) {
|
|
|
|
// Set an explicit background color.
|
2017-02-25 06:03:31 +01:00
|
|
|
background_color_ = CefColorSetARGB(255, 255, 255, 255);
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
// |browser_background_color_| should remain 0 to enable transparent painting.
|
|
|
|
if (!use_transparent_painting) {
|
|
|
|
browser_background_color_ = background_color_;
|
|
|
|
}
|
2015-01-29 21:53:53 +01:00
|
|
|
}
|
2015-01-27 01:03:25 +01:00
|
|
|
|
2015-01-29 21:53:53 +01:00
|
|
|
MainContextImpl::~MainContextImpl() {
|
|
|
|
// The context must either not have been initialized, or it must have also
|
|
|
|
// been shut down.
|
|
|
|
DCHECK(!initialized_ || shutdown_);
|
2015-01-23 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string MainContextImpl::GetConsoleLogPath() {
|
|
|
|
return GetAppWorkingDirectory() + "console.log";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MainContextImpl::GetMainURL() {
|
|
|
|
return main_url_;
|
|
|
|
}
|
|
|
|
|
2015-04-09 16:59:34 +02:00
|
|
|
cef_color_t MainContextImpl::GetBackgroundColor() {
|
|
|
|
return background_color_;
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:15:51 +02:00
|
|
|
bool MainContextImpl::UseChromeRuntime() {
|
|
|
|
return use_chrome_runtime_;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
bool MainContextImpl::UseViews() {
|
|
|
|
return use_views_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainContextImpl::UseWindowlessRendering() {
|
|
|
|
return use_windowless_rendering_;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:17:28 +01:00
|
|
|
bool MainContextImpl::TouchEventsEnabled() {
|
|
|
|
return command_line_->GetSwitchValue("touch-events") == "enabled";
|
|
|
|
}
|
|
|
|
|
2022-04-11 22:54:33 +02:00
|
|
|
bool MainContextImpl::UseDefaultPopup() {
|
|
|
|
return !use_windowless_rendering_ &&
|
|
|
|
command_line_->HasSwitch(switches::kUseDefaultPopup);
|
|
|
|
}
|
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
void MainContextImpl::PopulateSettings(CefSettings* settings) {
|
2021-04-09 20:34:45 +02:00
|
|
|
client::ClientAppBrowser::PopulateSettings(command_line_, *settings);
|
2016-05-04 20:00:03 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (use_chrome_runtime_) {
|
2021-02-18 02:58:25 +01:00
|
|
|
settings->chrome_runtime = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-02-18 02:58:25 +01:00
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
CefString(&settings->cache_path) =
|
2015-01-23 20:09:34 +01:00
|
|
|
command_line_->GetSwitchValue(switches::kCachePath);
|
2015-01-23 00:11:30 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (use_windowless_rendering_) {
|
2015-01-23 00:11:30 +01:00
|
|
|
settings->windowless_rendering_enabled = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-09 16:59:34 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser_background_color_ != 0) {
|
2017-04-20 21:28:17 +02:00
|
|
|
settings->background_color = browser_background_color_;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-28 19:12:06 +02: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");
|
|
|
|
}
|
2015-01-23 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainContextImpl::PopulateBrowserSettings(CefBrowserSettings* settings) {
|
2018-09-26 13:25:58 +02:00
|
|
|
settings->windowless_frame_rate = windowless_frame_rate_;
|
2017-04-20 21:28:17 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser_background_color_ != 0) {
|
2017-04-20 21:28:17 +02:00
|
|
|
settings->background_color = browser_background_color_;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-03-21 22:22:07 +01:00
|
|
|
|
|
|
|
if (use_chrome_runtime_ &&
|
|
|
|
command_line_->HasSwitch(switches::kHideChromeStatusBubble)) {
|
|
|
|
settings->chrome_status_bubble = STATE_DISABLED;
|
|
|
|
}
|
2015-01-23 00:11:30 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 13:25:58 +02:00
|
|
|
void MainContextImpl::PopulateOsrSettings(OsrRendererSettings* settings) {
|
2015-04-09 16:59:34 +02:00
|
|
|
settings->show_update_rect =
|
|
|
|
command_line_->HasSwitch(switches::kShowUpdateRect);
|
2017-04-20 21:28:17 +02:00
|
|
|
|
2018-09-26 13:25:58 +02:00
|
|
|
#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) {
|
2017-04-20 21:28:17 +02:00
|
|
|
settings->background_color = browser_background_color_;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-04-09 16:59:34 +02:00
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
RootWindowManager* MainContextImpl::GetRootWindowManager() {
|
2015-01-29 21:53:53 +01:00
|
|
|
DCHECK(InValidState());
|
2015-01-27 01:03:25 +01:00
|
|
|
return root_window_manager_.get();
|
|
|
|
}
|
|
|
|
|
2015-01-29 21:53:53 +01:00
|
|
|
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)) {
|
2015-01-29 21:53:53 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-29 21:53:53 +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;
|
|
|
|
}
|
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
} // namespace client
|