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() {
}
if (user_agent_partial_)
user_agent_ =
web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE, user_agent_);
user_agent_ = web::BuildMobileUserAgent(user_agent_);
// Cache
base::FilePath storage_path;
......
......@@ -81,8 +81,7 @@ std::unique_ptr<net::ProxyResolutionService> CreateProxyResolutionService(
// Creates default User-Agent request value, combining optional
// |partial_user_agent| with system-dependent values.
std::string CreateDefaultUserAgent(const std::string& partial_user_agent) {
return web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE,
partial_user_agent);
return web::BuildMobileUserAgent(partial_user_agent);
}
void SetNetworkThreadPriorityOnNetworkThread(double priority) {
......
......@@ -663,9 +663,8 @@ void AppendSwitchesFromExperimentalSettings(base::CommandLine* command_line) {
base::SysInfo::OperatingSystemVersionNumbers(&major, &minor, &bugfix);
std::string product = base::StringPrintf("Version/%d.%d", major, minor);
command_line->AppendSwitchASCII(
switches::kUserAgent,
web::BuildUserAgentFromProduct(web::UserAgentType::MOBILE, product));
command_line->AppendSwitchASCII(switches::kUserAgent,
web::BuildMobileUserAgent(product));
}
// Freeform commandline flags. These are added last, so that any flags added
......
......@@ -167,6 +167,7 @@ source_set("eg2_tests") {
":constants",
"//base/test:test_support",
"//components/strings",
"//components/version_info",
"//ios/chrome/app/strings",
"//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2",
......@@ -191,6 +192,7 @@ source_set("eg_tests") {
"//base",
"//base/test:test_support",
"//components/strings",
"//components/version_info",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ui/popup_menu:constants",
"//ios/chrome/browser/ui/util",
......
......@@ -5,6 +5,7 @@
#include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.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"
#include "ios/chrome/grit/ios_strings.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
......@@ -98,13 +99,17 @@ class UserAgentResponseProvider : public web::DataResponseProvider {
*headers = web::ResponseProvider::GetDefaultResponseHeaders();
std::string userAgent;
std::string desktop_product =
"CriOS/" + version_info::GetMajorVersionNumber();
std::string desktop_user_agent =
web::BuildUserAgentFromProduct(web::UserAgentType::DESKTOP, "");
web::BuildDesktopUserAgent(desktop_product);
if (request.headers.GetHeader("User-Agent", &userAgent) &&
userAgent == desktop_user_agent) {
response_body->assign(std::string("Desktop\n") + purge_additions);
response_body->assign(std::string(kDesktopSiteLabel) + "\n" +
purge_additions);
} 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 {
bool IsEmbedderBlockRestoreUrlEnabled() override;
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.
UIView* windowed_container_ = nil;
......
......@@ -10,6 +10,7 @@
#import "base/ios/ios_util.h"
#import "base/ios/ns_error_util.h"
#include "base/mac/bundle_locations.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/google/core/common/google_util.h"
......@@ -52,6 +53,9 @@
#endif
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
// bundled resource file with the given name (excluding extension).
NSString* GetPageScript(NSString* script_file_name) {
......@@ -91,6 +95,24 @@ NSString* GetSafeBrowsingErrorPageHTML(web::WebState* web_state,
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
ChromeWebClient::ChromeWebClient() {}
......@@ -169,7 +191,9 @@ std::string ChromeWebClient::GetUserAgent(web::UserAgentType type) const {
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 {
......@@ -299,12 +323,6 @@ UIView* ChromeWebClient::GetWindowedContainer() {
return windowed_container_;
}
std::string ChromeWebClient::GetProduct() const {
std::string product("CriOS/");
product += version_info::GetVersionNumber();
return product;
}
bool ChromeWebClient::ForceMobileVersionByDefault(const GURL& url) {
DCHECK(web::features::UseWebClientDefaultUserAgent());
if (base::FeatureList::IsEnabled(web::kMobileGoogleSRP)) {
......
......@@ -274,7 +274,7 @@ void IOSIOThread::Init() {
quic_user_agent_id.append(
version_info::GetProductNameAndVersionForUserAgent());
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.
network_session_configurator::ParseCommandLineAndFieldTrials(
......
......@@ -38,13 +38,13 @@ std::string GetUserAgentTypeDescription(UserAgentType type);
UserAgentType GetUserAgentTypeWithDescription(const std::string& description);
// 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.
// The returned user agent is very similar to that used by Mobile Safari, for
// web page compatibility.
std::string BuildUserAgentFromProduct(UserAgentType type,
const std::string& product);
std::string BuildDesktopUserAgent(const std::string& desktop_product);
std::string BuildMobileUserAgent(const std::string& mobile_product);
} // namespace web
......
......@@ -24,9 +24,9 @@
namespace {
const char kDesktopUserAgent[] =
const char kDesktopUserAgentProductPlaceholder[] =
"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 "
"Safari/605.1.15";
......@@ -36,14 +36,13 @@ const char kUserAgentTypeNoneDescription[] = "NONE";
const char kUserAgentTypeMobileDescription[] = "MOBILE";
const char kUserAgentTypeDesktopDescription[] = "DESKTOP";
std::string OSVersion(web::UserAgentType type) {
std::string OSVersion() {
int32_t os_major_version = 0;
int32_t os_minor_version = 0;
int32_t os_bugfix_version = 0;
base::SysInfo::OperatingSystemVersionNumbers(
&os_major_version, &os_minor_version, &os_bugfix_version);
DCHECK_EQ(web::UserAgentType::MOBILE, type);
std::string os_version;
base::StringAppendF(&os_version, "%d_%d", os_major_version, os_minor_version);
return os_version;
......@@ -75,9 +74,8 @@ UserAgentType GetUserAgentTypeWithDescription(const std::string& description) {
return UserAgentType::NONE;
}
std::string BuildOSCpuInfo(web::UserAgentType type) {
std::string BuildOSCpuInfo() {
std::string os_cpu;
DCHECK_EQ(web::UserAgentType::MOBILE, type);
// Remove the end of the platform name. For example "iPod touch" becomes
// "iPod".
std::string platform =
......@@ -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(),
(platform == "iPad") ? "OS" : "iPhone OS",
OSVersion(type).c_str());
OSVersion().c_str());
return os_cpu;
}
std::string BuildUserAgentFromProduct(UserAgentType type,
const std::string& product) {
if (type == web::UserAgentType::DESKTOP)
return kDesktopUserAgent;
std::string BuildDesktopUserAgent(const std::string& desktop_product) {
std::string product = desktop_product;
if (!desktop_product.empty()) {
// 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;
base::StringAppendF(&user_agent,
"Mozilla/5.0 (%s) AppleWebKit/605.1.15"
" (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;
}
......
......@@ -18,6 +18,12 @@
#endif
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[] =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) "
......@@ -80,18 +86,21 @@ TEST_F(UserAgentTest, MobileUserAgentForProduct) {
"like Gecko) %s Mobile/15E148 Safari/604.1",
platform.c_str(), cpu.c_str(), os_version.c_str(), product.c_str());
std::string result =
BuildUserAgentFromProduct(web::UserAgentType::MOBILE, product);
std::string result = BuildMobileUserAgent(product);
EXPECT_EQ(expected_user_agent, result);
}
// Tests the desktop user agent, checking that the product isn't taken into
// account.
// account when it is empty.
TEST_F(UserAgentTest, DesktopUserAgentForProduct) {
EXPECT_EQ(kDesktopUserAgent,
BuildUserAgentFromProduct(web::UserAgentType::DESKTOP,
"my_product_name"));
EXPECT_EQ(kDesktopUserAgent, BuildDesktopUserAgent(""));
}
// Tests the desktop user agent for a specific product name.
TEST_F(UserAgentTest, DesktopUserAgentWithProduct) {
EXPECT_EQ(kDesktopUserAgentWithProduct,
BuildDesktopUserAgent("desktop_product_name"));
}
} // namespace web
......@@ -58,8 +58,7 @@ ShellBrowserState* ShellWebClient::browser_state() const {
}
std::string ShellWebClient::GetUserAgent(UserAgentType type) const {
return web::BuildUserAgentFromProduct(UserAgentType::MOBILE,
"CriOS/36.77.34.45");
return web::BuildMobileUserAgent("CriOS/36.77.34.45");
}
base::StringPiece ShellWebClient::GetDataResource(
......
......@@ -72,8 +72,7 @@ bool WebViewWebClient::IsAppSpecificURL(const GURL& url) const {
}
std::string WebViewWebClient::GetUserAgent(web::UserAgentType type) const {
return web::BuildUserAgentFromProduct(
web::UserAgentType::MOBILE,
return web::BuildMobileUserAgent(
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