cef/tests/cefclient/browser/main_context_impl.cc

149 lines
4.4 KiB
C++

// 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 "cefclient/browser/main_context_impl.h"
#include "include/cef_parser.h"
#include "cefclient/common/client_switches.h"
namespace client {
namespace {
// 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 0U;
}
} // 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),
initialized_(false),
shutdown_(false),
background_color_(CefColorSetARGB(255, 255, 255, 255)) {
DCHECK(command_line_.get());
// Set the main URL.
if (command_line_->HasSwitch(switches::kUrl))
main_url_ = command_line_->GetSwitchValue(switches::kUrl);
if (main_url_.empty())
main_url_ = kDefaultUrl;
if (command_line_->HasSwitch(switches::kBackgroundColor)) {
// Parse the background color value.
background_color_ =
ParseColor(command_line_->GetSwitchValue(switches::kBackgroundColor));
}
}
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_;
}
void MainContextImpl::PopulateSettings(CefSettings* settings) {
#if defined(OS_WIN)
settings->multi_threaded_message_loop =
command_line_->HasSwitch(switches::kMultiThreadedMessageLoop);
#endif
CefString(&settings->cache_path) =
command_line_->GetSwitchValue(switches::kCachePath);
if (command_line_->HasSwitch(switches::kOffScreenRenderingEnabled))
settings->windowless_rendering_enabled = true;
settings->background_color = background_color_;
}
void MainContextImpl::PopulateBrowserSettings(CefBrowserSettings* settings) {
if (command_line_->HasSwitch(switches::kOffScreenFrameRate)) {
settings->windowless_frame_rate = atoi(command_line_->
GetSwitchValue(switches::kOffScreenFrameRate).ToString().c_str());
}
}
void MainContextImpl::PopulateOsrSettings(OsrRenderer::Settings* settings) {
settings->transparent =
command_line_->HasSwitch(switches::kTransparentPaintingEnabled);
settings->show_update_rect =
command_line_->HasSwitch(switches::kShowUpdateRect);
settings->background_color = background_color_;
}
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_);
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 MainContextImpl::Shutdown() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(initialized_);
DCHECK(!shutdown_);
root_window_manager_.reset();
CefShutdown();
shutdown_ = true;
}
} // namespace client