From b5de91af758c752728cf73781c8e1b394841bee6 Mon Sep 17 00:00:00 2001 From: lanyue-llk <270302213+lanyue-llk@users.noreply.github.com> Date: Thu, 12 Nov 2026 12:48:00 +0700 Subject: [PATCH] fix(browser): keep omitted window bounds and add getWindowBounds --- moli-protocol-cdp-defs/src/actions.rs | 1 + moli-protocol/src/domains/browser.rs | 65 +++++++++++++------ .../browser_target.rs | 36 ++++++++++ 3 files changed, 82 insertions(+), 20 deletions(-) diff --git a/moli-protocol-cdp-defs/src/actions.rs b/moli-protocol-cdp-defs/src/actions.rs index 44291ec9071..efea9b61032 100644 --- a/moli-protocol-cdp-defs/src/actions.rs +++ b/moli-protocol-cdp-defs/src/actions.rs @@ -49,6 +49,7 @@ pub enum AutofillAction { pub enum BrowserAction { GetVersion, GetWindowForTarget, + GetWindowBounds, SetWindowBounds, SetDownloadBehavior, CancelDownload, diff --git a/moli-protocol/src/domains/browser.rs b/moli-protocol/src/domains/browser.rs index e3fc95aff80..e8adccb4d4f 100644 --- a/moli-protocol/src/domains/browser.rs +++ b/moli-protocol/src/domains/browser.rs @@ -2,7 +2,7 @@ use anyhow::Result; use anyhow::Context; use chromiumoxide_cdp::cdp::browser_protocol::browser::{ - CancelDownloadParams, SetWindowBoundsParams, + CancelDownloadParams, GetWindowBoundsParams, SetWindowBoundsParams, }; use serde::Deserialize; use serde_json::{Value, json}; @@ -162,6 +162,9 @@ pub(crate) fn try_start_browser_command_dispatch( BrowserAction::GetWindowForTarget => { BrowserCommandTaskStep::Complete(get_window_for_target(conn, cmd)) } + BrowserAction::GetWindowBounds => { + BrowserCommandTaskStep::Complete(get_window_bounds(conn, cmd)) + } BrowserAction::SetWindowBounds => { BrowserCommandTaskStep::Complete(set_window_bounds(conn, cmd)) } @@ -309,19 +312,8 @@ fn optional_i64_to_u32(value: Option) -> Result, ()> { value.map(u32::try_from).transpose().map_err(|_| ()) } -fn set_window_bounds(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> CommandOutputPlan { - let params: SetWindowBoundsParams = match cmd.get_params() { - Ok(Some(params)) => params, - _ => { - return CommandOutputPlan::error(-32602, "InvalidParams"); - } - }; - - let Ok(window_id) = u32::try_from(*params.window_id.inner()) else { - return CommandOutputPlan::error(-32602, "InvalidParams"); - }; - let window_is_live = conn - .browser_window_ids +fn window_is_live(conn: &CdpConnection, window_id: u32) -> bool { + conn.browser_window_ids .iter() .any(|((context_id, local_id), id)| { *id == window_id @@ -336,8 +328,40 @@ fn set_window_bounds(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> CommandOutputPl || (*local_id == 0 && context_id == conn.default_browser_context_id() && conn.default_placeholder_is_logically_active(conn.default_target_id()))) - }); - if !window_is_live { + }) +} + +fn get_window_bounds(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> CommandOutputPlan { + let params: GetWindowBoundsParams = match cmd.get_params() { + Ok(Some(params)) => params, + _ => { + return CommandOutputPlan::error(-32602, "InvalidParams"); + } + }; + let Ok(window_id) = u32::try_from(*params.window_id.inner()) else { + return CommandOutputPlan::error(-32602, "InvalidParams"); + }; + if !window_is_live(conn, window_id) { + return CommandOutputPlan::error(-32000, "Browser window not found"); + } + let Some(bounds) = conn.browser_window_bounds.get(&window_id) else { + return CommandOutputPlan::error(-32000, "Browser window not found"); + }; + CommandOutputPlan::result(json!({ "bounds": bounds_json(bounds) })) +} + +fn set_window_bounds(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> CommandOutputPlan { + let params: SetWindowBoundsParams = match cmd.get_params() { + Ok(Some(params)) => params, + _ => { + return CommandOutputPlan::error(-32602, "InvalidParams"); + } + }; + + let Ok(window_id) = u32::try_from(*params.window_id.inner()) else { + return CommandOutputPlan::error(-32602, "InvalidParams"); + }; + if !window_is_live(conn, window_id) { return CommandOutputPlan::error(-32000, "Browser window not found"); } let Some(window_bounds) = conn.browser_window_bounds.get_mut(&window_id) else { @@ -368,10 +392,11 @@ fn set_window_bounds(conn: &mut CdpConnection, cmd: &Cmd<'_>) -> CommandOutputPl return CommandOutputPlan::error(-32602, "InvalidParams"); } }; - window_bounds.left = left; - window_bounds.top = top; - window_bounds.width = width; - window_bounds.height = height; + // Omitted fields keep their current value, as in Chromium. + window_bounds.left = left.or(window_bounds.left); + window_bounds.top = top.or(window_bounds.top); + window_bounds.width = width.or(window_bounds.width); + window_bounds.height = height.or(window_bounds.height); if let Some(window_state) = params.bounds.window_state { window_bounds.window_state = window_state.as_ref().to_owned(); } diff --git a/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/browser_target.rs b/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/browser_target.rs index 2f72de3ff27..354f55bcb24 100644 --- a/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/browser_target.rs +++ b/moli-protocol/src/domains/target/tests/tests_cdp_chromium_imports/browser_target.rs @@ -90,6 +90,42 @@ async fn rust_cdp_capability_browser_window_bounds_round_trip() { assert_eq!(updated["result"]["bounds"]["top"], 22); assert_eq!(updated["result"]["bounds"]["width"], 800); assert_eq!(updated["result"]["bounds"]["height"], 600); + + ctx.process_async(json!({ + "id": 101_008, + "method": "Browser.setWindowBounds", + "sessionId": attached.session_id, + "params": { "windowId": window_id, "bounds": { "width": 700 } } + })) + .await; + ctx.expect_result(101_008, json!({}), Some(&attached.session_id)); + + ctx.process_async(json!({ + "id": 101_009, + "method": "Browser.getWindowBounds", + "sessionId": attached.session_id, + "params": { "windowId": window_id } + })) + .await; + ctx.expect_result( + 101_009, + json!({ + "bounds": { + "left": 11, "top": 22, "width": 700, "height": 600, "windowState": "normal" + } + }), + Some(&attached.session_id), + ); + + ctx.process_async(json!({ + "id": 101_010, + "method": "Browser.getWindowBounds", + "sessionId": attached.session_id, + "params": { "windowId": 999_999 } + })) + .await; + let missing = take_response_by_id(&mut ctx, 101_010); + assert_eq!(missing["error"]["code"], -32000, "{missing}"); } // Capability source: docs/WEB_CAPABILITIES.md download ability.