// 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 "CEF/HumanAppContextImpl.h" #include #include "include/cef_parser.h" #include "CEF/HumanAppBrowser.h" #include "CEF/HumanAppSwitches.h" #include "CEF/RootWindoManager.h" // The default URL to load in a browser window. const char kDefaultUrl[] = "http://www.google.com"; // Returns the ARGB value for |color|. cef_color_t ParseColor(const std::string& color) { std::string colorToLower; colorToLower.resize(color.size()); std::transform(color.begin(), color.end(), colorToLower.begin(), ::tolower); if (colorToLower == "black") return CefColorSetARGB(255, 0, 0, 0); else if (colorToLower == "blue") return CefColorSetARGB(255, 0, 0, 255); else if (colorToLower == "green") return CefColorSetARGB(255, 0, 255, 0); else if (colorToLower == "red") return CefColorSetARGB(255, 255, 0, 0); else if (colorToLower == "white") return CefColorSetARGB(255, 255, 255, 255); // Use the default color. return 0; } HumanAppContextImpl::HumanAppContextImpl(CefRefPtr 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. if (command_line_->HasSwitch(kUrl)) main_url_ = command_line_->GetSwitchValue(kUrl); if (main_url_.empty()) main_url_ = kDefaultUrl; // Whether windowless (off-screen) rendering will be used. use_windowless_rendering_ = command_line_->HasSwitch(kOffScreenRenderingEnabled); if (use_windowless_rendering_ && command_line_->HasSwitch(kOffScreenFrameRate)) { windowless_frame_rate_ = atoi(command_line_->GetSwitchValue(kOffScreenFrameRate) .ToString() .c_str()); } // Whether transparent painting is used with windowless rendering. const bool use_transparent_painting = use_windowless_rendering_ && command_line_->HasSwitch(kTransparentPaintingEnabled); #if defined(OS_WIN) // Shared texture is only supported on Windows. shared_texture_enabled_ = use_windowless_rendering_ && command_line_->HasSwitch(kSharedTextureEnabled); #endif external_begin_frame_enabled_ = use_windowless_rendering_ && command_line_->HasSwitch(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(kEnableChromeRuntime); if (use_windowless_rendering_ && use_chrome_runtime_) { LOG(ERROR) << "Windowless rendering is not supported with the Chrome runtime."; use_chrome_runtime_ = false; } // Whether the Views framework will be used. use_views_ = command_line_->HasSwitch(kUseViews); if (use_windowless_rendering_ && use_views_) { LOG(ERROR) << "Windowless rendering is not supported by the Views framework."; use_views_ = false; } #if defined(OS_WIN) || defined(OS_LINUX) if (use_chrome_runtime_ && !use_views_ && !command_line->HasSwitch(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; } #endif // !(defined(OS_WIN) || defined(OS_LINUX)) if (use_views_ && command_line->HasSwitch(kHideFrame) && !command_line_->HasSwitch(kUrl)) { // Use the draggable regions test as the default URL for frameless windows. main_url_ = "http://tests/draggable"; } if (command_line_->HasSwitch(kBackgroundColor)) { // Parse the background color value. background_color_ = ParseColor(command_line_->GetSwitchValue(kBackgroundColor)); } if (background_color_ == 0 && !use_views_) { // Set an explicit background color. background_color_ = CefColorSetARGB(0, 0, 0, 0); } // |browser_background_color_| should remain 0 to enable transparent painting. if (!use_transparent_painting) { browser_background_color_ = background_color_; } } HumanAppContextImpl::~HumanAppContextImpl() { // The context must either not have been initialized, or it must have also // been shut down. DCHECK(!initialized_ || shutdown_); } std::string HumanAppContextImpl::GetConsoleLogPath() { return GetAppWorkingDirectory() + "console.log"; } std::string HumanAppContextImpl::GetMainURL() { return main_url_; } cef_color_t HumanAppContextImpl::GetBackgroundColor() { return background_color_; } bool HumanAppContextImpl::UseChromeRuntime() { return use_chrome_runtime_; } bool HumanAppContextImpl::UseViews() { return use_views_; } bool HumanAppContextImpl::UseWindowlessRendering() { return use_windowless_rendering_; } bool HumanAppContextImpl::TouchEventsEnabled() { return command_line_->GetSwitchValue("touch-events") == "enabled"; } bool HumanAppContextImpl::UseDefaultPopup() { return !use_windowless_rendering_ && command_line_->HasSwitch(kUseDefaultPopup); } void HumanAppContextImpl::PopulateSettings(CefSettings* settings) { HumanAppBrowser::PopulateSettings(command_line_, *settings); if (use_chrome_runtime_) settings->chrome_runtime = true; CefString(&settings->cache_path) = command_line_->GetSwitchValue(kCachePath); if (use_windowless_rendering_) settings->windowless_rendering_enabled = true; if (browser_background_color_ != 0) settings->background_color = browser_background_color_; 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 HumanAppContextImpl::PopulateBrowserSettings(CefBrowserSettings* settings) { settings->windowless_frame_rate = windowless_frame_rate_; if (browser_background_color_ != 0) settings->background_color = browser_background_color_; if (use_chrome_runtime_ && command_line_->HasSwitch(kHideChromeStatusBubble)) { settings->chrome_status_bubble = STATE_DISABLED; } } void HumanAppContextImpl::PopulateOsrSettings(OsrRendererSettings* settings) { settings->show_update_rect = command_line_->HasSwitch(kShowUpdateRect); #if defined(OS_WIN) settings->render_type = shared_texture_enabled_ ? OsrRendererSettings::OsrRendererType::D3D11 : OsrRendererSettings::OsrRendererType::OpenGL; #endif settings->external_begin_frame_enabled = external_begin_frame_enabled_; settings->begin_frame_rate = windowless_frame_rate_; if (browser_background_color_ != 0) settings->background_color = browser_background_color_; } RootWindowManager* HumanAppContextImpl::GetRootWindowManager() { DCHECK(InValidState()); return root_window_manager_.get(); } bool HumanAppContextImpl::Initialize(const CefMainArgs& args, const CefSettings& settings, CefRefPtr application, void* windows_sandbox_info) { DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(!initialized_); DCHECK(!shutdown_); if (!CefInitialize(args, settings, application, windows_sandbox_info)) return false; // 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 HumanAppContextImpl::Shutdown() { DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(initialized_); DCHECK(!shutdown_); root_window_manager_.reset(); CefShutdown(); shutdown_ = true; }