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): preserve device metric reset semantics

ldm04bef2b6dfdb3
diff --git a/moli-protocol/src/conn/state/emulation.rs b/moli-protocol/src/conn/state/emulation.rs
index f494e8763..a2824eb3a 100644
--- a/moli-protocol/src/conn/state/emulation.rs
+++ b/moli-protocol/src/conn/state/emulation.rs
@@ -4,9 +4,15 @@ use serde_json::json;
pub struct EmulatedDeviceMetrics {
pub width: u32,
pub height: u32,
+ /// Actual visible widget dimensions, retained across single-axis overrides.
+ pub visible_width: u32,
+ pub visible_height: u32,
+ pub outer_width: u32,
+ pub outer_height: u32,
pub device_scale_factor: f64,
pub screen_width: u32,
pub screen_height: u32,
+ pub screen_avail_height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -24,16 +30,6 @@ impl EmulatedNetworkConditions {
}
}
-const DEFAULT_VIEWPORT_WIDTH: u32 = 1920;
-const DEFAULT_VIEWPORT_HEIGHT: u32 = 1080;
-const DEFAULT_SCREEN_WIDTH: u32 = 1920;
-const DEFAULT_SCREEN_HEIGHT: u32 = 1080;
-const DEFAULT_SCREEN_AVAIL_HEIGHT: u32 = 1040;
-
-fn screen_avail_height_from_screen_height(screen_height: u32) -> u32 {
- screen_height.min(DEFAULT_SCREEN_AVAIL_HEIGHT)
-}
-
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct EmulatedViewportSurface {
pub inner_width: u32,
@@ -49,16 +45,17 @@ pub(crate) struct EmulatedViewportSurface {
impl Default for EmulatedViewportSurface {
fn default() -> Self {
+ let profile = moli_browser_profile::DEFAULT_WINDOW_SURFACE_PROFILE;
Self {
- inner_width: DEFAULT_VIEWPORT_WIDTH,
- inner_height: DEFAULT_VIEWPORT_HEIGHT,
- outer_width: DEFAULT_VIEWPORT_WIDTH,
- outer_height: DEFAULT_VIEWPORT_HEIGHT,
- device_pixel_ratio: 1.0,
- screen_width: DEFAULT_SCREEN_WIDTH,
- screen_height: DEFAULT_SCREEN_HEIGHT,
- screen_avail_width: DEFAULT_SCREEN_WIDTH,
- screen_avail_height: screen_avail_height_from_screen_height(DEFAULT_SCREEN_HEIGHT),
+ inner_width: profile.inner_width as u32,
+ inner_height: profile.inner_height as u32,
+ outer_width: profile.inner_width as u32,
+ outer_height: profile.inner_height as u32,
+ device_pixel_ratio: profile.device_pixel_ratio,
+ screen_width: profile.screen_width as u32,
+ screen_height: profile.screen_height as u32,
+ screen_avail_width: profile.screen_avail_width as u32,
+ screen_avail_height: profile.screen_avail_height as u32,
}
}
}
@@ -100,7 +97,7 @@ impl EmulatedViewportSurface {
impl EmulatedDeviceMetrics {
pub(crate) fn screen_avail_height(&self) -> u32 {
- screen_avail_height_from_screen_height(self.screen_height)
+ self.screen_avail_height
}
pub(crate) fn device_pixel_ratio(&self) -> f64 {
@@ -115,8 +112,8 @@ impl EmulatedDeviceMetrics {
EmulatedViewportSurface {
inner_width: self.width,
inner_height: self.height,
- outer_width: self.width,
- outer_height: self.height,
+ outer_width: self.outer_width,
+ outer_height: self.outer_height,
device_pixel_ratio: self.device_pixel_ratio(),
screen_width: self.screen_width,
screen_height: self.screen_height,
@@ -386,22 +383,22 @@ impl EffectiveTargetEmulationState {
mod tests {
use super::{
EffectiveTargetEmulationState, EmulatedDeviceMetrics, EmulatedViewportSurface,
- screen_avail_height_from_screen_height, viewport_surface_install_script,
+ viewport_surface_install_script,
};
- #[test]
- fn screen_available_height_never_exceeds_zero_screen_height() {
- assert_eq!(screen_avail_height_from_screen_height(0), 0);
- }
-
#[test]
fn device_pixel_ratio_normalizes_non_positive_and_non_finite_values() {
let mut metrics = EmulatedDeviceMetrics {
width: 800,
height: 600,
+ visible_width: 800,
+ visible_height: 600,
+ outer_width: 800,
+ outer_height: 600,
device_scale_factor: 2.0,
screen_width: 800,
screen_height: 600,
+ screen_avail_height: 600,
};
assert_eq!(metrics.device_pixel_ratio(), 2.0);
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rs
index 8a0838c3f..cb91561fe 100644
--- a/moli-protocol/src/domains/emulation.rs
+++ b/moli-protocol/src/domains/emulation.rs
@@ -838,47 +838,29 @@ fn start_device_metrics_override_command(
{
return EmulationCommandTaskStep::Complete(CommandOutputPlan::result(json!({})));
}
- let (Ok(width), Ok(height)) = (u32::try_from(params.width), u32::try_from(params.height))
- else {
- return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(
- -32602,
- "InvalidParams",
- ));
- };
- let screen_width = match params.screen_width {
- Some(value) => match value.try_into() {
- Ok(value) => value,
- Err(_) => {
- return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(
- -32602,
- "InvalidParams",
- ));
- }
- },
- None => width,
- };
- let screen_height = match params.screen_height {
- Some(value) => match value.try_into() {
- Ok(value) => value,
- Err(_) => {
- return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(
- -32602,
- "InvalidParams",
- ));
- }
- },
- None => height,
- };
- let command = DevToolsSetViewportCommand {
- context: cmd.devtools_command_context(None::<&str>, None::<&str>),
- browser_context_ids: Vec::new(),
- viewport: DevToolsViewportSetting::Dimensions { width, height },
- device_pixel_ratio: DevToolsDevicePixelRatioSetting::Scale(params.device_scale_factor),
- screen_width: Some(screen_width),
- screen_height: Some(screen_height),
- };
let owner = CommandOwnerScope::capture(conn, cmd.session_id);
- match start_devtools_set_viewport_command(conn, cmd.id, command, owner) {
+ let previous = conn.target_session_owner_emulated_device_metrics_for_owner(&owner);
+ let mut base = EmulatedViewportSurface::default();
+ if let Some(state) = conn.target_owner_state_for_owner(&owner) {
+ let geometry = state.window_surface_geometry;
+ if geometry.width != 0 {
+ base.inner_width = geometry.width;
+ base.outer_width = geometry.width;
+ }
+ if geometry.height != 0 {
+ base.inner_height = geometry.height;
+ base.outer_height = geometry.height;
+ }
+ }
+ let metrics = match device::metrics_from_cdp(params, previous.as_ref(), &base) {
+ Ok(metrics) => metrics,
+ Err(error) => {
+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::from_devtools_error(
+ error,
+ ));
+ }
+ };
+ match start_apply_device_metrics(conn, cmd.id, metrics, owner) {
Ok(Some(pending)) => EmulationCommandTaskStep::Pending(pending),
Ok(None) => EmulationCommandTaskStep::Complete(CommandOutputPlan::success()),
Err(error) => {
@@ -966,6 +948,15 @@ fn start_devtools_set_viewport_command(
return Ok(None);
}
let metrics = set_viewport_metrics_from_command(conn, &owner_scope, &command)?;
+ start_apply_device_metrics(conn, command_id, metrics, owner_scope)
+}
+
+fn start_apply_device_metrics(
+ conn: &mut CdpConnection,
+ command_id: Option<u64>,
+ metrics: EmulatedDeviceMetrics,
+ owner_scope: CommandOwnerScope,
+) -> Result<Option<PendingEmulationCommandDispatch>, DevToolsError> {
let had_existing_device_metrics = conn
.target_session_owner_emulated_device_metrics_for_owner(&owner_scope)
.is_some();
@@ -1052,9 +1043,16 @@ fn set_viewport_metrics_from_current(
Ok(EmulatedDeviceMetrics {
width,
height,
+ visible_width: width,
+ visible_height: height,
+ outer_width: width,
+ outer_height: height,
device_scale_factor,
screen_width: command.screen_width.unwrap_or(width),
screen_height: command.screen_height.unwrap_or(height),
+ // Preserve the existing WebDriver/BiDi headless work area. CDP device
+ // emulation supplies its own available screen dimensions instead.
+ screen_avail_height: command.screen_height.unwrap_or(height).min(1040),
})
}
diff --git a/moli-protocol/src/domains/emulation/device.rs b/moli-protocol/src/domains/emulation/device.rs
index 016c98149..2812c099e 100644
--- a/moli-protocol/src/domains/emulation/device.rs
+++ b/moli-protocol/src/domains/emulation/device.rs
@@ -52,6 +52,85 @@ pub(super) const LIVE_DEVICE_METRICS_CLEAR_SCRIPT: &str = r#"
})()
"#;
+/// 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,
+ previous: Option<&EmulatedDeviceMetrics>,
+ base: &crate::conn::EmulatedViewportSurface,
+) -> Result<EmulatedDeviceMetrics, crate::devtools_runtime::DevToolsError> {
+ use crate::devtools_runtime::{DevToolsError, DevToolsErrorKind};
+ let invalid = || DevToolsError::new(DevToolsErrorKind::InvalidArgument, "InvalidParams");
+ let size = |value: i64| {
+ if (0..=10_000_000).contains(&value) {
+ Ok(value as u32)
+ } else {
+ Err(invalid())
+ }
+ };
+ let width = size(params.width)?;
+ let height = size(params.height)?;
+ let screen_width = size(params.screen_width.unwrap_or(0))?;
+ let screen_height = size(params.screen_height.unwrap_or(0))?;
+ let scale = params.scale.unwrap_or(1.0);
+ if !params.device_scale_factor.is_finite()
+ || params.device_scale_factor < 0.0
+ || !scale.is_finite()
+ || scale <= 0.0
+ || scale > 10.0
+ {
+ return Err(invalid());
+ }
+ let mut visible_width = previous.map_or(base.inner_width, |m| m.visible_width);
+ let mut visible_height = previous.map_or(base.inner_height, |m| m.visible_height);
+ if width != 0 && height != 0 && !params.dont_set_visible_size.unwrap_or(false) {
+ visible_width = width;
+ visible_height = height;
+ }
+ let width = if width == 0 {
+ (f64::from(visible_width) / scale).round() as u32
+ } else {
+ width
+ };
+ let height = if height == 0 {
+ (f64::from(visible_height) / scale).round() as u32
+ } else {
+ height
+ };
+ 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)
+ };
+ Ok(EmulatedDeviceMetrics {
+ width,
+ height,
+ visible_width,
+ visible_height,
+ outer_width: if params.mobile {
+ width
+ } else {
+ base.outer_width
+ },
+ outer_height: if params.mobile {
+ height
+ } else {
+ base.outer_height
+ },
+ device_scale_factor: if params.device_scale_factor == 0.0 {
+ base.device_pixel_ratio
+ } else {
+ params.device_scale_factor
+ },
+ screen_width,
+ screen_height,
+ screen_avail_height: screen_height,
+ })
+}
+
#[cfg(test)]
mod tests {
use super::LIVE_DEVICE_METRICS_CLEAR_SCRIPT;
diff --git a/moli-protocol/src/domains/emulation/tests/native_navigator.rs b/moli-protocol/src/domains/emulation/tests/native_navigator.rs
index a88ae2490..18ecc2f59 100644
--- a/moli-protocol/src/domains/emulation/tests/native_navigator.rs
+++ b/moli-protocol/src/domains/emulation/tests/native_navigator.rs
@@ -333,3 +333,80 @@ async fn max_touch_points_updates_native_getters_and_rejects_invalid_counts_atom
.await;
assert_eq!(evaluate(&mut ctx, "initialTouch").await, json!(5));
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn device_metrics_zero_axes_use_visible_size_and_clear_restores_native_defaults() {
+ let mut ctx = setup().await;
+ let snapshot = "[innerWidth, innerHeight, devicePixelRatio, outerWidth, outerHeight, screen.width, screen.height]";
+ let baseline = evaluate(&mut ctx, snapshot).await;
+ let base = baseline.as_array().unwrap();
+ for (width, height, dpr, expected_width, expected_height) in [
+ (640, 480, 2, 640, 480),
+ (1000, 0, 2, 1000, 480),
+ (0, 0, 0, 640, 480),
+ (0, 200, 0, 640, 200),
+ (0, 0, 0, 640, 480),
+ ] {
+ expect_session_command_result(
+ &mut ctx,
+ 88001,
+ "SID-1",
+ "Emulation.setDeviceMetricsOverride",
+ json!({"width": width,"height": height,"deviceScaleFactor": dpr,"mobile": false}),
+ )
+ .await;
+ assert_eq!(
+ evaluate(&mut ctx, snapshot).await,
+ json!([
+ expected_width,
+ expected_height,
+ if dpr == 0 {
+ base[2].clone()
+ } else {
+ json!(dpr)
+ },
+ base[3],
+ base[4],
+ base[5],
+ base[6]
+ ])
+ );
+ }
+ let previous = evaluate(&mut ctx, snapshot).await;
+ for invalid in [
+ json!({"width": -1}),
+ json!({"height": 10_000_001}),
+ json!({"deviceScaleFactor": -1}),
+ json!({"screenWidth": -1}),
+ ] {
+ let mut params = json!({"width":640,"height":480,"deviceScaleFactor":1,"mobile":false});
+ params
+ .as_object_mut()
+ .unwrap()
+ .extend(invalid.as_object().unwrap().clone());
+ ctx.process_async(json!({"id":88001,"sessionId":"SID-1", "method":"Emulation.setDeviceMetricsOverride","params":params})).await;
+ ctx.expect_error(88001, -32602, "InvalidParams");
+ assert_eq!(evaluate(&mut ctx, snapshot).await, previous);
+ }
+ expect_session_command_result(&mut ctx, 88001, "SID-1", "Emulation.setDeviceMetricsOverride",
+ json!({"width":640,"height":480,"deviceScaleFactor":1,"mobile":false,"screenWidth":1500,"screenHeight":1200})).await;
+ assert_eq!(
+ evaluate(&mut ctx, "[screen.width,screen.height,screen.availHeight]").await,
+ json!([1500, 1200, 1200])
+ );
+ expect_session_command_result(&mut ctx, 88001, "SID-1", "Emulation.setDeviceMetricsOverride",
+ json!({"width":640,"height":480,"deviceScaleFactor":1,"mobile":false,"screenWidth":1500,"screenHeight":0})).await;
+ assert_eq!(
+ evaluate(&mut ctx, "[screen.width,screen.height]").await,
+ json!([base[5], base[6]])
+ );
+ expect_session_command_result(
+ &mut ctx,
+ 88001,
+ "SID-1",
+ "Emulation.clearDeviceMetricsOverride",
+ json!({}),
+ )
+ .await;
+ assert_eq!(evaluate(&mut ctx, snapshot).await, baseline);
+}
diff --git a/moli-protocol/src/domains/page/tests/capture.rs b/moli-protocol/src/domains/page/tests/capture.rs
index 11d92d1c4..c0d1da9c9 100644
--- a/moli-protocol/src/domains/page/tests/capture.rs
+++ b/moli-protocol/src/domains/page/tests/capture.rs
@@ -519,9 +519,14 @@ async fn capture_screenshot_targets_loaded_background_owner_without_activation()
.emulated_device_metrics = Some(EmulatedDeviceMetrics {
width: 320,
height: 240,
+ visible_width: 320,
+ visible_height: 240,
+ outer_width: 320,
+ outer_height: 240,
device_scale_factor: 2.0,
screen_width: 320,
screen_height: 240,
+ screen_avail_height: 240,
});
ctx.process_async(json!({
@@ -567,9 +572,14 @@ async fn capture_screenshot_targets_inactive_loaded_owner_without_activation() {
.emulated_device_metrics = Some(EmulatedDeviceMetrics {
width: 500,
height: 300,
+ visible_width: 500,
+ visible_height: 300,
+ outer_width: 500,
+ outer_height: 300,
device_scale_factor: 1.5,
screen_width: 500,
screen_height: 300,
+ screen_avail_height: 300,
});
inactive.replace_loaded_page(Some(page));
ctx.conn