Skip to content

Commit

Permalink
chrome: Support CefSettings path config (see #3685)
Browse files Browse the repository at this point in the history
  • Loading branch information
magreenblatt committed May 2, 2024
1 parent 7328b9e commit 2938135
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 15 deletions.
19 changes: 19 additions & 0 deletions libcef/browser/chrome/chrome_content_browser_client_cef.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,32 @@ void ChromeContentBrowserClientCef::AppendExtraCommandLineSwitches(
// Propagate the following switches to all command lines (along with any
// associated values) if present in the browser command line.
static const char* const kSwitchNames[] = {
#if BUILDFLAG(IS_MAC)
switches::kFrameworkDirPath,
switches::kMainBundlePath,
#endif
switches::kLocalesDirPath,
switches::kResourcesDirPath,
switches::kUserAgentProductAndVersion,
};
command_line->CopySwitchesFrom(*browser_cmd, kSwitchNames);
}

const std::string& process_type =
command_line->GetSwitchValueASCII(switches::kProcessType);

#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
if (process_type == switches::kZygoteProcess &&
browser_cmd->HasSwitch(switches::kBrowserSubprocessPath)) {
// Force use of the sub-process executable path for the zygote process.
const base::FilePath& subprocess_path =
browser_cmd->GetSwitchValuePath(switches::kBrowserSubprocessPath);
if (!subprocess_path.empty()) {
command_line->SetProgram(subprocess_path);
}
}
#endif

if (process_type == switches::kRendererProcess) {
// Propagate the following switches to the renderer command line (along with
// any associated values) if present in the browser command line.
Expand Down
72 changes: 72 additions & 0 deletions libcef/common/chrome/chrome_main_delegate_cef.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/path_service.h"
#include "base/threading/threading_features.h"
#include "cef/libcef/browser/chrome/chrome_browser_context.h"
#include "cef/libcef/browser/chrome/chrome_content_browser_client_cef.h"
Expand All @@ -20,12 +21,14 @@
#include "cef/libcef/renderer/chrome/chrome_content_renderer_client_cef.h"
#include "chrome/browser/metrics/chrome_feature_list_creator.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/embedder_support/switches.h"
#include "content/public/common/content_switches.h"
#include "sandbox/policy/switches.h"
#include "third_party/blink/public/common/switches.h"
#include "ui/base/ui_base_paths.h"
#include "ui/base/ui_base_switches.h"

#if BUILDFLAG(IS_MAC)
Expand Down Expand Up @@ -86,6 +89,39 @@ std::optional<int> ChromeMainDelegateCef::BasicStartupComplete() {

bool no_sandbox = settings_->no_sandbox ? true : false;

if (settings_->browser_subprocess_path.length > 0) {
base::FilePath file_path =
base::FilePath(CefString(&settings_->browser_subprocess_path));
if (!file_path.empty()) {
command_line->AppendSwitchPath(switches::kBrowserSubprocessPath,
file_path);

#if BUILDFLAG(IS_WIN)
// The sandbox is not supported when using a separate subprocess
// executable on Windows.
no_sandbox = true;
#endif
}
}

#if BUILDFLAG(IS_MAC)
if (settings_->framework_dir_path.length > 0) {
base::FilePath file_path =
base::FilePath(CefString(&settings_->framework_dir_path));
if (!file_path.empty()) {
command_line->AppendSwitchPath(switches::kFrameworkDirPath, file_path);
}
}

if (settings_->main_bundle_path.length > 0) {
base::FilePath file_path =
base::FilePath(CefString(&settings_->main_bundle_path));
if (!file_path.empty()) {
command_line->AppendSwitchPath(switches::kMainBundlePath, file_path);
}
}
#endif

if (no_sandbox) {
command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox);
}
Expand Down Expand Up @@ -113,6 +149,22 @@ std::optional<int> ChromeMainDelegateCef::BasicStartupComplete() {
CefString(&settings_->javascript_flags).ToString());
}

if (settings_->resources_dir_path.length > 0) {
base::FilePath file_path =
base::FilePath(CefString(&settings_->resources_dir_path));
if (!file_path.empty()) {
command_line->AppendSwitchPath(switches::kResourcesDirPath, file_path);
}
}

if (settings_->locales_dir_path.length > 0) {
base::FilePath file_path =
base::FilePath(CefString(&settings_->locales_dir_path));
if (!file_path.empty()) {
command_line->AppendSwitchPath(switches::kLocalesDirPath, file_path);
}
}

if (settings_->remote_debugging_port >= 1024 &&
settings_->remote_debugging_port <= 65535) {
command_line->AppendSwitchASCII(
Expand Down Expand Up @@ -192,6 +244,26 @@ void ChromeMainDelegateCef::PreSandboxStartup() {
// Initialize crash reporting state for this process/module.
// chrome::DIR_CRASH_DUMPS must be configured before calling this function.
crash_reporting::PreSandboxStartup(*command_line, process_type);

base::FilePath resources_dir;
if (command_line->HasSwitch(switches::kResourcesDirPath)) {
resources_dir =
command_line->GetSwitchValuePath(switches::kResourcesDirPath);
}
if (resources_dir.empty()) {
resources_dir = resource_util::GetResourcesDir();
}
if (!resources_dir.empty()) {
base::PathService::Override(chrome::DIR_RESOURCES, resources_dir);
}

if (command_line->HasSwitch(switches::kLocalesDirPath)) {
const auto& locales_dir =
command_line->GetSwitchValuePath(switches::kLocalesDirPath);
if (!locales_dir.empty()) {
base::PathService::Override(ui::DIR_LOCALES, locales_dir);
}
}
}

std::optional<int> ChromeMainDelegateCef::PreBrowserMain() {
Expand Down
31 changes: 20 additions & 11 deletions libcef/common/resource_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "base/nix/xdg_util.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "cef/libcef/common/util_mac.h"
#endif

#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
#include "base/files/file_util.h"
#include "base/notreached.h"
Expand All @@ -30,7 +34,6 @@

#if BUILDFLAG(IS_MAC)
#include "base/apple/foundation_util.h"
#include "cef/libcef/common/util_mac.h"
#endif

#if BUILDFLAG(IS_WIN)
Expand Down Expand Up @@ -171,10 +174,6 @@ bool GetDefaultDownloadSafeDirectory(base::FilePath* result) {

#if BUILDFLAG(IS_MAC)

base::FilePath GetResourcesDir() {
return util_mac::GetFrameworkResourcesDirectory();
}

// Use a "~/Library/Logs/<app name>_debug.log" file where <app name> is the name
// of the running executable.
base::FilePath GetDefaultLogFilePath() {
Expand All @@ -186,12 +185,6 @@ base::FilePath GetDefaultLogFilePath() {

#else // !BUILDFLAG(IS_MAC)

base::FilePath GetResourcesDir() {
base::FilePath pak_dir;
base::PathService::Get(base::DIR_ASSETS, &pak_dir);
return pak_dir;
}

// Use a "debug.log" file in the running executable's directory.
base::FilePath GetDefaultLogFilePath() {
base::FilePath log_path;
Expand Down Expand Up @@ -224,6 +217,22 @@ bool IsScaleFactorSupported(ui::ResourceScaleFactor scale_factor) {

#endif // BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)

#if BUILDFLAG(IS_MAC)

base::FilePath GetResourcesDir() {
return util_mac::GetFrameworkResourcesDirectory();
}

#else // !BUILDFLAG(IS_MAC)

base::FilePath GetResourcesDir() {
base::FilePath pak_dir;
base::PathService::Get(base::DIR_ASSETS, &pak_dir);
return pak_dir;
}

#endif // !BUILDFLAG(IS_MAC)

void OverrideUserDataDir(CefSettings* settings,
const base::CommandLine* command_line) {
const base::FilePath& user_data_path =
Expand Down
8 changes: 4 additions & 4 deletions libcef/common/resource_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class FilePath;
namespace resource_util {

#if BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)
// Returns the directory that contains resource files (*.bin, *.dat, *.pak,
// etc).
base::FilePath GetResourcesDir();

// Returns the default path for the debug.log file.
base::FilePath GetDefaultLogFilePath();

Expand All @@ -37,6 +33,10 @@ void OverrideDefaultDownloadDir();
bool IsScaleFactorSupported(ui::ResourceScaleFactor scale_factor);
#endif // BUILDFLAG(ENABLE_ALLOY_BOOTSTRAP)

// Returns the directory that contains resource files (*.bin, *.dat, *.pak,
// etc).
base::FilePath GetResourcesDir();

void OverrideUserDataDir(CefSettings* settings,
const base::CommandLine* command_line);

Expand Down

0 comments on commit 2938135

Please sign in to comment.