Debian Codegraph @

third_party/rust/authenticator/src/transport/macos/transaction.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 extern crate libc;
6
7 use crate::errors;
8 use crate::statecallback::StateCallback;
9 use crate::transport::device_selector::{
10 DeviceBuildParameters, DeviceSelector, DeviceSelectorEvent,
11 };
12 use crate::transport::platform::iokit::{CFRunLoopEntryObserver, SendableRunLoop};
13 use crate::transport::platform::monitor::Monitor;
14 use core_foundation::runloop::*;
15 use std::os::raw::c_void;
16 use std::sync::mpsc::{channel, Sender};
17 use std::thread;
18
19 // A transaction will run the given closure in a new thread, thereby using a
20 // separate per-thread state machine for each HID. It will either complete or
21 // fail through user action, timeout, or be cancelled when overridden by a new
22 // transaction.
23 pub struct Transaction {
24 runloop: Option<SendableRunLoop>,
25 thread: Option<thread::JoinHandle<()>>,
26 device_selector: DeviceSelector,
27 }
28
29 impl Transaction {
30 pub fn new<F, T>(
31 timeout: u64,
32 callback: StateCallback<crate::Result<T>>,
33 status: Sender<crate::StatusUpdate>,
34 new_device_cb: F,
35 ) -> crate::Result<Self>
36 where
37 F: Fn(
38 DeviceBuildParameters,
39 Sender<DeviceSelectorEvent>,
40 Sender<crate::StatusUpdate>,
41 &dyn Fn() -> bool,
42 ) + Sync
43 + Send
44 + 'static,
45 T: 'static,
46 {
47 let (tx, rx) = channel();
48 let timeout = (timeout as f64) / 1000.0;
49 let device_selector = DeviceSelector::run();
50 let selector_sender = device_selector.clone_sender();
51 let builder = thread::Builder::new();
52 let thread = builder
53 .spawn(move || {
54 // Add a runloop observer that will be notified when we enter the
55 // runloop and tx.send() the current runloop to the owning thread.
56 // We need to ensure the runloop was entered before unblocking
57 // Transaction::new(), so we can always properly cancel.
58 let context = &tx as *const _ as *mut c_void;
59 let obs = CFRunLoopEntryObserver::new(Transaction::observe, context);
60 obs.add_to_current_runloop();
61
62 // Create a new HID device monitor and start polling.
63 let mut monitor = Monitor::new(new_device_cb, selector_sender, status);
64 try_or!(monitor.start(), |_| callback
65 .call(Err(errors::AuthenticatorError::Platform)));
66
67 // This will block until completion, abortion, or timeout.
68 unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, 0) };
69
70 // Close the monitor and its devices.
71 monitor.stop();
72
73 // Send an error, if the callback wasn't called already.
74 callback.call(Err(errors::AuthenticatorError::U2FToken(
75 errors::U2FTokenError::NotAllowed,
76 )));
77 })
78 .map_err(|_| errors::AuthenticatorError::Platform)?;
79
80 // Block until we enter the CFRunLoop.
81 let runloop = rx
82 .recv()
83 .map_err(|_| errors::AuthenticatorError::Platform)?;
84
85 Ok(Self {
86 runloop: Some(runloop),
87 thread: Some(thread),
88 device_selector,
89 })
90 }
91
92 extern "C" fn observe(_: CFRunLoopObserverRef, _: CFRunLoopActivity, context: *mut c_void) {
93 let tx: &Sender<SendableRunLoop> = unsafe { &*(context as *mut _) };
94
95 // Send the current runloop to the receiver to unblock it.
96 let _ = tx.send(SendableRunLoop::new(unsafe { CFRunLoopGetCurrent() }));
97 }
98
99 pub fn cancel(&mut self) {
100 // This must never be None. This won't block.
101 unsafe { CFRunLoopStop(*self.runloop.take().unwrap()) };
102
103 self.device_selector.stop();
104 // This must never be None. Ignore return value.
105 let _ = self.thread.take().unwrap().join();
106 }
107 }