refactor(bluetooth): factory fn returns constellation channel

This commit is contained in:
OJ Kwon 2018-04-06 12:25:03 -07:00
parent c2161da3ca
commit 410cf63a8e
No known key found for this signature in database
GPG key ID: FFCFEF3460FD1901
4 changed files with 26 additions and 21 deletions

View file

@ -7,6 +7,7 @@ extern crate bitflags;
extern crate bluetooth_traits;
extern crate device;
extern crate ipc_channel;
extern crate script_traits;
extern crate servo_config;
extern crate servo_rand;
#[cfg(target_os = "linux")]
@ -23,6 +24,7 @@ use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSeque
use device::bluetooth::{BluetoothAdapter, BluetoothDevice, BluetoothGATTCharacteristic};
use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use script_traits::BluetoothManagerMsg;
#[cfg(target_os = "linux")]
use servo_config::opts;
use servo_config::prefs::PREFS;
@ -68,23 +70,19 @@ macro_rules! return_if_cached(
);
);
pub trait BluetoothThreadFactory {
fn new() -> Self;
}
impl BluetoothThreadFactory for IpcSender<BluetoothRequest> {
fn new() -> IpcSender<BluetoothRequest> {
let (sender, receiver) = ipc::channel().unwrap();
let adapter = if Some(true) == PREFS.get("dom.bluetooth.enabled").as_boolean() {
BluetoothAdapter::init()
} else {
BluetoothAdapter::init_mock()
}.ok();
thread::Builder::new().name("BluetoothThread".to_owned()).spawn(move || {
BluetoothManager::new(receiver, adapter).start();
}).expect("Thread spawning failed");
sender
}
pub fn new_bluetooth_thread() -> (IpcSender<BluetoothRequest>, IpcSender<IpcSender<BluetoothManagerMsg>>) {
let (sender, receiver) = ipc::channel().unwrap();
let (constellation_sender, constellation_receiver) = ipc::channel().unwrap();
let adapter = if Some(true) == PREFS.get("dom.bluetooth.enabled").as_boolean() {
BluetoothAdapter::init()
} else {
BluetoothAdapter::init_mock()
}.ok();
thread::Builder::new().name("BluetoothThread".to_owned()).spawn(move || {
let constellation_chan = constellation_receiver.recv().unwrap();
BluetoothManager::new(receiver, adapter, constellation_chan).start();
}).expect("Thread spawning failed");
(sender, constellation_sender)
}
// https://webbluetoothcg.github.io/web-bluetooth/#matches-a-filter
@ -206,10 +204,13 @@ pub struct BluetoothManager {
cached_characteristics: HashMap<String, BluetoothGATTCharacteristic>,
cached_descriptors: HashMap<String, BluetoothGATTDescriptor>,
allowed_services: HashMap<String, HashSet<String>>,
constellation_chan: IpcSender<BluetoothManagerMsg>,
}
impl BluetoothManager {
pub fn new(receiver: IpcReceiver<BluetoothRequest>, adapter: Option<BluetoothAdapter>) -> BluetoothManager {
pub fn new(receiver: IpcReceiver<BluetoothRequest>,
adapter: Option<BluetoothAdapter>,
constellation_chan: IpcSender<BluetoothManagerMsg>) -> BluetoothManager {
BluetoothManager {
receiver: receiver,
adapter: adapter,
@ -222,6 +223,7 @@ impl BluetoothManager {
cached_characteristics: HashMap::new(),
cached_descriptors: HashMap::new(),
allowed_services: HashMap::new(),
constellation_chan: constellation_chan,
}
}