diff options
author | zakorgyula <gyula.zakor@gmail.com> | 2016-10-07 16:17:11 +0200 |
---|---|---|
committer | zakorgyula <gyula.zakor@gmail.com> | 2016-11-04 15:36:57 +0100 |
commit | e8c1c98a77dd64a902a4941a8889b9bd5603ce2d (patch) | |
tree | e65e36e8d1a67182795d4f8a79fcf322b27b18ba /components/script/dom/bluetooth.rs | |
parent | d30bcbd33948f41fc3389f8105885060fd7541a5 (diff) | |
download | servo-e8c1c98a77dd64a902a4941a8889b9bd5603ce2d.tar.gz servo-e8c1c98a77dd64a902a4941a8889b9bd5603ce2d.zip |
Return the same JS object for the same Bluetooth item.
Diffstat (limited to 'components/script/dom/bluetooth.rs')
-rw-r--r-- | components/script/dom/bluetooth.rs | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 3325ef150fe..7080a38668b 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -7,20 +7,25 @@ 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, 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."; @@ -42,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()), } } @@ -57,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() } @@ -102,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)) |