Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions moli-protocol-cdp-defs/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum AutofillAction {
pub enum BrowserAction {
GetVersion,
GetWindowForTarget,
GetWindowBounds,
SetWindowBounds,
SetDownloadBehavior,
CancelDownload,
Expand Down
65 changes: 45 additions & 20 deletions moli-protocol/src/domains/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -309,19 +312,8 @@ fn optional_i64_to_u32(value: Option<i64>) -> Result<Option<u32>, ()> {
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
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading