Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix: preserve input acknowledgements across navigation

ldm010739179394a
diff --git a/moli-core/src/page/input_support.rs b/moli-core/src/page/input_support.rs
index 05bb0d7c4..078a88000 100644
--- a/moli-core/src/page/input_support.rs
+++ b/moli-core/src/page/input_support.rs
@@ -5,6 +5,48 @@ use super::{
RendererPageCommand, RendererPageReply, RendererPointerEventProperties, RendererTouchPoint,
};
+impl CompletedPageCommand {
+ /// Decodes an input reply after the Page that produced it was replaced.
+ ///
+ /// Renderer completion is the acknowledgement boundary for an admitted
+ /// input command. Callers should normally finish through [`PageInputExt`]
+ /// so the originating Page receives its refreshed state. Once that Page
+ /// is no longer current, however, its frozen reply remains valid and must
+ /// not be applied to the replacement Page.
+ pub fn into_detached_input_dispatch_outcome(self) -> Result<RendererInputDispatchOutcome> {
+ let (reply, _page_state) = self.into_output().into_reply_and_state();
+ decode_input_dispatch_outcome(reply, "detached input event page command")
+ }
+
+ /// Decodes an admitted insert-text reply whose originating Page was
+ /// replaced before protocol delivery completed.
+ pub fn into_detached_insert_text_result(self) -> Result<bool> {
+ let (reply, _page_state) = self.into_output().into_reply_and_state();
+ decode_insert_text_result(reply)
+ }
+}
+
+fn decode_input_dispatch_outcome(
+ reply: RendererPageReply,
+ operation: &str,
+) -> Result<RendererInputDispatchOutcome> {
+ expect_page_reply!(
+ reply,
+ operation,
+ "an input dispatch outcome reply",
+ RendererPageReply::InputDispatchOutcome(value) => Ok(value),
+ )
+}
+
+fn decode_insert_text_result(reply: RendererPageReply) -> Result<bool> {
+ expect_page_reply!(
+ reply,
+ "insert text page command",
+ "a bool reply",
+ RendererPageReply::Bool(value) => Ok(value),
+ )
+}
+
#[allow(async_fn_in_trait)]
pub trait PageInputExt {
async fn dispatch_mouse_event_at_point_with_outcome_async(
@@ -250,12 +292,7 @@ impl PageInputExt for Page {
completion: CompletedPageCommand,
) -> Result<RendererInputDispatchOutcome> {
let reply = self.finish_page_command(completion);
- expect_page_reply!(
- reply,
- "mouse event page command",
- "an input dispatch outcome reply",
- RendererPageReply::InputDispatchOutcome(value) => Ok(value),
- )
+ decode_input_dispatch_outcome(reply, "mouse event page command")
}
async fn dispatch_touch_event_at_point_with_outcome_async(
@@ -311,12 +348,7 @@ impl PageInputExt for Page {
completion: CompletedPageCommand,
) -> Result<RendererInputDispatchOutcome> {
let reply = self.finish_page_command(completion);
- expect_page_reply!(
- reply,
- "touch event page command",
- "an input dispatch outcome reply",
- RendererPageReply::InputDispatchOutcome(value) => Ok(value),
- )
+ decode_input_dispatch_outcome(reply, "touch event page command")
}
async fn dispatch_drag_event_at_point_with_outcome_async(
@@ -366,12 +398,7 @@ impl PageInputExt for Page {
completion: CompletedPageCommand,
) -> Result<RendererInputDispatchOutcome> {
let reply = self.finish_page_command(completion);
- expect_page_reply!(
- reply,
- "drag event page command",
- "an input dispatch outcome reply",
- RendererPageReply::InputDispatchOutcome(value) => Ok(value),
- )
+ decode_input_dispatch_outcome(reply, "drag event page command")
}
async fn clear_active_drag_data_transfer_async(&mut self) -> Result<()> {
@@ -410,12 +437,7 @@ impl PageInputExt for Page {
completion: CompletedPageCommand,
) -> Result<bool> {
let reply = self.finish_page_command(completion);
- expect_page_reply!(
- reply,
- "insert text page command",
- "a bool reply",
- RendererPageReply::Bool(value) => Ok(value),
- )
+ decode_insert_text_result(reply)
}
async fn dispatch_key_event_with_outcome_async(
@@ -467,12 +489,7 @@ impl PageInputExt for Page {
completion: CompletedPageCommand,
) -> Result<RendererInputDispatchOutcome> {
let reply = self.finish_page_command(completion);
- expect_page_reply!(
- reply,
- "key event page command",
- "an input dispatch outcome reply",
- RendererPageReply::InputDispatchOutcome(value) => Ok(value),
- )
+ decode_input_dispatch_outcome(reply, "key event page command")
}
async fn dispatch_key_event_async(
diff --git a/moli-protocol/src/domains/input.rs b/moli-protocol/src/domains/input.rs
index fc740d283..d534dd876 100644
--- a/moli-protocol/src/domains/input.rs
+++ b/moli-protocol/src/domains/input.rs
@@ -1261,18 +1261,12 @@ async fn complete_pending_input_command(
};
}
};
- let outcome = {
- let Some(page) = loaded_page_mut(conn, session_id) else {
- let protocol_events = side_effects.into_events();
- return CompletedInputCommandResult {
- result: Err(DevToolsError::new(
- DevToolsErrorKind::Internal,
- "NoDocumentLoaded",
- )),
- protocol_events,
- };
- };
- match kind {
+ let outcome = finish_completed_input_command_for_owner(
+ conn,
+ session_id,
+ &owner,
+ result,
+ |page, result| match kind {
PendingInputCommandKind::DispatchMouseEvent => {
page.finish_dispatch_mouse_event_at_point_with_outcome(result)
}
@@ -1285,8 +1279,9 @@ async fn complete_pending_input_command(
}
PendingInputCommandKind::DispatchKeyEvent
| PendingInputCommandKind::InsertText => unreachable!(),
- }
- };
+ },
+ CompletedPageCommand::into_detached_input_dispatch_outcome,
+ );
match outcome {
Ok(outcome) => {
if let Err(error) = handle_input_dispatch_outcome_async(
@@ -1324,19 +1319,14 @@ async fn complete_pending_input_command(
};
}
};
- let outcome = {
- let Some(page) = loaded_page_mut(conn, session_id) else {
- let protocol_events = side_effects.into_events();
- return CompletedInputCommandResult {
- result: Err(DevToolsError::new(
- DevToolsErrorKind::Internal,
- "NoDocumentLoaded",
- )),
- protocol_events,
- };
- };
- page.finish_dispatch_key_event_with_outcome(result)
- };
+ let outcome = finish_completed_input_command_for_owner(
+ conn,
+ session_id,
+ &owner,
+ result,
+ Page::finish_dispatch_key_event_with_outcome,
+ CompletedPageCommand::into_detached_input_dispatch_outcome,
+ );
match outcome {
Ok(outcome) => {
if let Err(error) = handle_input_dispatch_outcome_async(
@@ -1374,19 +1364,14 @@ async fn complete_pending_input_command(
};
}
};
- let result = {
- let Some(page) = loaded_page_mut(conn, session_id) else {
- let protocol_events = side_effects.into_events();
- return CompletedInputCommandResult {
- result: Err(DevToolsError::new(
- DevToolsErrorKind::Internal,
- "NoDocumentLoaded",
- )),
- protocol_events,
- };
- };
- page.finish_insert_text_into_active_control(result)
- };
+ let result = finish_completed_input_command_for_owner(
+ conn,
+ session_id,
+ &owner,
+ result,
+ Page::finish_insert_text_into_active_control,
+ CompletedPageCommand::into_detached_insert_text_result,
+ );
match result {
Ok(_) => Ok(DevToolsCommandResult::Empty),
Err(error) => Err(DevToolsError::new(
@@ -1404,6 +1389,30 @@ async fn complete_pending_input_command(
}
}
+/// Finishes an input command against the exact Page residence that admitted
+/// it. Navigation is allowed to suspend that Page while its renderer reply is
+/// crossing protocol ingress, so completion bypasses the ordinary navigation
+/// access barrier. If navigation has already installed a replacement Page,
+/// consume the frozen reply without applying the old Page state to the new
+/// residence.
+fn finish_completed_input_command_for_owner<T>(
+ conn: &mut CdpConnection,
+ session_id: Option<&str>,
+ owner: &TargetPageResidenceIdentity,
+ completion: CompletedPageCommand,
+ finish_current: impl FnOnce(&mut Page, CompletedPageCommand) -> anyhow::Result<T>,
+ finish_detached: impl FnOnce(CompletedPageCommand) -> anyhow::Result<T>,
+) -> anyhow::Result<T> {
+ if !conn.target_page_residence_identity_is_current_for_session(session_id, owner) {
+ return finish_detached(completion);
+ }
+
+ let page = conn
+ .loaded_page_mut_for_interruptible_protocol_access(session_id)
+ .map_err(anyhow::Error::msg)?;
+ finish_current(page, completion)
+}
+
pub(crate) async fn complete_pending_input_command_output_plan(
conn: &mut CdpConnection,
completed: CompletedInputCommandDispatch,
diff --git a/moli-protocol/src/domains/input/tests.rs b/moli-protocol/src/domains/input/tests.rs
index 0569c7bbf..bf6750571 100644
--- a/moli-protocol/src/domains/input/tests.rs
+++ b/moli-protocol/src/domains/input/tests.rs
@@ -1,6 +1,6 @@
use super::*;
use crate::conn::{BrowserContext, CdpCommandTaskStep};
-use crate::testing::TestContext;
+use crate::testing::{TestContext, wait_until_frame_stopped_loading};
use moli_core::LayoutPolicy;
const INPUT_HIT_X: u32 = 20;
@@ -204,6 +204,89 @@ async fn coordinate_mouse_commands_hit_test_real_layout_and_dispatch_dom_events(
);
}
+#[tokio::test(flavor = "multi_thread")]
+async fn coordinate_mouse_release_acknowledges_real_link_navigation() {
+ tokio::task::LocalSet::new()
+ .run_until(async {
+ let mut ctx = TestContext::new();
+ ctx.enable_background_navigation_scheduler_for_test();
+ with_loaded_document(
+ &mut ctx,
+ r#"<html><body style='margin:0'>
+ <a id='next' href='data:text/html,destination'
+ style='display:block;width:80px;height:80px'>next</a>
+ </body></html>"#,
+ )
+ .await;
+ ctx.enable_page_events_for_test(None);
+
+ ctx.process_and_wait_for_response_async(json!({
+ "id": 101,
+ "method": "Input.dispatchMouseEvent",
+ "params": {
+ "type": "mousePressed",
+ "x": INPUT_HIT_X,
+ "y": INPUT_HIT_Y,
+ "button": "left",
+ "buttons": 1,
+ "clickCount": 1
+ }
+ }))
+ .await;
+ ctx.expect_result(101, json!({}), None);
+
+ ctx.process_and_wait_for_response_async(json!({
+ "id": 102,
+ "method": "Input.dispatchMouseEvent",
+ "params": {
+ "type": "mouseReleased",
+ "x": INPUT_HIT_X,
+ "y": INPUT_HIT_Y,
+ "button": "left",
+ "buttons": 0,
+ "clickCount": 1
+ }
+ }))
+ .await;
+
+ // Chromium acknowledges an input event once its renderer has consumed
+ // it. The resulting Document replacement is an independent lifecycle and
+ // must not retroactively turn this response into NoDocumentLoaded.
+ ctx.expect_result(102, json!({}), None);
+ wait_until_frame_stopped_loading(&mut ctx, "TID-1").await;
+ ctx.expect_event("Page.frameNavigated", None);
+ assert_eq!(
+ evaluate_string(&mut ctx, "document.body.textContent").await,
+ "destination"
+ );
+ })
+ .await;
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn coordinate_mouse_event_without_document_still_reports_no_document_loaded() {
+ let mut ctx = TestContext::new();
+ let mut bc = BrowserContext::new("BID-I".into());
+ bc.set_active_target_id("TID-1");
+ ctx.conn.browser_context = Some(bc);
+
+ ctx.process_async(json!({
+ "id": 103,
+ "method": "Input.dispatchMouseEvent",
+ "params": {
+ "type": "mouseReleased",
+ "x": INPUT_HIT_X,
+ "y": INPUT_HIT_Y,
+ "button": "left",
+ "buttons": 0,
+ "clickCount": 1
+ }
+ }))
+ .await;
+
+ ctx.expect_error(103, -32000, "NoDocumentLoaded");
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn coordinate_mouse_down_applies_chromium_focus_default_action() {
let mut ctx = TestContext::new();