mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
Separate the async bluetooth handling from networking code.
They're not at all related, besides both being asynchronous. This change adds a little extra code in response_async(), but makes this code more readable and reduces the unnecessary indirection. This change also makes the build system slightly more parallel, by dropping the dependency on bluetooth_traits from net_traits.
This commit is contained in:
parent
c2e4e4295e
commit
7af7a10e7e
6 changed files with 25 additions and 34 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1729,7 +1729,6 @@ dependencies = [
|
||||||
name = "net_traits"
|
name = "net_traits"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bluetooth_traits 0.0.1",
|
|
||||||
"cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"cookie 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"heapsize 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"heapsize_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -108,7 +108,3 @@ pub enum BluetoothResponse {
|
||||||
EnableNotification(()),
|
EnableNotification(()),
|
||||||
WatchAdvertisements(()),
|
WatchAdvertisements(()),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait BluetoothResponseListener {
|
|
||||||
fn response(&mut self, response: BluetoothResponseResult);
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,7 +10,6 @@ name = "net_traits"
|
||||||
path = "lib.rs"
|
path = "lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bluetooth_traits = {path = "../bluetooth_traits"}
|
|
||||||
msg = {path = "../msg"}
|
msg = {path = "../msg"}
|
||||||
ipc-channel = "0.5"
|
ipc-channel = "0.5"
|
||||||
heapsize = "0.3.0"
|
heapsize = "0.3.0"
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
|
|
||||||
extern crate bluetooth_traits;
|
|
||||||
extern crate cookie as cookie_rs;
|
extern crate cookie as cookie_rs;
|
||||||
extern crate heapsize;
|
extern crate heapsize;
|
||||||
#[macro_use] extern crate heapsize_derive;
|
#[macro_use] extern crate heapsize_derive;
|
||||||
|
@ -34,7 +33,6 @@ extern crate uuid;
|
||||||
extern crate webrender_traits;
|
extern crate webrender_traits;
|
||||||
extern crate websocket;
|
extern crate websocket;
|
||||||
|
|
||||||
use bluetooth_traits::{BluetoothResponseListener, BluetoothResponseResult};
|
|
||||||
use cookie_rs::Cookie;
|
use cookie_rs::Cookie;
|
||||||
use filemanager_thread::FileManagerThreadMsg;
|
use filemanager_thread::FileManagerThreadMsg;
|
||||||
use heapsize::HeapSizeOf;
|
use heapsize::HeapSizeOf;
|
||||||
|
@ -238,13 +236,6 @@ impl<T: FetchResponseListener> Action<T> for FetchResponseMsg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: BluetoothResponseListener> Action<T> for BluetoothResponseResult {
|
|
||||||
/// Execute the default action on a provided listener.
|
|
||||||
fn process(self, listener: &mut T) {
|
|
||||||
listener.response(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle to a resource thread
|
/// Handle to a resource thread
|
||||||
pub type CoreResourceThread = IpcSender<CoreResourceMsg>;
|
pub type CoreResourceThread = IpcSender<CoreResourceMsg>;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_traits::{BluetoothError, BluetoothRequest, GATTType};
|
use bluetooth_traits::{BluetoothError, BluetoothRequest, GATTType};
|
||||||
use bluetooth_traits::{BluetoothResponse, BluetoothResponseListener, BluetoothResponseResult};
|
use bluetooth_traits::{BluetoothResponse, BluetoothResponseResult};
|
||||||
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
|
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
|
||||||
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
|
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
|
||||||
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
|
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
|
||||||
|
@ -27,7 +27,7 @@ use dom::promise::Promise;
|
||||||
use ipc_channel::ipc::{self, IpcSender};
|
use ipc_channel::ipc::{self, IpcSender};
|
||||||
use ipc_channel::router::ROUTER;
|
use ipc_channel::router::ROUTER;
|
||||||
use js::jsapi::{JSAutoCompartment, JSContext};
|
use js::jsapi::{JSAutoCompartment, JSContext};
|
||||||
use network_listener::{NetworkListener, PreInvoke};
|
use script_thread::Runnable;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -61,9 +61,7 @@ pub trait AsyncBluetoothListener {
|
||||||
fn handle_response(&self, result: BluetoothResponse, cx: *mut JSContext, promise: &Rc<Promise>);
|
fn handle_response(&self, result: BluetoothResponse, cx: *mut JSContext, promise: &Rc<Promise>);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Listener: AsyncBluetoothListener + DomObject> PreInvoke for BluetoothContext<Listener> {}
|
impl<T: AsyncBluetoothListener + DomObject> BluetoothContext<T> {
|
||||||
|
|
||||||
impl<Listener: AsyncBluetoothListener + DomObject> BluetoothResponseListener for BluetoothContext<Listener> {
|
|
||||||
#[allow(unrooted_must_root)]
|
#[allow(unrooted_must_root)]
|
||||||
fn response(&mut self, response: BluetoothResponseResult) {
|
fn response(&mut self, response: BluetoothResponseResult) {
|
||||||
let promise = self.promise.take().expect("bt promise is missing").root();
|
let promise = self.promise.take().expect("bt promise is missing").root();
|
||||||
|
@ -182,13 +180,29 @@ pub fn response_async<T: AsyncBluetoothListener + DomObject + 'static>(
|
||||||
promise: Some(TrustedPromise::new(promise.clone())),
|
promise: Some(TrustedPromise::new(promise.clone())),
|
||||||
receiver: Trusted::new(receiver),
|
receiver: Trusted::new(receiver),
|
||||||
}));
|
}));
|
||||||
let listener = NetworkListener {
|
|
||||||
context: context,
|
|
||||||
task_source: task_source,
|
|
||||||
wrapper: None,
|
|
||||||
};
|
|
||||||
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
|
ROUTER.add_route(action_receiver.to_opaque(), box move |message| {
|
||||||
listener.notify_response(message.to().unwrap());
|
struct ListenerRunnable<T: AsyncBluetoothListener + DomObject> {
|
||||||
|
context: Arc<Mutex<BluetoothContext<T>>>,
|
||||||
|
action: BluetoothResponseResult,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: AsyncBluetoothListener + DomObject> Runnable for ListenerRunnable<T> {
|
||||||
|
fn handler(self: Box<Self>) {
|
||||||
|
let this = *self;
|
||||||
|
let mut context = this.context.lock().unwrap();
|
||||||
|
context.response(this.action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let runnable = box ListenerRunnable {
|
||||||
|
context: context.clone(),
|
||||||
|
action: message.to().unwrap(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = task_source.queue_wrapperless(runnable);
|
||||||
|
if let Err(err) = result {
|
||||||
|
warn!("failed to deliver network data: {:?}", err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
action_sender
|
action_sender
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_traits::{BluetoothResponseListener, BluetoothResponseResult};
|
|
||||||
use net_traits::{Action, FetchResponseListener, FetchResponseMsg};
|
use net_traits::{Action, FetchResponseListener, FetchResponseMsg};
|
||||||
use script_thread::{Runnable, RunnableWrapper};
|
use script_thread::{Runnable, RunnableWrapper};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
@ -41,13 +40,6 @@ impl<Listener: FetchResponseListener + PreInvoke + Send + 'static> NetworkListen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// helps type inference
|
|
||||||
impl<Listener: BluetoothResponseListener + PreInvoke + Send + 'static> NetworkListener<Listener> {
|
|
||||||
pub fn notify_response(&self, action: BluetoothResponseResult) {
|
|
||||||
self.notify(action);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A gating mechanism that runs before invoking the runnable on the target thread.
|
/// A gating mechanism that runs before invoking the runnable on the target thread.
|
||||||
/// If the `should_invoke` method returns false, the runnable is discarded without
|
/// If the `should_invoke` method returns false, the runnable is discarded without
|
||||||
/// being invoked.
|
/// being invoked.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue