Return the same JS object for the same Bluetooth item.

This commit is contained in:
zakorgyula 2016-10-07 16:17:11 +02:00
parent d30bcbd339
commit e8c1c98a77
12 changed files with 190 additions and 93 deletions

View file

@ -7,20 +7,25 @@ use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence}; use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence}; use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use core::clone::Clone; use core::clone::Clone;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothMethods, BluetoothRequestDeviceFilter}; use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothMethods, BluetoothRequestDeviceFilter};
use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions; use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions;
use dom::bindings::error::Error::{self, NotFound, Security, Type}; use dom::bindings::error::Error::{self, NotFound, Security, Type};
use dom::bindings::error::Fallible; 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::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::DOMString; use dom::bindings::str::DOMString;
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothdevice::BluetoothDevice; use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID}; use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
use dom::promise::Promise; use dom::promise::Promise;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use js::conversions::ToJSValConvertible; use js::conversions::ToJSValConvertible;
use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices."; 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] #[dom_struct]
pub struct Bluetooth { pub struct Bluetooth {
reflector_: Reflector, 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 { impl Bluetooth {
pub fn new_inherited() -> Bluetooth { pub fn new_inherited() -> Bluetooth {
Bluetooth { Bluetooth {
reflector_: Reflector::new(), 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) 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> { fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
self.global().as_window().bluetooth_thread() self.global().as_window().bluetooth_thread()
} }
@ -102,15 +128,21 @@ impl Bluetooth {
// Step 12-13. // Step 12-13.
match device { match device {
Ok(device) => { Ok(device) => {
let global = self.global(); let mut device_instance_map = self.device_instance_map.borrow_mut();
let ad_data = BluetoothAdvertisingData::new(&global, 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.appearance,
device.tx_power, device.tx_power,
device.rssi); device.rssi);
Ok(BluetoothDevice::new(&global, let bt_device = BluetoothDevice::new(&self.global(),
DOMString::from(device.id), DOMString::from(device.id.clone()),
device.name.map(DOMString::from), device.name.map(DOMString::from),
&ad_data)) &ad_data,
&self);
device_instance_map.insert(device.id, MutHeap::new(&bt_device));
Ok(bt_device)
}, },
Err(error) => { Err(error) => {
Err(Error::from(error)) Err(Error::from(error))

View file

@ -7,6 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMet
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap}; use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object}; use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::DOMString; use dom::bindings::str::DOMString;
use dom::bluetooth::Bluetooth;
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData; use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer; use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
@ -19,12 +20,14 @@ pub struct BluetoothDevice {
name: Option<DOMString>, name: Option<DOMString>,
ad_data: MutHeap<JS<BluetoothAdvertisingData>>, ad_data: MutHeap<JS<BluetoothAdvertisingData>>,
gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>, gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>,
context: MutHeap<JS<Bluetooth>>,
} }
impl BluetoothDevice { impl BluetoothDevice {
pub fn new_inherited(id: DOMString, pub fn new_inherited(id: DOMString,
name: Option<DOMString>, name: Option<DOMString>,
ad_data: &BluetoothAdvertisingData) ad_data: &BluetoothAdvertisingData,
context: &Bluetooth)
-> BluetoothDevice { -> BluetoothDevice {
BluetoothDevice { BluetoothDevice {
reflector_: Reflector::new(), reflector_: Reflector::new(),
@ -32,20 +35,27 @@ impl BluetoothDevice {
name: name, name: name,
ad_data: MutHeap::new(ad_data), ad_data: MutHeap::new(ad_data),
gatt: Default::default(), gatt: Default::default(),
context: MutHeap::new(context),
} }
} }
pub fn new(global: &GlobalScope, pub fn new(global: &GlobalScope,
id: DOMString, id: DOMString,
name: Option<DOMString>, name: Option<DOMString>,
adData: &BluetoothAdvertisingData) adData: &BluetoothAdvertisingData,
context: &Bluetooth)
-> Root<BluetoothDevice> { -> Root<BluetoothDevice> {
reflect_dom_object(box BluetoothDevice::new_inherited(id, reflect_dom_object(box BluetoothDevice::new_inherited(id,
name, name,
adData), adData,
context),
global, global,
BluetoothDeviceBinding::Wrap) BluetoothDeviceBinding::Wrap)
} }
pub fn get_context(&self) -> Root<Bluetooth> {
self.context.get()
}
} }
impl BluetoothDeviceMethods for BluetoothDevice { impl BluetoothDeviceMethods for BluetoothDevice {

View file

@ -96,10 +96,17 @@ impl BluetoothRemoteGATTCharacteristic {
let descriptor = receiver.recv().unwrap(); let descriptor = receiver.recv().unwrap();
match descriptor { match descriptor {
Ok(descriptor) => { Ok(descriptor) => {
Ok(BluetoothRemoteGATTDescriptor::new(&self.global(), let context = self.service.get().get_device().get_context();
self, let mut descriptor_map = context.get_descriptor_map().borrow_mut();
DOMString::from(descriptor.uuid), if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) {
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) => {
Err(Error::from(error)) Err(Error::from(error))
@ -123,18 +130,31 @@ impl BluetoothRemoteGATTCharacteristic {
if !self.Service().Device().Gatt().Connected() { if !self.Service().Device().Gatt().Connected() {
return Err(Network) return Err(Network)
} }
let mut descriptors = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap(); BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
let descriptors_vec = receiver.recv().unwrap(); let descriptors_vec = receiver.recv().unwrap();
match descriptors_vec { match descriptors_vec {
Ok(descriptor_vec) => { Ok(descriptor_vec) => {
Ok(descriptor_vec.into_iter() let context = self.service.get().get_device().get_context();
.map(|desc| BluetoothRemoteGATTDescriptor::new(&self.global(), let mut descriptor_map = context.get_descriptor_map().borrow_mut();
self, for descriptor in descriptor_vec {
DOMString::from(desc.uuid), let bt_descriptor = match descriptor_map.get(&descriptor.instance_id) {
desc.instance_id)) Some(existing_descriptor) => existing_descriptor.get(),
.collect()) 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) => {
Err(Error::from(error)) Err(Error::from(error))

View file

@ -81,11 +81,18 @@ impl BluetoothRemoteGATTServer {
let service = receiver.recv().unwrap(); let service = receiver.recv().unwrap();
match service { match service {
Ok(service) => { Ok(service) => {
Ok(BluetoothRemoteGATTService::new(&self.global(), let context = self.device.get().get_context();
&self.device.get(), let mut service_map = context.get_service_map().borrow_mut();
DOMString::from(service.uuid), if let Some(existing_service) = service_map.get(&service.instance_id) {
service.is_primary, return Ok(existing_service.get());
service.instance_id)) }
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) => {
Err(Error::from(error)) Err(Error::from(error))
@ -109,19 +116,32 @@ impl BluetoothRemoteGATTServer {
if !self.Device().Gatt().Connected() { if !self.Device().Gatt().Connected() {
return Err(Network) return Err(Network)
} }
let mut services = vec!();
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap(); BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
let services_vec = receiver.recv().unwrap(); let services_vec = receiver.recv().unwrap();
match services_vec { match services_vec {
Ok(service_vec) => { Ok(service_vec) => {
Ok(service_vec.into_iter() let context = self.device.get().get_context();
.map(|service| BluetoothRemoteGATTService::new(&self.global(), let mut service_map = context.get_service_map().borrow_mut();
&self.device.get(), for service in service_vec {
DOMString::from(service.uuid), let bt_service = match service_map.get(&service.instance_id) {
service.is_primary, Some(existing_service) => existing_service.get(),
service.instance_id)) None => {
.collect()) 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) => {
Err(Error::from(error)) Err(Error::from(error))

View file

@ -62,6 +62,10 @@ impl BluetoothRemoteGATTService {
BluetoothRemoteGATTServiceBinding::Wrap) BluetoothRemoteGATTServiceBinding::Wrap)
} }
pub fn get_device(&self) -> Root<BluetoothDevice> {
self.device.get()
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> { fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
self.global().as_window().bluetooth_thread() self.global().as_window().bluetooth_thread()
} }
@ -87,6 +91,11 @@ impl BluetoothRemoteGATTService {
let characteristic = receiver.recv().unwrap(); let characteristic = receiver.recv().unwrap();
match characteristic { match characteristic {
Ok(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 global = self.global();
let properties = BluetoothCharacteristicProperties::new(&global, let properties = BluetoothCharacteristicProperties::new(&global,
characteristic.broadcast, characteristic.broadcast,
@ -98,11 +107,13 @@ impl BluetoothRemoteGATTService {
characteristic.authenticated_signed_writes, characteristic.authenticated_signed_writes,
characteristic.reliable_write, characteristic.reliable_write,
characteristic.writable_auxiliaries); characteristic.writable_auxiliaries);
Ok(BluetoothRemoteGATTCharacteristic::new(&global, let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&global,
self, self,
DOMString::from(characteristic.uuid), DOMString::from(characteristic.uuid),
&properties, &properties,
characteristic.instance_id)) characteristic.instance_id.clone());
characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic));
Ok(bt_characteristic)
}, },
Err(error) => { Err(error) => {
Err(Error::from(error)) Err(Error::from(error))
@ -133,23 +144,35 @@ impl BluetoothRemoteGATTService {
let characteristics_vec = receiver.recv().unwrap(); let characteristics_vec = receiver.recv().unwrap();
match characteristics_vec { match characteristics_vec {
Ok(characteristic_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 { for characteristic in characteristic_vec {
let global = self.global(); let bt_characteristic = match characteristic_map.get(&characteristic.instance_id) {
let properties = BluetoothCharacteristicProperties::new(&global, Some(existing_characteristic) => existing_characteristic.get(),
characteristic.broadcast, None => {
characteristic.read, let properties =
characteristic.write_without_response, BluetoothCharacteristicProperties::new(&self.global(),
characteristic.write, characteristic.broadcast,
characteristic.notify, characteristic.read,
characteristic.indicate, characteristic.write_without_response,
characteristic.authenticated_signed_writes, characteristic.write,
characteristic.reliable_write, characteristic.notify,
characteristic.writable_auxiliaries); characteristic.indicate,
characteristics.push(BluetoothRemoteGATTCharacteristic::new(&global, characteristic.authenticated_signed_writes,
self, characteristic.reliable_write,
DOMString::from(characteristic.uuid), characteristic.writable_auxiliaries);
&properties,
characteristic.instance_id)); 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) Ok(characteristics)
}, },
@ -178,11 +201,18 @@ impl BluetoothRemoteGATTService {
let service = receiver.recv().unwrap(); let service = receiver.recv().unwrap();
match service { match service {
Ok(service) => { Ok(service) => {
Ok(BluetoothRemoteGATTService::new(&self.global(), let context = self.device.get().get_context();
&self.device.get(), let mut service_map = context.get_service_map().borrow_mut();
DOMString::from(service.uuid), if let Some(existing_service) = service_map.get(&service.instance_id) {
service.is_primary, return Ok(existing_service.get());
service.instance_id)) }
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) => {
Err(Error::from(error)) Err(Error::from(error))
@ -212,15 +242,28 @@ impl BluetoothRemoteGATTService {
uuid, uuid,
sender)).unwrap(); sender)).unwrap();
let services_vec = receiver.recv().unwrap(); let services_vec = receiver.recv().unwrap();
let mut services = vec!();
match services_vec { match services_vec {
Ok(service_vec) => { Ok(service_vec) => {
Ok(service_vec.into_iter() let context = self.device.get().get_context();
.map(|service| BluetoothRemoteGATTService::new(&self.global(), let mut service_map = context.get_service_map().borrow_mut();
&self.device.get(), for service in service_vec {
DOMString::from(service.uuid), let bt_service = match service_map.get(&service.instance_id) {
service.is_primary, Some(existing_service) => existing_service.get(),
service.instance_id)) None => {
.collect()) 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) => {
Err(Error::from(error)) Err(Error::from(error))

View file

@ -1,4 +0,0 @@
[get-same-characteristic.html]
type: testharness
[Calls to get the same characteristic should return the same object.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[get-same-characteristics.html]
type: testharness
[Calls to get the same characteristics should return the same objects.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[get-same-descriptor.html]
type: testharness
[Calls to get the same descriptor should return the same object.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[get-same-descriptors.html]
type: testharness
[Calls to get the same descriptor should return the same object.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[get-same-service.html]
type: testharness
[Calls to get the same service should return the same object.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[get-same-service.html]
type: testharness
[Calls to get the same service should return the same object.]
expected: FAIL

View file

@ -1,4 +0,0 @@
[same-device.html]
type: testharness
[Returned device should always be the same.]
expected: FAIL