diff options
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/bluetooth_blacklist.rs | 124 | ||||
-rw-r--r-- | components/script/dom/bluetooth.rs | 60 | ||||
-rw-r--r-- | components/script/dom/bluetoothdevice.rs | 16 | ||||
-rw-r--r-- | components/script/dom/bluetoothremotegattcharacteristic.rs | 52 | ||||
-rw-r--r-- | components/script/dom/bluetoothremotegattdescriptor.rs | 6 | ||||
-rw-r--r-- | components/script/dom/bluetoothremotegattserver.rs | 54 | ||||
-rw-r--r-- | components/script/dom/bluetoothremotegattservice.rs | 127 | ||||
-rw-r--r-- | components/script/dom/webidls/BluetoothDevice.webidl | 6 | ||||
-rw-r--r-- | components/script/lib.rs | 1 |
9 files changed, 238 insertions, 208 deletions
diff --git a/components/script/bluetooth_blacklist.rs b/components/script/bluetooth_blacklist.rs deleted file mode 100644 index f9d1c564692..00000000000 --- a/components/script/bluetooth_blacklist.rs +++ /dev/null @@ -1,124 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * 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/. */ - -use regex::Regex; -use std::cell::RefCell; -use std::collections::HashMap; -use std::io::BufRead; -use std::string::String; -use util::resource_files::read_resource_file; - -const BLACKLIST_FILE: &'static str = "gatt_blacklist.txt"; -const BLACKLIST_FILE_NOT_FOUND: &'static str = "Could not find gatt_blacklist.txt file"; -const EXCLUDE_READS: &'static str = "exclude-reads"; -const EXCLUDE_WRITES: &'static str = "exclude-writes"; -const VALID_UUID_REGEX: &'static str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; - -thread_local!(pub static BLUETOOTH_BLACKLIST: RefCell<BluetoothBlacklist> = - RefCell::new(BluetoothBlacklist(parse_blacklist()))); - -pub fn uuid_is_blacklisted(uuid: &str, exclude_type: Blacklist) -> bool { - BLUETOOTH_BLACKLIST.with(|blist| { - match exclude_type { - Blacklist::All => { - blist.borrow().is_blacklisted(uuid) - }, - Blacklist::Reads => { - blist.borrow().is_blacklisted_for_reads(uuid) - } - Blacklist::Writes => { - blist.borrow().is_blacklisted_for_writes(uuid) - } - } - }) -} - -pub struct BluetoothBlacklist(Option<HashMap<String, Blacklist>>); - -#[derive(Eq, PartialEq)] -pub enum Blacklist { - All, // Read and Write - Reads, - Writes, -} - -impl BluetoothBlacklist { - // https://webbluetoothcg.github.io/web-bluetooth/#blacklisted - pub fn is_blacklisted(&self, uuid: &str) -> bool { - match self.0 { - Some(ref map) => map.get(uuid).map_or(false, |et| et.eq(&Blacklist::All)), - None => false, - } - } - - // https://webbluetoothcg.github.io/web-bluetooth/#blacklisted-for-reads - pub fn is_blacklisted_for_reads(&self, uuid: &str) -> bool { - match self.0 { - Some(ref map) => map.get(uuid).map_or(false, |et| et.eq(&Blacklist::All) || - et.eq(&Blacklist::Reads)), - None => false, - } - } - - // https://webbluetoothcg.github.io/web-bluetooth/#blacklisted-for-writes - pub fn is_blacklisted_for_writes(&self, uuid: &str) -> bool { - match self.0 { - Some(ref map) => map.get(uuid).map_or(false, |et| et.eq(&Blacklist::All) || - et.eq(&Blacklist::Writes)), - None => false, - } - } -} - -// https://webbluetoothcg.github.io/web-bluetooth/#parsing-the-blacklist -fn parse_blacklist() -> Option<HashMap<String, Blacklist>> { - // Step 1 missing, currently we parse ./resources/gatt_blacklist.txt. - let valid_uuid_regex = Regex::new(VALID_UUID_REGEX).unwrap(); - let content = read_resource_file(BLACKLIST_FILE).expect(BLACKLIST_FILE_NOT_FOUND); - // Step 3 - let mut result = HashMap::new(); - // Step 2 and 4 - for line in content.lines() { - let line = match line { - Ok(l) => l, - Err(_) => return None, - }; - // Step 4.1 - if line.is_empty() || line.starts_with('#') { - continue; - } - let mut exclude_type = Blacklist::All; - let mut words = line.split_whitespace(); - let uuid = match words.next() { - Some(uuid) => uuid, - None => continue, - }; - if !valid_uuid_regex.is_match(uuid) { - return None; - } - match words.next() { - // Step 4.2 We already have an initialized exclude_type variable with Blacklist::All. - None => {}, - // Step 4.3 - Some(EXCLUDE_READS) => { - exclude_type = Blacklist::Reads; - }, - Some(EXCLUDE_WRITES) => { - exclude_type = Blacklist::Writes; - }, - // Step 4.4 - _ => { - return None; - }, - } - // Step 4.5 - if result.contains_key(uuid) { - return None; - } - // Step 4.6 - result.insert(uuid.to_string(), exclude_type); - } - // Step 5 - return Some(result); -} diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index b6f8544aeab..7080a38668b 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -2,30 +2,34 @@ * 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/. */ -use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::{BluetoothError, BluetoothMethodMsg}; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; use core::clone::Clone; +use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothMethods, BluetoothRequestDeviceFilter}; use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions; -use dom::bindings::error::Error::{self, Security, Type}; +use dom::bindings::error::Error::{self, NotFound, Security, Type}; use dom::bindings::error::Fallible; -use dom::bindings::js::Root; +use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothdevice::BluetoothDevice; +use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic; +use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor; +use dom::bluetoothremotegattservice::BluetoothRemoteGATTService; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use dom::globalscope::GlobalScope; use dom::promise::Promise; use ipc_channel::ipc::{self, IpcSender}; use js::conversions::ToJSValConvertible; +use std::collections::HashMap; use std::rc::Rc; const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices."; const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way."; -const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes."; // 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name. const MAX_DEVICE_NAME_LENGTH: usize = 248; // A device name can never be longer than 29 bytes. @@ -43,12 +47,20 @@ const OPTIONS_ERROR: &'static str = "Fields of 'options' conflict with each othe #[dom_struct] pub struct Bluetooth { reflector_: Reflector, + device_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothDevice>>>>, + service_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTService>>>>, + characteristic_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTCharacteristic>>>>, + descriptor_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTDescriptor>>>>, } impl Bluetooth { pub fn new_inherited() -> Bluetooth { Bluetooth { reflector_: Reflector::new(), + device_instance_map: DOMRefCell::new(HashMap::new()), + service_instance_map: DOMRefCell::new(HashMap::new()), + characteristic_instance_map: DOMRefCell::new(HashMap::new()), + descriptor_instance_map: DOMRefCell::new(HashMap::new()), } } @@ -58,6 +70,19 @@ impl Bluetooth { BluetoothBinding::Wrap) } + pub fn get_service_map(&self) -> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTService>>>> { + &self.service_instance_map + } + + pub fn get_characteristic_map(&self) + -> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTCharacteristic>>>> { + &self.characteristic_instance_map + } + + pub fn get_descriptor_map(&self) -> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTDescriptor>>>> { + &self.descriptor_instance_map + } + fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> { self.global().as_window().bluetooth_thread() } @@ -103,15 +128,21 @@ impl Bluetooth { // Step 12-13. match device { Ok(device) => { - let global = self.global(); - let ad_data = BluetoothAdvertisingData::new(&global, + let mut device_instance_map = self.device_instance_map.borrow_mut(); + if let Some(existing_device) = device_instance_map.get(&device.id.clone()) { + return Ok(existing_device.get()); + } + let ad_data = BluetoothAdvertisingData::new(&self.global(), device.appearance, device.tx_power, device.rssi); - Ok(BluetoothDevice::new(&global, - DOMString::from(device.id), - device.name.map(DOMString::from), - &ad_data)) + let bt_device = BluetoothDevice::new(&self.global(), + DOMString::from(device.id.clone()), + device.name.map(DOMString::from), + &ad_data, + &self); + device_instance_map.insert(device.id, MutHeap::new(&bt_device)); + Ok(bt_device) }, Err(error) => { Err(Error::from(error)) @@ -213,13 +244,13 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<Blueto return Err(Type(NAME_TOO_LONG_ERROR.to_owned())); } if name.len() > MAX_FILTER_NAME_LENGTH { - return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned())); + return Err(NotFound); } // Step 2.4.4.2. - name.to_string() + Some(name.to_string()) }, - None => String::new(), + None => None, }; // Step 2.4.5. @@ -233,7 +264,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<Blueto return Err(Type(NAME_TOO_LONG_ERROR.to_owned())); } if name_prefix.len() > MAX_FILTER_NAME_LENGTH { - return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned())); + return Err(NotFound); } // Step 2.4.5.2. @@ -289,6 +320,7 @@ impl From<BluetoothError> for Error { BluetoothError::NotFound => Error::NotFound, BluetoothError::NotSupported => Error::NotSupported, BluetoothError::Security => Error::Security, + BluetoothError::InvalidState => Error::InvalidState, } } } diff --git a/components/script/dom/bluetoothdevice.rs b/components/script/dom/bluetoothdevice.rs index f4348465108..39706befad6 100644 --- a/components/script/dom/bluetoothdevice.rs +++ b/components/script/dom/bluetoothdevice.rs @@ -7,6 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMet use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; +use dom::bluetooth::Bluetooth; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer; use dom::globalscope::GlobalScope; @@ -19,12 +20,14 @@ pub struct BluetoothDevice { name: Option<DOMString>, ad_data: MutHeap<JS<BluetoothAdvertisingData>>, gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>, + context: MutHeap<JS<Bluetooth>>, } impl BluetoothDevice { pub fn new_inherited(id: DOMString, name: Option<DOMString>, - ad_data: &BluetoothAdvertisingData) + ad_data: &BluetoothAdvertisingData, + context: &Bluetooth) -> BluetoothDevice { BluetoothDevice { reflector_: Reflector::new(), @@ -32,20 +35,27 @@ impl BluetoothDevice { name: name, ad_data: MutHeap::new(ad_data), gatt: Default::default(), + context: MutHeap::new(context), } } pub fn new(global: &GlobalScope, id: DOMString, name: Option<DOMString>, - adData: &BluetoothAdvertisingData) + adData: &BluetoothAdvertisingData, + context: &Bluetooth) -> Root<BluetoothDevice> { reflect_dom_object(box BluetoothDevice::new_inherited(id, name, - adData), + adData, + context), global, BluetoothDeviceBinding::Wrap) } + + pub fn get_context(&self) -> Root<Bluetooth> { + self.context.get() + } } impl BluetoothDeviceMethods for BluetoothDevice { diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 0fae7548ede..1b2a16c1a4b 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -2,8 +2,8 @@ * 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/. */ -use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::BluetoothMethodMsg; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding:: BluetoothCharacteristicPropertiesMethods; @@ -87,16 +87,26 @@ impl BluetoothRemoteGATTCharacteristic { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } + if !self.Service().Device().Gatt().Connected() { + return Err(Network) + } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap(); let descriptor = receiver.recv().unwrap(); match descriptor { Ok(descriptor) => { - Ok(BluetoothRemoteGATTDescriptor::new(&self.global(), - self, - DOMString::from(descriptor.uuid), - descriptor.instance_id)) + let context = self.service.get().get_device().get_context(); + let mut descriptor_map = context.get_descriptor_map().borrow_mut(); + if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) { + return Ok(existing_descriptor.get()); + } + let bt_descriptor = BluetoothRemoteGATTDescriptor::new(&self.global(), + self, + DOMString::from(descriptor.uuid), + descriptor.instance_id.clone()); + descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor)); + Ok(bt_descriptor) }, Err(error) => { Err(Error::from(error)) @@ -117,18 +127,34 @@ impl BluetoothRemoteGATTCharacteristic { } } }; + if !self.Service().Device().Gatt().Connected() { + return Err(Network) + } + let mut descriptors = vec!(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap(); let descriptors_vec = receiver.recv().unwrap(); match descriptors_vec { Ok(descriptor_vec) => { - Ok(descriptor_vec.into_iter() - .map(|desc| BluetoothRemoteGATTDescriptor::new(&self.global(), - self, - DOMString::from(desc.uuid), - desc.instance_id)) - .collect()) + let context = self.service.get().get_device().get_context(); + let mut descriptor_map = context.get_descriptor_map().borrow_mut(); + for descriptor in descriptor_vec { + let bt_descriptor = match descriptor_map.get(&descriptor.instance_id) { + Some(existing_descriptor) => existing_descriptor.get(), + None => { + BluetoothRemoteGATTDescriptor::new(&self.global(), + self, + DOMString::from(descriptor.uuid), + descriptor.instance_id.clone()) + }, + }; + if !descriptor_map.contains_key(&descriptor.instance_id) { + descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor)); + } + descriptors.push(bt_descriptor); + } + Ok(descriptors) }, Err(error) => { Err(Error::from(error)) @@ -182,10 +208,10 @@ impl BluetoothRemoteGATTCharacteristic { } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( - BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); + BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - Ok(_) => Ok(()), + Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))), Err(error) => { Err(Error::from(error)) }, diff --git a/components/script/dom/bluetoothremotegattdescriptor.rs b/components/script/dom/bluetoothremotegattdescriptor.rs index 5ca51f45439..30886baf473 100644 --- a/components/script/dom/bluetoothremotegattdescriptor.rs +++ b/components/script/dom/bluetoothremotegattdescriptor.rs @@ -2,8 +2,8 @@ * 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/. */ -use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::BluetoothMethodMsg; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding:: @@ -105,10 +105,10 @@ impl BluetoothRemoteGATTDescriptor { } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( - BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap(); + BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap(); let result = receiver.recv().unwrap(); match result { - Ok(_) => Ok(()), + Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))), Err(error) => { Err(Error::from(error)) }, diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index 5e589fe07af..9ac1ca4c4f4 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -2,13 +2,13 @@ * 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/. */ -use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::BluetoothMethodMsg; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::error::{ErrorResult, Fallible}; -use dom::bindings::error::Error::{self, Security}; +use dom::bindings::error::Error::{self, Network, Security}; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::str::DOMString; @@ -72,17 +72,27 @@ impl BluetoothRemoteGATTServer { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } + if !self.Device().Gatt().Connected() { + return Err(Network) + } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap(); let service = receiver.recv().unwrap(); match service { Ok(service) => { - Ok(BluetoothRemoteGATTService::new(&self.global(), - &self.device.get(), - DOMString::from(service.uuid), - service.is_primary, - service.instance_id)) + let context = self.device.get().get_context(); + let mut service_map = context.get_service_map().borrow_mut(); + if let Some(existing_service) = service_map.get(&service.instance_id) { + return Ok(existing_service.get()); + } + let bt_service = BluetoothRemoteGATTService::new(&self.global(), + &self.device.get(), + DOMString::from(service.uuid), + service.is_primary, + service.instance_id.clone()); + service_map.insert(service.instance_id, MutHeap::new(&bt_service)); + Ok(bt_service) }, Err(error) => { Err(Error::from(error)) @@ -103,19 +113,35 @@ impl BluetoothRemoteGATTServer { } } }; + if !self.Device().Gatt().Connected() { + return Err(Network) + } + let mut services = vec!(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); let services_vec = receiver.recv().unwrap(); match services_vec { Ok(service_vec) => { - Ok(service_vec.into_iter() - .map(|service| BluetoothRemoteGATTService::new(&self.global(), - &self.device.get(), - DOMString::from(service.uuid), - service.is_primary, - service.instance_id)) - .collect()) + let context = self.device.get().get_context(); + let mut service_map = context.get_service_map().borrow_mut(); + for service in service_vec { + let bt_service = match service_map.get(&service.instance_id) { + Some(existing_service) => existing_service.get(), + None => { + BluetoothRemoteGATTService::new(&self.global(), + &self.device.get(), + DOMString::from(service.uuid), + service.is_primary, + service.instance_id.clone()) + }, + }; + if !service_map.contains_key(&service.instance_id) { + service_map.insert(service.instance_id, MutHeap::new(&bt_service)); + } + services.push(bt_service); + } + Ok(services) }, Err(error) => { Err(Error::from(error)) diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index a4fc78c8d93..c597338e1ba 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -2,11 +2,13 @@ * 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/. */ -use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted}; use bluetooth_traits::BluetoothMethodMsg; +use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted}; +use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods; +use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; -use dom::bindings::error::Error::{self, Security}; +use dom::bindings::error::Error::{self, Network, Security}; use dom::bindings::error::Fallible; use dom::bindings::js::{JS, MutHeap, Root}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; @@ -60,6 +62,10 @@ impl BluetoothRemoteGATTService { BluetoothRemoteGATTServiceBinding::Wrap) } + pub fn get_device(&self) -> Root<BluetoothDevice> { + self.device.get() + } + fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> { self.global().as_window().bluetooth_thread() } @@ -76,12 +82,20 @@ impl BluetoothRemoteGATTService { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } + if !self.Device().Gatt().Connected() { + return Err(Network) + } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap(); let characteristic = receiver.recv().unwrap(); match characteristic { Ok(characteristic) => { + let context = self.device.get().get_context(); + let mut characteristic_map = context.get_characteristic_map().borrow_mut(); + if let Some(existing_characteristic) = characteristic_map.get(&characteristic.instance_id) { + return Ok(existing_characteristic.get()); + } let global = self.global(); let properties = BluetoothCharacteristicProperties::new(&global, characteristic.broadcast, @@ -93,11 +107,13 @@ impl BluetoothRemoteGATTService { characteristic.authenticated_signed_writes, characteristic.reliable_write, characteristic.writable_auxiliaries); - Ok(BluetoothRemoteGATTCharacteristic::new(&global, - self, - DOMString::from(characteristic.uuid), - &properties, - characteristic.instance_id)) + let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&global, + self, + DOMString::from(characteristic.uuid), + &properties, + characteristic.instance_id.clone()); + characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic)); + Ok(bt_characteristic) }, Err(error) => { Err(Error::from(error)) @@ -118,6 +134,9 @@ impl BluetoothRemoteGATTService { } } }; + if !self.Device().Gatt().Connected() { + return Err(Network) + } let mut characteristics = vec!(); let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( @@ -125,23 +144,35 @@ impl BluetoothRemoteGATTService { let characteristics_vec = receiver.recv().unwrap(); match characteristics_vec { Ok(characteristic_vec) => { + let context = self.device.get().get_context(); + let mut characteristic_map = context.get_characteristic_map().borrow_mut(); for characteristic in characteristic_vec { - let global = self.global(); - let properties = BluetoothCharacteristicProperties::new(&global, - characteristic.broadcast, - characteristic.read, - characteristic.write_without_response, - characteristic.write, - characteristic.notify, - characteristic.indicate, - characteristic.authenticated_signed_writes, - characteristic.reliable_write, - characteristic.writable_auxiliaries); - characteristics.push(BluetoothRemoteGATTCharacteristic::new(&global, - self, - DOMString::from(characteristic.uuid), - &properties, - characteristic.instance_id)); + let bt_characteristic = match characteristic_map.get(&characteristic.instance_id) { + Some(existing_characteristic) => existing_characteristic.get(), + None => { + let properties = + BluetoothCharacteristicProperties::new(&self.global(), + characteristic.broadcast, + characteristic.read, + characteristic.write_without_response, + characteristic.write, + characteristic.notify, + characteristic.indicate, + characteristic.authenticated_signed_writes, + characteristic.reliable_write, + characteristic.writable_auxiliaries); + + BluetoothRemoteGATTCharacteristic::new(&self.global(), + self, + DOMString::from(characteristic.uuid), + &properties, + characteristic.instance_id.clone()) + }, + }; + if !characteristic_map.contains_key(&characteristic.instance_id) { + characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic)); + } + characteristics.push(bt_characteristic); } Ok(characteristics) }, @@ -159,6 +190,9 @@ impl BluetoothRemoteGATTService { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } + if !self.Device().Gatt().Connected() { + return Err(Network) + } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetIncludedService(self.get_instance_id(), @@ -167,11 +201,18 @@ impl BluetoothRemoteGATTService { let service = receiver.recv().unwrap(); match service { Ok(service) => { - Ok(BluetoothRemoteGATTService::new(&self.global(), - &self.device.get(), - DOMString::from(service.uuid), - service.is_primary, - service.instance_id)) + let context = self.device.get().get_context(); + let mut service_map = context.get_service_map().borrow_mut(); + if let Some(existing_service) = service_map.get(&service.instance_id) { + return Ok(existing_service.get()); + } + let bt_service = BluetoothRemoteGATTService::new(&self.global(), + &self.device.get(), + DOMString::from(service.uuid), + service.is_primary, + service.instance_id.clone()); + service_map.insert(service.instance_id, MutHeap::new(&bt_service)); + Ok(bt_service) }, Err(error) => { Err(Error::from(error)) @@ -192,21 +233,37 @@ impl BluetoothRemoteGATTService { } } }; + if !self.Device().Gatt().Connected() { + return Err(Network) + } let (sender, receiver) = ipc::channel().unwrap(); self.get_bluetooth_thread().send( BluetoothMethodMsg::GetIncludedServices(self.get_instance_id(), uuid, sender)).unwrap(); let services_vec = receiver.recv().unwrap(); + let mut services = vec!(); match services_vec { Ok(service_vec) => { - Ok(service_vec.into_iter() - .map(|service| BluetoothRemoteGATTService::new(&self.global(), - &self.device.get(), - DOMString::from(service.uuid), - service.is_primary, - service.instance_id)) - .collect()) + let context = self.device.get().get_context(); + let mut service_map = context.get_service_map().borrow_mut(); + for service in service_vec { + let bt_service = match service_map.get(&service.instance_id) { + Some(existing_service) => existing_service.get(), + None => { + BluetoothRemoteGATTService::new(&self.global(), + &self.device.get(), + DOMString::from(service.uuid), + service.is_primary, + service.instance_id.clone()) + }, + }; + if !service_map.contains_key(&service.instance_id) { + service_map.insert(service.instance_id, MutHeap::new(&bt_service)); + } + services.push(bt_service); + } + Ok(services) }, Err(error) => { Err(Error::from(error)) diff --git a/components/script/dom/webidls/BluetoothDevice.webidl b/components/script/dom/webidls/BluetoothDevice.webidl index 65bfb9c1a4e..34a77230850 100644 --- a/components/script/dom/webidls/BluetoothDevice.webidl +++ b/components/script/dom/webidls/BluetoothDevice.webidl @@ -8,9 +8,13 @@ interface BluetoothDevice { readonly attribute DOMString id; readonly attribute DOMString? name; + // TODO: remove this after BluetoothAdvertisingEvent implemented. readonly attribute BluetoothAdvertisingData adData; readonly attribute BluetoothRemoteGATTServer gatt; - // readonly attribute FrozenArray[] uuids; + + // Promise<void> watchAdvertisements(); + // void unwatchAdvertisements(); + // readonly attribute boolean watchingAdvertisements; }; // BluetoothDevice implements EventTarget; diff --git a/components/script/lib.rs b/components/script/lib.rs index d847af7b62f..94e9683ffaf 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -96,7 +96,6 @@ extern crate webrender_traits; extern crate websocket; extern crate xml5ever; -pub mod bluetooth_blacklist; mod body; pub mod clipboard_provider; mod devtools; |