From 89c4219a3641a2bbf282ef38c66498e0df79adf9 Mon Sep 17 00:00:00 2001 From: Zakor Gyula Date: Fri, 11 Nov 2016 14:30:09 +0100 Subject: [PATCH] Update requestDevice step annotations. --- components/bluetooth/lib.rs | 13 ++++-- components/script/dom/bluetooth.rs | 73 +++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/components/bluetooth/lib.rs b/components/bluetooth/lib.rs index 9bc00e0c43fd..574c22d1d7ff 100644 --- a/components/bluetooth/lib.rs +++ b/components/bluetooth/lib.rs @@ -567,6 +567,7 @@ impl BluetoothManager { fn request_device(&mut self, options: RequestDeviceoptions, sender: IpcSender) { + // Step 6. let mut adapter = get_adapter_or_return_error!(self, sender); if let Ok(ref session) = adapter.create_discovery_session() { if session.start_discovery().is_ok() { @@ -577,18 +578,20 @@ impl BluetoothManager { let _ = session.stop_discovery(); } - // Step 6. - // Note: There is no requiredServiceUUIDS, we scan for all devices. + // Step 7. + // Note: There are no requiredServiceUUIDS, we scan for all devices. let mut matched_devices = self.get_and_cache_devices(&mut adapter); - // Step 7. + // Step 8. if !options.is_accepting_all_devices() { matched_devices = matched_devices.into_iter() .filter(|d| matches_filters(d, options.get_filters())) .collect(); } - // Step 8. + // Step 9. + // TODO: After the permission API implementation + // https://w3c.github.io/permissions/#prompt-the-user-to-choose if let Some(address) = self.select_device(matched_devices, &adapter) { let device_id = match self.address_to_id.get(&address) { Some(id) => id.clone(), @@ -610,7 +613,9 @@ impl BluetoothManager { return drop(sender.send(Ok(BluetoothResponse::RequestDevice(message)))); } } + // TODO: Step 10-11: Implement the permission API. return drop(sender.send(Err(BluetoothError::NotFound))); + // Step 12: Missing, because it is optional. } fn gatt_server_connect(&mut self, device_id: String, sender: IpcSender) { diff --git a/components/script/dom/bluetooth.rs b/components/script/dom/bluetooth.rs index 1578d12acdb7..ddd1e4d411a6 100644 --- a/components/script/dom/bluetooth.rs +++ b/components/script/dom/bluetooth.rs @@ -78,6 +78,8 @@ impl BluetoothResponseListener f let _ac = JSAutoCompartment::new(promise_cx, promise.reflector().get_jsobject().get()); match response { Ok(response) => self.receiver.root().handle_response(response, promise_cx, &promise), + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice + // Step 3-4. Err(error) => promise.reject_error(promise_cx, Error::from(error)), } } @@ -134,7 +136,7 @@ impl Bluetooth { optional_services: &Option>) { // TODO: Step 1: Triggered by user activation. - // Step 2.2: There is no requiredServiceUUIDS, we scan for all devices. + // Step 2.2: There are no requiredServiceUUIDS, we scan for all devices. let mut uuid_filters = vec!(); if let &Some(ref filters) = filters { @@ -144,18 +146,20 @@ impl Bluetooth { return; } - // Step 2.3: There is no requiredServiceUUIDS, we scan for all devices. + // Step 2.3: There are no requiredServiceUUIDS, we scan for all devices. // Step 2.4. for filter in filters { - // Step 2.4.8. + // Step 2.4.1. match canonicalize_filter(&filter) { + // Step 2.4.2. Ok(f) => uuid_filters.push(f), Err(e) => { p.reject_error(p.global().get_cx(), e); return; }, } + // Step 2.4.3: There are no requiredServiceUUIDS, we scan for all devices. } } @@ -172,8 +176,8 @@ impl Bluetooth { }; // Step 2.7. - // Note: What we are doing here is adding the not blacklisted UUIDs to the result vector, - // insted of removing them from an already filled vector. + // Note: What we are doing here, is adding the not blocklisted UUIDs to the result vector, + // instead of removing them from an already filled vector. if !uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) { optional_services_uuids.push(uuid); } @@ -212,8 +216,9 @@ pub fn response_async( action_sender } +// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothlescanfilterinit-canonicalizing fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible { - // Step 2.4.1. + // Step 1. if filter.services.is_none() && filter.name.is_none() && filter.namePrefix.is_none() && @@ -222,13 +227,13 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible { - // Step 2.4.3.1. + // Step 3.1. if services.is_empty() { return Err(Type(SERVICE_ERROR.to_owned())); } @@ -236,27 +241,26 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible vec!(), }; - // Step 2.4.4. + // Step 4. let name = match filter.name { Some(ref name) => { - // Step 2.4.4.1. + // Step 4.1. // Note: DOMString::len() gives back the size in bytes. if name.len() > MAX_DEVICE_NAME_LENGTH { return Err(Type(NAME_TOO_LONG_ERROR.to_owned())); @@ -265,16 +269,16 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible None, }; - // Step 2.4.5. + // Step 5. let name_prefix = match filter.namePrefix { Some(ref name_prefix) => { - // Step 2.4.5.1. + // Step 5.1. if name_prefix.is_empty() { return Err(Type(NAME_PREFIX_ERROR.to_owned())); } @@ -285,24 +289,30 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible String::new(), }; - // Step 2.4.6 - 2.4.7 + // Step 6 -7. let manufacturer_data = match filter.manufacturerData { Some(ref manufacturer_data_map) => { + // Note: If manufacturer_data_map is empty, that means there are no key values in it. if manufacturer_data_map.is_empty() { return Err(Type(MANUFACTURER_DATA_ERROR.to_owned())); } let mut map = HashMap::new(); for (key, bdfi) in manufacturer_data_map.iter() { + // Step 7.1-7.2. let manufacturer_id = match u16::from_str(key.as_ref()) { Ok(id) => id, Err(err) => return Err(Type(format!("{} {} {}", KEY_CONVERSION_ERROR, key, err))), }; + + // Step 7.3: No need to convert to IDL values since this is only used by native code. + + // Step 7.4 -7.5. map.insert(manufacturer_id, try!(canonicalize_bluetooth_data_filter_init(bdfi))); } Some(map) @@ -310,22 +320,33 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible None, }; - // Step 2.4.8 -2.4.9 + // Step 8-9. let service_data = match filter.serviceData { Some(ref service_data_map) => { + // Note: If service_data_map is empty, that means there are no key values in it. if service_data_map.is_empty() { return Err(Type(SERVICE_DATA_ERROR.to_owned())); } let mut map = HashMap::new(); for (key, bdfi) in service_data_map.iter() { let service_name = match u32::from_str(key.as_ref()) { + // Step 9.1. Ok(number) => StringOrUnsignedLong::UnsignedLong(number), + // Step 9.2. _ => StringOrUnsignedLong::String(key.clone()) }; + + // Step 9.3-9.4. let service = try!(BluetoothUUID::service(service_name)).to_string(); + + // Step 9.5. if uuid_is_blocklisted(service.as_ref(), Blocklist::All) { return Err(Security); } + + // Step 9.6: No need to convert to IDL values since this is only used by native code. + + // Step 9.7 -9.8. map.insert(service, try!(canonicalize_bluetooth_data_filter_init(bdfi))); } Some(map) @@ -341,14 +362,17 @@ fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible Fallible<(Vec, Vec)> { // Step 1. let data_prefix = bdfi.dataPrefix.clone().unwrap_or(vec![]); + // Step 2. // If no mask present, mask will be a sequence of 0xFF bytes the same length as dataPrefix. // Masking dataPrefix with this, leaves dataPrefix untouched. let mask = bdfi.mask.clone().unwrap_or(vec![0xFF; data_prefix.len()]); + // Step 3. if mask.len() != data_prefix.len() { return Err(Type(MASK_LENGTH_ERROR.to_owned())); } + // Step 4. Ok((data_prefix, mask)) } @@ -377,9 +401,10 @@ impl BluetoothMethods for Bluetooth { p.reject_error(p.global().get_cx(), Error::Type(OPTIONS_ERROR.to_owned())); return p; } + // Step 2. self.request_bluetooth_devices(&p, &option.filters, &option.optionalServices); - // TODO(#4282): Step 3-5: Reject and resolve promise. + //Note: Step 3-4. in response function, Step 5. in handle_response function. return p; } @@ -390,6 +415,8 @@ impl BluetoothMethods for Bluetooth { impl AsyncBluetoothListener for Bluetooth { fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc) { match response { + // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices + // Step 13-14. BluetoothResponse::RequestDevice(device) => { let mut device_instance_map = self.device_instance_map.borrow_mut(); if let Some(existing_device) = device_instance_map.get(&device.id.clone()) { @@ -405,6 +432,8 @@ impl AsyncBluetoothListener for Bluetooth { &ad_data, &self); device_instance_map.insert(device.id, MutHeap::new(&bt_device)); + // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice + // Step 5. promise.resolve_native(promise_cx, &bt_device); }, _ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),