Commit f3dce521 authored by Wez's avatar Wez Committed by Commit Bot

[fuchsia] Allow system config to request Android-ish user agent OS.

Allow WebEngine to be configured to report an Android-like UserAgent
via the system-provided config-data, to allow it to be fetch the right
form of content for the device form-factor.

This will likely be replaced with options to configure the Client Hints
reported, on a per-instance basis, once those are standardized.

Bug: b/171582703, 1010256
Change-Id: I9db408be09ec9b9a78facc7b078d635b96c3637b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522071
Commit-Queue: Wez <wez@chromium.org>
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Auto-Submit: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825042}
parent 4a5239db
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include <stdint.h> #include <stdint.h>
#include "base/feature_list.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/command_line.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
...@@ -99,13 +100,22 @@ std::string WebEngineContentBrowserClient::GetProduct() { ...@@ -99,13 +100,22 @@ std::string WebEngineContentBrowserClient::GetProduct() {
} }
std::string WebEngineContentBrowserClient::GetUserAgent() { std::string WebEngineContentBrowserClient::GetUserAgent() {
std::string user_agent = content::BuildUserAgentFromProduct(GetProduct()); std::string user_agent;
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUseLegacyAndroidUserAgent)) {
user_agent =
content::BuildUserAgentFromOSAndProduct("Linux; Android", GetProduct());
} else {
user_agent = content::BuildUserAgentFromProduct(GetProduct());
}
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kUserAgentProductAndVersion)) { switches::kUserAgentProductAndVersion)) {
user_agent += user_agent +=
" " + base::CommandLine::ForCurrentProcess()->GetSwitchValueNative( " " + base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
switches::kUserAgentProductAndVersion); switches::kUserAgentProductAndVersion);
} }
return user_agent; return user_agent;
} }
......
...@@ -168,6 +168,7 @@ bool MaybeAddCommandLineArgsFromConfig(const base::Value& config, ...@@ -168,6 +168,7 @@ bool MaybeAddCommandLineArgsFromConfig(const base::Value& config,
switches::kForceMaxTextureSize, switches::kForceMaxTextureSize,
switches::kMaxDecodedImageSizeMb, switches::kMaxDecodedImageSizeMb,
switches::kRendererProcessLimit, switches::kRendererProcessLimit,
switches::kUseLegacyAndroidUserAgent,
switches::kWebglAntialiasingMode, switches::kWebglAntialiasingMode,
switches::kWebglMSAASampleCount, switches::kWebglMSAASampleCount,
}; };
......
...@@ -18,5 +18,6 @@ const char kUseLegacyMetricsService[] = "use-legacy-metrics-service"; ...@@ -18,5 +18,6 @@ const char kUseLegacyMetricsService[] = "use-legacy-metrics-service";
const char kCorsExemptHeaders[] = "cors-exempt-headers"; const char kCorsExemptHeaders[] = "cors-exempt-headers";
const char kEnableCastStreamingReceiver[] = "enable-cast-streaming-receiver"; const char kEnableCastStreamingReceiver[] = "enable-cast-streaming-receiver";
const char kCdmDataDirectory[] = "cdm-data-directory"; const char kCdmDataDirectory[] = "cdm-data-directory";
const char kUseLegacyAndroidUserAgent[] = "use-legacy-android-user-agent";
} // namespace switches } // namespace switches
...@@ -53,6 +53,9 @@ extern const char kEnableCastStreamingReceiver[]; ...@@ -53,6 +53,9 @@ extern const char kEnableCastStreamingReceiver[];
// Data directory to be used for CDM user data. // Data directory to be used for CDM user data.
extern const char kCdmDataDirectory[]; extern const char kCdmDataDirectory[];
// Enables reporting of an Android-like User Agent string.
extern const char kUseLegacyAndroidUserAgent[];
} // namespace switches } // namespace switches
#endif // FUCHSIA_ENGINE_SWITCHES_H_ #endif // FUCHSIA_ENGINE_SWITCHES_H_
...@@ -71,7 +71,7 @@ class WebEngineIntegrationMediaTest : public WebEngineIntegrationTest { ...@@ -71,7 +71,7 @@ class WebEngineIntegrationMediaTest : public WebEngineIntegrationTest {
std::unique_ptr<media::FakeAudioConsumerService> fake_audio_consumer_service_; std::unique_ptr<media::FakeAudioConsumerService> fake_audio_consumer_service_;
}; };
class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest { class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTestBase {
protected: protected:
GURL GetEchoUserAgentUrl() { GURL GetEchoUserAgentUrl() {
static std::string echo_user_agent_header_path = static std::string echo_user_agent_header_path =
...@@ -81,6 +81,8 @@ class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest { ...@@ -81,6 +81,8 @@ class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest {
}; };
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) { TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Create a Context with just an embedder product specified. // Create a Context with just an embedder product specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams(); fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct); create_params.set_user_agent_product(kValidUserAgentProduct);
...@@ -93,12 +95,19 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) { ...@@ -93,12 +95,19 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) {
ExecuteJavaScriptWithStringResult("document.body.innerText;"); ExecuteJavaScriptWithStringResult("document.body.innerText;");
EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos); EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos);
// The UserAgent header should also include the desktop-ish Fuchsia
// platform & OS information, by default.
constexpr char kFuchsiaUserAgentContents[] = "(X11; Fuchsia)";
EXPECT_TRUE(result.find(kFuchsiaUserAgentContents) != std::string::npos);
// Query & verify that the navigator.userAgent contains the product tag. // Query & verify that the navigator.userAgent contains the product tag.
result = ExecuteJavaScriptWithStringResult("navigator.userAgent;"); result = ExecuteJavaScriptWithStringResult("navigator.userAgent;");
EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos); EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos);
} }
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) { TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Create a Context with both product and version specified. // Create a Context with both product and version specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams(); fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct); create_params.set_user_agent_product(kValidUserAgentProduct);
...@@ -120,6 +129,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) { ...@@ -120,6 +129,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) {
} }
TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) { TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with an invalid embedder product tag. // Try to create a Context with an invalid embedder product tag.
fuchsia::web::CreateContextParams create_params = DefaultContextParams(); fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kInvalidUserAgentProduct); create_params.set_user_agent_product(kInvalidUserAgentProduct);
...@@ -127,6 +138,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) { ...@@ -127,6 +138,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) {
} }
TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) { TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with an embedder version but no product. // Try to create a Context with an embedder version but no product.
fuchsia::web::CreateContextParams create_params = DefaultContextParams(); fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_version(kValidUserAgentVersion); create_params.set_user_agent_version(kValidUserAgentVersion);
...@@ -134,6 +147,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) { ...@@ -134,6 +147,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) {
} }
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) { TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with valid product tag, but invalid version. // Try to create a Context with valid product tag, but invalid version.
fuchsia::web::CreateContextParams create_params = DefaultContextParams(); fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct); create_params.set_user_agent_product(kValidUserAgentProduct);
...@@ -141,6 +156,25 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) { ...@@ -141,6 +156,25 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) {
CreateContextAndExpectError(std::move(create_params), ZX_ERR_INVALID_ARGS); CreateContextAndExpectError(std::move(create_params), ZX_ERR_INVALID_ARGS);
} }
TEST_F(WebEngineIntegrationUserAgentTest, UseLegacyAndroidUserAgent) {
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
command_line.AppendSwitch("use-legacy-android-user-agent");
StartWebEngine(std::move(command_line));
// Create a Context with both product and version specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
CreateContextAndFrameAndLoadUrl(std::move(create_params),
GetEchoUserAgentUrl());
// Query & verify that the UserAgent header echoed into the document body
// contains the legacy Android data.
constexpr char kLegacyAndroidUserAgentContents[] = "(Linux; Android)";
std::string result =
ExecuteJavaScriptWithStringResult("document.body.innerText;");
EXPECT_TRUE(result.find(kLegacyAndroidUserAgentContents) !=
std::string::npos);
}
// Check that if the CreateContextParams has |remote_debugging_port| set then: // Check that if the CreateContextParams has |remote_debugging_port| set then:
// - DevTools becomes available when the first debuggable Frame is created. // - DevTools becomes available when the first debuggable Frame is created.
// - DevTools closes when the last debuggable Frame is closed. // - DevTools closes when the last debuggable Frame is closed.
......
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