Commit 449df8fb authored by Maks Orlovich's avatar Maks Orlovich Committed by Commit Bot

User agent freezing experiment: include the major version even if frozen

This is almost entirely from Aaron Tagliaboschi's work at
https://chromium-review.googlesource.com/c/chromium/src/+/2019602

This code also removes the unconditional use of GetFrozenUserAgent in
isolated_prerender_tab_helper.cc which looked like a mistake;
::GetUserAgent uses that when the experiment is on.

Bug: 955620
Change-Id: Id4b719e0b4b1aada85418a889203bb5a85a3e72b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2079466
Commit-Queue: Maksim Orlovich <morlovich@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Reviewed-by: default avatarYoav Weiss <yoavweiss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756001}
parent 85ff2462
...@@ -1105,8 +1105,8 @@ std::string GetUserAgent() { ...@@ -1105,8 +1105,8 @@ std::string GetUserAgent() {
if (base::FeatureList::IsEnabled(blink::features::kFreezeUserAgent)) { if (base::FeatureList::IsEnabled(blink::features::kFreezeUserAgent)) {
return content::GetFrozenUserAgent( return content::GetFrozenUserAgent(
command_line->HasSwitch(switches::kUseMobileUserAgent)) command_line->HasSwitch(switches::kUseMobileUserAgent),
.as_string(); version_info::GetMajorVersionNumber());
} }
std::string product = GetProduct(); std::string product = GetProduct();
......
...@@ -444,7 +444,9 @@ TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) { ...@@ -444,7 +444,9 @@ TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) {
{ {
ChromeContentBrowserClient content_browser_client; ChromeContentBrowserClient content_browser_client;
std::string buffer = content_browser_client.GetUserAgent(); std::string buffer = content_browser_client.GetUserAgent();
EXPECT_EQ(buffer, content::frozen_user_agent_strings::kAndroid); EXPECT_EQ(buffer, base::StringPrintf(
content::frozen_user_agent_strings::kAndroid,
version_info::GetMajorVersionNumber().c_str()));
} }
// 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
...@@ -454,13 +456,17 @@ TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) { ...@@ -454,13 +456,17 @@ TEST(ChromeContentBrowserClientTest, UserAgentStringFrozen) {
{ {
ChromeContentBrowserClient content_browser_client; ChromeContentBrowserClient content_browser_client;
std::string buffer = content_browser_client.GetUserAgent(); std::string buffer = content_browser_client.GetUserAgent();
EXPECT_EQ(buffer, content::frozen_user_agent_strings::kAndroidMobile); EXPECT_EQ(buffer, base::StringPrintf(
content::frozen_user_agent_strings::kAndroidMobile,
version_info::GetMajorVersionNumber().c_str()));
} }
#else #else
{ {
ChromeContentBrowserClient content_browser_client; ChromeContentBrowserClient content_browser_client;
std::string buffer = content_browser_client.GetUserAgent(); std::string buffer = content_browser_client.GetUserAgent();
EXPECT_EQ(buffer, content::frozen_user_agent_strings::kDesktop); EXPECT_EQ(buffer, base::StringPrintf(
content::frozen_user_agent_strings::kDesktop,
version_info::GetMajorVersionNumber().c_str()));
} }
#endif #endif
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h" #include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h"
#include "chrome/browser/net/prediction_options.h" #include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prerender/isolated/isolated_prerender_features.h" #include "chrome/browser/prerender/isolated/isolated_prerender_features.h"
...@@ -33,7 +34,6 @@ ...@@ -33,7 +34,6 @@
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/common/content_constants.h" #include "content/public/common/content_constants.h"
#include "content/public/common/user_agent.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/base/network_isolation_key.h" #include "net/base/network_isolation_key.h"
...@@ -545,7 +545,7 @@ void IsolatedPrerenderTabHelper::CreateIsolatedURLLoaderFactory() { ...@@ -545,7 +545,7 @@ void IsolatedPrerenderTabHelper::CreateIsolatedURLLoaderFactory() {
IsolatedPrerenderServiceFactory::GetForProfile(profile_); IsolatedPrerenderServiceFactory::GetForProfile(profile_);
auto context_params = network::mojom::NetworkContextParams::New(); auto context_params = network::mojom::NetworkContextParams::New();
context_params->user_agent = content::GetFrozenUserAgent(true).as_string(); context_params->user_agent = ::GetUserAgent();
context_params->initial_custom_proxy_config = context_params->initial_custom_proxy_config =
isolated_prerender_service->proxy_configurator() isolated_prerender_service->proxy_configurator()
->CreateCustomProxyConfig(); ->CreateCustomProxyConfig();
......
...@@ -163,12 +163,16 @@ std::string BuildOSCpuInfo(bool include_android_build_number) { ...@@ -163,12 +163,16 @@ std::string BuildOSCpuInfo(bool include_android_build_number) {
return os_cpu; return os_cpu;
} }
base::StringPiece GetFrozenUserAgent(bool mobile) { std::string GetFrozenUserAgent(bool mobile, std::string major_version) {
std::string user_agent;
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
return mobile ? frozen_user_agent_strings::kAndroidMobile user_agent = mobile ? frozen_user_agent_strings::kAndroidMobile
: frozen_user_agent_strings::kAndroid; : frozen_user_agent_strings::kAndroid;
#else
user_agent = frozen_user_agent_strings::kDesktop;
#endif #endif
return frozen_user_agent_strings::kDesktop;
return base::StringPrintf(user_agent.c_str(), major_version.c_str());
} }
std::string BuildUserAgentFromProduct(const std::string& product) { std::string BuildUserAgentFromProduct(const std::string& product) {
......
...@@ -18,14 +18,14 @@ namespace frozen_user_agent_strings { ...@@ -18,14 +18,14 @@ namespace frozen_user_agent_strings {
const char kDesktop[] = const char kDesktop[] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, " "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
"like Gecko) Chrome/75.0.3764.0 Safari/537.36"; "like Gecko) Chrome/%s.0.0.0 Safari/537.36";
const char kAndroid[] = const char kAndroid[] =
"Mozilla/5.0 (Linux; Android 9; Unspecified Device) " "Mozilla/5.0 (Linux; Android 9; Unspecified Device) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3764.0 " "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s.0.0.0 "
"Safari/537.36"; "Safari/537.36";
const char kAndroidMobile[] = const char kAndroidMobile[] =
"Mozilla/5.0 (Linux; Android 9; Unspecified Device) " "Mozilla/5.0 (Linux; Android 9; Unspecified Device) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3764.0 Mobile " "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s.0.0.0 Mobile "
"Safari/537.36"; "Safari/537.36";
} // namespace frozen_user_agent_strings } // namespace frozen_user_agent_strings
...@@ -50,7 +50,8 @@ CONTENT_EXPORT std::string GetOSVersion(bool include_android_build_number); ...@@ -50,7 +50,8 @@ CONTENT_EXPORT std::string GetOSVersion(bool include_android_build_number);
// Returns the frozen User-agent string for // Returns the frozen User-agent string for
// https://github.com/WICG/ua-client-hints. // https://github.com/WICG/ua-client-hints.
CONTENT_EXPORT base::StringPiece GetFrozenUserAgent(bool mobile); CONTENT_EXPORT std::string GetFrozenUserAgent(bool mobile,
std::string major_version);
// Helper function to generate a full user agent string from a short // Helper function to generate a full user agent string from a short
// product name. // product name.
......
...@@ -100,8 +100,8 @@ std::string GetShellUserAgent() { ...@@ -100,8 +100,8 @@ std::string GetShellUserAgent() {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (base::FeatureList::IsEnabled(blink::features::kFreezeUserAgent)) { if (base::FeatureList::IsEnabled(blink::features::kFreezeUserAgent)) {
return content::GetFrozenUserAgent( return content::GetFrozenUserAgent(
command_line->HasSwitch(switches::kUseMobileUserAgent)) command_line->HasSwitch(switches::kUseMobileUserAgent),
.as_string(); CONTENT_SHELL_MAJOR_VERSION);
} }
if (command_line->HasSwitch(switches::kUseMobileUserAgent)) if (command_line->HasSwitch(switches::kUseMobileUserAgent))
product += " Mobile"; product += " Mobile";
......
...@@ -12,11 +12,12 @@ ...@@ -12,11 +12,12 @@
var platform = navigator.platform; var platform = navigator.platform;
// Validate the user agent string using the following frozen user agent strings found in content/common/user_agent.h: // Validate the user agent string using the following frozen user agent strings found in content/common/user_agent.h:
var desktopUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3764.0 Safari/537.36" // The version is 99 since that's what content_shell uses.
var desktopUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.0.0 Safari/537.36"
var frozenUserAgents = [ var frozenUserAgents = [
desktopUserAgent, desktopUserAgent,
"Mozilla/5.0 (Linux; Android 9; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3764.0 Safari/537.36", "Mozilla/5.0 (Linux; Android 9; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.0.0 Safari/537.36",
"Mozilla/5.0 (Linux; Android 9; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3764.0 Mobile Safari/537.36" "Mozilla/5.0 (Linux; Android 9; Unspecified Device) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.0.0 Mobile Safari/537.36"
]; ];
// Platform values should match strings defined in third_party/blink/renderer/core/frame/navigator.cc. // Platform values should match strings defined in third_party/blink/renderer/core/frame/navigator.cc.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment