Execute a single command from a [`DaemonRequest`]. Handles target resolution, session attachment, dialog configuration, and target ID enrichment on success.
(client: &mut CdpClient, req: &DaemonRequest)
| 124 | /// Handles target resolution, session attachment, dialog configuration, |
| 125 | /// and target ID enrichment on success. |
| 126 | pub async fn execute_command(client: &mut CdpClient, req: &DaemonRequest) -> Result<CommandResult> { |
| 127 | let cmd = req.command.as_str(); |
| 128 | |
| 129 | // Event-collecting commands drain the buffer via read_events_for, |
| 130 | // so they can capture events that arrived between commands. |
| 131 | // All other commands clear stale events to prevent memory buildup. |
| 132 | if !matches!(cmd, "console" | "network" | "sw-logs") { |
| 133 | client.clear_events(); |
| 134 | } |
| 135 | |
| 136 | let args = &req.args; |
| 137 | |
| 138 | validate_args(cmd, args)?; |
| 139 | |
| 140 | if is_browser_level(cmd) { |
| 141 | return match cmd { |
| 142 | "list-pages" => commands::pages::list_pages(client, req.format()).await, |
| 143 | "new-page" => { |
| 144 | let url = args |
| 145 | .get("url") |
| 146 | .and_then(|v| v.as_str()) |
| 147 | .ok_or(anyhow!("url required"))?; |
| 148 | |
| 149 | let viewport = args.get("viewport").and_then(|v| v.as_str()); |
| 150 | let geolocation = args.get("geolocation").and_then(|v| v.as_str()); |
| 151 | |
| 152 | let params = commands::emulation::EmulateParams { |
| 153 | viewport: viewport.map(|s| s.to_string()), |
| 154 | device_scale_factor: args.get("device_scale_factor").and_then(|v| v.as_f64()), |
| 155 | mobile: args |
| 156 | .get("mobile") |
| 157 | .and_then(|v| v.as_bool()) |
| 158 | .unwrap_or(false), |
| 159 | geolocation: geolocation.map(|s| s.to_string()), |
| 160 | accuracy: args.get("accuracy").and_then(|v| v.as_f64()), |
| 161 | clear_viewport: false, |
| 162 | clear_geolocation: false, |
| 163 | clear_all: false, |
| 164 | // Apply the global --block-url/--unblock-url flags to the new |
| 165 | // tab during its initial load, matching direct mode and the |
| 166 | // viewport/geolocation flags handled above. |
| 167 | block_url: req.block_url.clone(), |
| 168 | unblock_url: req.unblock_url.clone(), |
| 169 | clear_blocks: false, |
| 170 | }; |
| 171 | params.validate()?; |
| 172 | |
| 173 | let params = if params.has_emulation() { |
| 174 | Some(params) |
| 175 | } else { |
| 176 | None |
| 177 | }; |
| 178 | |
| 179 | commands::pages::new_page( |
| 180 | client, |
| 181 | url, |
| 182 | params, |
| 183 | args.get("extra_headers").and_then(|v| v.as_str()), |
no test coverage detected