Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(emulation): reject unsupported device metrics modes

ldm08dfd1015b06a
diff --git a/README.md b/README.md
index 718c6dcf8..a90d7fe3a 100644
--- a/README.md
+++ b/README.md
@@ -361,6 +361,12 @@ Its current intentional boundaries include:
at or below 1 disable throttling; non-positive latency and throughput values
disable network throttling. Unsupported combinations are rejected before
changing offline state.
+- Device metrics support desktop geometry and capture transforms. `mobile:true`
+ returns an unsupported error; mobile viewport-meta processing, text autosizing
+ and overlay scrollbars are not implemented. Display features, device posture,
+ forced overlay scrollbars, orientation-lock emulation and explicit viewport-meta
+ processing are also rejected before changing existing metrics. Default scrollbar
+ and viewport-meta settings, and disabling orientation-lock emulation, are accepted.
- `--layout` supports software screenshots and raster-backed CDP PDF
generation, but not every Chrome screenshot or print mode is implemented.
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rs
index 123d5be47..bd35fd188 100644
--- a/moli-protocol/src/domains/emulation.rs
+++ b/moli-protocol/src/domains/emulation.rs
@@ -920,13 +920,6 @@ fn start_device_metrics_override_command(
));
}
};
- if conn.browser_context.is_none()
- && conn
- .target_owner_identity_for_session(cmd.session_id)
- .is_none()
- {
- return EmulationCommandTaskStep::Complete(CommandOutputPlan::result(json!({})));
- }
let owner = CommandOwnerScope::capture(conn, cmd.session_id);
let previous = conn.target_session_owner_emulated_device_metrics_for_owner(&owner);
let mut base = conn
@@ -954,6 +947,13 @@ fn start_device_metrics_override_command(
));
}
};
+ if conn.browser_context.is_none()
+ && conn
+ .target_owner_identity_for_session(cmd.session_id)
+ .is_none()
+ {
+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::result(json!({})));
+ }
match start_apply_device_metrics(conn, cmd.id, metrics, owner) {
Ok(Some(pending)) => EmulationCommandTaskStep::Pending(pending),
Ok(None) => EmulationCommandTaskStep::Complete(CommandOutputPlan::success()),
diff --git a/moli-protocol/src/domains/emulation/device.rs b/moli-protocol/src/domains/emulation/device.rs
index 480d5b979..9bdc6ebe1 100644
--- a/moli-protocol/src/domains/emulation/device.rs
+++ b/moli-protocol/src/domains/emulation/device.rs
@@ -1,10 +1,11 @@
+use super::params::{ScrollbarType, SetDeviceMetricsOverrideParams, ViewportMeta};
use crate::conn::EmulatedDeviceMetrics;
/// CDP zero dimensions remove the layout override for that axis. The actual
/// visible widget is resized only when both axes are supplied; it can differ
/// from the previous emulated layout after a single-axis command.
pub(super) fn metrics_from_cdp(
- params: super::params::SetDeviceMetricsOverrideParams,
+ params: SetDeviceMetricsOverrideParams,
previous: Option<&EmulatedDeviceMetrics>,
base: &crate::conn::EmulatedViewportSurface,
) -> Result<EmulatedDeviceMetrics, crate::devtools_runtime::DevToolsError> {
@@ -31,7 +32,6 @@ pub(super) fn metrics_from_cdp(
}
let (window_x, window_y) = match (params.position_x, params.position_y) {
(Some(x), Some(y)) => (x as i32, y as i32),
- _ if params.mobile => (0, 0),
_ => (base.window_x, base.window_y),
};
let screen_orientation = if let Some(orientation) = params.screen_orientation {
@@ -61,6 +61,33 @@ pub(super) fn metrics_from_cdp(
{
return Err(invalid());
}
+ // Mobile emulation also needs viewport-meta processing, autosizing and
+ // overlay scrollbars. Reject it instead of publishing only its geometry.
+ // Default/disabled options below retain the native desktop behavior.
+ for (unsupported, setting) in [
+ (params.mobile, "mobile=true"),
+ (params.display_feature.is_some(), "displayFeature"),
+ (params.device_posture.is_some(), "devicePosture"),
+ (
+ matches!(params.scrollbar_type, Some(ScrollbarType::Overlay)),
+ "scrollbarType=overlay",
+ ),
+ (
+ params.screen_orientation_lock_emulation == Some(true),
+ "screenOrientationLockEmulation=true",
+ ),
+ (
+ matches!(params.viewport_meta, Some(ViewportMeta::Enable)),
+ "viewportMeta=enable",
+ ),
+ ] {
+ if unsupported {
+ return Err(DevToolsError::new(
+ DevToolsErrorKind::Unsupported,
+ format!("Emulation.setDeviceMetricsOverride does not support {setting}."),
+ ));
+ }
+ }
let device_scale_factor = if params.device_scale_factor == 0.0 {
base.device_pixel_ratio
} else {
@@ -115,8 +142,6 @@ pub(super) fn metrics_from_cdp(
};
let (screen_width, screen_height) = if screen_width != 0 && screen_height != 0 {
(screen_width, screen_height)
- } else if params.mobile {
- (width, height)
} else {
(base.screen_width, base.screen_height)
};
@@ -130,16 +155,8 @@ pub(super) fn metrics_from_cdp(
native_device_pixel_ratio: base.device_pixel_ratio,
viewport,
}),
- outer_width: if params.mobile {
- width
- } else {
- base.outer_width
- },
- outer_height: if params.mobile {
- height
- } else {
- base.outer_height
- },
+ outer_width: base.outer_width,
+ outer_height: base.outer_height,
device_scale_factor,
screen_width,
screen_height,
diff --git a/moli-protocol/src/domains/emulation/params.rs b/moli-protocol/src/domains/emulation/params.rs
index 1e3051d1e..2310e0de7 100644
--- a/moli-protocol/src/domains/emulation/params.rs
+++ b/moli-protocol/src/domains/emulation/params.rs
@@ -1,9 +1,52 @@
pub(super) use chromiumoxide_cdp::cdp::browser_protocol::emulation::{
ClearGeolocationOverrideParams, ClearIdleOverrideParams, SetAutomationOverrideParams,
SetCpuThrottlingRateParams, SetDataSaverOverrideParams,
- SetDefaultBackgroundColorOverrideParams, SetDeviceMetricsOverrideParams,
- SetEmitTouchEventsForMouseParams, SetEmulatedMediaParams, SetFocusEmulationEnabledParams,
- SetGeolocationOverrideParams, SetHardwareConcurrencyOverrideParams, SetIdleOverrideParams,
- SetLocaleOverrideParams, SetScriptExecutionDisabledParams, SetTimezoneOverrideParams,
- SetTouchEmulationEnabledParams,
+ SetDefaultBackgroundColorOverrideParams, SetEmitTouchEventsForMouseParams,
+ SetEmulatedMediaParams, SetFocusEmulationEnabledParams, SetGeolocationOverrideParams,
+ SetHardwareConcurrencyOverrideParams, SetIdleOverrideParams, SetLocaleOverrideParams,
+ SetScriptExecutionDisabledParams, SetTimezoneOverrideParams, SetTouchEmulationEnabledParams,
};
+
+use chromiumoxide_cdp::cdp::browser_protocol::{
+ emulation::{DevicePosture, DisplayFeature, ScreenOrientation},
+ page::Viewport,
+};
+
+// The generated bindings omit newer optional fields on this command. Keep
+// them at the wire boundary so we can reject unsupported modes explicitly.
+// Unknown extension fields remain ignorable, as in the other CDP commands.
+#[derive(serde::Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub(super) struct SetDeviceMetricsOverrideParams {
+ pub width: i64,
+ pub height: i64,
+ pub device_scale_factor: f64,
+ pub mobile: bool,
+ pub scale: Option<f64>,
+ pub screen_width: Option<i64>,
+ pub screen_height: Option<i64>,
+ pub position_x: Option<i64>,
+ pub position_y: Option<i64>,
+ pub dont_set_visible_size: Option<bool>,
+ pub screen_orientation: Option<ScreenOrientation>,
+ pub viewport: Option<Viewport>,
+ pub display_feature: Option<DisplayFeature>,
+ pub device_posture: Option<DevicePosture>,
+ pub scrollbar_type: Option<ScrollbarType>,
+ pub screen_orientation_lock_emulation: Option<bool>,
+ pub viewport_meta: Option<ViewportMeta>,
+}
+
+#[derive(serde::Deserialize)]
+#[serde(rename_all = "lowercase")]
+pub(super) enum ScrollbarType {
+ Default,
+ Overlay,
+}
+
+#[derive(serde::Deserialize)]
+#[serde(rename_all = "lowercase")]
+pub(super) enum ViewportMeta {
+ Default,
+ Enable,
+}
diff --git a/moli-protocol/src/domains/emulation/tests/device_geometry.rs b/moli-protocol/src/domains/emulation/tests/device_geometry.rs
index b686b3673..cc7be8526 100644
--- a/moli-protocol/src/domains/emulation/tests/device_geometry.rs
+++ b/moli-protocol/src/domains/emulation/tests/device_geometry.rs
@@ -27,6 +27,127 @@ async fn set_metrics(ctx: &mut TestContext, extra: serde_json::Value) {
.await;
}
+fn unsupported_modes() -> [(&'static str, serde_json::Value); 7] {
+ [
+ ("mobile=true", json!({"mobile": true})),
+ (
+ "displayFeature",
+ json!({"displayFeature": {
+ "orientation": "vertical", "offset": 200, "maskLength": 0
+ }}),
+ ),
+ (
+ "devicePosture",
+ json!({"devicePosture": {"type": "folded"}}),
+ ),
+ (
+ "devicePosture",
+ json!({"devicePosture": {"type": "continuous"}}),
+ ),
+ ("scrollbarType=overlay", json!({"scrollbarType": "overlay"})),
+ (
+ "screenOrientationLockEmulation=true",
+ json!({"screenOrientationLockEmulation": true}),
+ ),
+ ("viewportMeta=enable", json!({"viewportMeta": "enable"})),
+ ]
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn device_metrics_unsupported_modes_preserve_live_and_saved_state() {
+ let mut ctx = TestContext::new();
+ load_session_page_for_pending_emulation_test(&mut ctx).await;
+ set_metrics(&mut ctx, json!({})).await;
+ let snapshot = "[innerWidth, innerHeight, devicePixelRatio, screenX, screenY, screen.orientation.type, screen.orientation.angle]";
+ let before = evaluate(&mut ctx, snapshot).await;
+ let owner = crate::conn::CommandOwnerScope::for_session("SID-1");
+ let saved = ctx
+ .conn
+ .target_session_owner_emulated_device_metrics_for_owner(&owner);
+
+ for (setting, extra) in unsupported_modes() {
+ let mut params = metrics(extra);
+ params["width"] = json!(390);
+ params["height"] = json!(844);
+ params["deviceScaleFactor"] = json!(3);
+ params["screenOrientation"] = json!({"type": "portraitPrimary", "angle": 90});
+ ctx.process_async(json!({
+ "id": 88410, "sessionId": "SID-1",
+ "method": "Emulation.setDeviceMetricsOverride", "params": params
+ }))
+ .await;
+ ctx.expect_error(
+ 88410,
+ -32000,
+ &format!("Emulation.setDeviceMetricsOverride does not support {setting}."),
+ );
+ assert_eq!(
+ ctx.conn
+ .target_session_owner_emulated_device_metrics_for_owner(&owner),
+ saved
+ );
+ assert_eq!(evaluate(&mut ctx, snapshot).await, before, "{setting}");
+ }
+
+ ctx.install_navigation_fixture_for_session_owner(
+ "data:text/html,<meta name=viewport content='width=device-width'><body>next",
+ Some("SID-1"),
+ )
+ .await;
+ assert_eq!(
+ evaluate(&mut ctx, snapshot).await,
+ before,
+ "rejected settings must not become the next document's bootstrap state"
+ );
+
+ set_metrics(
+ &mut ctx,
+ json!({
+ "width": 300, "height": 250, "scrollbarType": "default",
+ "screenOrientationLockEmulation": false, "viewportMeta": "default",
+ "unknownClientExtension": {"value": 1}
+ }),
+ )
+ .await;
+ assert_eq!(
+ evaluate(&mut ctx, "[innerWidth, innerHeight]").await,
+ json!([300, 250])
+ );
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn device_metrics_validate_known_options_even_without_a_target() {
+ let mut ctx = TestContext::new();
+ assert!(ctx.conn.browser_context.is_none());
+ for (setting, extra) in unsupported_modes() {
+ ctx.process_async(json!({
+ "id": 88411, "method": "Emulation.setDeviceMetricsOverride", "params": metrics(extra)
+ }))
+ .await;
+ ctx.expect_error(
+ 88411,
+ -32000,
+ &format!("Emulation.setDeviceMetricsOverride does not support {setting}."),
+ );
+ }
+ for invalid in [
+ json!({"mobile": "true"}),
+ json!({"displayFeature": {"orientation": "vertical", "offset": 200}}),
+ json!({"displayFeature": {"orientation": "diagonal", "offset": 200, "maskLength": 0}}),
+ json!({"devicePosture": {"type": "unknown"}}),
+ json!({"scrollbarType": "unknown"}),
+ json!({"screenOrientationLockEmulation": "true"}),
+ json!({"viewportMeta": "unknown"}),
+ json!({"width": -1}),
+ ] {
+ ctx.process_async(json!({
+ "id": 88412, "method": "Emulation.setDeviceMetricsOverride", "params": metrics(invalid)
+ }))
+ .await;
+ ctx.expect_error(88412, -32602, "InvalidParams");
+ }
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn screen_orientation_and_position_update_held_objects_and_clear() {
let mut ctx = TestContext::new();
diff --git a/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/emulation_input_storage.rs b/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/emulation_input_storage.rs
index ca8792afc..44a0fe3ab 100644
--- a/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/emulation_input_storage.rs
+++ b/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/emulation_input_storage.rs
@@ -269,7 +269,7 @@ async fn rust_cdp_chromium_import_emulation_device_metrics_hot_apply_window_surf
&mut ctx,
&attached.session_id,
117_107,
- "JSON.stringify({innerWidth, innerHeight, devicePixelRatio})",
+ "JSON.stringify({innerWidth, innerHeight, outerWidth, outerHeight, devicePixelRatio})",
)
.await;
let original_payload: Value = serde_json::from_str(
@@ -287,7 +287,7 @@ async fn rust_cdp_chromium_import_emulation_device_metrics_hot_apply_window_surf
"width": 375,
"height": 667,
"deviceScaleFactor": 2,
- "mobile": true,
+ "mobile": false,
"screenWidth": 400,
"screenHeight": 700
}
@@ -318,8 +318,8 @@ async fn rust_cdp_chromium_import_emulation_device_metrics_hot_apply_window_surf
.expect("viewport payload json");
assert_eq!(payload["innerWidth"], 375);
assert_eq!(payload["innerHeight"], 667);
- assert_eq!(payload["outerWidth"], 375);
- assert_eq!(payload["outerHeight"], 667);
+ assert_eq!(payload["outerWidth"], original_payload["outerWidth"]);
+ assert_eq!(payload["outerHeight"], original_payload["outerHeight"]);
assert_eq!(payload["devicePixelRatio"], 2);
assert_eq!(payload["screenWidth"], 400);
assert_eq!(payload["screenHeight"], 700);
@@ -335,7 +335,7 @@ async fn rust_cdp_chromium_import_emulation_device_metrics_hot_apply_window_surf
&mut ctx,
&attached.session_id,
117_111,
- "JSON.stringify({innerWidth, innerHeight, devicePixelRatio})",
+ "JSON.stringify({innerWidth, innerHeight, outerWidth, outerHeight, devicePixelRatio})",
)
.await;
let cleared_payload: Value = serde_json::from_str(