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 @@
#include <stdint.h>
#include "base/feature_list.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
......
......@@ -8,6 +8,7 @@
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "components/version_info/version_info.h"
......@@ -99,13 +100,22 @@ std::string WebEngineContentBrowserClient::GetProduct() {
}
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(
switches::kUserAgentProductAndVersion)) {
user_agent +=
" " + base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
switches::kUserAgentProductAndVersion);
}
return user_agent;
}
......
......@@ -168,6 +168,7 @@ bool MaybeAddCommandLineArgsFromConfig(const base::Value& config,
switches::kForceMaxTextureSize,
switches::kMaxDecodedImageSizeMb,
switches::kRendererProcessLimit,
switches::kUseLegacyAndroidUserAgent,
switches::kWebglAntialiasingMode,
switches::kWebglMSAASampleCount,
};
......
......@@ -18,5 +18,6 @@ const char kUseLegacyMetricsService[] = "use-legacy-metrics-service";
const char kCorsExemptHeaders[] = "cors-exempt-headers";
const char kEnableCastStreamingReceiver[] = "enable-cast-streaming-receiver";
const char kCdmDataDirectory[] = "cdm-data-directory";
const char kUseLegacyAndroidUserAgent[] = "use-legacy-android-user-agent";
} // namespace switches
......@@ -53,6 +53,9 @@ extern const char kEnableCastStreamingReceiver[];
// Data directory to be used for CDM user data.
extern const char kCdmDataDirectory[];
// Enables reporting of an Android-like User Agent string.
extern const char kUseLegacyAndroidUserAgent[];
} // namespace switches
#endif // FUCHSIA_ENGINE_SWITCHES_H_
......@@ -71,7 +71,7 @@ class WebEngineIntegrationMediaTest : public WebEngineIntegrationTest {
std::unique_ptr<media::FakeAudioConsumerService> fake_audio_consumer_service_;
};
class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest {
class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTestBase {
protected:
GURL GetEchoUserAgentUrl() {
static std::string echo_user_agent_header_path =
......@@ -81,6 +81,8 @@ class WebEngineIntegrationUserAgentTest : public WebEngineIntegrationTest {
};
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Create a Context with just an embedder product specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
......@@ -93,12 +95,19 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductOnly) {
ExecuteJavaScriptWithStringResult("document.body.innerText;");
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.
result = ExecuteJavaScriptWithStringResult("navigator.userAgent;");
EXPECT_TRUE(result.find(kValidUserAgentProduct) != std::string::npos);
}
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Create a Context with both product and version specified.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
......@@ -120,6 +129,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndVersion) {
}
TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with an invalid embedder product tag.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kInvalidUserAgentProduct);
......@@ -127,6 +138,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, InvalidProduct) {
}
TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with an embedder version but no product.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_version(kValidUserAgentVersion);
......@@ -134,6 +147,8 @@ TEST_F(WebEngineIntegrationUserAgentTest, VersionOnly) {
}
TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) {
StartWebEngine(base::CommandLine(base::CommandLine::NO_PROGRAM));
// Try to create a Context with valid product tag, but invalid version.
fuchsia::web::CreateContextParams create_params = DefaultContextParams();
create_params.set_user_agent_product(kValidUserAgentProduct);
......@@ -141,6 +156,25 @@ TEST_F(WebEngineIntegrationUserAgentTest, ValidProductAndInvalidVersion) {
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:
// - DevTools becomes available when the first debuggable Frame is created.
// - 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