Commit ba163d43 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Add CriOS to Desktop UserAgent

This CL adds the CriOS tag + the major version to the User Agent used
to request the desktop version of websites for Chrome.
It also don't change anything for the other embedder of ios/web.

Bug: 1083999
Change-Id: Ief1bd073d4a664e3f02a3b3459ea34bd35152558
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2207314Reviewed-by: default avatarJohn Wu <jzw@chromium.org>
Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772236}
parent 6a868fea
...@@ -315,8 +315,7 @@ void CronetEnvironment::InitializeOnNetworkThread() { ...@@ -315,8 +315,7 @@ void CronetEnvironment::InitializeOnNetworkThread() {
} }
if (user_agent_partial_) if (user_agent_partial_)
user_agent_ = user_agent_ = web::BuildMobileUserAgent(user_agent_);
web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE, user_agent_);
// Cache // Cache
base::FilePath storage_path; base::FilePath storage_path;
......
...@@ -81,8 +81,7 @@ std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService( ...@@ -81,8 +81,7 @@ std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService(
// Creates default User-Agent request value, combining optional // Creates default User-Agent request value, combining optional
// |partial_user_agent| with system-dependent values. // |partial_user_agent| with system-dependent values.
std::string CreateDefaultUserAgent(const std::string& partial_user_agent) { std::string CreateDefaultUserAgent(const std::string& partial_user_agent) {
return web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE, return web::BuildMobileUserAgent(partial_user_agent);
partial_user_agent);
} }
void SetNetworkThreadPriorityOnNetworkThread(double priority) { void SetNetworkThreadPriorityOnNetworkThread(double priority) {
......
...@@ -663,9 +663,8 @@ void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) { ...@@ -663,9 +663,8 @@ void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) {
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix); base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
std::string product = base::StringPrintf("Version/%d.%d", major, minor); std::string product = base::StringPrintf("Version/%d.%d", major, minor);
command_line->AppendSwitchASCII( command_line->AppendSwitchASCII(switches::kUserAgent,
switches::kUserAgent, web::BuildMobileUserAgent(product));
web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE, product));
} }
// Freeform commandline flags. These are added last, so that any flags added // Freeform commandline flags. These are added last, so that any flags added
......
...@@ -167,6 +167,7 @@ source_set("eg2_tests") { ...@@ -167,6 +167,7 @@ source_set("eg2_tests") {
":constants", ":constants",
"//base/test:test_support", "//base/test:test_support",
"//components/strings", "//components/strings",
"//components/version_info",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/test/earl_grey:eg_test_support+eg2", "//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2", "//ios/testing/earl_grey:eg_test_support+eg2",
...@@ -191,6 +192,7 @@ source_set("eg_tests") { ...@@ -191,6 +192,7 @@ source_set("eg_tests") {
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//components/strings", "//components/strings",
"//components/version_info",
"//ios/chrome/app/strings", "//ios/chrome/app/strings",
"//ios/chrome/browser/ui/popup_menu:constants", "//ios/chrome/browser/ui/popup_menu:constants",
"//ios/chrome/browser/ui/util", "//ios/chrome/browser/ui/util",
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "components/version_info/version_info.h"
#import "ios/chrome/browser/ui/popup_menu/popup_menu_constants.h" #import "ios/chrome/browser/ui/popup_menu/popup_menu_constants.h"
#include "ios/chrome/grit/ios_strings.h" #include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h" #import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
...@@ -98,13 +99,17 @@ class UserAgentResponseProvider : public web::DataResponseProvider { ...@@ -98,13 +99,17 @@ class UserAgentResponseProvider : public web::DataResponseProvider {
*headers = web::ResponseProvider::GetDefaultResponseHeaders(); *headers = web::ResponseProvider::GetDefaultResponseHeaders();
std::string userAgent; std::string userAgent;
std::string desktop_product =
"CriOS/" + version_info::GetMajorVersionNumber();
std::string desktop_user_agent = std::string desktop_user_agent =
web::BuildUserAgentFromProduct(web::UserAgentType::DESKTOP, ""); web::BuildDesktopUserAgent(desktop_product);
if (request.headers.GetHeader("User-Agent", &userAgent) && if (request.headers.GetHeader("User-Agent", &userAgent) &&
userAgent == desktop_user_agent) { userAgent == desktop_user_agent) {
response_body->assign(std::string("Desktop\n") + purge_additions); response_body->assign(std::string(kDesktopSiteLabel) + "\n" +
purge_additions);
} else { } else {
response_body->assign(std::string("Mobile\n") + purge_additions); response_body->assign(std::string(kMobileSiteLabel) + "\n" +
purge_additions);
} }
} }
}; };
......
...@@ -66,10 +66,6 @@ class ChromeWebClient : public web::WebClient { ...@@ -66,10 +66,6 @@ class ChromeWebClient : public web::WebClient {
bool IsEmbedderBlockRestoreUrlEnabled() override; bool IsEmbedderBlockRestoreUrlEnabled() override;
private: private:
// Returns a string describing the product name and version, of the
// form "productname/version". Used as part of the user agent string.
std::string GetProduct() const;
// Reference to a view that is attached to a window. // Reference to a view that is attached to a window.
UIView* windowed_container_ = nil; UIView* windowed_container_ = nil;
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#import "base/ios/ios_util.h" #import "base/ios/ios_util.h"
#import "base/ios/ns_error_util.h" #import "base/ios/ns_error_util.h"
#include "base/mac/bundle_locations.h" #include "base/mac/bundle_locations.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "components/dom_distiller/core/url_constants.h" #include "components/dom_distiller/core/url_constants.h"
#include "components/google/core/common/google_util.h" #include "components/google/core/common/google_util.h"
...@@ -52,6 +53,9 @@ ...@@ -52,6 +53,9 @@
#endif #endif
namespace { namespace {
// The tag describing the product name with a placeholder for the version.
const char kProductTagWithPlaceholder[] = "CriOS/%s";
// Returns an autoreleased string containing the JavaScript loaded from a // Returns an autoreleased string containing the JavaScript loaded from a
// bundled resource file with the given name (excluding extension). // bundled resource file with the given name (excluding extension).
NSString* GetPageScript(NSString* script_file_name) { NSString* GetPageScript(NSString* script_file_name) {
...@@ -91,6 +95,24 @@ NSString* GetSafeBrowsingErrorPageHTML(web::WebState* web_state, ...@@ -91,6 +95,24 @@ NSString* GetSafeBrowsingErrorPageHTML(web::WebState* web_state,
return base::SysUTF8ToNSString(error_page_content); return base::SysUTF8ToNSString(error_page_content);
} }
// Returns a string describing the product name and version, of the
// form "productname/version". Used as part of the user agent string.
std::string GetMobileProduct() {
return base::StringPrintf(kProductTagWithPlaceholder,
version_info::GetVersionNumber().c_str());
}
// Returns a string describing the product name and version, of the
// form "productname/version". Used as part of the user agent string.
// The Desktop UserAgent is only using the major version to reduce the surface
// for fingerprinting. The Mobile one is using the full version for legacy
// reasons.
std::string GetDesktopProduct() {
return base::StringPrintf(kProductTagWithPlaceholder,
version_info::GetMajorVersionNumber().c_str());
}
} // namespace } // namespace
ChromeWebClient::ChromeWebClient() {} ChromeWebClient::ChromeWebClient() {}
...@@ -169,7 +191,9 @@ std::string ChromeWebClient::GetUserAgent(web::UserAgentType type) const { ...@@ -169,7 +191,9 @@ std::string ChromeWebClient::GetUserAgent(web::UserAgentType type) const {
LOG(WARNING) << "Ignored invalid value for flag --" << switches::kUserAgent; LOG(WARNING) << "Ignored invalid value for flag --" << switches::kUserAgent;
} }
return web::BuildUserAgentFromProduct(type, GetProduct()); if (type == web::UserAgentType::DESKTOP)
return web::BuildDesktopUserAgent(GetDesktopProduct());
return web::BuildMobileUserAgent(GetMobileProduct());
} }
base::string16 ChromeWebClient::GetLocalizedString(int message_id) const { base::string16 ChromeWebClient::GetLocalizedString(int message_id) const {
...@@ -299,12 +323,6 @@ UIView* ChromeWebClient::GetWindowedContainer() { ...@@ -299,12 +323,6 @@ UIView* ChromeWebClient::GetWindowedContainer() {
return windowed_container_; return windowed_container_;
} }
std::string ChromeWebClient::GetProduct() const {
std::string product("CriOS/");
product += version_info::GetVersionNumber();
return product;
}
bool ChromeWebClient::ForceMobileVersionByDefault(const GURL& url) { bool ChromeWebClient::ForceMobileVersionByDefault(const GURL& url) {
DCHECK(web::features::UseWebClientDefaultUserAgent()); DCHECK(web::features::UseWebClientDefaultUserAgent());
if (base::FeatureList::IsEnabled(web::kMobileGoogleSRP)) { if (base::FeatureList::IsEnabled(web::kMobileGoogleSRP)) {
......
...@@ -274,7 +274,7 @@ void IOSIOThread::Init() { ...@@ -274,7 +274,7 @@ void IOSIOThread::Init() {
quic_user_agent_id.append( quic_user_agent_id.append(
version_info::GetProductNameAndVersionForUserAgent()); version_info::GetProductNameAndVersionForUserAgent());
quic_user_agent_id.push_back(' '); quic_user_agent_id.push_back(' ');
quic_user_agent_id.append(web::BuildOSCpuInfo(web::UserAgentType::MOBILE)); quic_user_agent_id.append(web::BuildOSCpuInfo());
// Set up field trials, ignoring debug command line options. // Set up field trials, ignoring debug command line options.
network_session_configurator::ParseCommandLineAndFieldTrials( network_session_configurator::ParseCommandLineAndFieldTrials(
......
...@@ -38,13 +38,13 @@ std::string GetUserAgentTypeDescription(UserAgentType type); ...@@ -38,13 +38,13 @@ std::string GetUserAgentTypeDescription(UserAgentType type);
UserAgentType GetUserAgentTypeWithDescription(const std::string& description); UserAgentType GetUserAgentTypeWithDescription(const std::string& description);
// Returns the os cpu info portion for a user agent. // Returns the os cpu info portion for a user agent.
std::string BuildOSCpuInfo(UserAgentType type); std::string BuildOSCpuInfo();
// Returns the user agent to use for the given product name. // Returns the user agent to use for the given product name.
// The returned user agent is very similar to that used by Mobile Safari, for // The returned user agent is very similar to that used by Mobile Safari, for
// web page compatibility. // web page compatibility.
std::string BuildUserAgentFromProduct(UserAgentType type, std::string BuildDesktopUserAgent(const std::string& desktop_product);
const std::string& product); std::string BuildMobileUserAgent(const std::string& mobile_product);
} // namespace web } // namespace web
......
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
namespace { namespace {
const char kDesktopUserAgent[] = const char kDesktopUserAgentProductPlaceholder[] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) " "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) " "AppleWebKit/605.1.15 (KHTML, like Gecko) %s"
"Version/11.1.1 " "Version/11.1.1 "
"Safari/605.1.15"; "Safari/605.1.15";
...@@ -36,14 +36,13 @@ const char kUserAgentTypeNoneDescription[] = "NONE"; ...@@ -36,14 +36,13 @@ const char kUserAgentTypeNoneDescription[] = "NONE";
const char kUserAgentTypeMobileDescription[] = "MOBILE"; const char kUserAgentTypeMobileDescription[] = "MOBILE";
const char kUserAgentTypeDesktopDescription[] = "DESKTOP"; const char kUserAgentTypeDesktopDescription[] = "DESKTOP";
std::string OSVersion(web::UserAgentType type) { std::string OSVersion() {
int32_t os_major_version = 0; int32_t os_major_version = 0;
int32_t os_minor_version = 0; int32_t os_minor_version = 0;
int32_t os_bugfix_version = 0; int32_t os_bugfix_version = 0;
base::SysInfo::OperatingSystemVersionNumbers( base::SysInfo::OperatingSystemVersionNumbers(
&os_major_version, &os_minor_version, &os_bugfix_version); &os_major_version, &os_minor_version, &os_bugfix_version);
DCHECK_EQ(web::UserAgentType::MOBILE, type);
std::string os_version; std::string os_version;
base::StringAppendF(&os_version, "%d_%d", os_major_version, os_minor_version); base::StringAppendF(&os_version, "%d_%d", os_major_version, os_minor_version);
return os_version; return os_version;
...@@ -75,9 +74,8 @@ UserAgentType GetUserAgentTypeWithDescription(const std::string& description) { ...@@ -75,9 +74,8 @@ UserAgentType GetUserAgentTypeWithDescription(const std::string& description) {
return UserAgentType::NONE; return UserAgentType::NONE;
} }
std::string BuildOSCpuInfo(web::UserAgentType type) { std::string BuildOSCpuInfo() {
std::string os_cpu; std::string os_cpu;
DCHECK_EQ(web::UserAgentType::MOBILE, type);
// Remove the end of the platform name. For example "iPod touch" becomes // Remove the end of the platform name. For example "iPod touch" becomes
// "iPod". // "iPod".
std::string platform = std::string platform =
...@@ -88,22 +86,29 @@ std::string BuildOSCpuInfo(web::UserAgentType type) { ...@@ -88,22 +86,29 @@ std::string BuildOSCpuInfo(web::UserAgentType type) {
base::StringAppendF(&os_cpu, "%s; CPU %s %s like Mac OS X", platform.c_str(), base::StringAppendF(&os_cpu, "%s; CPU %s %s like Mac OS X", platform.c_str(),
(platform == "iPad") ? "OS" : "iPhone OS", (platform == "iPad") ? "OS" : "iPhone OS",
OSVersion(type).c_str()); OSVersion().c_str());
return os_cpu; return os_cpu;
} }
std::string BuildUserAgentFromProduct(UserAgentType type, std::string BuildDesktopUserAgent(const std::string& desktop_product) {
const std::string& product) { std::string product = desktop_product;
if (type == web::UserAgentType::DESKTOP) if (!desktop_product.empty()) {
return kDesktopUserAgent; // In case the product isn't empty, add a space after it.
product = product + " ";
}
std::string user_agent;
base::StringAppendF(&user_agent, kDesktopUserAgentProductPlaceholder,
product.c_str());
return user_agent;
}
DCHECK_EQ(web::UserAgentType::MOBILE, type); std::string BuildMobileUserAgent(const std::string& mobile_product) {
std::string user_agent; std::string user_agent;
base::StringAppendF(&user_agent, base::StringAppendF(&user_agent,
"Mozilla/5.0 (%s) AppleWebKit/605.1.15" "Mozilla/5.0 (%s) AppleWebKit/605.1.15"
" (KHTML, like Gecko) %s Mobile/15E148 Safari/604.1", " (KHTML, like Gecko) %s Mobile/15E148 Safari/604.1",
BuildOSCpuInfo(type).c_str(), product.c_str()); BuildOSCpuInfo().c_str(), mobile_product.c_str());
return user_agent; return user_agent;
} }
......
...@@ -18,6 +18,12 @@ ...@@ -18,6 +18,12 @@
#endif #endif
namespace { namespace {
const char kDesktopUserAgentWithProduct[] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) desktop_product_name "
"Version/11.1.1 "
"Safari/605.1.15";
const char kDesktopUserAgent[] = const char kDesktopUserAgent[] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) " "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) " "AppleWebKit/605.1.15 (KHTML, like Gecko) "
...@@ -80,18 +86,21 @@ TEST_F(UserAgentTest, MobileUserAgentForProduct) { ...@@ -80,18 +86,21 @@ TEST_F(UserAgentTest, MobileUserAgentForProduct) {
"like Gecko) %s Mobile/15E148 Safari/604.1", "like Gecko) %s Mobile/15E148 Safari/604.1",
platform.c_str(), cpu.c_str(), os_version.c_str(), product.c_str()); platform.c_str(), cpu.c_str(), os_version.c_str(), product.c_str());
std::string result = std::string result = BuildMobileUserAgent(product);
BuildUserAgentFromProduct(web::UserAgentType::MOBILE, product);
EXPECT_EQ(expected_user_agent, result); EXPECT_EQ(expected_user_agent, result);
} }
// Tests the desktop user agent, checking that the product isn't taken into // Tests the desktop user agent, checking that the product isn't taken into
// account. // account when it is empty.
TEST_F(UserAgentTest, DesktopUserAgentForProduct) { TEST_F(UserAgentTest, DesktopUserAgentForProduct) {
EXPECT_EQ(kDesktopUserAgent, EXPECT_EQ(kDesktopUserAgent, BuildDesktopUserAgent(""));
BuildUserAgentFromProduct(web::UserAgentType::DESKTOP, }
"my_product_name"));
// Tests the desktop user agent for a specific product name.
TEST_F(UserAgentTest, DesktopUserAgentWithProduct) {
EXPECT_EQ(kDesktopUserAgentWithProduct,
BuildDesktopUserAgent("desktop_product_name"));
} }
} // namespace web } // namespace web
...@@ -58,8 +58,7 @@ ShellBrowserState* ShellWebClient::browser_state() const { ...@@ -58,8 +58,7 @@ ShellBrowserState* ShellWebClient::browser_state() const {
} }
std::string ShellWebClient::GetUserAgent(UserAgentType type) const { std::string ShellWebClient::GetUserAgent(UserAgentType type) const {
return web::BuildUserAgentFromProduct(UserAgentType::MOBILE, return web::BuildMobileUserAgent("CriOS/36.77.34.45");
"CriOS/36.77.34.45");
} }
base::StringPiece ShellWebClient::GetDataResource( base::StringPiece ShellWebClient::GetDataResource(
......
...@@ -72,8 +72,7 @@ bool WebViewWebClient::IsAppSpecificURL(const GURL& url) const { ...@@ -72,8 +72,7 @@ bool WebViewWebClient::IsAppSpecificURL(const GURL& url) const {
} }
std::string WebViewWebClient::GetUserAgent(web::UserAgentType type) const { std::string WebViewWebClient::GetUserAgent(web::UserAgentType type) const {
return web::BuildUserAgentFromProduct( return web::BuildMobileUserAgent(
web::UserAgentType::MOBILE,
base::SysNSStringToUTF8([CWVWebView userAgentProduct])); base::SysNSStringToUTF8([CWVWebView userAgentProduct]));
} }
......
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