Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): finish Edge 153 session cleanup when its accessibility tree is briefly empty (#4158)

Francesco Bonacci0b9a1bb46ffc
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs b/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
index 7f42fa7a9e..98cb11de7a 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/browser_setup_ui.rs
@@ -37,6 +37,21 @@ fn release_nodes(nodes: &[UiaNode]) {
}
}
+/// Browser chrome only: tab strip, toolbar, omnibox, and the active tab's
+/// `Document` node without its renderer-owned descendants. Tab topology and
+/// the committed setup URL are native facts, so reading them must not depend
+/// on the page renderer answering UIA (#4121).
+fn native_nodes(hwnd: u64) -> Result<Vec<UiaNode>, BrowserRefusal> {
+ crate::uia::walk_native_tree(hwnd).map_err(|error| {
+ refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ format!(
+ "the approved browser window's native accessibility tree was unavailable: {error}"
+ ),
+ )
+ })
+}
+
fn unique_web_actionable(
nodes: &[UiaNode],
control_type: &str,
@@ -176,9 +191,14 @@ fn stable_native_tab_count(hwnd: u64, initial_count: usize) -> Result<usize, Bro
let mut previous = initial_count;
loop {
std::thread::sleep(Duration::from_millis(100));
- let tree = crate::uia::walk_tree(hwnd, None);
- let current = native_tab_count(&tree.nodes);
- release_nodes(&tree.nodes);
+ let current = match native_nodes(hwnd) {
+ Ok(nodes) => {
+ let current = native_tab_count(&nodes);
+ release_nodes(&nodes);
+ current
+ }
+ Err(_) => 0,
+ };
if current > 0 && current == previous {
return Ok(current);
}
@@ -212,6 +232,13 @@ fn setup_page_proven(nodes: &[UiaNode], descriptor: &BrowserSetupDescriptor) ->
exact_url_count == 1 && document_count == 1
}
+/// Whether the setup flow must read renderer-owned nodes before it acts: only
+/// when the committed active tab is already the exact setup page, whose
+/// checkbox lives in web content. Every other page is left unread (#4121).
+fn needs_setup_page_renderer(native: &[UiaNode], descriptor: &BrowserSetupDescriptor) -> bool {
+ setup_page_proven(native, descriptor)
+}
+
fn exact_setup_checkbox(
nodes: &[UiaNode],
descriptor: &BrowserSetupDescriptor,
@@ -428,9 +455,11 @@ impl SetupUiHandle {
if !self.opened_setup_page {
return Ok(None);
}
- let tree = crate::uia::walk_tree(self.hwnd, None);
- let proven = setup_page_proven(&tree.nodes, self.descriptor);
- release_nodes(&tree.nodes);
+ let proven = native_nodes(self.hwnd).is_ok_and(|nodes| {
+ let proven = setup_page_proven(&nodes, self.descriptor);
+ release_nodes(&nodes);
+ proven
+ });
if !proven {
let error = refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
@@ -454,9 +483,11 @@ impl SetupUiHandle {
if !self.opened_setup_page {
return None;
}
- let tree = crate::uia::walk_tree(self.hwnd, None);
- let proven = setup_page_proven(&tree.nodes, self.descriptor);
- release_nodes(&tree.nodes);
+ let proven = native_nodes(self.hwnd).is_ok_and(|nodes| {
+ let proven = setup_page_proven(&nodes, self.descriptor);
+ release_nodes(&nodes);
+ proven
+ });
Some(
proven
&& crate::input::keyboard::send_key_synthesized(self.hwnd, "w", &["ctrl"]).is_ok(),
@@ -510,8 +541,19 @@ fn set_remote_debugging(
descriptor: &'static BrowserSetupDescriptor,
desired_enabled: bool,
) -> Result<SetupUiHandle, BrowserRefusal> {
- let initial = crate::uia::walk_tree(hwnd, None);
- let initial_checkbox = exact_setup_checkbox(&initial.nodes, descriptor);
+ let initial_native = native_nodes(hwnd)?;
+ let setup_page_active = needs_setup_page_renderer(&initial_native, descriptor);
+ let initial_tab_count = native_tab_count(&initial_native);
+ release_nodes(&initial_native);
+ // Only the setup page's own checkbox needs renderer-owned nodes. Any other
+ // active page is left unread, so an unresponsive renderer cannot stall
+ // setup or cleanup before the dedicated setup tab exists.
+ let initial = if setup_page_active {
+ crate::uia::walk_tree(hwnd, None).nodes
+ } else {
+ Vec::new()
+ };
+ let initial_checkbox = exact_setup_checkbox(&initial, descriptor);
let mut handle = match initial_checkbox {
Ok(Some(_)) => SetupUiHandle {
hwnd,
@@ -524,8 +566,7 @@ fn set_remote_debugging(
enable_attempted: false,
},
Ok(None) => {
- let initial_tab_count = native_tab_count(&initial.nodes);
- release_nodes(&initial.nodes);
+ release_nodes(&initial);
let tab_count_before = stable_native_tab_count(hwnd, initial_tab_count)?;
let mut handle = SetupUiHandle {
@@ -538,11 +579,14 @@ fn set_remote_debugging(
injected_global_input: false,
enable_attempted: false,
};
- let tab_tree = crate::uia::walk_tree(hwnd, None);
- let new_tab_button = match exact_native_new_tab_button(&tab_tree.nodes) {
+ let tab_tree = match native_nodes(hwnd) {
+ Ok(nodes) => nodes,
+ Err(error) => return Err(handle.abort(error)),
+ };
+ let new_tab_button = match exact_native_new_tab_button(&tab_tree) {
Ok(Some(element)) => element,
Ok(None) => {
- release_nodes(&tab_tree.nodes);
+ release_nodes(&tab_tree);
return Err(handle.abort(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
format!(
@@ -552,25 +596,25 @@ fn set_remote_debugging(
)));
}
Err(error) => {
- release_nodes(&tab_tree.nodes);
+ release_nodes(&tab_tree);
return Err(handle.abort(error));
}
};
if let Err(error) = unsafe { invoke(new_tab_button, "native new-tab button") } {
- release_nodes(&tab_tree.nodes);
+ release_nodes(&tab_tree);
return Err(handle.abort(error));
}
- release_nodes(&tab_tree.nodes);
+ release_nodes(&tab_tree);
handle.opened_setup_page = true;
let deadline = Instant::now() + Duration::from_secs(3);
let mut created = loop {
- let tree = crate::uia::walk_tree(hwnd, None);
- let tab_count_after = native_tab_count(&tree.nodes);
+ let tree = native_nodes(hwnd).unwrap_or_default();
+ let tab_count_after = native_tab_count(&tree);
if tab_count_after == tab_count_before + 1 {
break tree;
}
- release_nodes(&tree.nodes);
+ release_nodes(&tree);
if Instant::now() >= deadline {
return Err(handle.abort(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
@@ -582,10 +626,10 @@ fn set_remote_debugging(
}
std::thread::sleep(Duration::from_millis(100));
};
- let omnibox = match unique_native_actionable(&created.nodes, "Edit", "set_value") {
+ let omnibox = match unique_native_actionable(&created, "Edit", "set_value") {
Ok(Some(element)) => element,
Ok(None) => {
- release_nodes(&created.nodes);
+ release_nodes(&created);
return Err(handle.abort(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
format!(
@@ -595,40 +639,42 @@ fn set_remote_debugging(
)));
}
Err(error) => {
- release_nodes(&created.nodes);
+ release_nodes(&created);
return Err(handle.abort(error));
}
};
if let Err(error) = unsafe { set_value(omnibox, descriptor.setup_url) } {
- release_nodes(&created.nodes);
+ release_nodes(&created);
return Err(handle.abort(error));
}
- release_nodes(&created.nodes);
- created = crate::uia::walk_tree(hwnd, None);
- let refreshed_omnibox =
- match unique_native_actionable(&created.nodes, "Edit", "set_value") {
- Ok(Some(element))
- if created.nodes.iter().any(|node| {
- node.element_ptr == element
- && node.value.as_deref().is_some_and(|value| {
- value.trim().eq_ignore_ascii_case(descriptor.setup_url)
- })
- }) =>
- {
- element
- }
- Ok(_) => {
- release_nodes(&created.nodes);
- return Err(handle.abort(refusal(
- BrowserRefusalCode::BrowserWrongTargetRefused,
- "the unique native address field did not retain the exact setup URL",
- )));
- }
- Err(error) => {
- release_nodes(&created.nodes);
- return Err(handle.abort(error));
- }
- };
+ release_nodes(&created);
+ created = match native_nodes(hwnd) {
+ Ok(nodes) => nodes,
+ Err(error) => return Err(handle.abort(error)),
+ };
+ let refreshed_omnibox = match unique_native_actionable(&created, "Edit", "set_value") {
+ Ok(Some(element))
+ if created.iter().any(|node| {
+ node.element_ptr == element
+ && node.value.as_deref().is_some_and(|value| {
+ value.trim().eq_ignore_ascii_case(descriptor.setup_url)
+ })
+ }) =>
+ {
+ element
+ }
+ Ok(_) => {
+ release_nodes(&created);
+ return Err(handle.abort(refusal(
+ BrowserRefusalCode::BrowserWrongTargetRefused,
+ "the unique native address field did not retain the exact setup URL",
+ )));
+ }
+ Err(error) => {
+ release_nodes(&created);
+ return Err(handle.abort(error));
+ }
+ };
if let Err(error) = confirm_setup_navigation(
hwnd,
refreshed_omnibox,
@@ -636,19 +682,19 @@ fn set_remote_debugging(
&mut handle.injected_global_input,
&mut handle.focused_setup_address_field,
) {
- release_nodes(&created.nodes);
+ release_nodes(&created);
return Err(handle.abort(error));
}
- release_nodes(&created.nodes);
+ release_nodes(&created);
handle
}
Err(error) => {
- release_nodes(&initial.nodes);
+ release_nodes(&initial);
return Err(error);
}
};
if !handle.opened_setup_page {
- release_nodes(&initial.nodes);
+ release_nodes(&initial);
}
let deadline = Instant::now() + EXISTING_PROFILE_SETUP_READY_TIMEOUT;
@@ -998,4 +1044,166 @@ mod tests {
Some(30)
);
}
+
+ /// Native Edge 153.0.4234.48 chrome captured on hosted Windows while the
+ /// fixture renderer stopped answering UIA during `end_session` (#4121).
+ fn edge_153_native_chrome(url: &str, document: &str) -> Vec<UiaNode> {
+ let mut rows = Vec::new();
+ let mut push = |control_type: &str,
+ name: &str,
+ value: Option<&str>,
+ actions: &[&str],
+ depth: usize,
+ ptr: usize,
+ rect: Option<(i32, i32, i32, i32)>| {
+ let mut row = node(control_type, name, value, actions);
+ row.depth = depth;
+ row.element_ptr = ptr;
+ row.element_index = (!actions.is_empty()).then_some(ptr);
+ row.rect = rect;
+ rows.push(row);
+ };
+ push(
+ "Window",
+ "cua-driver Web Harness - Profile 1 - Microsoft Edge",
+ None,
+ &[],
+ 0,
+ 1,
+ None,
+ );
+ push(
+ "Pane",
+ "cua-driver Web Harness - Microsoft Edge",
+ None,
+ &[],
+ 1,
+ 2,
+ None,
+ );
+ push(
+ "Button",
+ "Minimize",
+ None,
+ &["invoke"],
+ 5,
+ 3,
+ Some((794, 41, 840, 80)),
+ );
+ push(
+ "Button",
+ "Maximize",
+ None,
+ &["invoke"],
+ 5,
+ 4,
+ Some((840, 41, 886, 80)),
+ );
+ push(
+ "Button",
+ "Close",
+ None,
+ &["invoke"],
+ 5,
+ 5,
+ Some((886, 41, 932, 80)),
+ );
+ push("ToolBar", "App bar", None, &[], 6, 6, None);
+ push(
+ "Button",
+ "Back",
+ None,
+ &["invoke"],
+ 7,
+ 7,
+ Some((52, 84, 84, 116)),
+ );
+ push(
+ "Button",
+ "Refresh",
+ None,
+ &["invoke"],
+ 7,
+ 8,
+ Some((88, 84, 120, 116)),
+ );
+ push(
+ "Edit",
+ "Address and search bar",
+ Some(url),
+ &["expand", "set_value", "text"],
+ 8,
+ 9,
+ Some((152, 88, 689, 112)),
+ );
+ push(
+ "Document",
+ document,
+ None,
+ &["set_value", "text", "scroll"],
+ 7,
+ 10,
+ Some((48, 127, 932, 672)),
+ );
+ push("Tab", "Tab bar", None, &[], 5, 11, None);
+ push(
+ "Button",
+ "Search tabs",
+ None,
+ &["expand"],
+ 6,
+ 12,
+ Some((52, 40, 84, 76)),
+ );
+ push(
+ "TabItem",
+ "cua-driver Web Harness - Memory usage - 26.6 MB",
+ None,
+ &["select"],
+ 8,
+ 13,
+ Some((80, 40, 336, 81)),
+ );
+ push(
+ "Button",
+ "Close tab",
+ None,
+ &["invoke"],
+ 9,
+ 14,
+ Some((304, 52, 320, 68)),
+ );
+ push(
+ "Button",
+ "New Tab",
+ None,
+ &["invoke"],
+ 6,
+ 15,
+ Some((332, 40, 364, 81)),
+ );
+ rows
+ }
+
+ #[test]
+ fn edge_153_cleanup_uses_native_chrome_without_reading_the_page_renderer() {
+ let edge = existing_profile_setup_descriptor(BrowserProduct::MicrosoftEdge).unwrap();
+ let fixture = edge_153_native_chrome("127.0.0.1:58963/fixture", "cua-driver Web Harness");
+ assert!(
+ !needs_setup_page_renderer(&fixture, edge),
+ "an unrelated active page must not require renderer-owned nodes"
+ );
+ assert_eq!(native_tab_count(&fixture), 1);
+ assert_eq!(exact_native_new_tab_button(&fixture).unwrap(), Some(15));
+ assert_eq!(
+ unique_native_actionable_with_focus(&fixture, "Edit", "set_value", |_| false).unwrap(),
+ Some(9)
+ );
+
+ let setup = edge_153_native_chrome(edge.setup_url, edge.page_titles[0]);
+ assert!(
+ needs_setup_page_renderer(&setup, edge),
+ "the committed setup page still requires its renderer-owned checkbox"
+ );
+ }
}
diff --git a/libs/cua-driver/rust/crates/platform-windows/src/uia/mod.rs b/libs/cua-driver/rust/crates/platform-windows/src/uia/mod.rs
index 24e2217e48..6a15ce99b4 100644
--- a/libs/cua-driver/rust/crates/platform-windows/src/uia/mod.rs
+++ b/libs/cua-driver/rust/crates/platform-windows/src/uia/mod.rs
@@ -18,13 +18,14 @@ use windows::Win32::UI::Accessibility::{
CUIAutomation, IUIAutomation, IUIAutomationCacheRequest, IUIAutomationElement,
IUIAutomationExpandCollapsePattern, IUIAutomationInvokePattern,
IUIAutomationSelectionItemPattern, IUIAutomationTogglePattern, ToggleState_Off, ToggleState_On,
- TreeScope_Children, TreeScope_Subtree, UIA_AutomationIdPropertyId,
- UIA_BoundingRectanglePropertyId, UIA_ControlTypePropertyId, UIA_ExpandCollapsePatternId,
- UIA_HelpTextPropertyId, UIA_InvokePatternId, UIA_IsEnabledPropertyId,
- UIA_IsOffscreenPropertyId, UIA_NamePropertyId, UIA_ProcessIdPropertyId,
- UIA_RangeValuePatternId, UIA_ScrollPatternId, UIA_SelectionItemIsSelectedPropertyId,
- UIA_SelectionItemPatternId, UIA_TextPatternId, UIA_TogglePatternId,
- UIA_ToggleToggleStatePropertyId, UIA_ValuePatternId, UIA_ValueValuePropertyId,
+ TreeScope, TreeScope_Children, TreeScope_Element, TreeScope_Subtree,
+ UIA_AutomationIdPropertyId, UIA_BoundingRectanglePropertyId, UIA_ControlTypePropertyId,
+ UIA_ExpandCollapsePatternId, UIA_HelpTextPropertyId, UIA_InvokePatternId,
+ UIA_IsEnabledPropertyId, UIA_IsOffscreenPropertyId, UIA_NamePropertyId,
+ UIA_ProcessIdPropertyId, UIA_RangeValuePatternId, UIA_ScrollPatternId,
+ UIA_SelectionItemIsSelectedPropertyId, UIA_SelectionItemPatternId, UIA_TextPatternId,
+ UIA_TogglePatternId, UIA_ToggleToggleStatePropertyId, UIA_ValuePatternId,
+ UIA_ValueValuePropertyId,
};
pub mod cache;
@@ -277,7 +278,7 @@ unsafe fn walk_tree_unsafe(
};
// Build a cache request that fetches everything we need in ONE bulk RPC.
- let cache_req: IUIAutomationCacheRequest = match automation.CreateCacheRequest() {
+ let cache_req = match node_cache_request(&automation) {
Ok(r) => r,
Err(e) => {
return UiaTreeResult {
@@ -287,36 +288,6 @@ unsafe fn walk_tree_unsafe(
}
};
- // Properties to pre-fetch.
- for prop in &[
- UIA_ControlTypePropertyId,
- UIA_NamePropertyId,
- UIA_ValueValuePropertyId,
- UIA_AutomationIdPropertyId,
- UIA_HelpTextPropertyId,
- UIA_IsEnabledPropertyId,
- UIA_IsOffscreenPropertyId,
- UIA_BoundingRectanglePropertyId,
- UIA_ToggleToggleStatePropertyId,
- UIA_SelectionItemIsSelectedPropertyId,
- ] {
- let _ = cache_req.AddProperty(*prop);
- }
-
- // Patterns to pre-fetch (for action detection).
- for pat in &[
- UIA_InvokePatternId,
- UIA_TogglePatternId,
- UIA_SelectionItemPatternId,
- UIA_ExpandCollapsePatternId,
- UIA_ValuePatternId,
- UIA_RangeValuePatternId,
- UIA_TextPatternId,
- UIA_ScrollPatternId,
- ] {
- let _ = cache_req.AddPattern(*pat);
- }
-
// Fetch entire subtree in one call.
let _ = cache_req.SetTreeScope(TreeScope_Subtree);
@@ -665,6 +636,181 @@ unsafe fn walk_cached_bounded(
return;
}
+ let (control_type, emitted_parent) = emit_cached_node(
+ element,
+ depth,
+ parent_index,
+ in_web_content,
+ nodes,
+ lines,
+ counter,
+ );
+
+ // Recurse using cached children (no additional RPC).
+ if let Ok(children) = element.GetCachedChildren() {
+ let len = children.Length().unwrap_or(0);
+ for i in 0..len {
+ if let Ok(child) = children.GetElement(i) {
+ walk_cached_bounded(
+ &child,
+ depth + 1,
+ emitted_parent,
+ in_web_content || control_type.eq_ignore_ascii_case("Document"),
+ nodes,
+ lines,
+ counter,
+ budget,
+ max_depth,
+ );
+ }
+ }
+ }
+}
+
+/// Cache request carrying every property and pattern the node projection
+/// reads. Callers choose the tree scope and filter.
+unsafe fn node_cache_request(
+ automation: &IUIAutomation,
+) -> windows::core::Result<IUIAutomationCacheRequest> {
+ let cache_req = automation.CreateCacheRequest()?;
+ for prop in &[
+ UIA_ControlTypePropertyId,
+ UIA_NamePropertyId,
+ UIA_ValueValuePropertyId,
+ UIA_AutomationIdPropertyId,
+ UIA_HelpTextPropertyId,
+ UIA_IsEnabledPropertyId,
+ UIA_IsOffscreenPropertyId,
+ UIA_BoundingRectanglePropertyId,
+ UIA_ToggleToggleStatePropertyId,
+ UIA_SelectionItemIsSelectedPropertyId,
+ ] {
+ let _ = cache_req.AddProperty(*prop);
+ }
+ for pat in &[
+ UIA_InvokePatternId,
+ UIA_TogglePatternId,
+ UIA_SelectionItemPatternId,
+ UIA_ExpandCollapsePatternId,
+ UIA_ValuePatternId,
+ UIA_RangeValuePatternId,
+ UIA_TextPatternId,
+ UIA_ScrollPatternId,
+ ] {
+ let _ = cache_req.AddPattern(*pat);
+ }
+ Ok(cache_req)
+}
+
+/// Walk only a window's native (non-renderer) accessibility tree.
+///
+/// [`walk_tree`] fetches the whole subtree in one `BuildUpdatedCache` call,
+/// which includes the active tab's web document. When a Chromium renderer
+/// stops answering UIA, that one call times out and the walk returns no
+/// nodes at all, even though the browser's own tab strip, toolbar and
+/// omnibox are still live (#4121: Edge 153 after a CDP session, about 10 s
+/// per walk). This walker fetches one level per call and never descends
+/// below a `Document`, so browser chrome stays observable. The `Document`
+/// node itself is still reported, so callers can prove which page is
+/// committed without trusting any renderer-owned descendant.
+///
+/// Returns `Err` when any native level cannot be read. A partial native tree
+/// would misstate tab topology, so callers must treat it as unavailable.
+pub fn walk_native_tree(hwnd: u64) -> Result<Vec<UiaNode>, String> {
+ unsafe {
+ let _ = CoInitializeEx(None, COINIT_MULTITHREADED);
+ let automation: IUIAutomation =
+ CoCreateInstance(&CUIAutomation, None, CLSCTX_INPROC_SERVER)
+ .map_err(|e| format!("UIA init failed: {e}"))?;
+ let cache_req = node_cache_request(&automation)
+ .map_err(|e| format!("CreateCacheRequest failed: {e}"))?;
+ let _ = cache_req.SetTreeScope(TreeScope(TreeScope_Element.0 | TreeScope_Children.0));
+ if let Ok(ctrl_cond) = automation.ControlViewCondition() {
+ let _ = cache_req.SetTreeFilter(&ctrl_cond);
+ }
+ let root = automation
+ .ElementFromHandle(windows::Win32::Foundation::HWND(hwnd as *mut _))
+ .and_then(|element| element.BuildUpdatedCache(&cache_req))
+ .map_err(|e| format!("native UIA root could not be read: {e}"))?;
+ let mut nodes = Vec::new();
+ let mut lines = Vec::new();
+ let mut counter = 0usize;
+ let mut budget = WalkBudget::nodes_only(DEFAULT_MAX_TOTAL_ELEMENTS);
+ let walked = walk_native_level(
+ &root,
+ &cache_req,
+ 0,
+ None,
+ &mut nodes,
+ &mut lines,
+ &mut counter,
+ &mut budget,
+ );
+ match walked {
+ Ok(()) => Ok(nodes),
+ Err(error) => {
+ release_walk_nodes(nodes);
+ Err(error)
+ }
+ }
+ }
+}
+
+#[allow(clippy::too_many_arguments)]
+unsafe fn walk_native_level(
+ element: &IUIAutomationElement,
+ cache_req: &IUIAutomationCacheRequest,
+ depth: usize,
+ parent_index: Option<usize>,
+ nodes: &mut Vec<UiaNode>,
+ lines: &mut Vec<(usize, String)>,
+ counter: &mut usize,
+ budget: &mut WalkBudget,
+) -> Result<(), String> {
+ if depth > DEFAULT_MAX_DEPTH {
+ return Ok(());
+ }
+ if !budget.admit() {
+ return Err("the native UIA tree exceeded the element budget".to_owned());
+ }
+ let (control_type, emitted_parent) =
+ emit_cached_node(element, depth, parent_index, false, nodes, lines, counter);
+ if control_type.eq_ignore_ascii_case("Document") {
+ return Ok(());
+ }
+ let Ok(children) = element.GetCachedChildren() else {
+ return Ok(());
+ };
+ for i in 0..children.Length().unwrap_or(0) {
+ let child = children
+ .GetElement(i)
+ .and_then(|child| child.BuildUpdatedCache(cache_req))
+ .map_err(|e| format!("a native UIA level could not be read: {e}"))?;
+ walk_native_level(
+ &child,
+ cache_req,
+ depth + 1,
+ emitted_parent,
+ nodes,
+ lines,
+ counter,
+ budget,
+ )?;
+ }
+ Ok(())
+}
+
+/// Project one cached element into `nodes`/`lines`. Returns its control type
+/// and the parent index its descendants should inherit.
+fn emit_cached_node(
+ element: &IUIAutomationElement,
+ depth: usize,
+ parent_index: Option<usize>,
+ in_web_content: bool,
+ nodes: &mut Vec<UiaNode>,
+ lines: &mut Vec<(usize, String)>,
+ counter: &mut usize,
+) -> (String, Option<usize>) {
let control_type = read_cached_control_type(element);
let name = read_cached_bstr_name(element);
let value = read_cached_bstr_value(element);
@@ -741,26 +887,7 @@ unsafe fn walk_cached_bounded(
lines.push((depth, format_node_line(&node)));
nodes.push(node);
}
-
- // Recurse using cached children (no additional RPC).
- if let Ok(children) = element.GetCachedChildren() {
- let len = children.Length().unwrap_or(0);
- for i in 0..len {
- if let Ok(child) = children.GetElement(i) {
- walk_cached_bounded(
- &child,
- depth + 1,
- emitted_parent,
- in_web_content || control_type.eq_ignore_ascii_case("Document"),
- nodes,
- lines,
- counter,
- budget,
- max_depth,
- );
- }
- }
- }
+ (control_type, emitted_parent)
}
fn read_cached_control_type(element: &IUIAutomationElement) -> String {