Skip to content

Commit

Permalink
refactor(bluetooth): uses embedderproxy directly
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 18, 2018
1 parent 5574b42 commit 61b4a89
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 68 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion components/bluetooth/Cargo.toml
Expand Up @@ -12,9 +12,10 @@ path = "lib.rs"
[dependencies]
bitflags = "1.0"
bluetooth_traits = {path = "../bluetooth_traits"}
compositing = {path = "../compositing"}
device = {git = "https://github.com/servo/devices", features = ["bluetooth-test"]}
ipc-channel = "0.10"
script_traits = {path = "../script_traits"}
log = "0.4"
servo_config = {path = "../config"}
servo_rand = {path = "../rand"}
uuid = {version = "0.6", features = ["v4"]}
55 changes: 34 additions & 21 deletions components/bluetooth/lib.rs
Expand Up @@ -5,9 +5,11 @@
#[macro_use]
extern crate bitflags;
extern crate bluetooth_traits;
extern crate compositing;
extern crate device;
extern crate ipc_channel;
extern crate script_traits;
#[macro_use]
extern crate log;
extern crate servo_config;
extern crate servo_rand;
extern crate uuid;
Expand All @@ -19,10 +21,10 @@ use bluetooth_traits::{BluetoothDeviceMsg, BluetoothRequest, BluetoothResponse,
use bluetooth_traits::{BluetoothError, BluetoothResponseResult, BluetoothResult};
use bluetooth_traits::blocklist::{uuid_is_blocklisted, Blocklist};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
use compositing::compositor_thread::{EmbedderMsg, EmbedderProxy};
use device::bluetooth::{BluetoothAdapter, BluetoothDevice, BluetoothGATTCharacteristic};
use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use script_traits::BluetoothManagerMsg;
use servo_config::opts;
use servo_config::prefs::PREFS;
use servo_rand::Rng;
Expand Down Expand Up @@ -61,19 +63,23 @@ macro_rules! return_if_cached(
);
);

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)
pub trait BluetoothThreadFactory {
fn new(embedder_proxy: EmbedderProxy) -> Self;
}

impl BluetoothThreadFactory for IpcSender<BluetoothRequest> {
fn new(embedder_proxy: EmbedderProxy) -> 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, embedder_proxy).start();
}).expect("Thread spawning failed");
sender
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#matches-a-filter
Expand Down Expand Up @@ -195,13 +201,13 @@ pub struct BluetoothManager {
cached_characteristics: HashMap<String, BluetoothGATTCharacteristic>,
cached_descriptors: HashMap<String, BluetoothGATTDescriptor>,
allowed_services: HashMap<String, HashSet<String>>,
constellation_chan: IpcSender<BluetoothManagerMsg>,
embedder_proxy: EmbedderProxy,
}

