2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/context.h"
|
2020-06-24 22:26:12 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.h"
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2018-07-12 19:57:06 +02:00
|
|
|
#include "base/run_loop.h"
|
2020-08-29 00:39:23 +02:00
|
|
|
#include "base/task/current_thread.h"
|
2019-03-24 19:41:42 +01:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/browser_info_manager.h"
|
|
|
|
#include "cef/libcef/browser/request_context_impl.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/browser/trace_subscriber.h"
|
|
|
|
#include "cef/libcef/common/cef_switches.h"
|
2017-07-27 01:19:27 +02:00
|
|
|
#include "components/network_session_configurator/common/network_switches.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "ui/base/ui_base_switches.h"
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2022-10-21 00:23:32 +02:00
|
|
|
#include "base/debug/alias.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/internal/cef_win.h"
|
2019-07-16 19:59:21 +02:00
|
|
|
#include "chrome/chrome_elf/chrome_elf_main.h"
|
2018-09-04 11:43:21 +02:00
|
|
|
#include "chrome/install_static/initialize_from_primary_module.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#endif
|
|
|
|
|
2013-09-03 20:06:25 +02:00
|
|
|
namespace {
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-01-15 14:36:24 +01:00
|
|
|
CefContext* g_context = nullptr;
|
2013-09-03 20:06:25 +02:00
|
|
|
|
2024-03-12 20:47:10 +01:00
|
|
|
// Invalid value before CefInitialize is called.
|
|
|
|
int g_exit_code = -1;
|
|
|
|
|
2017-05-09 21:52:52 +02:00
|
|
|
#if DCHECK_IS_ON()
|
|
|
|
// When the process terminates check if CefShutdown() has been called.
|
|
|
|
class CefShutdownChecker {
|
2013-09-03 20:06:25 +02:00
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
~CefShutdownChecker() { DCHECK(!g_context) << "CefShutdown was not called"; }
|
2017-05-09 21:52:52 +02:00
|
|
|
} g_shutdown_checker;
|
|
|
|
#endif // DCHECK_IS_ON()
|
2013-09-03 20:06:25 +02:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2017-04-27 19:02:21 +02:00
|
|
|
|
2018-09-04 11:43:21 +02:00
|
|
|
// Transfer state from chrome_elf.dll to the libcef.dll. Accessed when
|
|
|
|
// loading chrome://system.
|
|
|
|
void InitInstallDetails() {
|
|
|
|
static bool initialized = false;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (initialized) {
|
2018-09-04 11:43:21 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-09-04 11:43:21 +02:00
|
|
|
initialized = true;
|
|
|
|
install_static::InitializeFromPrimaryModule();
|
|
|
|
}
|
|
|
|
|
2017-04-27 19:02:21 +02:00
|
|
|
// Signal chrome_elf to initialize crash reporting, rather than doing it in
|
|
|
|
// DllMain. See https://crbug.com/656800 for details.
|
|
|
|
void InitCrashReporter() {
|
|
|
|
static bool initialized = false;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (initialized) {
|
2017-04-27 19:02:21 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-04-27 19:02:21 +02:00
|
|
|
initialized = true;
|
|
|
|
SignalInitializeCrashReporting();
|
|
|
|
}
|
2022-10-20 19:47:25 +02:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2016-01-28 17:10:36 +01:00
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
bool GetColor(const cef_color_t cef_in, bool is_windowless, SkColor* sk_out) {
|
|
|
|
// Windowed browser colors must be fully opaque.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!is_windowless && CefColorGetA(cef_in) != SK_AlphaOPAQUE) {
|
2017-04-20 21:28:17 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-04-20 21:28:17 +02:00
|
|
|
|
|
|
|
// Windowless browser colors may be fully transparent.
|
|
|
|
if (is_windowless && CefColorGetA(cef_in) == SK_AlphaTRANSPARENT) {
|
|
|
|
*sk_out = SK_ColorTRANSPARENT;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the alpha component.
|
|
|
|
*sk_out = SkColorSetRGB(CefColorGetR(cef_in), CefColorGetG(cef_in),
|
|
|
|
CefColorGetB(cef_in));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-23 23:45:04 +02:00
|
|
|
// Convert |path_str| to a normalized FilePath.
|
|
|
|
base::FilePath NormalizePath(const cef_string_t& path_str,
|
|
|
|
const char* name,
|
|
|
|
bool* has_error = nullptr) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (has_error) {
|
2020-04-23 23:45:04 +02:00
|
|
|
*has_error = false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-04-23 23:45:04 +02:00
|
|
|
|
|
|
|
base::FilePath path = base::FilePath(CefString(&path_str));
|
|
|
|
if (path.EndsWithSeparator()) {
|
|
|
|
// Remove the trailing separator because it will interfere with future
|
|
|
|
// equality checks.
|
|
|
|
path = path.StripTrailingSeparators();
|
|
|
|
}
|
|
|
|
|
2024-06-17 23:52:26 +02:00
|
|
|
if (!path.empty()) {
|
|
|
|
if (!path.IsAbsolute()) {
|
|
|
|
LOG(ERROR) << "The " << name << " directory (" << path.value()
|
|
|
|
<< ") is not an absolute path. Defaulting to empty.";
|
|
|
|
if (has_error) {
|
|
|
|
*has_error = true;
|
|
|
|
}
|
|
|
|
return base::FilePath();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2024-06-17 23:52:26 +02:00
|
|
|
|
|
|
|
#if BUILDFLAG(IS_POSIX)
|
|
|
|
// Always resolve symlinks to absolute paths. This avoids issues with
|
|
|
|
// mismatched paths when mixing Chromium and OS filesystem functions.
|
|
|
|
// See https://crbug.com/40229712.
|
|
|
|
base::ScopedAllowBlockingForTesting allow_blocking;
|
|
|
|
const base::FilePath& resolved_path = base::MakeAbsoluteFilePath(path);
|
|
|
|
if (!resolved_path.empty()) {
|
|
|
|
return resolved_path;
|
|
|
|
} else if (errno != 0 && errno != ENOENT) {
|
|
|
|
PLOG(ERROR) << "realpath(" << path.value() << ") failed";
|
|
|
|
}
|
|
|
|
#endif // BUILDFLAG(IS_POSIX)
|
2020-04-23 23:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPath(cef_string_t& path_str, const base::FilePath& path) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-04-23 23:45:04 +02:00
|
|
|
CefString(&path_str).FromWString(path.value());
|
|
|
|
#else
|
|
|
|
CefString(&path_str).FromString(path.value());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert |path_str| to a normalized FilePath and update the |path_str| value.
|
|
|
|
base::FilePath NormalizePathAndSet(cef_string_t& path_str, const char* name) {
|
|
|
|
const base::FilePath& path = NormalizePath(path_str, name);
|
|
|
|
SetPath(path_str, path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that |cache_path| is valid and create it if necessary.
|
|
|
|
bool ValidateCachePath(const base::FilePath& cache_path,
|
|
|
|
const base::FilePath& root_cache_path) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cache_path.empty()) {
|
2020-04-23 23:45:04 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-04-23 23:45:04 +02:00
|
|
|
|
|
|
|
if (!root_cache_path.empty() && root_cache_path != cache_path &&
|
|
|
|
!root_cache_path.IsParent(cache_path)) {
|
|
|
|
LOG(ERROR) << "The cache_path directory (" << cache_path.value()
|
|
|
|
<< ") is not a child of the root_cache_path directory ("
|
|
|
|
<< root_cache_path.value() << ")";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:50:53 +01:00
|
|
|
base::ScopedAllowBlockingForTesting allow_blocking;
|
2020-04-23 23:45:04 +02:00
|
|
|
if (!base::DirectoryExists(cache_path) &&
|
|
|
|
!base::CreateDirectory(cache_path)) {
|
|
|
|
LOG(ERROR) << "The cache_path directory (" << cache_path.value()
|
|
|
|
<< ") could not be created.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like NormalizePathAndSet but with additional checks specific to the
|
|
|
|
// cache_path value.
|
|
|
|
base::FilePath NormalizeCachePathAndSet(cef_string_t& path_str,
|
|
|
|
const base::FilePath& root_cache_path) {
|
|
|
|
bool has_error = false;
|
|
|
|
base::FilePath path = NormalizePath(path_str, "cache_path", &has_error);
|
|
|
|
if (has_error || !ValidateCachePath(path, root_cache_path)) {
|
|
|
|
LOG(ERROR) << "The cache_path is invalid. Defaulting to in-memory storage.";
|
|
|
|
path = base::FilePath();
|
|
|
|
}
|
|
|
|
SetPath(path_str, path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:23:32 +02:00
|
|
|
// Based on chrome/app/chrome_exe_main_win.cc.
|
|
|
|
// In 32-bit builds, the main thread starts with the default (small) stack size.
|
|
|
|
// The ARCH_CPU_32_BITS blocks here and below are in support of moving the main
|
|
|
|
// thread to a fiber with a larger stack size.
|
|
|
|
#if BUILDFLAG(IS_WIN) && defined(ARCH_CPU_32_BITS)
|
|
|
|
// The information needed to transfer control to the large-stack fiber and later
|
|
|
|
// pass the main routine's exit code back to the small-stack fiber prior to
|
|
|
|
// termination.
|
|
|
|
struct FiberState {
|
|
|
|
FiberState(wWinMainPtr wWinMain,
|
|
|
|
HINSTANCE hInstance,
|
|
|
|
LPWSTR lpCmdLine,
|
|
|
|
int nCmdShow) {
|
|
|
|
this->wWinMain = wWinMain;
|
|
|
|
this->hInstance = hInstance;
|
|
|
|
this->lpCmdLine = lpCmdLine;
|
|
|
|
this->nCmdShow = nCmdShow;
|
|
|
|
}
|
|
|
|
|
|
|
|
FiberState(mainPtr main, int argc, char** argv) {
|
|
|
|
this->main = main;
|
|
|
|
this->argc = argc;
|
|
|
|
this->argv = argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
wWinMainPtr wWinMain = nullptr;
|
|
|
|
HINSTANCE hInstance;
|
|
|
|
LPWSTR lpCmdLine;
|
|
|
|
int nCmdShow;
|
|
|
|
|
|
|
|
mainPtr main = nullptr;
|
|
|
|
int argc;
|
|
|
|
char** argv;
|
|
|
|
|
|
|
|
LPVOID original_fiber;
|
|
|
|
int fiber_result;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A PFIBER_START_ROUTINE function run on a large-stack fiber that calls the
|
|
|
|
// main routine, stores its return value, and returns control to the small-stack
|
|
|
|
// fiber. |params| must be a pointer to a FiberState struct.
|
|
|
|
void WINAPI FiberBinder(void* params) {
|
|
|
|
auto* fiber_state = static_cast<FiberState*>(params);
|
|
|
|
// Call the main routine from the fiber. Reusing the entry point minimizes
|
|
|
|
// confusion when examining call stacks in crash reports - seeing wWinMain on
|
|
|
|
// the stack is a handy hint that this is the main thread of the process.
|
|
|
|
if (fiber_state->main) {
|
|
|
|
fiber_state->fiber_result =
|
|
|
|
fiber_state->main(fiber_state->argc, fiber_state->argv);
|
|
|
|
} else {
|
|
|
|
fiber_state->fiber_result =
|
|
|
|
fiber_state->wWinMain(fiber_state->hInstance, nullptr,
|
|
|
|
fiber_state->lpCmdLine, fiber_state->nCmdShow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch back to the main thread to exit.
|
|
|
|
::SwitchToFiber(fiber_state->original_fiber);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RunMainWithPreferredStackSize(FiberState& fiber_state) {
|
|
|
|
enum class FiberStatus { kConvertFailed, kCreateFiberFailed, kSuccess };
|
|
|
|
FiberStatus fiber_status = FiberStatus::kSuccess;
|
|
|
|
// GetLastError result if fiber conversion failed.
|
|
|
|
DWORD fiber_error = ERROR_SUCCESS;
|
|
|
|
if (!::IsThreadAFiber()) {
|
|
|
|
// Make the main thread's stack size 4 MiB so that it has roughly the same
|
|
|
|
// effective size as the 64-bit build's 8 MiB stack.
|
|
|
|
constexpr size_t kStackSize = 4 * 1024 * 1024; // 4 MiB
|
|
|
|
// Leak the fiber on exit.
|
|
|
|
LPVOID original_fiber =
|
|
|
|
::ConvertThreadToFiberEx(nullptr, FIBER_FLAG_FLOAT_SWITCH);
|
|
|
|
if (original_fiber) {
|
|
|
|
fiber_state.original_fiber = original_fiber;
|
|
|
|
// Create a fiber with a bigger stack and switch to it. Leak the fiber on
|
|
|
|
// exit.
|
|
|
|
LPVOID big_stack_fiber = ::CreateFiberEx(
|
|
|
|
0, kStackSize, FIBER_FLAG_FLOAT_SWITCH, FiberBinder, &fiber_state);
|
|
|
|
if (big_stack_fiber) {
|
|
|
|
::SwitchToFiber(big_stack_fiber);
|
|
|
|
// The fibers must be cleaned up to avoid obscure TLS-related shutdown
|
|
|
|
// crashes.
|
|
|
|
::DeleteFiber(big_stack_fiber);
|
|
|
|
::ConvertFiberToThread();
|
|
|
|
// Control returns here after CEF has finished running on FiberMain.
|
|
|
|
return fiber_state.fiber_result;
|
|
|
|
}
|
|
|
|
fiber_status = FiberStatus::kCreateFiberFailed;
|
|
|
|
} else {
|
|
|
|
fiber_status = FiberStatus::kConvertFailed;
|
|
|
|
}
|
|
|
|
// If we reach here then creating and switching to a fiber has failed. This
|
|
|
|
// probably means we are low on memory and will soon crash. Try to report
|
|
|
|
// this error once crash reporting is initialized.
|
|
|
|
fiber_error = ::GetLastError();
|
|
|
|
base::debug::Alias(&fiber_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are already a fiber then continue normal execution.
|
|
|
|
// Intentionally crash if converting to a fiber failed.
|
|
|
|
CHECK_EQ(fiber_status, FiberStatus::kSuccess);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif // BUILDFLAG(IS_WIN) && defined(ARCH_CPU_32_BITS)
|
|
|
|
|
2013-09-03 20:06:25 +02:00
|
|
|
} // namespace
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
int CefExecuteProcess(const CefMainArgs& args,
|
2013-11-15 19:47:02 +01:00
|
|
|
CefRefPtr<CefApp> application,
|
|
|
|
void* windows_sandbox_info) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-09-04 11:43:21 +02:00
|
|
|
InitInstallDetails();
|
2017-04-27 19:02:21 +02:00
|
|
|
InitCrashReporter();
|
|
|
|
#endif
|
2016-01-28 17:10:36 +01:00
|
|
|
|
2020-06-24 22:26:12 +02:00
|
|
|
return CefMainRunner::RunAsHelperProcess(args, application,
|
|
|
|
windows_sandbox_info);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefInitialize(const CefMainArgs& args,
|
|
|
|
const CefSettings& settings,
|
2013-11-15 19:47:02 +01:00
|
|
|
CefRefPtr<CefApp> application,
|
|
|
|
void* windows_sandbox_info) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-09-04 11:43:21 +02:00
|
|
|
InitInstallDetails();
|
2017-04-27 19:02:21 +02:00
|
|
|
InitCrashReporter();
|
|
|
|
#endif
|
2016-01-28 17:10:36 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Return true if the global context already exists.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (g_context) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (settings.size != sizeof(cef_settings_t)) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "invalid CefSettings structure size";
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new global context object.
|
2013-09-03 20:06:25 +02:00
|
|
|
g_context = new CefContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Initialize the global context.
|
2024-03-12 20:47:10 +01:00
|
|
|
const bool initialized =
|
|
|
|
g_context->Initialize(args, settings, application, windows_sandbox_info);
|
|
|
|
g_exit_code = g_context->exit_code();
|
|
|
|
|
|
|
|
if (!initialized) {
|
2023-11-29 02:33:44 +01:00
|
|
|
// Initialization failed. Delete the global context object.
|
|
|
|
delete g_context;
|
|
|
|
g_context = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2024-03-12 20:47:10 +01:00
|
|
|
int CefGetExitCode() {
|
|
|
|
DCHECK_NE(g_exit_code, -1) << "invalid call to CefGetExitCode";
|
|
|
|
return g_exit_code;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefShutdown() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "context not valid";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
2013-09-03 20:06:25 +02:00
|
|
|
if (!g_context->OnInitThread()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shut down the global context. This will block until shutdown is complete.
|
2013-09-03 20:06:25 +02:00
|
|
|
g_context->Shutdown();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Delete the global context object.
|
2013-09-03 20:06:25 +02:00
|
|
|
delete g_context;
|
2020-01-15 14:36:24 +01:00
|
|
|
g_context = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDoMessageLoopWork() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "context not valid";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
2013-09-03 20:06:25 +02:00
|
|
|
if (!g_context->OnInitThread()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:57:06 +02:00
|
|
|
base::RunLoop run_loop;
|
|
|
|
run_loop.RunUntilIdle();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefRunMessageLoop() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "context not valid";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
2013-09-03 20:06:25 +02:00
|
|
|
if (!g_context->OnInitThread()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
g_context->RunMessageLoop();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefQuitMessageLoop() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "context not valid";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must always be called on the same thread as Initialize.
|
2013-09-03 20:06:25 +02:00
|
|
|
if (!g_context->OnInitThread()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "called on invalid thread";
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
g_context->QuitMessageLoop();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2022-10-21 00:23:32 +02:00
|
|
|
|
|
|
|
#if defined(ARCH_CPU_32_BITS)
|
|
|
|
int CefRunWinMainWithPreferredStackSize(wWinMainPtr wWinMain,
|
|
|
|
HINSTANCE hInstance,
|
|
|
|
LPWSTR lpCmdLine,
|
|
|
|
int nCmdShow) {
|
|
|
|
CHECK(wWinMain && hInstance);
|
|
|
|
FiberState fiber_state(wWinMain, hInstance, lpCmdLine, nCmdShow);
|
|
|
|
return RunMainWithPreferredStackSize(fiber_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefRunMainWithPreferredStackSize(mainPtr main, int argc, char* argv[]) {
|
|
|
|
CHECK(main);
|
|
|
|
FiberState fiber_state(main, argc, argv);
|
|
|
|
return RunMainWithPreferredStackSize(fiber_state);
|
|
|
|
}
|
|
|
|
#endif // defined(ARCH_CPU_32_BITS)
|
|
|
|
|
|
|
|
void CefSetOSModalLoop(bool osModalLoop) {
|
2013-08-22 19:11:19 +02:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "context not valid";
|
2013-08-22 19:11:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-04 03:34:56 +02:00
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT, base::BindOnce(CefSetOSModalLoop, osModalLoop));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::CurrentThread::Get()->set_os_modal_loop(osModalLoop);
|
2013-08-22 19:11:19 +02:00
|
|
|
}
|
|
|
|
|
2022-10-21 00:23:32 +02:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefContext
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefContext::CefContext() = default;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefContext::~CefContext() = default;
|
2013-09-03 20:06:25 +02:00
|
|
|
|
|
|
|
// static
|
|
|
|
CefContext* CefContext::Get() {
|
|
|
|
return g_context;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::Initialize(const CefMainArgs& args,
|
|
|
|
const CefSettings& settings,
|
2013-11-15 19:47:02 +01:00
|
|
|
CefRefPtr<CefApp> application,
|
|
|
|
void* windows_sandbox_info) {
|
2012-04-03 03:34:16 +02:00
|
|
|
init_thread_id_ = base::PlatformThread::CurrentId();
|
|
|
|
settings_ = settings;
|
2020-06-25 04:34:12 +02:00
|
|
|
application_ = application;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if !(BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX))
|
2012-04-03 03:34:16 +02:00
|
|
|
if (settings.multi_threaded_message_loop) {
|
2012-04-11 20:00:55 +02:00
|
|
|
NOTIMPLEMENTED() << "multi_threaded_message_loop is not supported.";
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-11 20:00:55 +02:00
|
|
|
#endif
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-10-17 20:14:44 +02:00
|
|
|
// Signal Chrome Elf that Chrome has begun to start.
|
|
|
|
SignalChromeElf();
|
|
|
|
#endif
|
|
|
|
|
2019-03-24 19:41:42 +01:00
|
|
|
const base::FilePath& root_cache_path =
|
2020-04-23 23:45:04 +02:00
|
|
|
NormalizePathAndSet(settings_.root_cache_path, "root_cache_path");
|
|
|
|
const base::FilePath& cache_path =
|
|
|
|
NormalizeCachePathAndSet(settings_.cache_path, root_cache_path);
|
2019-03-24 19:41:42 +01:00
|
|
|
if (root_cache_path.empty() && !cache_path.empty()) {
|
2020-04-23 23:45:04 +02:00
|
|
|
CefString(&settings_.root_cache_path) = cache_path.value();
|
2019-03-24 19:41:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 23:45:04 +02:00
|
|
|
// All other paths that need to be normalized.
|
|
|
|
NormalizePathAndSet(settings_.browser_subprocess_path,
|
|
|
|
"browser_subprocess_path");
|
|
|
|
NormalizePathAndSet(settings_.framework_dir_path, "framework_dir_path");
|
|
|
|
NormalizePathAndSet(settings_.main_bundle_path, "main_bundle_path");
|
|
|
|
NormalizePathAndSet(settings_.resources_dir_path, "resources_dir_path");
|
|
|
|
NormalizePathAndSet(settings_.locales_dir_path, "locales_dir_path");
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
browser_info_manager_ = std::make_unique<CefBrowserInfoManager>();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
main_runner_ = std::make_unique<CefMainRunner>(
|
|
|
|
settings_.multi_threaded_message_loop, settings_.external_message_pump);
|
2024-03-12 20:47:10 +01:00
|
|
|
|
|
|
|
const bool initialized = main_runner_->Initialize(
|
|
|
|
&settings_, application, args, windows_sandbox_info, &initialized_,
|
|
|
|
base::BindOnce(&CefContext::OnContextInitialized,
|
|
|
|
base::Unretained(this)));
|
|
|
|
exit_code_ = main_runner_->exit_code();
|
|
|
|
|
|
|
|
if (!initialized) {
|
2023-11-29 02:33:44 +01:00
|
|
|
shutting_down_ = true;
|
|
|
|
FinalizeShutdown();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
void CefContext::RunMessageLoop() {
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(OnInitThread());
|
|
|
|
|
|
|
|
// Blocks until QuitMessageLoop() is called.
|
|
|
|
main_runner_->RunMessageLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::QuitMessageLoop() {
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(OnInitThread());
|
|
|
|
|
|
|
|
main_runner_->QuitMessageLoop();
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CefContext::Shutdown() {
|
|
|
|
// Must always be called on the same thread as Initialize.
|
|
|
|
DCHECK(OnInitThread());
|
|
|
|
|
|
|
|
shutting_down_ = true;
|
|
|
|
|
2020-06-24 22:26:12 +02:00
|
|
|
main_runner_->Shutdown(
|
|
|
|
base::BindOnce(&CefContext::ShutdownOnUIThread, base::Unretained(this)),
|
|
|
|
base::BindOnce(&CefContext::FinalizeShutdown, base::Unretained(this)));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::OnInitThread() {
|
|
|
|
return (base::PlatformThread::CurrentId() == init_thread_id_);
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
SkColor CefContext::GetBackgroundColor(
|
|
|
|
const CefBrowserSettings* browser_settings,
|
|
|
|
cef_state_t windowless_state) const {
|
2017-05-17 11:29:28 +02:00
|
|
|
bool is_windowless = windowless_state == STATE_ENABLED
|
|
|
|
? true
|
|
|
|
: (windowless_state == STATE_DISABLED
|
|
|
|
? false
|
|
|
|
: !!settings_.windowless_rendering_enabled);
|
2017-04-20 21:28:17 +02:00
|
|
|
|
|
|
|
// Default to opaque white if no acceptable color values are found.
|
|
|
|
SkColor sk_color = SK_ColorWHITE;
|
|
|
|
|
|
|
|
if (!browser_settings ||
|
|
|
|
!GetColor(browser_settings->background_color, is_windowless, &sk_color)) {
|
|
|
|
GetColor(settings_.background_color, is_windowless, &sk_color);
|
|
|
|
}
|
|
|
|
return sk_color;
|
|
|
|
}
|
|
|
|
|
2012-10-18 00:45:49 +02:00
|
|
|
CefTraceSubscriber* CefContext::GetTraceSubscriber() {
|
|
|
|
CEF_REQUIRE_UIT();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (shutting_down_) {
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (!trace_subscriber_.get()) {
|
2024-01-20 23:48:57 +01:00
|
|
|
trace_subscriber_ = std::make_unique<CefTraceSubscriber>();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-10-18 00:45:49 +02:00
|
|
|
return trace_subscriber_.get();
|
|
|
|
}
|
|
|
|
|
2020-04-23 23:45:04 +02:00
|
|
|
void CefContext::PopulateGlobalRequestContextSettings(
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRequestContextSettings* settings) {
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
2020-04-23 23:45:04 +02:00
|
|
|
|
|
|
|
// This value was already normalized in Initialize.
|
2015-03-02 21:25:14 +01:00
|
|
|
CefString(&settings->cache_path) = CefString(&settings_.cache_path);
|
2020-04-23 23:45:04 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
settings->persist_session_cookies =
|
|
|
|
settings_.persist_session_cookies ||
|
|
|
|
command_line->HasSwitch(switches::kPersistSessionCookies);
|
2021-04-09 20:34:45 +02:00
|
|
|
|
|
|
|
CefString(&settings->cookieable_schemes_list) =
|
|
|
|
CefString(&settings_.cookieable_schemes_list);
|
|
|
|
settings->cookieable_schemes_exclude_defaults =
|
|
|
|
settings_.cookieable_schemes_exclude_defaults;
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 23:45:04 +02:00
|
|
|
void CefContext::NormalizeRequestContextSettings(
|
|
|
|
CefRequestContextSettings* settings) {
|
|
|
|
// The |root_cache_path| value was already normalized in Initialize.
|
|
|
|
const base::FilePath& root_cache_path = CefString(&settings_.root_cache_path);
|
|
|
|
NormalizeCachePathAndSet(settings->cache_path, root_cache_path);
|
2019-03-24 19:41:42 +01:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
void CefContext::AddObserver(Observer* observer) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
observers_.AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::RemoveObserver(Observer* observer) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
observers_.RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefContext::HasObserver(Observer* observer) const {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
return observers_.HasObserver(observer);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
void CefContext::OnContextInitialized() {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
if (application_) {
|
2021-04-15 01:28:22 +02:00
|
|
|
// Notify the handler after the global browser context has initialized.
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
CefRequestContext::GetGlobalContext();
|
|
|
|
auto impl = static_cast<CefRequestContextImpl*>(request_context.get());
|
|
|
|
impl->ExecuteWhenBrowserContextInitialized(base::BindOnce(
|
|
|
|
[](CefRefPtr<CefApp> app) {
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler =
|
|
|
|
app->GetBrowserProcessHandler();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handler) {
|
2021-04-15 01:28:22 +02:00
|
|
|
handler->OnContextInitialized();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-15 01:28:22 +02:00
|
|
|
},
|
|
|
|
application_));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 22:26:12 +02:00
|
|
|
void CefContext::ShutdownOnUIThread() {
|
2012-04-03 03:34:16 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
browser_info_manager_->DestroyAllBrowsers();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
for (auto& observer : observers_) {
|
2019-05-31 11:50:12 +02:00
|
|
|
observer.OnContextDestroyed();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (trace_subscriber_.get()) {
|
2020-01-15 14:36:24 +01:00
|
|
|
trace_subscriber_.reset(nullptr);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefContext::FinalizeShutdown() {
|
2020-01-15 14:36:24 +01:00
|
|
|
browser_info_manager_.reset(nullptr);
|
2020-06-25 04:34:12 +02:00
|
|
|
application_ = nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|