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): read macOS browser checkbox state (#3404)

injaneityb11709305896
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs b/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
index f50f240905..02ba09c01c 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/ax/bindings.rs
@@ -230,6 +230,35 @@ pub unsafe fn copy_bool_attr(element: AXUIElementRef, attr_name: &str) -> Option
None
}
+unsafe fn coerce_binary_value(value: CFTypeRef) -> Option<bool> {
+ use core_foundation::boolean::CFBoolean;
+ use core_foundation::number::CFNumber;
+ let type_id = core_foundation::base::CFGetTypeID(value);
+ if type_id == CFBoolean::type_id() {
+ return Some(CFBoolean::wrap_under_get_rule(value as _).into());
+ }
+ if type_id == CFNumber::type_id() {
+ return match CFNumber::wrap_under_get_rule(value as _).to_f64()? {
+ 0.0 => Some(false),
+ 1.0 => Some(true),
+ _ => None,
+ };
+ }
+ None
+}
+
+pub unsafe fn copy_binary_attr(element: AXUIElementRef, attr_name: &str) -> Option<bool> {
+ let attr = CFStr::new(attr_name);
+ let mut value: CFTypeRef = std::ptr::null();
+ let err = AXUIElementCopyAttributeValue(element, attr.as_concrete_TypeRef(), &mut value);
+ if err != kAXErrorSuccess || value.is_null() {
+ return None;
+ }
+ let result = coerce_binary_value(value);
+ CFRelease(value);
+ result
+}
+
/// A copied AX attribute represented for both existing string-only consumers
/// and the wider structured control-state response.
#[derive(Debug, PartialEq, Eq)]
@@ -724,6 +753,62 @@ mod tests {
use super::*;
use core_foundation::{boolean::CFBoolean, number::CFNumber};
+ #[test]
+ fn binary_value_accepts_booleans_and_exact_zero_or_one() {
+ let true_value = CFBoolean::true_value();
+ let false_value = CFBoolean::false_value();
+ let zero = CFNumber::from(0.0);
+ let one = CFNumber::from(1.0);
+ let fractional = CFNumber::from(0.5);
+ let other = CFNumber::from(2.0);
+ let string = CFStr::new("1");
+
+ assert_eq!(
+ unsafe { coerce_binary_value(true_value.as_CFTypeRef()) },
+ Some(true)
+ );
+ assert_eq!(
+ unsafe { coerce_binary_value(false_value.as_CFTypeRef()) },
+ Some(false)
+ );
+ assert_eq!(
+ unsafe { coerce_binary_value(zero.as_CFTypeRef()) },
+ Some(false)
+ );
+ assert_eq!(
+ unsafe { coerce_binary_value(one.as_CFTypeRef()) },
+ Some(true)
+ );
+ assert_eq!(
+ unsafe { coerce_binary_value(fractional.as_CFTypeRef()) },
+ None
+ );
+ assert_eq!(unsafe { coerce_binary_value(other.as_CFTypeRef()) }, None);
+ assert_eq!(unsafe { coerce_binary_value(string.as_CFTypeRef()) }, None);
+ }
+
+ #[test]
+ fn binary_value_rejects_near_binary_and_non_finite_numbers() {
+ for value in [
+ 1e-20,
+ -1e-20,
+ f64::from_bits(1),
+ -f64::from_bits(1),
+ f64::from_bits(1.0_f64.to_bits() - 1),
+ f64::from_bits(1.0_f64.to_bits() + 1),
+ f64::NAN,
+ f64::INFINITY,
+ f64::NEG_INFINITY,
+ ] {
+ let number = CFNumber::from(value);
+ assert_eq!(
+ unsafe { coerce_binary_value(number.as_CFTypeRef()) },
+ None,
+ "unexpected binary state for {value:?}"
+ );
+ }
+ }
+
#[test]
fn stringish_value_coerces_cfstring_cfnumber_and_cfboolean() {
let string = CFStr::new("Search");
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/browser/setup_ui.rs b/libs/cua-driver/rust/crates/platform-macos/src/browser/setup_ui.rs
index dcdc4ec0fe..270b4cb41c 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/browser/setup_ui.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/browser/setup_ui.rs
@@ -13,7 +13,7 @@ use cua_driver_core::browser::{
};
use crate::ax::bindings::{
- copy_number_attr, copy_string_attr, element_screen_center, focused_element_of_pid,
+ copy_binary_attr, copy_string_attr, element_screen_center, focused_element_of_pid,
kAXErrorSuccess, perform_action, set_bool_attr_true, set_string_attr, AXUIElementRef,
};
use crate::ax::tree::{walk_tree, AXNode, TreeWalkResult};
@@ -875,11 +875,11 @@ fn press_pixel_checkbox(
})
}
-fn checkbox_state(value: Option<f64>) -> Result<CheckboxState, BrowserRefusal> {
+fn checkbox_state(value: Option<bool>) -> Result<CheckboxState, BrowserRefusal> {
match value {
- Some(value) if value.abs() < f64::EPSILON => Ok(CheckboxState::Off),
- Some(value) if (value - 1.0).abs() < f64::EPSILON => Ok(CheckboxState::On),
- _ => Err(refusal(
+ Some(false) => Ok(CheckboxState::Off),
+ Some(true) => Ok(CheckboxState::On),
+ None => Err(refusal(
BrowserRefusalCode::BrowserWrongTargetRefused,
"the exact remote-debugging checkbox had an unknown checked state",
)),
@@ -923,7 +923,7 @@ impl SetupUiHandle {
let checkbox = exact_setup_checkbox(&tree, self.descriptor);
let result = match checkbox {
Ok(Some(element)) => {
- let value = unsafe { copy_number_attr(element as AXUIElementRef, "AXValue") };
+ let value = unsafe { copy_binary_attr(element as AXUIElementRef, "AXValue") };
match checkbox_state(value) {
Ok(CheckboxState::Off) => Some(true),
Ok(CheckboxState::On) => {
@@ -1493,7 +1493,7 @@ fn set_remote_debugging(
let checkbox = exact_setup_checkbox(&tree, descriptor);
match checkbox {
Ok(Some(element)) => {
- let value = unsafe { copy_number_attr(element as AXUIElementRef, "AXValue") };
+ let value = unsafe { copy_binary_attr(element as AXUIElementRef, "AXValue") };
match checkbox_state(value) {
Ok(state) if (state == CheckboxState::On) == desired_enabled => {
if desired_enabled
@@ -2348,15 +2348,11 @@ mod tests {
#[test]
fn unknown_checkbox_values_refuse() {
- assert_eq!(checkbox_state(Some(0.0)).unwrap(), CheckboxState::Off);
- assert_eq!(checkbox_state(Some(1.0)).unwrap(), CheckboxState::On);
+ assert_eq!(checkbox_state(Some(false)).unwrap(), CheckboxState::Off);
+ assert_eq!(checkbox_state(Some(true)).unwrap(), CheckboxState::On);
assert_eq!(
checkbox_state(None).unwrap_err().code,
BrowserRefusalCode::BrowserWrongTargetRefused
);
- assert_eq!(
- checkbox_state(Some(0.5)).unwrap_err().code,
- BrowserRefusalCode::BrowserWrongTargetRefused
- );
}
}