extern crate libc;
use crate::errors;
use crate::statecallback::StateCallback;
use crate::transport::device_selector::{
DeviceBuildParameters, DeviceSelector, DeviceSelectorEvent,
};
use crate::transport::platform::iokit::{CFRunLoopEntryObserver, SendableRunLoop};
use crate::transport::platform::monitor::Monitor;
use core_foundation::runloop::*;
use std::os::raw::c_void;
use std::sync::mpsc::{channel, Sender};
use std::thread;
pub struct Transaction {
runloop: Option<SendableRunLoop>,
thread: Option<thread::JoinHandle<()>>,
device_selector: DeviceSelector,
}
impl Transaction {
pub fn new<F, T>(
timeout: u64,
callback: StateCallback<crate::Result<T>>,
status: Sender<crate::StatusUpdate>,
new_device_cb: F,
) -> crate::Result<Self>
where
F: Fn(
DeviceBuildParameters,
Sender<DeviceSelectorEvent>,
Sender<crate::StatusUpdate>,
&dyn Fn() -> bool,
) + Sync
+ Send
+ 'static,
T: 'static,
{
let (tx, rx) = channel();
let timeout = (timeout as f64) / 1000.0;
let status_sender = status.clone();
let device_selector = DeviceSelector::run(status);
let selector_sender = device_selector.clone_sender();
let builder = thread::Builder::new();
let thread = builder
.spawn(move || {
let context = &tx as *const _ as *mut c_void;
let obs = CFRunLoopEntryObserver::new(Transaction::observe, context);
obs.add_to_current_runloop();
let mut monitor = Monitor::new(new_device_cb, selector_sender, status_sender);
try_or!(monitor.start(), |_| callback
.call(Err(errors::AuthenticatorError::Platform)));
unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, 0) };
monitor.stop();
callback.call(Err(errors::AuthenticatorError::U2FToken(
errors::U2FTokenError::NotAllowed,
)));
})
.map_err(|_| errors::AuthenticatorError::Platform)?;
let runloop = rx
.recv()
.map_err(|_| errors::AuthenticatorError::Platform)?;
Ok(Self {
runloop: Some(runloop),
thread: Some(thread),
device_selector,
})
}
extern "C" fn observe(_: CFRunLoopObserverRef, _: CFRunLoopActivity, context: *mut c_void) {
let tx: &Sender<SendableRunLoop> = unsafe { &*(context as *mut _) };
let _ = tx.send(SendableRunLoop::new(unsafe { CFRunLoopGetCurrent() }));
}
pub fn cancel(&mut self) {
unsafe { CFRunLoopStop(*self.runloop.take().unwrap()) };
self.device_selector.stop();
let _ = self.thread.take().unwrap().join();
}
}