diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index eecaf6489be..42ad4c2730a 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -88,7 +88,7 @@ impl Bluetooth { // TODO: Step 1: Triggered by user activation. // Step 2. - let option = try!(convert_request_device_options(self.global().r(), filters, optional_services)); + let option = try!(convert_request_device_options(filters, optional_services)); // TODO: Step 3-5: Implement the permission API. @@ -123,8 +123,7 @@ impl Bluetooth { } // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices -fn convert_request_device_options(global: GlobalRef, - filters: &Option>, +fn convert_request_device_options(filters: &Option>, optional_services: &Option>) -> Fallible { // Step 2.2: There is no requiredServiceUUIDS, we scan for all devices. @@ -141,7 +140,7 @@ fn convert_request_device_options(global: GlobalRef, // Step 2.4. for filter in filters { // Step 2.4.8. - uuid_filters.push(try!(canonicalize_filter(&filter, global))); + uuid_filters.push(try!(canonicalize_filter(&filter))); } } @@ -149,7 +148,7 @@ fn convert_request_device_options(global: GlobalRef, if let &Some(ref opt_services) = optional_services { for opt_service in opt_services { // Step 2.5 - 2.6. - let uuid = try!(BluetoothUUID::GetService(global, opt_service.clone())).to_string(); + let uuid = try!(BluetoothUUID::service(opt_service.clone())).to_string(); // Step 2.7. // Note: What we are doing here is adding the not blacklisted UUIDs to the result vector, @@ -165,7 +164,7 @@ fn convert_request_device_options(global: GlobalRef, } // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices -fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) -> Fallible { +fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible { // Step 2.4.1. if filter.services.is_none() && filter.name.is_none() && @@ -190,7 +189,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) for service in services { // Step 2.4.3.2 - 2.4.3.3. - let uuid = try!(BluetoothUUID::GetService(global, service.clone())).to_string(); + let uuid = try!(BluetoothUUID::service(service.clone())).to_string(); // Step 2.4.3.4. if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { @@ -251,7 +250,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter, global: GlobalRef) let service_data_uuid = match filter.serviceDataUUID { Some(ref service_data_uuid) => { // Step 2.4.7.1 - 2.4.7.2. - let uuid = try!(BluetoothUUID::GetService(global, service_data_uuid.clone())).to_string(); + let uuid = try!(BluetoothUUID::service(service_data_uuid.clone())).to_string(); // Step 2.4.7.3. if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { diff --git a/components/script/dom/bluetoothremotegattcharacteristic.rs b/components/script/dom/bluetoothremotegattcharacteristic.rs index 633b0b060e2..6b7ba1dcfad 100644 --- a/components/script/dom/bluetoothremotegattcharacteristic.rs +++ b/components/script/dom/bluetoothremotegattcharacteristic.rs @@ -85,7 +85,7 @@ impl BluetoothRemoteGATTCharacteristic { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-getdescriptor fn get_descriptor(&self, descriptor: BluetoothDescriptorUUID) -> Fallible> { - let uuid = try!(BluetoothUUID::GetDescriptor(self.global().r(), descriptor)).to_string(); + let uuid = try!(BluetoothUUID::descriptor(descriptor)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } @@ -112,7 +112,7 @@ impl BluetoothRemoteGATTCharacteristic { -> Fallible>> { let mut uuid: Option = None; if let Some(d) = descriptor { - uuid = Some(try!(BluetoothUUID::GetDescriptor(self.global().r(), d)).to_string()); + uuid = Some(try!(BluetoothUUID::descriptor(d)).to_string()); if let Some(ref uuid) = uuid { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) diff --git a/components/script/dom/bluetoothremotegattserver.rs b/components/script/dom/bluetoothremotegattserver.rs index c9fa5477f3f..3439ba53350 100644 --- a/components/script/dom/bluetoothremotegattserver.rs +++ b/components/script/dom/bluetoothremotegattserver.rs @@ -70,7 +70,7 @@ impl BluetoothRemoteGATTServer { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice fn get_primary_service(&self, service: BluetoothServiceUUID) -> Fallible> { - let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); + let uuid = try!(BluetoothUUID::service(service)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } @@ -98,7 +98,7 @@ impl BluetoothRemoteGATTServer { -> Fallible>> { let mut uuid: Option = None; if let Some(s) = service { - uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); + uuid = Some(try!(BluetoothUUID::service(s)).to_string()); if let Some(ref uuid) = uuid { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) diff --git a/components/script/dom/bluetoothremotegattservice.rs b/components/script/dom/bluetoothremotegattservice.rs index 709de16cf5f..3ece2507cd3 100644 --- a/components/script/dom/bluetoothremotegattservice.rs +++ b/components/script/dom/bluetoothremotegattservice.rs @@ -74,7 +74,7 @@ impl BluetoothRemoteGATTService { fn get_characteristic(&self, characteristic: BluetoothCharacteristicUUID) -> Fallible> { - let uuid = try!(BluetoothUUID::GetCharacteristic(self.global().r(), characteristic)).to_string(); + let uuid = try!(BluetoothUUID::characteristic(characteristic)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } @@ -112,7 +112,7 @@ impl BluetoothRemoteGATTService { -> Fallible>> { let mut uuid: Option = None; if let Some(c) = characteristic { - uuid = Some(try!(BluetoothUUID::GetCharacteristic(self.global().r(), c)).to_string()); + uuid = Some(try!(BluetoothUUID::characteristic(c)).to_string()); if let Some(ref uuid) = uuid { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) @@ -155,7 +155,7 @@ impl BluetoothRemoteGATTService { fn get_included_service(&self, service: BluetoothServiceUUID) -> Fallible> { - let uuid = try!(BluetoothUUID::GetService(self.global().r(), service)).to_string(); + let uuid = try!(BluetoothUUID::service(service)).to_string(); if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) } @@ -185,7 +185,7 @@ impl BluetoothRemoteGATTService { -> Fallible>> { let mut uuid: Option = None; if let Some(s) = service { - uuid = Some(try!(BluetoothUUID::GetService(self.global().r(), s)).to_string()); + uuid = Some(try!(BluetoothUUID::service(s)).to_string()); if let Some(ref uuid) = uuid { if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) { return Err(Security) diff --git a/components/script/dom/bluetoothuuid.rs b/components/script/dom/bluetoothuuid.rs index dafda71a12b..e9669cc8acb 100644 --- a/components/script/dom/bluetoothuuid.rs +++ b/components/script/dom/bluetoothuuid.rs @@ -272,60 +272,36 @@ const VALID_UUID_REGEX: &'static str = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0- impl BluetoothUUID { // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-canonicaluuid pub fn CanonicalUUID(_: GlobalRef, alias: u32) -> UUID { - DOMString::from(format!("{:08x}", &alias) + BASE_UUID) + canonical_uuid(alias) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getservice - pub fn GetService(globalref: GlobalRef, name: BluetoothServiceUUID) -> Fallible { - BluetoothUUID::resolve_uuid_name(globalref, - name, - BLUETOOTH_ASSIGNED_SERVICES, - DOMString::from(SERVICE_PREFIX)) + pub fn GetService(_: GlobalRef, name: BluetoothServiceUUID) -> Fallible { + Self::service(name) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getcharacteristic - pub fn GetCharacteristic(globalref: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible { - BluetoothUUID::resolve_uuid_name(globalref, - name, - BLUETOOTH_ASSIGNED_CHARCTERISTICS, - DOMString::from(CHARACTERISTIC_PREFIX)) + pub fn GetCharacteristic(_: GlobalRef, name: BluetoothCharacteristicUUID) -> Fallible { + Self::characteristic(name) } // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothuuid-getdescriptor - pub fn GetDescriptor(globalref: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible { - BluetoothUUID::resolve_uuid_name(globalref, - name, - BLUETOOTH_ASSIGNED_DESCRIPTORS, - DOMString::from(DESCRIPTOR_PREFIX)) + pub fn GetDescriptor(_: GlobalRef, name: BluetoothDescriptorUUID) -> Fallible { + Self::descriptor(name) + } +} + +impl BluetoothUUID { + pub fn service(name: BluetoothServiceUUID) -> Fallible { + resolve_uuid_name(name, BLUETOOTH_ASSIGNED_SERVICES, SERVICE_PREFIX) } - // https://webbluetoothcg.github.io/web-bluetooth/#resolveuuidname - pub fn resolve_uuid_name(globalref: GlobalRef, - name: StringOrUnsignedLong, - assigned_numbers_table: &'static [(&'static str, u32)], - prefix: DOMString) - -> Fallible { - match name { - // Step 1 - StringOrUnsignedLong::UnsignedLong(unsigned32) => { - Ok(BluetoothUUID::CanonicalUUID(globalref, unsigned32)) - }, - StringOrUnsignedLong::String(dstring) => { - // Step 2 - let regex = Regex::new(VALID_UUID_REGEX).unwrap(); - if regex.is_match(&*dstring) { - Ok(dstring) - } else { - // Step 3 - let concatenated = format!("{}.{}", prefix, dstring); - let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated); - match is_in_table { - Some(&(_, alias)) => Ok(BluetoothUUID::CanonicalUUID(globalref, alias)), - None => Err(Syntax), - } - } - }, - } + pub fn characteristic(name: BluetoothServiceUUID) -> Fallible { + resolve_uuid_name(name, BLUETOOTH_ASSIGNED_CHARCTERISTICS, CHARACTERISTIC_PREFIX) + } + + pub fn descriptor(name: BluetoothDescriptorUUID) -> Fallible { + resolve_uuid_name(name, BLUETOOTH_ASSIGNED_DESCRIPTORS, DESCRIPTOR_PREFIX) } } @@ -337,3 +313,36 @@ impl Clone for StringOrUnsignedLong { } } } + +fn canonical_uuid(alias: u32) -> UUID { + UUID::from(format!("{:08x}", &alias) + BASE_UUID) +} + +// https://webbluetoothcg.github.io/web-bluetooth/#resolveuuidname +fn resolve_uuid_name( + name: StringOrUnsignedLong, + assigned_numbers_table: &'static [(&'static str, u32)], + prefix: &str) + -> Fallible { + match name { + // Step 1 + StringOrUnsignedLong::UnsignedLong(unsigned32) => { + Ok(canonical_uuid(unsigned32)) + }, + StringOrUnsignedLong::String(dstring) => { + // Step 2 + let regex = Regex::new(VALID_UUID_REGEX).unwrap(); + if regex.is_match(&*dstring) { + Ok(dstring) + } else { + // Step 3 + let concatenated = format!("{}.{}", prefix, dstring); + let is_in_table = assigned_numbers_table.iter().find(|p| p.0 == concatenated); + match is_in_table { + Some(&(_, alias)) => Ok(canonical_uuid(alias)), + None => Err(Syntax), + } + } + }, + } +}