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 and validate max touch points

ldm0cb12920334e1
diff --git a/moli-protocol/src/conn/browser_context/emulation_owner.rs b/moli-protocol/src/conn/browser_context/emulation_owner.rs
index b91fc184d..31411999d 100644
--- a/moli-protocol/src/conn/browser_context/emulation_owner.rs
+++ b/moli-protocol/src/conn/browser_context/emulation_owner.rs
@@ -46,9 +46,9 @@ impl TargetEmulationStateUpdate<'_> {
self.effective.cpu_throttling_rate = cpu_throttling_rate;
}
- pub(crate) fn set_touch_emulation_enabled(&mut self, enabled: bool) {
- self.raw.touch_emulation_enabled = enabled;
- self.effective.touch_emulation_enabled = enabled;
+ pub(crate) fn set_max_touch_points(&mut self, max_touch_points: u32) {
+ self.raw.max_touch_points = max_touch_points;
+ self.effective.max_touch_points = max_touch_points;
}
pub(crate) fn set_emit_touch_events_for_mouse(&mut self, enabled: bool) {
diff --git a/moli-protocol/src/conn/page_state/surfaces.rs b/moli-protocol/src/conn/page_state/surfaces.rs
index 63fc640f4..c22a4e4cd 100644
--- a/moli-protocol/src/conn/page_state/surfaces.rs
+++ b/moli-protocol/src/conn/page_state/surfaces.rs
@@ -12,7 +12,7 @@ struct SurfaceOverrideInputs {
network_conditions: Option<EmulatedNetworkConditions>,
geolocation_override: Option<EmulatedGeolocationOverrideState>,
emulated_device_metrics: Option<EmulatedDeviceMetrics>,
- touch_emulation_enabled: bool,
+ max_touch_points: u32,
focus_emulation_enabled: bool,
active_target_surface: bool,
window_document_hidden: bool,
@@ -24,10 +24,10 @@ impl SurfaceOverrideInputs {
network_conditions: browser_context.effective_active_network_conditions(),
geolocation_override: browser_context.effective_active_geolocation_override(),
emulated_device_metrics: browser_context.effective_active_emulated_device_metrics(),
- touch_emulation_enabled: browser_context
+ max_touch_points: browser_context
.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled,
+ .max_touch_points,
focus_emulation_enabled: browser_context
.active_page_target()
.effective_emulation_state
@@ -61,7 +61,7 @@ impl SurfaceOverrideInputs {
.emulated_device_metrics
.clone()
.or(default_emulated_device_metrics),
- touch_emulation_enabled: state.effective_emulation_state.touch_emulation_enabled,
+ max_touch_points: state.effective_emulation_state.max_touch_points,
focus_emulation_enabled: state.effective_emulation_state.focus_emulation_enabled,
active_target_surface: false,
window_document_hidden: false,
@@ -69,7 +69,7 @@ impl SurfaceOverrideInputs {
}
fn max_touch_points(&self) -> u32 {
- if self.touch_emulation_enabled { 1 } else { 0 }
+ self.max_touch_points
}
fn document_has_focus(&self) -> bool {
@@ -367,15 +367,9 @@ impl BrowserContext {
}
pub fn max_touch_points(&self) -> u32 {
- if self
- .active_page_target()
+ self.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled
- {
- 1
- } else {
- 0
- }
+ .max_touch_points
}
pub fn document_has_focus(&self) -> bool {
diff --git a/moli-protocol/src/conn/state/devtools_session.rs b/moli-protocol/src/conn/state/devtools_session.rs
index b37c48c5d..cd0308ef4 100644
--- a/moli-protocol/src/conn/state/devtools_session.rs
+++ b/moli-protocol/src/conn/state/devtools_session.rs
@@ -675,7 +675,7 @@ pub(crate) struct DevToolsEmulationSessionState {
pub(crate) emulated_media: super::EmulatedMediaOverrides,
pub(crate) emulated_device_metrics: Option<super::EmulatedDeviceMetrics>,
pub(crate) cpu_throttling_rate: f64,
- pub(crate) touch_emulation_enabled: bool,
+ pub(crate) max_touch_points: u32,
pub(crate) emit_touch_events_for_mouse: bool,
pub(crate) focus_emulation_enabled: bool,
pub(crate) script_execution_disabled: bool,
@@ -690,7 +690,7 @@ impl Default for DevToolsEmulationSessionState {
emulated_media: super::EmulatedMediaOverrides::default(),
emulated_device_metrics: None,
cpu_throttling_rate: 1.0,
- touch_emulation_enabled: false,
+ max_touch_points: 0,
emit_touch_events_for_mouse: false,
focus_emulation_enabled: false,
script_execution_disabled: false,
diff --git a/moli-protocol/src/conn/state/emulation.rs b/moli-protocol/src/conn/state/emulation.rs
index ef5b6e629..f494e8763 100644
--- a/moli-protocol/src/conn/state/emulation.rs
+++ b/moli-protocol/src/conn/state/emulation.rs
@@ -286,7 +286,7 @@ pub(crate) struct EffectiveTargetEmulationState {
pub(crate) emulated_media: EmulatedMediaOverrides,
pub(crate) emulated_device_metrics: Option<EmulatedDeviceMetrics>,
pub(crate) cpu_throttling_rate: f64,
- pub(crate) touch_emulation_enabled: bool,
+ pub(crate) max_touch_points: u32,
pub(crate) emit_touch_events_for_mouse: bool,
pub(crate) focus_emulation_enabled: bool,
pub(crate) script_execution_disabled: bool,
@@ -300,7 +300,7 @@ impl Default for EffectiveTargetEmulationState {
emulated_media: EmulatedMediaOverrides::default(),
emulated_device_metrics: None,
cpu_throttling_rate: 1.0,
- touch_emulation_enabled: false,
+ max_touch_points: 0,
emit_touch_events_for_mouse: false,
focus_emulation_enabled: false,
script_execution_disabled: false,
@@ -315,7 +315,7 @@ pub(crate) struct EffectiveTargetEmulationStateDelta {
pub(crate) emulated_media: bool,
pub(crate) emulated_device_metrics: bool,
pub(crate) cpu_throttling_rate: bool,
- pub(crate) touch_emulation_enabled: bool,
+ pub(crate) max_touch_points: bool,
pub(crate) focus_emulation_enabled: bool,
pub(crate) script_execution_disabled: bool,
}
@@ -325,7 +325,7 @@ impl EffectiveTargetEmulationStateDelta {
self.network_conditions
|| self.geolocation_override
|| self.emulated_device_metrics
- || self.touch_emulation_enabled
+ || self.max_touch_points
|| self.focus_emulation_enabled
}
}
@@ -354,8 +354,8 @@ impl EffectiveTargetEmulationState {
if raw.cpu_throttling_rate != 1.0 {
self.cpu_throttling_rate = 1.0;
}
- if raw.touch_emulation_enabled {
- self.touch_emulation_enabled = false;
+ if raw.max_touch_points != 0 {
+ self.max_touch_points = 0;
}
if raw.emit_touch_events_for_mouse {
self.emit_touch_events_for_mouse = false;
@@ -373,8 +373,7 @@ impl EffectiveTargetEmulationState {
emulated_device_metrics: previous.emulated_device_metrics
!= self.emulated_device_metrics,
cpu_throttling_rate: previous.cpu_throttling_rate != self.cpu_throttling_rate,
- touch_emulation_enabled: previous.touch_emulation_enabled
- != self.touch_emulation_enabled,
+ max_touch_points: previous.max_touch_points != self.max_touch_points,
focus_emulation_enabled: previous.focus_emulation_enabled
!= self.focus_emulation_enabled,
script_execution_disabled: previous.script_execution_disabled
diff --git a/moli-protocol/src/domains/emulation.rs b/moli-protocol/src/domains/emulation.rs
index 04c0df9d5..8a0838c3f 100644
--- a/moli-protocol/src/domains/emulation.rs
+++ b/moli-protocol/src/domains/emulation.rs
@@ -296,12 +296,23 @@ fn start_touch_emulation_enabled_command(
));
}
};
+ let max_touch_points = params.max_touch_points.unwrap_or(1);
+ if !(1..=16).contains(&max_touch_points) {
+ return EmulationCommandTaskStep::Complete(CommandOutputPlan::error(
+ -32602,
+ "Touch points must be between 1 and 16",
+ ));
+ }
if conn.browser_context.is_none() {
return EmulationCommandTaskStep::Complete(CommandOutputPlan::result(json!({})));
}
if let Err(message) =
page_session::update_page_emulation_state(conn, cmd.session_id, |mut state| {
- state.set_touch_emulation_enabled(params.enabled);
+ state.set_max_touch_points(if params.enabled {
+ max_touch_points as u32
+ } else {
+ 0
+ });
})
{
let code = if message == "BrowserContextNotLoaded" {
diff --git a/moli-protocol/src/domains/emulation/tests.rs b/moli-protocol/src/domains/emulation/tests.rs
index 81d002c71..cdde7918c 100644
--- a/moli-protocol/src/domains/emulation/tests.rs
+++ b/moli-protocol/src/domains/emulation/tests.rs
@@ -853,7 +853,8 @@ async fn pure_state_emulation_commands_complete_through_command_dispatch() {
browser_context
.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled
+ .max_touch_points
+ != 0
);
assert!(
browser_context
@@ -1545,7 +1546,8 @@ async fn async_emulation_device_state_updates_browser_context() {
.unwrap()
.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled
+ .max_touch_points
+ != 0
);
ctx.process_async(json!({
diff --git a/moli-protocol/src/domains/emulation/tests/native_navigator.rs b/moli-protocol/src/domains/emulation/tests/native_navigator.rs
index dc26fa605..a88ae2490 100644
--- a/moli-protocol/src/domains/emulation/tests/native_navigator.rs
+++ b/moli-protocol/src/domains/emulation/tests/native_navigator.rs
@@ -279,3 +279,57 @@ async fn focus_override_updates_loaded_background_page_and_preserves_real_focus(
json!([true, false, "visible"])
);
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn max_touch_points_updates_native_getters_and_rejects_invalid_counts_atomically() {
+ let mut ctx = setup().await;
+ evaluate(&mut ctx, "globalThis.touchGetter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'maxTouchPoints').get").await;
+ for (params, expected) in [
+ (json!({"enabled": true}), 1),
+ (json!({"enabled": true, "maxTouchPoints": 5}), 5),
+ (json!({"enabled": true, "maxTouchPoints": 16}), 16),
+ (json!({"enabled": false, "maxTouchPoints": 5}), 0),
+ (json!({"enabled": true, "maxTouchPoints": 5}), 5),
+ ] {
+ expect_session_command_result(
+ &mut ctx,
+ 88001,
+ "SID-1",
+ "Emulation.setTouchEmulationEnabled",
+ params,
+ )
+ .await;
+ assert_eq!(evaluate(&mut ctx, "[navigator.maxTouchPoints, touchGetter.call(navigator), touchGetter === Object.getOwnPropertyDescriptor(Navigator.prototype, 'maxTouchPoints').get]").await, json!([expected, expected, true]));
+ }
+ for enabled in [true, false] {
+ for max_touch_points in [0, 17, -1] {
+ ctx.process_async(json!({"id": 88001,"sessionId": "SID-1", "method": "Emulation.setTouchEmulationEnabled", "params": {"enabled": enabled,"maxTouchPoints": max_touch_points}})).await;
+ ctx.expect_error(88001, -32602, "Touch points must be between 1 and 16");
+ assert_eq!(
+ evaluate(&mut ctx, "navigator.maxTouchPoints").await,
+ json!(5)
+ );
+ }
+ }
+ assert_eq!(
+ evaluate(
+ &mut ctx,
+ r#"(async () => {
+ const frame = document.createElement('iframe');
+ frame.srcdoc = '<body>child</body>';
+ await new Promise(resolve => { frame.onload = resolve; document.body.append(frame); });
+ return frame.contentWindow.navigator.maxTouchPoints;
+ })()"#
+ )
+ .await,
+ json!(5)
+ );
+ ctx.install_buffered_navigation_fixture_for_session_owner(
+ url::Url::parse("https://geolocation.example/next-touch").unwrap(),
+ "<!doctype html><script>globalThis.initialTouch = navigator.maxTouchPoints;</script>"
+ .into(),
+ Some("SID-1"),
+ )
+ .await;
+ assert_eq!(evaluate(&mut ctx, "initialTouch").await, json!(5));
+}
diff --git a/moli-protocol/src/domains/target/tests/tests_background_staging.rs b/moli-protocol/src/domains/target/tests/tests_background_staging.rs
index 706644251..db7004853 100644
--- a/moli-protocol/src/domains/target/tests/tests_background_staging.rs
+++ b/moli-protocol/src/domains/target/tests/tests_background_staging.rs
@@ -3576,7 +3576,8 @@ async fn same_context_background_session_can_stage_its_own_emulation_overrides_b
active
.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled
+ .max_touch_points
+ != 0
);
assert!(
active
@@ -3603,7 +3604,7 @@ async fn same_context_background_session_can_stage_its_own_emulation_overrides_b
)),
Some((640, 360, 1.0, 800, 600))
);
- assert!(!staged.effective_emulation_state.touch_emulation_enabled);
+ assert_eq!(staged.effective_emulation_state.max_touch_points, 0);
assert!(!staged.effective_emulation_state.focus_emulation_enabled);
}
@@ -4107,10 +4108,11 @@ async fn same_context_background_session_can_clear_its_own_touch_and_focus_befor
.as_ref()
.expect("active browser context");
assert!(
- !active
+ active
.active_page_target()
.effective_emulation_state
- .touch_emulation_enabled,
+ .max_touch_points
+ == 0,
"active target should keep default touch emulation"
);
assert!(