impl BluetoothManager {
pub fn new(receiver: IpcReceiver<BluetoothRequest>,
adapter: Option<BluetoothAdapter>,
constellation_chan: IpcSender<BluetoothManagerMsg>) -> BluetoothManager {
embedder_proxy: EmbedderProxy) -> BluetoothManager {
BluetoothManager {
receiver: receiver,
adapter: adapter,
Expand All @@ -214,7 +220,7 @@ impl BluetoothManager {
cached_characteristics: HashMap::new(),
cached_descriptors: HashMap::new(),
allowed_services: HashMap::new(),
constellation_chan: constellation_chan,
embedder_proxy: embedder_proxy,
}
}

Expand Down Expand Up @@ -376,9 +382,16 @@ impl BluetoothManager {
}

let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!");
let msg = BluetoothManagerMsg::OpenDeviceSelectDialog(dialog_rows, ipc_sender);

self.constellation_chan.send(msg).map(|_| ipc_receiver.recv().unwrap()).unwrap_or_default()
let msg = EmbedderMsg::GetSelectedBluetoothDevice(dialog_rows, ipc_sender);
self.embedder_proxy.send(msg);

match ipc_receiver.recv() {
Ok(result) => result,
Err(e) => {
warn!("Failed to receive files from embedder ({}).", e);
None
}
}
}

fn generate_device_id(&mut self) -> String {
Expand Down
34 changes: 4 additions & 30 deletions components/constellation/constellation.rs
Expand Up @@ -123,12 +123,12 @@ use pipeline::{InitialPipelineState, Pipeline};
use profile_traits::mem;
use profile_traits::time;
use script_traits::{AnimationState, AnimationTickType, CompositorEvent};
use script_traits::{BluetoothManagerMsg, SWManagerMsg, ScopeThings, UpdatePipelineIdReason, WebDriverCommandMsg};
use script_traits::{ConstellationControlMsg, ConstellationMsg as FromCompositorMsg, DiscardBrowsingContext};
use script_traits::{DocumentActivity, DocumentState, LayoutControlMsg, LoadData};
use script_traits::{IFrameLoadInfo, IFrameLoadInfoWithData, IFrameSandboxState, TimerSchedulerMsg};
use script_traits::{LayoutMsg as FromLayoutMsg, ScriptMsg as FromScriptMsg, ScriptThreadFactory};
use script_traits::{LogEntry, ScriptToConstellationChan, ServiceWorkerMsg, webdriver_msg};
use script_traits::{SWManagerMsg, ScopeThings, UpdatePipelineIdReason, WebDriverCommandMsg};
use script_traits::{WindowSizeData, WindowSizeType};
use serde::{Deserialize, Serialize};
use servo_config::opts;
Expand Down Expand Up @@ -174,9 +174,6 @@ pub struct Constellation<Message, LTF, STF> {
/// This is the constellation's view of `script_sender`.
script_receiver: Receiver<Result<(PipelineId, FromScriptMsg), IpcError>>,

/// A channel for the constellation to receive messages from bluetooth threads.
bluetoothmanager_receiver: Receiver<Result<BluetoothManagerMsg, IpcError>>,

/// An IPC channel for layout threads to send messages to the constellation.
/// This is the layout threads' view of `layout_receiver`.
layout_sender: IpcSender<FromLayoutMsg>,
Expand Down Expand Up @@ -549,23 +546,17 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
STF: ScriptThreadFactory<Message=Message>
{
/// Create a new constellation thread.
pub fn start(state: InitialConstellationState)
-> (Sender<FromCompositorMsg>, IpcSender<SWManagerMsg>, IpcSender<BluetoothManagerMsg>) {
pub fn start(state: InitialConstellationState) -> (Sender<FromCompositorMsg>, IpcSender<SWManagerMsg>) {
let (compositor_sender, compositor_receiver) = channel();

// service worker manager to communicate with constellation
let (swmanager_sender, swmanager_receiver) = ipc::channel().expect("ipc channel failure");
let sw_mgr_clone = swmanager_sender.clone();

let (bluetoothmanager_sender, bluetoothmanager_receiver) = ipc::channel().expect("ipc channel failure");

thread::Builder::new().name("Constellation".to_owned()).spawn(move || {
let (ipc_script_sender, ipc_script_receiver) = ipc::channel().expect("ipc channel failure");
let script_receiver = route_ipc_receiver_to_new_mpsc_receiver_preserving_errors(ipc_script_receiver);

let bluetoothmanager_receiver =
route_ipc_receiver_to_new_mpsc_receiver_preserving_errors(bluetoothmanager_receiver);

let (ipc_layout_sender, ipc_layout_receiver) = ipc::channel().expect("ipc channel failure");
let layout_receiver = route_ipc_receiver_to_new_mpsc_receiver_preserving_errors(ipc_layout_receiver);

Expand All @@ -579,7 +570,6 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
script_sender: ipc_script_sender,
layout_sender: ipc_layout_sender,
script_receiver: script_receiver,
bluetoothmanager_receiver: bluetoothmanager_receiver,
compositor_receiver: compositor_receiver,
layout_receiver: layout_receiver,
network_listener_sender: network_listener_sender,
Expand Down Expand Up @@ -646,7 +636,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
constellation.run();
}).expect("Thread spawning failed");

(compositor_sender, swmanager_sender, bluetoothmanager_sender)
(compositor_sender, swmanager_sender)
}

/// The main event loop for the constellation.
Expand Down Expand Up @@ -840,7 +830,6 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
Layout(FromLayoutMsg),
NetworkListener((PipelineId, FetchResponseMsg)),
FromSWManager(SWManagerMsg),
FromBluetoothManager(BluetoothManagerMsg),
}

// Get one incoming request.
Expand All @@ -860,7 +849,6 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
let receiver_from_layout = &self.layout_receiver;
let receiver_from_network_listener = &self.network_listener_receiver;
let receiver_from_swmanager = &self.swmanager_receiver;
let receiver_from_bluetoothmanager = &self.bluetoothmanager_receiver;
select! {
msg = receiver_from_script.recv() =>
msg.expect("Unexpected script channel panic in constellation").map(Request::Script),
Expand All @@ -873,9 +861,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
msg.expect("Unexpected network listener channel panic in constellation")
)),
msg = receiver_from_swmanager.recv() =>
msg.expect("Unexpected panic channel panic in constellation").map(Request::FromSWManager),
msg = receiver_from_bluetoothmanager.recv() =>
msg.expect("Unexpected bluetooth channel panic in constellation").map(Request::FromBluetoothManager)
msg.expect("Unexpected panic channel panic in constellation").map(Request::FromSWManager)
}
};

Expand All @@ -899,9 +885,6 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
},
Request::FromSWManager(message) => {
self.handle_request_from_swmanager(message);
},
Request::FromBluetoothManager(message) => {
self.handle_request_from_bluetoothmanager(message);
}
}
}
Expand Down Expand Up @@ -931,15 +914,6 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
}
}

