Skip to content

Commit

Permalink
Return the same JS object for the same Bluetooth item.
Browse files Browse the repository at this point in the history
  • Loading branch information
zakorgy committed Nov 4, 2016
1 parent d30bcbd commit e8c1c98
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 93 deletions.
46 changes: 39 additions & 7 deletions components/script/dom/bluetooth.rs
Expand Up @@ -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.";
Expand All @@ -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()),
}
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 13 additions & 3 deletions components/script/dom/bluetoothdevice.rs
Expand Up @@ -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;
Expand All @@ -19,33 +20,42 @@ 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(),
id: id,
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 {
Expand Down
40 changes: 30 additions & 10 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
44 changes: 32 additions & 12 deletions components/script/dom/bluetoothremotegattserver.rs
Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit e8c1c98

Please sign in to comment.