From c03a6f43861ebd7035d841cd7ae2bfcd1df12976 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Tue, 27 Apr 2021 12:39:09 -0400 Subject: [PATCH] chrome: Support configuration of user agent and locale (see issue #2969) This change adds support for CefSettings and command-line configuration of user_agent, user_agent_product (formerly product_version) and locale. --- include/cef_api_hash.h | 8 ++++---- include/internal/cef_types.h | 4 ++-- include/internal/cef_types_wrappers.h | 6 +++--- .../alloy/alloy_content_browser_client.cc | 2 +- .../chrome_content_browser_client_cef.cc | 10 ++++++++++ libcef/common/alloy/alloy_main_delegate.cc | 7 ++++--- libcef/common/cef_switches.cc | 3 +++ libcef/common/cef_switches.h | 1 + .../common/chrome/chrome_main_delegate_cef.cc | 18 ++++++++++++++++++ patch/patches/embedder_product_override.patch | 12 +++++++----- 10 files changed, 53 insertions(+), 18 deletions(-) diff --git a/include/cef_api_hash.h b/include/cef_api_hash.h index f1ee3c76c..dd7676577 100644 --- a/include/cef_api_hash.h +++ b/include/cef_api_hash.h @@ -42,13 +42,13 @@ // way that may cause binary incompatibility with other builds. The universal // hash value will change if any platform is affected whereas the platform hash // values will change only if that particular platform is affected. -#define CEF_API_HASH_UNIVERSAL "d128245052a84dd90cd38fed0e6be65824d37de5" +#define CEF_API_HASH_UNIVERSAL "d026196d35d8894a836ab3a3d033b84195cdb835" #if defined(OS_WIN) -#define CEF_API_HASH_PLATFORM "9b1a4706bf1fca26d542aa5f8b05d222f483c872" +#define CEF_API_HASH_PLATFORM "4150bd26e7bf639a9b1f3e5860af8c76eeae8570" #elif defined(OS_MAC) -#define CEF_API_HASH_PLATFORM "0038a822915e3567f2434053ebc49723fe6951d5" +#define CEF_API_HASH_PLATFORM "5cc32f88bd134410eff86b21095138b339d572f2" #elif defined(OS_LINUX) -#define CEF_API_HASH_PLATFORM "66613a535ec6a1aafce6ece8e98cd3876f79633b" +#define CEF_API_HASH_PLATFORM "b227b3fdd6142a9d8ff0f2252a71425f15960800" #endif #ifdef __cplusplus diff --git a/include/internal/cef_types.h b/include/internal/cef_types.h index c455f0064..0ec9fcb5c 100644 --- a/include/internal/cef_types.h +++ b/include/internal/cef_types.h @@ -304,9 +304,9 @@ typedef struct _cef_settings_t { // Value that will be inserted as the product portion of the default // User-Agent string. If empty the Chromium product version will be used. If // |userAgent| is specified this value will be ignored. Also configurable - // using the "product-version" command-line switch. + // using the "user-agent-product" command-line switch. /// - cef_string_t product_version; + cef_string_t user_agent_product; /// // The locale string that will be passed to WebKit. If empty the default diff --git a/include/internal/cef_types_wrappers.h b/include/internal/cef_types_wrappers.h index 0e2c195a3..c9a9ba000 100644 --- a/include/internal/cef_types_wrappers.h +++ b/include/internal/cef_types_wrappers.h @@ -548,7 +548,7 @@ struct CefSettingsTraits { cef_string_clear(&s->root_cache_path); cef_string_clear(&s->user_data_path); cef_string_clear(&s->user_agent); - cef_string_clear(&s->product_version); + cef_string_clear(&s->user_agent_product); cef_string_clear(&s->locale); cef_string_clear(&s->log_file); cef_string_clear(&s->javascript_flags); @@ -587,8 +587,8 @@ struct CefSettingsTraits { cef_string_set(src->user_agent.str, src->user_agent.length, &target->user_agent, copy); - cef_string_set(src->product_version.str, src->product_version.length, - &target->product_version, copy); + cef_string_set(src->user_agent_product.str, src->user_agent_product.length, + &target->user_agent_product, copy); cef_string_set(src->locale.str, src->locale.length, &target->locale, copy); cef_string_set(src->log_file.str, src->log_file.length, &target->log_file, diff --git a/libcef/browser/alloy/alloy_content_browser_client.cc b/libcef/browser/alloy/alloy_content_browser_client.cc index 932c74692..877909af7 100644 --- a/libcef/browser/alloy/alloy_content_browser_client.cc +++ b/libcef/browser/alloy/alloy_content_browser_client.cc @@ -695,9 +695,9 @@ void AlloyContentBrowserClient::AppendExtraCommandLineSwitches( switches::kLocalesDirPath, switches::kLogFile, switches::kLogSeverity, - switches::kProductVersion, switches::kResourcesDirPath, embedder_support::kUserAgent, + switches::kUserAgentProductAndVersion, }; command_line->CopySwitchesFrom(*browser_cmd, kSwitchNames, base::size(kSwitchNames)); diff --git a/libcef/browser/chrome/chrome_content_browser_client_cef.cc b/libcef/browser/chrome/chrome_content_browser_client_cef.cc index debe29b48..2c8d7f1d2 100644 --- a/libcef/browser/chrome/chrome_content_browser_client_cef.cc +++ b/libcef/browser/chrome/chrome_content_browser_client_cef.cc @@ -91,6 +91,16 @@ void ChromeContentBrowserClientCef::AppendExtraCommandLineSwitches( const base::CommandLine* browser_cmd = base::CommandLine::ForCurrentProcess(); + { + // 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[] = { + switches::kUserAgentProductAndVersion, + }; + command_line->CopySwitchesFrom(*browser_cmd, kSwitchNames, + base::size(kSwitchNames)); + } + const std::string& process_type = command_line->GetSwitchValueASCII(switches::kProcessType); if (process_type == switches::kRendererProcess) { diff --git a/libcef/common/alloy/alloy_main_delegate.cc b/libcef/common/alloy/alloy_main_delegate.cc index b9882fb56..ba5b08cd7 100644 --- a/libcef/common/alloy/alloy_main_delegate.cc +++ b/libcef/common/alloy/alloy_main_delegate.cc @@ -150,9 +150,10 @@ bool AlloyMainDelegate::BasicStartupComplete(int* exit_code) { if (settings_->user_agent.length > 0) { command_line->AppendSwitchASCII(embedder_support::kUserAgent, CefString(&settings_->user_agent)); - } else if (settings_->product_version.length > 0) { - command_line->AppendSwitchASCII(switches::kProductVersion, - CefString(&settings_->product_version)); + } else if (settings_->user_agent_product.length > 0) { + command_line->AppendSwitchASCII( + switches::kUserAgentProductAndVersion, + CefString(&settings_->user_agent_product)); } if (settings_->locale.length > 0) { diff --git a/libcef/common/cef_switches.cc b/libcef/common/cef_switches.cc index 0572f6583..68cb64a1d 100644 --- a/libcef/common/cef_switches.cc +++ b/libcef/common/cef_switches.cc @@ -126,6 +126,9 @@ const char kEnableChromeRuntime[] = "enable-chrome-runtime"; // using the Chrome runtime. const char kDisableChromeLoginPrompt[] = "disable-chrome-login-prompt"; +// Override the product component of the default User-Agent string. +const char kUserAgentProductAndVersion[] = "user-agent-product"; + #if defined(OS_MAC) // Path to the framework directory. const char kFrameworkDirPath[] = "framework-dir-path"; diff --git a/libcef/common/cef_switches.h b/libcef/common/cef_switches.h index a8ca0f34a..a2aa23afb 100644 --- a/libcef/common/cef_switches.h +++ b/libcef/common/cef_switches.h @@ -55,6 +55,7 @@ extern const char kDisableNewBrowserInfoTimeout[]; extern const char kDevToolsProtocolLogFile[]; extern const char kEnableChromeRuntime[]; extern const char kDisableChromeLoginPrompt[]; +extern const char kUserAgentProductAndVersion[]; #if defined(OS_MAC) extern const char kFrameworkDirPath[]; diff --git a/libcef/common/chrome/chrome_main_delegate_cef.cc b/libcef/common/chrome/chrome_main_delegate_cef.cc index 3a5f9039f..8469922e6 100644 --- a/libcef/common/chrome/chrome_main_delegate_cef.cc +++ b/libcef/common/chrome/chrome_main_delegate_cef.cc @@ -16,8 +16,10 @@ #include "base/command_line.h" #include "base/lazy_instance.h" #include "chrome/common/chrome_switches.h" +#include "components/embedder_support/switches.h" #include "content/public/common/content_switches.h" #include "sandbox/policy/switches.h" +#include "ui/base/ui_base_switches.h" #if defined(OS_MAC) #include "libcef/common/util_mac.h" @@ -79,6 +81,22 @@ bool ChromeMainDelegateCef::BasicStartupComplete(int* exit_code) { command_line->AppendSwitch(sandbox::policy::switches::kNoSandbox); } + if (settings_->user_agent.length > 0) { + command_line->AppendSwitchASCII(embedder_support::kUserAgent, + CefString(&settings_->user_agent)); + } else if (settings_->user_agent_product.length > 0) { + command_line->AppendSwitchASCII( + switches::kUserAgentProductAndVersion, + CefString(&settings_->user_agent_product)); + } + + if (settings_->locale.length > 0) { + command_line->AppendSwitchASCII(switches::kLang, + CefString(&settings_->locale)); + } else if (!command_line->HasSwitch(switches::kLang)) { + command_line->AppendSwitchASCII(switches::kLang, "en-US"); + } + if (settings_->javascript_flags.length > 0) { command_line->AppendSwitchASCII(switches::kJavaScriptFlags, CefString(&settings_->javascript_flags)); diff --git a/patch/patches/embedder_product_override.patch b/patch/patches/embedder_product_override.patch index f6b4b3844..63985a31a 100644 --- a/patch/patches/embedder_product_override.patch +++ b/patch/patches/embedder_product_override.patch @@ -1,22 +1,24 @@ diff --git components/embedder_support/user_agent_utils.cc components/embedder_support/user_agent_utils.cc -index 569518f209aeb..a4944e7060570 100644 +index 569518f209aeb..0cf7c2cc23c43 100644 --- components/embedder_support/user_agent_utils.cc +++ components/embedder_support/user_agent_utils.cc @@ -7,6 +7,7 @@ #include "base/command_line.h" #include "base/strings/strcat.h" #include "build/branding_buildflags.h" -+#include "chrome/common/chrome_switches.h" ++#include "cef/libcef/common/cef_switches.h" #include "components/embedder_support/switches.h" #include "components/version_info/version_info.h" #include "content/public/browser/web_contents.h" -@@ -20,6 +21,10 @@ +@@ -20,6 +21,12 @@ namespace embedder_support { std::string GetProduct() { + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); -+ if (command_line->HasSwitch(switches::kProductVersion)) -+ return command_line->GetSwitchValueASCII(switches::kProductVersion); ++ if (command_line->HasSwitch(switches::kUserAgentProductAndVersion)) { ++ return command_line->GetSwitchValueASCII( ++ switches::kUserAgentProductAndVersion); ++ } + return version_info::GetProductNameAndVersionForUserAgent(); }