The headless browser for AI agents and web scraping
fix(frames): finish document work before iframe load
SGavrl20299be35100
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rsindex 34a76408..72ae729a 100644--- a/crates/obscura-browser/src/page.rs+++ b/crates/obscura-browser/src/page.rs@@ -1330,6 +1330,7 @@ impl Page {let Some(PendingFrameWork::Attached {frame_id,+ parent_frame_id,frame_url,sources,..@@ -1338,6 +1339,7 @@ impl Page {return false;};let frame_id = *frame_id;+ let parent_frame_id = *parent_frame_id;let frame_url = frame_url.clone();let sources = std::mem::take(sources);let Some(index) = self@@ -1359,6 +1361,11 @@ impl Page {}}self.pending_frame_work.pop_front();+ self.execute_frame_owner_script(parent_frame_id, &format!(+ "(function(){{ const frame = globalThis.__obscura_frameElements[{frame_id}]; \+ if (frame && frame.isConnected && frame._frameId === {frame_id}) \+ frame.dispatchEvent(new Event('load')); }})()"+ ));true}@@ -1447,13 +1454,13 @@ impl Page {}}- /// Executes cleanup in the realm that owns the iframe element. Nested+ /// Executes code in the realm that owns the iframe element. Nested/// iframe registries live in their parent frame, not in the page realm.fn execute_frame_owner_script(&mut self, parent_frame_id: u32, script: &str) {if parent_frame_id == 0 {if let Some(js) = self.js.as_mut() {- if let Err(error) = js.execute_script("<frame-detach>", script) {- tracing::debug!("releasing frame references failed: {error}");+ if let Err(error) = js.execute_script("<frame-owner>", script) {+ tracing::debug!("frame owner script failed: {error}");}}return;@@ -1468,7 +1475,7 @@ impl Page {};let Some(js) = self.js.as_mut() else { return };if let Err(error) = self.frames[index].execute_script(js, script) {- tracing::debug!("releasing nested frame references failed: {error}");+ tracing::debug!("nested frame owner script failed: {error}");}}@@ -4385,9 +4392,10 @@ impl Page {return_by_value: bool,await_promise: bool,) -> obscura_js::runtime::RemoteObjectInfo {- if let Some(js) = &mut self.js {- match js- .evaluate_for_cdp(expression, return_by_value, await_promise)+ if self.js.is_some() {+ match self+ .evaluate_for_cdp_with_timeout(expression, return_by_value, await_promise,+ obscura_js::runtime::DEFAULT_CDP_AWAIT_TIMEOUT_MS).await{Ok(info) => info,@@ -4433,13 +4441,8 @@ impl Page {await_timeout_ms: u64,) -> Result<obscura_js::runtime::RemoteObjectInfo, String> {if let Some(js) = &mut self.js {- js.evaluate_for_cdp_with_timeout(- expression,- return_by_value,- await_promise,- await_timeout_ms,- )- .await+ let evaluation = js.start_cdp_evaluation(expression, return_by_value, await_promise)?;+ self.await_cdp_evaluation(evaluation, return_by_value, await_timeout_ms, "Runtime.evaluate").await} else {let value = self.evaluate(expression);Ok(obscura_js::runtime::RemoteObjectInfo {@@ -4468,14 +4471,15 @@ impl Page {return_by_value: bool,await_promise: bool,) -> obscura_js::runtime::RemoteObjectInfo {- if let Some(js) = &mut self.js {- match js- .call_function_on_for_cdp(+ if self.js.is_some() {+ match self+ .call_function_on_for_cdp_with_timeout(function_declaration,object_id,args,return_by_value,await_promise,+ obscura_js::runtime::DEFAULT_CDP_AWAIT_TIMEOUT_MS,).await{@@ -4518,15 +4522,45 @@ impl Page {await_timeout_ms: u64,) -> Result<obscura_js::runtime::RemoteObjectInfo, String> {let js = self.js.as_mut().ok_or("JavaScript runtime unavailable")?;- js.call_function_on_for_cdp_with_timeout(+ let evaluation = js.start_cdp_call(function_declaration,object_id,args,return_by_value,await_promise,- await_timeout_ms,- )- .await+ )?;+ self.await_cdp_evaluation(evaluation, return_by_value, await_timeout_ms, "Runtime.callFunctionOn").await+ }++ async fn await_cdp_evaluation(+ &mut self,+ evaluation: obscura_js::runtime::CdpEvaluation,+ return_by_value: bool,+ timeout_ms: u64,+ method: &str,+ ) -> Result<obscura_js::runtime::RemoteObjectInfo, String> {+ let pending = match evaluation {+ obscura_js::runtime::CdpEvaluation::Complete(info) => return Ok(info),+ obscura_js::runtime::CdpEvaluation::Pending(pending) => pending,+ };+ let deadline = tokio::time::Instant::now() + std::time::Duration::from_millis(timeout_ms);+ let wait = async {+ loop {+ let queued = !self.pending_frame_work.is_empty();+ let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());+ let js = self.js.as_mut().ok_or("JavaScript runtime unavailable")?;+ let mut ready = false;+ let progressed = js.resolve_promises_until(|js| {+ ready = js.cdp_evaluation_ready(&pending);+ ready || queued || js.has_pending_frame_tasks()+ }, duration_millis_u64(remaining)).await;+ if ready { return js.finish_cdp_evaluation(pending, return_by_value); }+ if !progressed { return Err(format!("{method} promise did not settle within {timeout_ms}ms")); }+ self.advance_frames().await;+ }+ };+ tokio::time::timeout_at(deadline, wait).await+ .unwrap_or_else(|_| Err(format!("{method} promise did not settle within {timeout_ms}ms")))}pub fn set_blocked_urls(&mut self, patterns: Vec<String>) {@@ -7056,6 +7090,41 @@ mod tests {(format!("http://{addr}"), requests)}+ #[tokio::test(flavor = "current_thread")]+ async fn iframe_load_waits_for_the_real_document_and_author_scripts() {+ let (base, _, _) = spawn_slow_frame_script_server().await;+ let mut page = import_map_test_page("frame-load-ready", &base, "<html><body></body></html>");+ page.add_preload_script("globalThis.__order = ['preload'];");+ page.js.as_mut().unwrap().evaluate(+ "(function(){ globalThis.loads = []; const frame = document.createElement('iframe'); \+ frame.onload = function(){ loads.push([typeof frame.contentDocument.appendChild, \+ frame.contentWindow.__childReady === true, frame.contentDocument.readyState]); }; \+ frame.src = '/child.html'; document.body.appendChild(frame); return 1; })()"+ ).unwrap();+ tokio::time::timeout(+ std::time::Duration::from_secs(2), page.js.as_mut().unwrap().run_event_loop(),+ ).await.unwrap().unwrap();+ assert_eq!(page.js.as_mut().unwrap().evaluate("loads").unwrap(), serde_json::json!([]),+ "the iframe fired load before its scripts could run");+ tokio::time::timeout(std::time::Duration::from_secs(2), page.advance_frames())+ .await.unwrap();+ assert_eq!(page.js.as_mut().unwrap().evaluate("loads").unwrap(),+ serde_json::json!([["function", true, "complete"]]));+ page.advance_frames().await;+ assert_eq!(page.js.as_mut().unwrap().evaluate("loads.length === 1").unwrap(), serde_json::json!(true));++ let script = "new Promise(resolve => { const frame = document.createElement('iframe'); \+ frame.onload = () => resolve([typeof frame.contentDocument.appendChild, \+ frame.contentWindow.__childReady === true]); \+ frame.src = '/child.html'; document.body.appendChild(frame); })";+ let evaluated = page.evaluate_for_cdp_with_timeout(script, true, true, 2_000).await.unwrap();+ assert_eq!(evaluated.value.unwrap(), serde_json::json!(["function", true]));+ let called = page.call_function_on_for_cdp_with_timeout(+ &format!("() => {script}"), None, &[], true, true, 2_000,+ ).await.unwrap();+ assert_eq!(called.value.unwrap(), serde_json::json!(["function", true]));+ }+#[tokio::test(flavor = "current_thread")]async fn cancelling_frame_script_fetch_keeps_all_siblings_resumable() {let (mut page, requests, slow_seen) =diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex dfbd1d76..c45f1380 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -4707,10 +4707,7 @@ class Element extends Node {const html = response.body || '';const loadedUrl = response.url || fullUrl;el._iframeLoadedUrl = loadedUrl;- // Hand the document to the host, which gives this frame a realm of its- // own and runs the scripts that came with it (issue #600). The shim- // document below stays: it is what the parent reads through- // contentDocument.+ // The host fires load after creating the realm and running its scripts.const box = el.getBoundingClientRect();if (el._frameId) globalThis.__obscura_forgetFrame(el._frameId);el._frameId = __obscuraCore.ops.op_frame_document_ready(@@ -4725,6 +4722,7 @@ class Element extends Node {el._iframeWin._frameId = el._frameId;globalThis.__obscura_frameWindows[el._frameId] = el._iframeWin;globalThis.__obscura_frameElements[el._frameId] = el;+ return;}} else {el._iframeLoadedUrl = fullUrl;diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex daf0faa6..2c353943 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -130,7 +130,8 @@ fn enter_tokio_context() -> Option<tokio::runtime::EnterGuard<'static>> {Some(FALLBACK.enter())}-const DEFAULT_CDP_AWAIT_TIMEOUT_MS: u64 = 30_000;+#[doc(hidden)]+pub const DEFAULT_CDP_AWAIT_TIMEOUT_MS: u64 = 30_000;const HEAP_LIMIT_RECOVERY_HEADROOM_BYTES: usize = 64 * 1024 * 1024;#[cfg(feature = "render")]const MAX_PENDING_RENDER_RESOURCES: usize = 128;@@ -230,6 +231,20 @@ pub struct RemoteObjectInfo {pub unserializable_value: Option<String>,}+/// A started CDP command, so the browser can service frame work while awaiting it.+#[doc(hidden)]+pub enum CdpEvaluation {+ Complete(RemoteObjectInfo),+ Pending(PendingCdpEvaluation),+}++#[doc(hidden)]+pub struct PendingCdpEvaluation {+ object_id: String,+ done_counter: u64,+ recipe: Option<String>,+}+/// CDP remote objects that can be rebuilt when a page's V8 runtime is/// temporarily replaced during tab switching.///@@ -1122,6 +1137,12 @@ impl ObscuraJsRuntime {std::mem::take(&mut state.pending_frames)}+ #[doc(hidden)]+ pub fn has_pending_frame_tasks(&self) -> bool {+ let state = self.state.borrow();+ !state.pending_frames.is_empty() || !state.pending_frame_messages.is_empty()+ }+/// postMessage traffic waiting to be delivered to another realm.pub fn take_pending_frame_messages(&self) -> Vec<crate::ops::PendingFrameMessage> {let mut state = self.state.borrow_mut();@@ -2572,6 +2593,17 @@ impl ObscuraJsRuntime {await_promise: bool,await_timeout_ms: u64,) -> Result<RemoteObjectInfo, String> {+ let evaluation = self.start_cdp_evaluation(expression, return_by_value, await_promise)?;+ self.await_cdp_evaluation(evaluation, return_by_value, await_timeout_ms, "Runtime.evaluate").await+ }++ #[doc(hidden)]+ pub fn start_cdp_evaluation(+ &mut self,+ expression: &str,+ return_by_value: bool,+ await_promise: bool,+ ) -> Result<CdpEvaluation, String> {// Every shape of this command now travels the same path. The// by-value sync case used to short-circuit through `evaluate`,// whose wrapper answers an exception with `null` — indistinguishable@@ -2655,44 +2687,13 @@ impl ObscuraJsRuntime {.execute_runtime_script("<eval-remote>", meta_code).map_err(|e| format!("JS error: {}", e))?;- let meta_str = if await_promise {- let __t0 = std::time::Instant::now();- let sentinel = format!("globalThis.__obscura_done_{done_counter} === true && delete globalThis.__obscura_done_{done_counter}");- let settled = self- .resolve_promises_until(- |rt| {- rt.execute_runtime_script("<done?>", sentinel.clone())- .ok()- .and_then(|v| rt.v8_to_json(v).ok())- .and_then(|j| j.as_bool())- .unwrap_or(false)- },- await_timeout_ms,- )- .await;- if !settled {- return Err(format!(- "Runtime.evaluate promise did not settle within {await_timeout_ms}ms"- ));- }- let __dt = __t0.elapsed();- if __dt > std::time::Duration::from_secs(1) {- let preview: String = expression- .chars()- .take(200)- .map(|c| if c == '\n' || c == '\t' { ' ' } else { c })- .collect();- tracing::debug!(- "Runtime.evaluate awaitPromise took {}ms; expr={}",- __dt.as_millis(),- preview,- );- }- self.execute_runtime_script("<readMeta>", "globalThis.__obscura_await_meta".to_string())- .map_err(|e| format!("JS error: {}", e))?- } else {- result- };+ if await_promise {+ return Ok(CdpEvaluation::Pending(PendingCdpEvaluation {+ object_id: oid, done_counter,+ recipe: if return_by_value { None } else { Some(expression.to_string()) },+ }));+ }+ let meta_str = result;// Neither a rejection nor a synchronous throw is a protocol failure.// CDP answers the command and puts the value in `exceptionDetails`,@@ -2708,10 +2709,10 @@ impl ObscuraJsRuntime {).map_err(|e| format!("JS error: {}", e))?;if self.v8_to_json(thrown)?.as_bool().unwrap_or(false) {- return self.thrown_info(&oid);+ return self.thrown_info(&oid).map(CdpEvaluation::Complete);}if return_by_value {- return self.take_cdp_result_by_value(&oid);+ return self.take_cdp_result_by_value(&oid).map(CdpEvaluation::Complete);}let meta_str = self.v8_to_json(meta_str)?;let meta_json = if let serde_json::Value::String(s) = &meta_str {@@ -2724,7 +2725,7 @@ impl ObscuraJsRuntime {if info.object_id.is_some() {self.evaluation_recipes.insert(oid, expression.to_string());}- Ok(info)+ Ok(CdpEvaluation::Complete(info))}pub async fn call_function_on_for_cdp(@@ -2755,6 +2756,19 @@ impl ObscuraJsRuntime {await_promise: bool,await_timeout_ms: u64,) -> Result<RemoteObjectInfo, String> {+ let evaluation = self.start_cdp_call(function_declaration, object_id, arguments, return_by_value, await_promise)?;+ self.await_cdp_evaluation(evaluation, return_by_value, await_timeout_ms, "Runtime.callFunctionOn").await+ }++ #[doc(hidden)]+ pub fn start_cdp_call(+ &mut self,+ function_declaration: &str,+ object_id: Option<&str>,+ arguments: &[serde_json::Value],+ return_by_value: bool,+ await_promise: bool,+ ) -> Result<CdpEvaluation, String> {self.begin_javascript_task();let this_expr = self.resolve_this(object_id);let (setup, args_list) = self.build_args(arguments);@@ -2798,69 +2812,9 @@ impl ObscuraJsRuntime {self.execute_runtime_script("<callFnAsync>", code).map_err(|e| format!("JS error: {}", e))?;- let __t0 = std::time::Instant::now();- let sentinel = format!("globalThis.__obscura_done_{done_counter} === true && delete globalThis.__obscura_done_{done_counter}");- let settled = self- .resolve_promises_until(- |rt| {- rt.execute_runtime_script("<done?>", sentinel.clone())- .ok()- .and_then(|v| rt.v8_to_json(v).ok())- .and_then(|j| j.as_bool())- .unwrap_or(false)- },- await_timeout_ms,- )- .await;- if !settled {- return Err(format!(- "Runtime.callFunctionOn promise did not settle within {await_timeout_ms}ms"- ));- }- let __dt = __t0.elapsed();- if __dt > std::time::Duration::from_secs(1) {- let preview: String = function_declaration- .chars()- .take(300)- .map(|c| if c == '\n' || c == '\t' { ' ' } else { c })- .collect();- tracing::debug!(- "Runtime.callFunctionOn awaitPromise took {}ms; fn={}",- __dt.as_millis(),- preview,- );- }-- // Same rule as evaluate: a rejected call is answered, not failed,- // and the value is never serialized by value. Without this the- // wrapper stored the error under the object id a success uses, so- // a rejection came back as an ordinary result: an Error as `{}`,- // and `Promise.reject({code: 42})` as `{code: 42}`, which is- // indistinguishable from resolving with it.- let rejected = self- .execute_runtime_script(- "<readRejected>",- "globalThis.__obscura_await_rejected".to_string(),- )- .map_err(|e| format!("JS error: {}", e))?;- if self.v8_to_json(rejected)?.as_bool().unwrap_or(false) {- return self.thrown_info(&oid);- }-- if return_by_value {- return self.take_cdp_result_by_value(&oid);- }-- let meta_result = self- .execute_runtime_script("<readMeta>", "globalThis.__obscura_await_meta".to_string())- .map_err(|e| format!("JS error: {}", e))?;- let meta_str = self.v8_to_json(meta_result)?;- let meta_json = if let serde_json::Value::String(s) = &meta_str {- serde_json::from_str(s).unwrap_or(meta_str.clone())- } else {- meta_str- };- return self.finish_cdp_result(&meta_json, &oid);+ return Ok(CdpEvaluation::Pending(PendingCdpEvaluation {+ object_id: oid, done_counter, recipe: None,+ }));}if return_by_value {@@ -2879,7 +2833,7 @@ impl ObscuraJsRuntime {let result = self.execute_runtime_script("<callFnByValue>", code).map_err(|e| format!("JS error: {}", e))?;- return self.v8_to_cdp_value(result);+ return self.v8_to_cdp_value(result).map(CdpEvaluation::Complete);}let code = format!(@@ -2907,7 +2861,61 @@ impl ObscuraJsRuntime {} else {meta_str};- self.finish_cdp_result(&meta_json, &oid)+ self.finish_cdp_result(&meta_json, &oid).map(CdpEvaluation::Complete)+ }++ #[doc(hidden)]+ pub fn cdp_evaluation_ready(&mut self, pending: &PendingCdpEvaluation) -> bool {+ let counter = pending.done_counter;+ self.execute_runtime_script("<done?>", format!(+ "globalThis.__obscura_done_{counter} === true && delete globalThis.__obscura_done_{counter}"+ )).ok().and_then(|value| self.v8_to_json(value).ok())+ .and_then(|value| value.as_bool()).unwrap_or(false)+ }++ #[doc(hidden)]+ pub fn finish_cdp_evaluation(+ &mut self,+ pending: PendingCdpEvaluation,+ return_by_value: bool,+ ) -> Result<RemoteObjectInfo, String> {+ let oid = pending.object_id;+ let rejected = self.execute_runtime_script(+ "<readRejected>", "globalThis.__obscura_await_rejected".to_string(),+ ).map_err(|e| format!("JS error: {e}"))?;+ if self.v8_to_json(rejected)?.as_bool().unwrap_or(false) {+ return self.thrown_info(&oid);+ }+ if return_by_value { return self.take_cdp_result_by_value(&oid); }+ let meta = self.execute_runtime_script(+ "<readMeta>", "globalThis.__obscura_await_meta".to_string(),+ ).map_err(|e| format!("JS error: {e}"))?;+ let meta = self.v8_to_json(meta)?;+ let meta = if let serde_json::Value::String(value) = &meta {+ serde_json::from_str(value).unwrap_or(meta)+ } else { meta };+ let info = self.finish_cdp_result(&meta, &oid)?;+ if info.object_id.is_some() {+ if let Some(recipe) = pending.recipe { self.evaluation_recipes.insert(oid, recipe); }+ }+ Ok(info)+ }++ async fn await_cdp_evaluation(+ &mut self,+ evaluation: CdpEvaluation,+ return_by_value: bool,+ timeout_ms: u64,+ method: &str,+ ) -> Result<RemoteObjectInfo, String> {+ let pending = match evaluation {+ CdpEvaluation::Complete(info) => return Ok(info),+ CdpEvaluation::Pending(pending) => pending,+ };+ if !self.resolve_promises_until(|rt| rt.cdp_evaluation_ready(&pending), timeout_ms).await {+ return Err(format!("{method} promise did not settle within {timeout_ms}ms"));+ }+ self.finish_cdp_evaluation(pending, return_by_value)}pub async fn call_function_on(&mut self,@@ -22097,8 +22105,8 @@ mod tests {#[tokio::test(flavor = "current_thread")]async fn iframe_origin_follows_the_final_redirect_url() {let mut rt = setup_runtime("<html><head></head><body></body></html>");- let result = rt- .call_function_on_for_cdp(+ let evaluation = rt+ .start_cdp_call(r#"async () => {const originalFetchOp = __obscura_test_ops.op_fetch_url;try {@@ -22127,9 +22135,21 @@ mod tests {true,true,)- .await.unwrap();+ // The embedder owns frame creation and fires the element's load after it.+ rt.run_event_loop_bounded(100).await.unwrap();+ let frame = rt.take_pending_frames().remove(0);+ let realm = crate::frame::FrameRealm::new(+ &mut rt, frame.frame_id, frame.parent_frame_id, &frame.url, &frame.html,+ ).unwrap();+ realm.dispatch_load_events(&mut rt).unwrap();+ rt.execute_script("frame-load", &format!(+ "globalThis.__obscura_frameElements[{}].dispatchEvent(new Event('load'));", frame.frame_id,+ )).unwrap();+ let result = rt.await_cdp_evaluation(evaluation, true, 1_000, "Runtime.callFunctionOn")+ .await.unwrap();+assert_eq!(result.value.unwrap(),serde_json::json!({