Debian Codegraph @

third_party/rust/authenticator/src/transport/windows/monitor.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::transport::device_selector::{DeviceID, DeviceSelectorEvent};
6 use crate::transport::platform::winapi::DeviceInfoSet;
7 use runloop::RunLoop;
8 use std::collections::{HashMap, HashSet};
9 use std::error::Error;
10 use std::iter::FromIterator;
11 use std::sync::{mpsc::Sender, Arc};
12 use std::thread;
13 use std::time::Duration;
14
15 pub struct Monitor<F>
16 where
17 F: Fn(String, Sender<DeviceSelectorEvent>, Sender<crate::StatusUpdate>, &dyn Fn() -> bool)
18 + Sync,
19 {
20 runloops: HashMap<String, RunLoop>,
21 new_device_cb: Arc<F>,
22 selector_sender: Sender<DeviceSelectorEvent>,
23 status_sender: Sender<crate::StatusUpdate>,
24 }
25
26 impl<F> Monitor<F>
27 where
28 F: Fn(String, Sender<DeviceSelectorEvent>, Sender<crate::StatusUpdate>, &dyn Fn() -> bool)
29 + Send
30 + Sync
31 + 'static,
32 {
33 pub fn new(
34 new_device_cb: F,
35 selector_sender: Sender<DeviceSelectorEvent>,
36 status_sender: Sender<crate::StatusUpdate>,
37 ) -> Self {
38 Self {
39 runloops: HashMap::new(),
40 new_device_cb: Arc::new(new_device_cb),
41 selector_sender,
42 status_sender,
43 }
44 }
45
46 pub fn run(&mut self, alive: &dyn Fn() -> bool) -> Result<(), Box<dyn Error>> {
47 let mut current = HashSet::new();
48 let mut previous;
49
50 while alive() {
51 let device_info_set = DeviceInfoSet::new()?;
52 previous = current;
53 current = HashSet::from_iter(device_info_set.devices());
54
55 // Remove devices that are gone.
56 for path in previous.difference(&current) {
57 self.remove_device(path);
58 }
59
60 let added: Vec<String> = current.difference(&previous).cloned().collect();
61
62 // We have to notify additions in batches to avoid
63 // arbitrarily selecting the first added device.
64 if !added.is_empty()
65 && self
66 .selector_sender
67 .send(DeviceSelectorEvent::DevicesAdded(added.clone()))
68 .is_err()
69 {
70 // Send only fails if the receiver hung up. We should exit the loop.
71 break;
72 }
73
74 // Add devices that were plugged in.
75 for path in added {
76 self.add_device(&path);
77 }
78
79 // Wait a little before looking for devices again.
80 thread::sleep(Duration::from_millis(100));
81 }
82
83 // Remove all tracked devices.
84 self.remove_all_devices();
85
86 Ok(())
87 }
88
89 fn add_device(&mut self, path: &DeviceID) {
90 let f = self.new_device_cb.clone();
91 let path = path.clone();
92 let key = path.clone();
93 let selector_sender = self.selector_sender.clone();
94 let status_sender = self.status_sender.clone();
95 debug!("Adding device {}", path);
96
97 let runloop = RunLoop::new(move |alive| {
98 if alive() {
99 f(path, selector_sender, status_sender, alive);
100 }
101 });
102
103 if let Ok(runloop) = runloop {
104 self.runloops.insert(key, runloop);
105 }
106 }
107
108 fn remove_device(&mut self, path: &DeviceID) {
109 let _ = self
110 .selector_sender
111 .send(DeviceSelectorEvent::DeviceRemoved(path.clone()));
112
113 debug!("Removing device {}", path);
114 if let Some(runloop) = self.runloops.remove(path) {
115 runloop.cancel();
116 }
117 }
118
119 fn remove_all_devices(&mut self) {
120 while !self.runloops.is_empty() {
121 let path = self.runloops.keys().next().unwrap().clone();
122 self.remove_device(&path);
123 }
124 }
125 }