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

@ -14,6 +14,7 @@ bitflags = "1.0"
bluetooth_traits = {path = "../bluetooth_traits"}
device = {git = "https://github.com/servo/devices", features = ["bluetooth-test"]}
ipc-channel = "0.10"
script_traits = {path = "../script_traits"}
servo_config = {path = "../config"}
servo_rand = {path = "../rand"}
uuid = {version = "0.6", features = ["v4"]}

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,
}
}

View file

@ -67,8 +67,7 @@ fn webdriver(port: u16, constellation: Sender<ConstellationMsg>) {
#[cfg(not(feature = "webdriver"))]
fn webdriver(_port: u16, _constellation: Sender<ConstellationMsg>) { }
use bluetooth::BluetoothThreadFactory;
use bluetooth_traits::BluetoothRequest;
use bluetooth::new_bluetooth_thread;
use canvas::gl_context::GLContextFactory;
use canvas::webgl_thread::WebGLThreads;
use compositing::{IOCompositor, ShutdownState, RenderNotifier};
@ -458,7 +457,7 @@ fn create_constellation(user_agent: Cow<'static, str>,
webrender_api_sender: webrender_api::RenderApiSender,
window_gl: Rc<gl::Gl>)
-> (Sender<ConstellationMsg>, SWManagerSenders) {
let bluetooth_thread: IpcSender<BluetoothRequest> = BluetoothThreadFactory::new();
let (bluetooth_thread, bluetooth_constellation_sender) = new_bluetooth_thread();
let (public_resource_threads, private_resource_threads) =
new_resource_threads(user_agent,
@ -538,6 +537,8 @@ fn create_constellation(user_agent: Cow<'static, str>,
webvr_constellation_sender.send(constellation_chan.clone()).unwrap();
}
bluetooth_constellation_sender.send(from_bluetoothmanager_sender.clone()).unwrap();
// channels to communicate with Service Worker Manager
let sw_senders = SWManagerSenders {
swmanager_sender: from_swmanager_sender,