fn handle_request_from_bluetoothmanager(&mut self, message: BluetoothManagerMsg) {
match message {
BluetoothManagerMsg::OpenDeviceSelectDialog(devices, sender) => {
let msg = EmbedderMsg::GetSelectedBluetoothDevice(devices, sender);
self.embedder_proxy.send(msg);
}
}
}

fn handle_request_from_compositor(&mut self, message: FromCompositorMsg) {
match message {
FromCompositorMsg::Exit => {
Expand Down
2 changes: 1 addition & 1 deletion components/script_traits/lib.rs
Expand Up @@ -73,7 +73,7 @@ use webrender_api::{ExternalScrollId, DevicePixel, DeviceUintSize, DocumentId, I
use webvr_traits::{WebVREvent, WebVRMsg};

pub use script_msg::{LayoutMsg, ScriptMsg, EventResult, LogEntry};
pub use script_msg::{BluetoothManagerMsg, ServiceWorkerMsg, ScopeThings, SWManagerMsg, SWManagerSenders, DOMMessage};
pub use script_msg::{ServiceWorkerMsg, ScopeThings, SWManagerMsg, SWManagerSenders, DOMMessage};

/// The address of a node. Layout sends these back. They must be validated via
/// `from_untrusted_node_address` before they can be used, because we do not trust layout.
Expand Down
7 changes: 0 additions & 7 deletions components/script_traits/script_msg.rs
Expand Up @@ -213,10 +213,3 @@ pub enum SWManagerMsg {
/// Provide the constellation with a means of communicating with the Service Worker Manager
OwnSender(IpcSender<ServiceWorkerMsg>),
}

/// Messages outgoing from the Bluetooth Manager thread to constellation
#[derive(Deserialize, Serialize)]
pub enum BluetoothManagerMsg {
/// Requesting to open device select dialog
OpenDeviceSelectDialog(Vec<String>, IpcSender<Option<String>>)
}
11 changes: 4 additions & 7 deletions components/servo/lib.rs
Expand Up @@ -67,7 +67,8 @@ fn webdriver(port: u16, constellation: Sender<ConstellationMsg>) {
#[cfg(not(feature = "webdriver"))]
fn webdriver(_port: u16, _constellation: Sender<ConstellationMsg>) { }

use bluetooth::new_bluetooth_thread;
use bluetooth::BluetoothThreadFactory;
use bluetooth_traits::BluetoothRequest;
use canvas::gl_context::GLContextFactory;
use canvas::webgl_thread::WebGLThreads;
use compositing::{IOCompositor, ShutdownState, RenderNotifier};
Expand Down Expand Up @@ -457,7 +458,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, bluetooth_constellation_sender) = new_bluetooth_thread();
let bluetooth_thread: IpcSender<BluetoothRequest> = BluetoothThreadFactory::new(embedder_proxy.clone());

let (public_resource_threads, private_resource_threads) =
new_resource_threads(user_agent,
Expand Down Expand Up @@ -525,9 +526,7 @@ fn create_constellation(user_agent: Cow<'static, str>,
webgl_threads,
webvr_chan,
};
let (constellation_chan,
from_swmanager_sender,
from_bluetoothmanager_sender) =
let (constellation_chan, from_swmanager_sender) =
Constellation::<script_layout_interface::message::Msg,
layout_thread::LayoutThread,
script::script_thread::ScriptThread>::start(initial_state);
Expand All @@ -537,8 +536,6 @@ 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,
Expand Down

0 comments on commit 61b4a89

Please sign in to comment.