mirror of
https://github.com/servo/servo.git
synced 2025-06-21 07:38:59 +01:00
Return the same JS object for the same Bluetooth item.
This commit is contained in:
parent
d30bcbd339
commit
e8c1c98a77
12 changed files with 190 additions and 93 deletions
|
@ -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),
|
||||
let bt_device = BluetoothDevice::new(&self.global(),
|
||||
DOMString::from(device.id.clone()),
|
||||
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::from(error))
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -96,10 +96,17 @@ impl BluetoothRemoteGATTCharacteristic {
|
|||
let descriptor = receiver.recv().unwrap();
|
||||
match descriptor {
|
||||
Ok(descriptor) => {
|
||||
Ok(BluetoothRemoteGATTDescriptor::new(&self.global(),
|
||||
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))
|
||||
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(),
|
||||
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(desc.uuid),
|
||||
desc.instance_id))
|
||||
.collect())
|
||||
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))
|
||||
|
|
|
@ -81,11 +81,18 @@ impl BluetoothRemoteGATTServer {
|
|||
let service = receiver.recv().unwrap();
|
||||
match service {
|
||||
Ok(service) => {
|
||||
Ok(BluetoothRemoteGATTService::new(&self.global(),
|
||||
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))
|
||||
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(),
|
||||
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))
|
||||
.collect())
|
||||
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))
|
||||
|
|
|
@ -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,
|
||||
let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&global,
|
||||
self,
|
||||
DOMString::from(characteristic.uuid),
|
||||
&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::from(error))
|
||||
|
@ -133,9 +144,14 @@ 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,
|
||||
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,
|
||||
|
@ -145,11 +161,18 @@ impl BluetoothRemoteGATTService {
|
|||
characteristic.authenticated_signed_writes,
|
||||
characteristic.reliable_write,
|
||||
characteristic.writable_auxiliaries);
|
||||
characteristics.push(BluetoothRemoteGATTCharacteristic::new(&global,
|
||||
|
||||
BluetoothRemoteGATTCharacteristic::new(&self.global(),
|
||||
self,
|
||||
DOMString::from(characteristic.uuid),
|
||||
&properties,
|
||||
characteristic.instance_id));
|
||||
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(),
|
||||
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))
|
||||
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(),
|
||||
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))
|
||||
.collect())
|
||||
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))
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-characteristic.html]
|
||||
type: testharness
|
||||
[Calls to get the same characteristic should return the same object.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-characteristics.html]
|
||||
type: testharness
|
||||
[Calls to get the same characteristics should return the same objects.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-descriptor.html]
|
||||
type: testharness
|
||||
[Calls to get the same descriptor should return the same object.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-descriptors.html]
|
||||
type: testharness
|
||||
[Calls to get the same descriptor should return the same object.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-service.html]
|
||||
type: testharness
|
||||
[Calls to get the same service should return the same object.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[get-same-service.html]
|
||||
type: testharness
|
||||
[Calls to get the same service should return the same object.]
|
||||
expected: FAIL
|
|
@ -1,4 +0,0 @@
|
|||
[same-device.html]
|
||||
type: testharness
|
||||
[Returned device should always be the same.]
|
||||
expected: FAIL
|
Loading…
Add table
Add a link
Reference in a new issue