Debian Codegraph @

third_party/rust/authenticator/src/transport/linux/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 use crate::errors;
6 use crate::statecallback::StateCallback;
7 use crate::transport::device_selector::{
8 DeviceBuildParameters, DeviceSelector, DeviceSelectorEvent,
9 };
10 use crate::transport::platform::monitor::Monitor;
11 use runloop::RunLoop;
12 use std::sync::mpsc::Sender;
13
14 pub struct Transaction {
15 // Handle to the thread loop.
16 thread: RunLoop,
17 device_selector: DeviceSelector,
18 }
19
20 impl Transaction {
21 pub fn new<F, T>(
22 timeout: u64,
23 callback: StateCallback<crate::Result<T>>,
24 status: Sender<crate::StatusUpdate>,
25 new_device_cb: F,
26 ) -> crate::Result<Self>
27 where
28 F: Fn(
29 DeviceBuildParameters,
30 Sender<DeviceSelectorEvent>,
31 Sender<crate::StatusUpdate>,
32 &dyn Fn() -> bool,
33 ) + Sync
34 + Send
35 + 'static,
36 T: 'static,
37 {
38 let device_selector = DeviceSelector::run();
39 let selector_sender = device_selector.clone_sender();
40 let thread = RunLoop::new_with_timeout(
41 move |alive| {
42 // Create a new device monitor.
43 let mut monitor = Monitor::new(new_device_cb, selector_sender, status);
44
45 // Start polling for new devices.
46 try_or!(monitor.run(alive), |_| callback
47 .call(Err(errors::AuthenticatorError::Platform)));
48
49 // Send an error, if the callback wasn't called already.
50 callback.call(Err(errors::AuthenticatorError::U2FToken(
51 errors::U2FTokenError::NotAllowed,
52 )));
53 },
54 timeout,
55 )
56 .map_err(|_| errors::AuthenticatorError::Platform)?;
57
58 Ok(Self {
59 thread,
60 device_selector,
61 })
62 }
63
64 pub fn cancel(&mut self) {
65 info!("Transaction was cancelled.");
66 self.device_selector.stop();
67 self.thread.cancel();
68 }
69 }