aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorzakorgyula <gyula.zakor@gmail.com>2016-10-07 16:17:11 +0200
committerzakorgyula <gyula.zakor@gmail.com>2016-11-04 15:36:57 +0100
commite8c1c98a77dd64a902a4941a8889b9bd5603ce2d (patch)
treee65e36e8d1a67182795d4f8a79fcf322b27b18ba /components
parentd30bcbd33948f41fc3389f8105885060fd7541a5 (diff)
downloadservo-e8c1c98a77dd64a902a4941a8889b9bd5603ce2d.tar.gz
servo-e8c1c98a77dd64a902a4941a8889b9bd5603ce2d.zip
Return the same JS object for the same Bluetooth item.
Diffstat (limited to 'components')
-rw-r--r--components/script/dom/bluetooth.rs46
-rw-r--r--components/script/dom/bluetoothdevice.rs16
-rw-r--r--components/script/dom/bluetoothremotegattcharacteristic.rs40
-rw-r--r--components/script/dom/bluetoothremotegattserver.rs44
-rw-r--r--components/script/dom/bluetoothremotegattservice.rs109
5 files changed, 190 insertions, 65 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))
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 299ef921ee6..1b2a16c1a4b 100644
--- a/components/script/dom/bluetoothremotegattcharacteristic.rs
+++ b/components/script/dom/bluetoothremotegattcharacteristic.rs
@@ -96,10 +96,17 @@ impl BluetoothRemoteGATTCharacteristic {
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))
@@ -123,18 +130,31 @@ 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))
diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs
index 3499e431f1c..9ac1ca4c4f4 100644
--- a/components/script/dom/bluetoothremotegattserver.rs
+++ b/components/script/dom/bluetoothremotegattserver.rs
@@ -81,11 +81,18 @@ impl BluetoothRemoteGATTServer {
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))
@@ -109,19 +116,32 @@ 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 cd7c6ab4366..c597338e1ba 100644
--- a/components/script/dom/bluetoothremotegattservice.rs
+++ b/components/script/dom/bluetoothremotegattservice.rs
@@ -62,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()
}
@@ -87,6 +91,11 @@ impl BluetoothRemoteGATTService {
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,
@@ -98,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))
@@ -133,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)
},
@@ -178,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))
@@ -212,15 +242,28 @@ impl BluetoothRemoteGATTService {
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))