Execute the unified emulation command.
(
client: &mut CdpClient,
session_id: &str,
params: EmulateParams,
)
| 64 | |
| 65 | /// Execute the unified emulation command. |
| 66 | pub async fn emulate( |
| 67 | client: &mut CdpClient, |
| 68 | session_id: &str, |
| 69 | params: EmulateParams, |
| 70 | ) -> Result<CommandResult> { |
| 71 | let mut actions = Vec::new(); |
| 72 | |
| 73 | // 0. Handle network blocklist (applied to the persistent session, not the |
| 74 | // per-command session — so the blocklist survives across commands/targets). |
| 75 | let network_changed = !params.block_url.is_empty() |
| 76 | || !params.unblock_url.is_empty() |
| 77 | || params.clear_blocks |
| 78 | || params.clear_all; |
| 79 | |
| 80 | if params.clear_all || params.clear_blocks { |
| 81 | client.blocklist.clear(); |
| 82 | actions.push("Network blocks cleared".to_string()); |
| 83 | } |
| 84 | |
| 85 | for pattern in ¶ms.block_url { |
| 86 | if !client.blocklist.contains(pattern) { |
| 87 | client.blocklist.push(pattern.clone()); |
| 88 | } |
| 89 | } |
| 90 | if !params.block_url.is_empty() { |
| 91 | actions.push(format!("Blocked URLs: {}", params.block_url.join(", "))); |
| 92 | } |
| 93 | |
| 94 | for pattern in ¶ms.unblock_url { |
| 95 | client.blocklist.retain(|p| p != pattern); |
| 96 | } |
| 97 | if !params.unblock_url.is_empty() { |
| 98 | actions.push(format!( |
| 99 | "Un-blocked URLs: {}", |
| 100 | params.unblock_url.join(", ") |
| 101 | )); |
| 102 | } |
| 103 | |
| 104 | if network_changed { |
| 105 | // Apply to persistent session if available; otherwise fall back to the |
| 106 | // command's session (e.g., direct mode or degraded daemon path). |
| 107 | if client.persistent_session.is_some() { |
| 108 | client.apply_network_rules().await?; |
| 109 | } else { |
| 110 | client.apply_network_rules_internal(session_id).await?; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // 1. Handle clearing |
| 115 | if params.clear_all || params.clear_viewport { |
| 116 | client |
| 117 | .send_to_target( |
| 118 | session_id, |
| 119 | "Emulation.clearDeviceMetricsOverride", |
| 120 | json!({}), |
| 121 | ) |
| 122 | .await?; |
| 123 | client.viewport = None; |
no test coverage detected