0

Move kUseMobileUserAgent to //components/embedder_support

This is part of the process of making user_agent_utils.{cc,h} stop
depending on //content.

The flag has been moved to the main "embedder_support" target, which
does not depend on any other target and has other command-line
switches.

Bug: 389970327
Change-Id: Ib646ada998e8ea2eb23be2b9420d8049aa8f0bac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6242721
Reviewed-by: Dave Tapuska <dtapuska@chromium.org>
Commit-Queue: Raphael Kubo da Costa <kubo@igalia.com>
Reviewed-by: Bo Liu <boliu@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1425012}
This commit is contained in:
Raphael Kubo da Costa
2025-02-26 00:59:09 -08:00
committed by Chromium LUCI CQ
parent 853358ef97
commit 2d21daa8bf
15 changed files with 36 additions and 30 deletions

@@ -215,7 +215,7 @@ std::string GetUserAgent() {
// "Version/4.0" had been hardcoded in the legacy WebView. // "Version/4.0" had been hardcoded in the legacy WebView.
std::string product = "Version/4.0 " + GetProduct(); std::string product = "Version/4.0 " + GetProduct();
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseMobileUserAgent)) { embedder_support::kUseMobileUserAgent)) {
product += " Mobile"; product += " Mobile";
} }

@@ -33,6 +33,9 @@ const char kOriginTrialPublicKey[] = "origin-trial-public-key";
// Sets the Reporting API delay to under a second to allow much quicker reports. // Sets the Reporting API delay to under a second to allow much quicker reports.
const char kShortReportingDelay[] = "short-reporting-delay"; const char kShortReportingDelay[] = "short-reporting-delay";
// Set when Chromium should use a mobile user agent.
const char kUseMobileUserAgent[] = "use-mobile-user-agent";
// A string used to override the default user agent with a custom one. // A string used to override the default user agent with a custom one.
const char kUserAgent[] = "user-agent"; const char kUserAgent[] = "user-agent";

@@ -19,6 +19,7 @@ extern const char kOriginTrialDisabledFeatures[];
extern const char kOriginTrialDisabledTokens[]; extern const char kOriginTrialDisabledTokens[];
extern const char kOriginTrialPublicKey[]; extern const char kOriginTrialPublicKey[];
extern const char kShortReportingDelay[]; extern const char kShortReportingDelay[];
extern const char kUseMobileUserAgent[];
extern const char kUserAgent[]; extern const char kUserAgent[];
} // namespace embedder_support } // namespace embedder_support

@@ -26,8 +26,6 @@
#include "components/policy/core/common/policy_pref_names.h" #include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/user_agent.h" #include "content/public/common/user_agent.h"
#include "net/http/http_util.h" #include "net/http/http_util.h"
#include "third_party/blink/public/common/features.h" #include "third_party/blink/public/common/features.h"
@@ -235,9 +233,9 @@ std::string GetUserAgentInternal(
} }
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(kUseMobileUserAgent)) {
switches::kUseMobileUserAgent))
product += " Mobile"; product += " Mobile";
}
#endif #endif
// In User-Agent reduction phase 5, only apply the <unifiedPlatform> to // In User-Agent reduction phase 5, only apply the <unifiedPlatform> to
@@ -455,8 +453,8 @@ blink::UserAgentMetadata GetUserAgentMetadata(const PrefService* pref_service,
GetUserAgentBrandMajorVersionListInternal(std::nullopt); GetUserAgentBrandMajorVersionListInternal(std::nullopt);
metadata.mobile = false; metadata.mobile = false;
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
metadata.mobile = base::CommandLine::ForCurrentProcess()->HasSwitch( metadata.mobile =
switches::kUseMobileUserAgent); base::CommandLine::ForCurrentProcess()->HasSwitch(kUseMobileUserAgent);
#endif #endif
metadata.platform = GetPlatformForUAMetadata(); metadata.platform = GetPlatformForUAMetadata();

@@ -405,12 +405,12 @@ TEST_F(UserAgentUtilsTest, UserAgentStringOrdering) {
base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine(); base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
// Do it for regular devices. // Do it for regular devices.
ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_FALSE(command_line->HasSwitch(kUseMobileUserAgent));
CheckUserAgentStringOrdering(false); CheckUserAgentStringOrdering(false);
// Do it for mobile devices. // Do it for mobile devices.
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(kUseMobileUserAgent);
ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_TRUE(command_line->HasSwitch(kUseMobileUserAgent));
CheckUserAgentStringOrdering(true); CheckUserAgentStringOrdering(true);
#else #else
CheckUserAgentStringOrdering(false); CheckUserAgentStringOrdering(false);
@@ -478,13 +478,13 @@ TEST_F(UserAgentUtilsTest, UserAgentStringReduced) {
// Verify the mobile user agent string is not returned when not using a mobile // Verify the mobile user agent string is not returned when not using a mobile
// user agent. // user agent.
ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_FALSE(command_line->HasSwitch(kUseMobileUserAgent));
EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent()); EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent());
// Verify the mobile user agent string is returned when using a mobile user // Verify the mobile user agent string is returned when using a mobile user
// agent. // agent.
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(kUseMobileUserAgent);
ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_TRUE(command_line->HasSwitch(kUseMobileUserAgent));
EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix)); EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix));
#else #else
EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent()); EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent());
@@ -546,7 +546,7 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentPlatformOsCpu) {
{blink::features::kReduceUserAgentAndroidVersionDeviceModel}); {blink::features::kReduceUserAgentAndroidVersionDeviceModel});
// Verify the mobile platform and oscpu user agent string is not reduced when // Verify the mobile platform and oscpu user agent string is not reduced when
// not using a mobile user agent. // not using a mobile user agent.
ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_FALSE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
EXPECT_NE(GetUserAgent(), GenerateExpectedUserAgent()); EXPECT_NE(GetUserAgent(), GenerateExpectedUserAgent());
EXPECT_NE(content::GetUnifiedPlatformForTesting().c_str(), EXPECT_NE(content::GetUnifiedPlatformForTesting().c_str(),
@@ -555,8 +555,8 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentPlatformOsCpu) {
// Verify the mobile platform and oscpu user agent string is not reduced when // Verify the mobile platform and oscpu user agent string is not reduced when
// using a mobile user agent. // using a mobile user agent.
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(kUseMobileUserAgent);
ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_TRUE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
EXPECT_NE(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix)); EXPECT_NE(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix));
} }
@@ -567,7 +567,7 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentPlatformOsCpu) {
{blink::features::kReduceUserAgentMinorVersion, {blink::features::kReduceUserAgentMinorVersion,
blink::features::kReduceUserAgentPlatformOsCpu}, blink::features::kReduceUserAgentPlatformOsCpu},
{}); {});
ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_FALSE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
// Verify unified platform user agent is returned. // Verify unified platform user agent is returned.
EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent()); EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent());
@@ -577,8 +577,8 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentPlatformOsCpu) {
// On iOS, also check the kUseMobileUserAgent flag with the features above. // On iOS, also check the kUseMobileUserAgent flag with the features above.
// This is similar to the Android case above, but we do not care about // This is similar to the Android case above, but we do not care about
// kReduceUserAgentAndroidVersionDeviceModel here. // kReduceUserAgentAndroidVersionDeviceModel here.
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(kUseMobileUserAgent);
ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_TRUE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix)); EXPECT_EQ(GetUserAgent(), GenerateExpectedUserAgent(kMobileProductSuffix));
} }
@@ -620,7 +620,7 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentAndroidVersionDeviceModel) {
// Verify the mobile deviceModel and androidVersion in the user agent string // Verify the mobile deviceModel and androidVersion in the user agent string
// is reduced when not using a mobile user agent. // is reduced when not using a mobile user agent.
ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_FALSE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
std::string buffer = GetUserAgent(); std::string buffer = GetUserAgent();
EXPECT_EQ("Linux; Android 10; K", GetUserAgentPlatformOsCpu(buffer)); EXPECT_EQ("Linux; Android 10; K", GetUserAgentPlatformOsCpu(buffer));
@@ -629,8 +629,8 @@ TEST_F(UserAgentUtilsTest, ReduceUserAgentAndroidVersionDeviceModel) {
// Verify the mobile deviceModel and androidVersion in the user agent string // Verify the mobile deviceModel and androidVersion in the user agent string
// is reduced when using a mobile user agent. // is reduced when using a mobile user agent.
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(kUseMobileUserAgent);
ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); ASSERT_TRUE(command_line->HasSwitch(kUseMobileUserAgent));
{ {
std::string buffer = GetUserAgent(); std::string buffer = GetUserAgent();
EXPECT_EQ("Linux; Android 10; K", GetUserAgentPlatformOsCpu(buffer)); EXPECT_EQ("Linux; Android 10; K", GetUserAgentPlatformOsCpu(buffer));

@@ -2,6 +2,7 @@ include_rules = [
"+chromeos/startup", "+chromeos/startup",
"+components/discardable_memory", "+components/discardable_memory",
"+components/download", "+components/download",
"+components/embedder_support/switches.h",
"+components/power_monitor", "+components/power_monitor",
"+components/tracing/common", "+components/tracing/common",
"+content", "+content",

@@ -39,6 +39,7 @@
#include "base/trace_event/trace_config.h" #include "base/trace_event/trace_config.h"
#include "base/trace_event/trace_log.h" #include "base/trace_event/trace_log.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/embedder_support/switches.h"
#include "components/tracing/common/trace_to_console.h" #include "components/tracing/common/trace_to_console.h"
#include "components/tracing/common/tracing_switches.h" #include "components/tracing/common/tracing_switches.h"
#include "content/app/content_main_runner_impl.h" #include "content/app/content_main_runner_impl.h"
@@ -308,7 +309,7 @@ NO_STACK_PROTECTOR int RunContentProcess(
#if BUILDFLAG(IS_IOS) #if BUILDFLAG(IS_IOS)
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kEnableViewport); command_line->AppendSwitch(switches::kEnableViewport);
command_line->AppendSwitch(switches::kUseMobileUserAgent); command_line->AppendSwitch(embedder_support::kUseMobileUserAgent);
#endif #endif
#if BUILDFLAG(IS_TVOS) #if BUILDFLAG(IS_TVOS)

@@ -101,6 +101,7 @@ source_set("browser") {
"//components/discardable_memory/service", "//components/discardable_memory/service",
"//components/download/database", "//components/download/database",
"//components/download/public/common:public", "//components/download/public/common:public",
"//components/embedder_support",
"//components/file_access", "//components/file_access",
"//components/filename_generation", "//components/filename_generation",
"//components/input", "//components/input",

@@ -10,6 +10,7 @@ include_rules = [
"+components/discardable_memory/service", "+components/discardable_memory/service",
"+components/download/database", "+components/download/database",
"+components/download/public/common", "+components/download/public/common",
"+components/embedder_support/switches.h",
"+components/file_access", "+components/file_access",
"+components/filename_generation", "+components/filename_generation",
"+components/grit", "+components/grit",

@@ -6,6 +6,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "components/embedder_support/switches.h"
#include "content/browser/loader/url_loader_factory_utils.h" #include "content/browser/loader/url_loader_factory_utils.h"
#include "content/browser/preloading/prefetch/prefetch_network_context_client.h" #include "content/browser/preloading/prefetch/prefetch_network_context_client.h"
#include "content/browser/preloading/prefetch/prefetch_proxy_configurator.h" #include "content/browser/preloading/prefetch/prefetch_proxy_configurator.h"
@@ -96,7 +97,7 @@ void PrefetchNetworkContext::CreateIsolatedURLLoaderFactory(
context_params->file_paths = network::mojom::NetworkContextFilePaths::New(); context_params->file_paths = network::mojom::NetworkContextFilePaths::New();
context_params->user_agent = context_params->user_agent =
GetReducedUserAgent(base::CommandLine::ForCurrentProcess()->HasSwitch( GetReducedUserAgent(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseMobileUserAgent), embedder_support::kUseMobileUserAgent),
delegate ? delegate->GetMajorVersionNumber() : ""); delegate ? delegate->GetMajorVersionNumber() : "");
// The verifier created here does not have the same parameters as used in the // The verifier created here does not have the same parameters as used in the
// profile (where additional parameters are added in // profile (where additional parameters are added in

@@ -69,6 +69,7 @@
#include "base/tracing/protos/chrome_track_event.pbzero.h" #include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "cc/base/switches.h" #include "cc/base/switches.h"
#include "components/embedder_support/switches.h"
#include "components/input/utils.h" #include "components/input/utils.h"
#include "components/metrics/histogram_controller.h" #include "components/metrics/histogram_controller.h"
#include "components/metrics/single_sample_metrics.h" #include "components/metrics/single_sample_metrics.h"
@@ -3447,7 +3448,7 @@ void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
switches::kUseCmdDecoder, switches::kUseCmdDecoder,
switches::kUseFakeCodecForPeerConnection, switches::kUseFakeCodecForPeerConnection,
switches::kUseFakeUIForMediaStream, switches::kUseFakeUIForMediaStream,
switches::kUseMobileUserAgent, embedder_support::kUseMobileUserAgent,
switches::kVideoCaptureUseGpuMemoryBuffer, switches::kVideoCaptureUseGpuMemoryBuffer,
switches::kVideoThreads, switches::kVideoThreads,
switches::kWaitForDebuggerOnNavigation, switches::kWaitForDebuggerOnNavigation,

@@ -784,9 +784,6 @@ const char kVideoImageTextureTarget[] = "video-image-texture-target";
const char kUseContextSnapshotSwitch[] = "use-context-snapshot"; const char kUseContextSnapshotSwitch[] = "use-context-snapshot";
#endif #endif
// Set when Chromium should use a mobile user agent.
const char kUseMobileUserAgent[] = "use-mobile-user-agent";
// Use the MockCertVerifier. This only works in test code. // Use the MockCertVerifier. This only works in test code.
const char kUseMockCertVerifierForTesting[] = const char kUseMockCertVerifierForTesting[] =
"use-mock-cert-verifier-for-testing"; "use-mock-cert-verifier-for-testing";

@@ -212,7 +212,6 @@ CONTENT_EXPORT extern const char kVideoImageTextureTarget[];
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(INCLUDE_BOTH_V8_SNAPSHOTS) #if BUILDFLAG(IS_ANDROID) && BUILDFLAG(INCLUDE_BOTH_V8_SNAPSHOTS)
CONTENT_EXPORT extern const char kUseContextSnapshotSwitch[]; CONTENT_EXPORT extern const char kUseContextSnapshotSwitch[];
#endif #endif
CONTENT_EXPORT extern const char kUseMobileUserAgent[];
CONTENT_EXPORT extern const char kUseMockCertVerifierForTesting[]; CONTENT_EXPORT extern const char kUseMockCertVerifierForTesting[];
extern const char kUtilityCmdPrefix[]; extern const char kUtilityCmdPrefix[];
CONTENT_EXPORT extern const char kUtilityProcess[]; CONTENT_EXPORT extern const char kUtilityProcess[];

@@ -315,6 +315,7 @@ static_library("content_shell_lib") {
"//components/cdm/renderer", "//components/cdm/renderer",
"//components/custom_handlers", "//components/custom_handlers",
"//components/custom_handlers:test_support", "//components/custom_handlers:test_support",
"//components/embedder_support",
"//components/embedder_support:user_agent", "//components/embedder_support:user_agent",
"//components/input", "//components/input",
"//components/keyed_service/content", "//components/keyed_service/content",

@@ -34,6 +34,7 @@
#include "components/custom_handlers/protocol_handler_registry.h" #include "components/custom_handlers/protocol_handler_registry.h"
#include "components/custom_handlers/protocol_handler_throttle.h" #include "components/custom_handlers/protocol_handler_throttle.h"
#include "components/custom_handlers/simple_protocol_handler_registry_factory.h" #include "components/custom_handlers/simple_protocol_handler_registry_factory.h"
#include "components/embedder_support/switches.h"
#include "components/embedder_support/user_agent_utils.h" #include "components/embedder_support/user_agent_utils.h"
#include "components/metrics/client_info.h" #include "components/metrics/client_info.h"
#include "components/metrics/metrics_service.h" #include "components/metrics/metrics_service.h"
@@ -752,7 +753,7 @@ std::string ShellContentBrowserClient::GetUserAgent() {
base::StringPrintf("Chrome/%s.0.0.0", CONTENT_SHELL_MAJOR_VERSION); base::StringPrintf("Chrome/%s.0.0.0", CONTENT_SHELL_MAJOR_VERSION);
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseMobileUserAgent)) { embedder_support::kUseMobileUserAgent)) {
product += " Mobile"; product += " Mobile";
} }
#endif #endif