Commit 65f29586 authored by Alex Clarke's avatar Alex Clarke Committed by Commit Bot

Add Emulation.setNavigatorPlatform

Sometimes in headless we need to emulate mobile devices including changing
navigator.platform. This DevTools command achieves that.

Bug: 
Change-Id: Id305db9fa595d527225c2e19bb269cb60f29110f
Reviewed-on: https://chromium-review.googlesource.com/603715
Commit-Queue: Alex Clarke <alexclarke@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501225}
parent 621a1da9
Tests that navigator.platform can be overridden.
navigator.platform = TestPlatform
(async function(testRunner) {
var {page, session, dp} = await testRunner.startBlank('Tests that navigator.platform can be overridden.');
await dp.Emulation.setNavigatorOverrides({ platform: 'TestPlatform' });
var result = await dp.Runtime.evaluate({ expression: 'navigator.platform' });
testRunner.log('navigator.platform = ' + result.result.result.value);
testRunner.completeTest();
})
......@@ -59,6 +59,14 @@ String Navigator::vendorSub() const {
return "";
}
String Navigator::platform() const {
if (GetFrame() &&
!GetFrame()->GetSettings()->GetNavigatorPlatformOverride().IsEmpty()) {
return GetFrame()->GetSettings()->GetNavigatorPlatformOverride();
}
return NavigatorID::platform();
}
String Navigator::userAgent() const {
// If the frame is already detached it no longer has a meaningful useragent.
if (!GetFrame() || !GetFrame()->GetPage())
......
......@@ -57,6 +57,7 @@ class CORE_EXPORT Navigator final : public GarbageCollected<Navigator>,
String vendor() const;
String vendorSub() const;
String platform() const override;
String userAgent() const override;
// NavigatorLanguage
......
......@@ -55,7 +55,7 @@ String NavigatorID::appVersion() {
return agent.Substring(agent.find('/') + 1);
}
String NavigatorID::platform() {
String NavigatorID::platform() const {
#if defined(OS_MACOSX)
// Match Safari and Mozilla on Mac x86.
return "MacIntel";
......
......@@ -41,7 +41,7 @@ class CORE_EXPORT NavigatorID {
String appCodeName();
String appName();
String appVersion();
String platform();
virtual String platform() const;
String product();
virtual String userAgent() const = 0;
};
......
......@@ -971,5 +971,9 @@
name: "forceDisplayList2dCanvasEnabled",
initial: false,
},
{
name: "navigatorPlatformOverride",
type: "String",
},
],
}
......@@ -31,6 +31,7 @@ static const char kMaxTouchPoints[] = "maxTouchPoints";
static const char kEmulatedMedia[] = "emulatedMedia";
static const char kDefaultBackgroundColorOverrideRGBA[] =
"defaultBackgroundColorOverrideRGBA";
static const char kNavigatorPlatform[] = "navigatorPlatform";
}
InspectorEmulationAgent* InspectorEmulationAgent::Create(
......@@ -72,6 +73,10 @@ void InspectorEmulationAgent::Restore() {
Maybe<protocol::DOM::RGBA>(std::move(rgba)));
}
}
String navigator_platform;
state_->getString(EmulationAgentState::kNavigatorPlatform,
&navigator_platform);
setNavigatorOverrides(navigator_platform);
}
Response InspectorEmulationAgent::disable() {
......@@ -84,6 +89,7 @@ Response InspectorEmulationAgent::disable() {
web_local_frame_->View()->Scheduler()->RemoveVirtualTimeObserver(this);
virtual_time_observer_registered_ = false;
}
setNavigatorOverrides(String());
return Response::OK();
}
......@@ -160,6 +166,14 @@ Response InspectorEmulationAgent::setVirtualTimePolicy(const String& policy,
return Response::OK();
}
Response InspectorEmulationAgent::setNavigatorOverrides(
const String& platform) {
state_->setString(EmulationAgentState::kNavigatorPlatform, platform);
GetWebViewImpl()->GetPage()->GetSettings().SetNavigatorPlatformOverride(
platform);
return Response::OK();
}
void InspectorEmulationAgent::VirtualTimeBudgetExpired() {
web_local_frame_->View()->Scheduler()->SetVirtualTimePolicy(
WebViewScheduler::VirtualTimePolicy::PAUSE);
......
......@@ -50,6 +50,7 @@ class CORE_EXPORT InspectorEmulationAgent final
protocol::Response setVirtualTimePolicy(
const String& policy,
protocol::Maybe<int> virtual_time_budget_ms) override;
protocol::Response setNavigatorOverrides(const String& platform) override;
protocol::Response setDefaultBackgroundColorOverride(
protocol::Maybe<protocol::DOM::RGBA>) override;
......
......@@ -1081,6 +1081,14 @@
],
"experimental": true
},
{
"name": "setNavigatorOverrides",
"description": "Overrides value returned by the javascript navigator object.",
"parameters": [
{ "name": "platform", "type": "string", "description": "The platform navigator.platform should return." }
],
"experimental": true
},
{
"name": "setDefaultBackgroundColorOverride",
"description": "Sets or clears an override of the default background color of the frame. This override is used if the content does not specify one.",
......
......@@ -90,7 +90,7 @@
{
"domain": "Emulation",
"include": ["forceViewport", "resetViewport", "resetPageScaleFactor", "setPageScaleFactor", "setScriptExecutionDisabled", "setTouchEmulationEnabled",
"setEmulatedMedia", "setCPUThrottlingRate", "setVirtualTimePolicy", "setDefaultBackgroundColorOverride"],
"setEmulatedMedia", "setCPUThrottlingRate", "setVirtualTimePolicy", "setNavigatorOverrides", "setDefaultBackgroundColorOverride"],
"include_events": ["virtualTimeBudgetExpired", "virtualTimePaused"]
},
{
......
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