use bluetooth_traits::{BluetoothError, BluetoothRequest, GATTType};
use bluetooth_traits::{BluetoothResponse, BluetoothResponseResult};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use crate::realms::{AlreadyInRealm, InRealm};
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::BluetoothBinding::BluetoothDataFilterInit;
use crate::dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothMethods, RequestDeviceOptions};
use crate::dom::bindings::codegen::Bindings::BluetoothBinding::BluetoothLEScanFilterInit;
use crate::dom::bindings::codegen::Bindings::BluetoothPermissionResultBinding::BluetoothPermissionDescriptor;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServer_Binding::
BluetoothRemoteGATTServerMethods;
use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::{PermissionName, PermissionState};
use crate::dom::bindings::codegen::UnionTypes::{ArrayBufferViewOrArrayBuffer, StringOrUnsignedLong};
use crate::dom::bindings::error::Error::{self, Network, Security, Type};
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{DomObject, reflect_dom_object};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bluetoothdevice::BluetoothDevice;
use crate::dom::bluetoothpermissionresult::BluetoothPermissionResult;
use crate::dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID, UUID};
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::permissions::{get_descriptor_permission_state, PermissionAlgorithm};
use crate::dom::promise::Promise;
use crate::script_runtime::{CanGc, JSContext};
use crate::task::TaskOnce;
use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use js::conversions::ConversionResult;
use js::jsapi::JSObject;
use js::jsval::{ObjectValue, UndefinedValue};
use profile_traits::ipc as ProfiledIpc;
use std::collections::HashMap;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
const KEY_CONVERSION_ERROR: &str =
"This `manufacturerData` key can not be parsed as unsigned short:";
const FILTER_EMPTY_ERROR: &str =
"'filters' member, if present, must be nonempty to find any devices.";
const FILTER_ERROR: &str = "A filter must restrict the devices in some way.";
const MANUFACTURER_DATA_ERROR: &str =
"'manufacturerData', if present, must be non-empty to filter devices.";
const MASK_LENGTH_ERROR: &str = "`mask`, if present, must have the same length as `dataPrefix`.";
const MAX_DEVICE_NAME_LENGTH: usize = 248;
const NAME_PREFIX_ERROR: &str = "'namePrefix', if present, must be nonempty.";
const NAME_TOO_LONG_ERROR: &str = "A device name can't be longer than 248 bytes.";
const SERVICE_DATA_ERROR: &str = "'serviceData', if present, must be non-empty to filter devices.";
const SERVICE_ERROR: &str = "'services', if present, must contain at least one service.";
const OPTIONS_ERROR: &str = "Fields of 'options' conflict with each other.
Either 'acceptAllDevices' member must be true, or 'filters' member must be set to a value.";
const BT_DESC_CONVERSION_ERROR: &str =
"Can't convert to an IDL value of type BluetoothPermissionDescriptor";
#[derive(JSTraceable, MallocSizeOf)]
#[allow(non_snake_case)]
pub struct AllowedBluetoothDevice {
pub deviceId: DOMString,
pub mayUseGATT: bool,
}
#[derive(JSTraceable, MallocSizeOf)]
pub struct BluetoothExtraPermissionData {
allowed_devices: DomRefCell<Vec<AllowedBluetoothDevice>>,
}
impl BluetoothExtraPermissionData {
pub fn new() -> BluetoothExtraPermissionData {
BluetoothExtraPermissionData {
allowed_devices: DomRefCell::new(Vec::new()),
}
}
pub fn add_new_allowed_device(&self, allowed_device: AllowedBluetoothDevice) {
self.allowed_devices.borrow_mut().push(allowed_device);
}
fn get_allowed_devices(&self) -> Ref<Vec<AllowedBluetoothDevice>> {
self.allowed_devices.borrow()
}
pub fn allowed_devices_contains_id(&self, id: DOMString) -> bool {
self.allowed_devices
.borrow()
.iter()
.any(|d| d.deviceId == id)
}
}
impl Default for BluetoothExtraPermissionData {
fn default() -> Self {
Self::new()
}
}
struct BluetoothContext<T: AsyncBluetoothListener + DomObject> {
promise: Option<TrustedPromise>,
receiver: Trusted<T>,
}
pub trait AsyncBluetoothListener {
fn handle_response(&self, result: BluetoothResponse, promise: &Rc<Promise>, can_gc: CanGc);
}
impl<T> BluetoothContext<T>
where
T: AsyncBluetoothListener + DomObject,
{
#[allow(crown::unrooted_must_root)]
fn response(&mut self, response: BluetoothResponseResult, can_gc: CanGc) {
let promise = self.promise.take().expect("bt promise is missing").root();
match response {
Ok(response) => self
.receiver
.root()
.handle_response(response, &promise, can_gc),
Err(error) => promise.reject_error(Error::from(error)),
}
}
}
#[dom_struct]
pub struct Bluetooth {
eventtarget: EventTarget,
device_instance_map: DomRefCell<HashMap<String, Dom<BluetoothDevice>>>,
}
impl Bluetooth {
pub fn new_inherited() -> Bluetooth {
Bluetooth {
eventtarget: EventTarget::new_inherited(),
device_instance_map: DomRefCell::new(HashMap::new()),
}
}
pub fn new(global: &GlobalScope) -> DomRoot<Bluetooth> {
reflect_dom_object(Box::new(Bluetooth::new_inherited()), global)
}
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
pub fn get_device_map(&self) -> &DomRefCell<HashMap<String, Dom<BluetoothDevice>>> {
&self.device_instance_map
}
fn request_bluetooth_devices(
&self,
p: &Rc<Promise>,
filters: &Option<Vec<BluetoothLEScanFilterInit>>,
optional_services: &[BluetoothServiceUUID],
sender: IpcSender<BluetoothResponseResult>,
) {
let mut uuid_filters = vec![];
if let Some(filters) = filters {
if filters.is_empty() {
p.reject_error(Type(FILTER_EMPTY_ERROR.to_owned()));
return;
}
for filter in filters {
match canonicalize_filter(filter) {
Ok(f) => uuid_filters.push(f),
Err(e) => {
p.reject_error(e);
return;
},
}
}
}
let mut optional_services_uuids = vec![];
for opt_service in optional_services {
let uuid = match BluetoothUUID::service(opt_service.clone()) {
Ok(u) => u.to_string(),
Err(e) => {
p.reject_error(e);
return;
},
};
if !uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
optional_services_uuids.push(uuid);
}
}
let option = RequestDeviceoptions::new(
BluetoothScanfilterSequence::new(uuid_filters),
ServiceUUIDSequence::new(optional_services_uuids),
);
if let PermissionState::Denied =
get_descriptor_permission_state(PermissionName::Bluetooth, None)
{
return p.reject_error(Error::NotFound);
}
self.get_bluetooth_thread()
.send(BluetoothRequest::RequestDevice(option, sender))
.unwrap();
}
}
pub fn response_async<T: AsyncBluetoothListener + DomObject + 'static>(
promise: &Rc<Promise>,
receiver: &T,
) -> IpcSender<BluetoothResponseResult> {
let (action_sender, action_receiver) = ipc::channel().unwrap();
let task_source = receiver.global().networking_task_source();
let context = Arc::new(Mutex::new(BluetoothContext {
promise: Some(TrustedPromise::new(promise.clone())),
receiver: Trusted::new(receiver),
}));
ROUTER.add_typed_route(
action_receiver,
Box::new(move |message| {
struct ListenerTask<T: AsyncBluetoothListener + DomObject> {
context: Arc<Mutex<BluetoothContext<T>>>,
action: BluetoothResponseResult,
}
impl<T> TaskOnce for ListenerTask<T>
where
T: AsyncBluetoothListener + DomObject,
{
fn run_once(self) {
let mut context = self.context.lock().unwrap();
context.response(self.action, CanGc::note());
}
}
let task = ListenerTask {
context: context.clone(),
action: message.unwrap(),
};
let result = task_source.queue_unconditionally(task);
if let Err(err) = result {
warn!("failed to deliver network data: {:?}", err);
}
}),
);
action_sender
}
#[allow(clippy::too_many_arguments)]
pub fn get_gatt_children<T, F>(
attribute: &T,
single: bool,
uuid_canonicalizer: F,
uuid: Option<StringOrUnsignedLong>,
instance_id: String,
connected: bool,
child_type: GATTType,
can_gc: CanGc,
) -> Rc<Promise>
where
T: AsyncBluetoothListener + DomObject + 'static,
F: FnOnce(StringOrUnsignedLong) -> Fallible<UUID>,
{
let in_realm_proof = AlreadyInRealm::assert();
let p = Promise::new_in_current_realm(InRealm::Already(&in_realm_proof), can_gc);
let result_uuid = if let Some(u) = uuid {
let canonicalized = match uuid_canonicalizer(u) {
Ok(canonicalized_uuid) => canonicalized_uuid.to_string(),
Err(e) => {
p.reject_error(e);
return p;
},
};
if uuid_is_blocklisted(canonicalized.as_ref(), Blocklist::All) {
p.reject_error(Security);
return p;
}
Some(canonicalized)
} else {
None
};
if !connected {
p.reject_error(Network);
return p;
}
let sender = response_async(&p, attribute);
attribute
.global()
.as_window()
.bluetooth_thread()
.send(BluetoothRequest::GetGATTChildren(
instance_id,
result_uuid,
single,
child_type,
sender,
))
.unwrap();
p
}
fn canonicalize_filter(filter: &BluetoothLEScanFilterInit) -> Fallible<BluetoothScanfilter> {
if filter.services.is_none() &&
filter.name.is_none() &&
filter.namePrefix.is_none() &&
filter.manufacturerData.is_none() &&
filter.serviceData.is_none()
{
return Err(Type(FILTER_ERROR.to_owned()));
}
let services_vec = match filter.services {
Some(ref services) => {
if services.is_empty() {
return Err(Type(SERVICE_ERROR.to_owned()));
}
let mut services_vec = vec![];
for service in services {
let uuid = BluetoothUUID::service(service.clone())?.to_string();
if uuid_is_blocklisted(uuid.as_ref(), Blocklist::All) {
return Err(Security);
}
services_vec.push(uuid);
}
services_vec
},
None => vec![],
};
let name = match filter.name {
Some(ref name) => {
if name.len() > MAX_DEVICE_NAME_LENGTH {
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
}
Some(name.to_string())
},
None => None,
};
let name_prefix = match filter.namePrefix {
Some(ref name_prefix) => {
if name_prefix.is_empty() {
return Err(Type(NAME_PREFIX_ERROR.to_owned()));
}
if name_prefix.len() > MAX_DEVICE_NAME_LENGTH {
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
}
name_prefix.to_string()
},
None => String::new(),
};
let manufacturer_data = match filter.manufacturerData {
Some(ref manufacturer_data_map) => {
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() {
let manufacturer_id = match u16::from_str(key.as_ref()) {
Ok(id) => id,
Err(err) => {
return Err(Type(format!("{} {} {}", KEY_CONVERSION_ERROR, key, err)));
},
};
map.insert(
manufacturer_id,
canonicalize_bluetooth_data_filter_init(bdfi)?,
);
}
Some(map)
},
None => None,
};
let service_data = match filter.serviceData {
Some(ref service_data_map) => {
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()) {
Ok(number) => StringOrUnsignedLong::UnsignedLong(number),
_ => StringOrUnsignedLong::String(key.clone()),
};
let service = BluetoothUUID::service(service_name)?.to_string();
if uuid_is_blocklisted(service.as_ref(), Blocklist::All) {
return Err(Security);
}
map.insert(service, canonicalize_bluetooth_data_filter_init(bdfi)?);
}
Some(map)
},
None => None,
};
Ok(BluetoothScanfilter::new(
name,
name_prefix,
services_vec,
manufacturer_data,
service_data,
))
}
fn canonicalize_bluetooth_data_filter_init(
bdfi: &BluetoothDataFilterInit,
) -> Fallible<(Vec<u8>, Vec<u8>)> {
let data_prefix = match bdfi.dataPrefix {
Some(ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref avb)) => avb.to_vec(),
Some(ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref ab)) => ab.to_vec(),
None => vec![],
};
let mask = match bdfi.mask {
Some(ArrayBufferViewOrArrayBuffer::ArrayBufferView(ref avb)) => avb.to_vec(),
Some(ArrayBufferViewOrArrayBuffer::ArrayBuffer(ref ab)) => ab.to_vec(),
None => vec![0xFF; data_prefix.len()],
};
if mask.len() != data_prefix.len() {
return Err(Type(MASK_LENGTH_ERROR.to_owned()));
}
Ok((data_prefix, mask))
}
impl From<BluetoothError> for Error {
fn from(error: BluetoothError) -> Self {
match error {
BluetoothError::Type(message) => Error::Type(message),
BluetoothError::Network => Error::Network,
BluetoothError::NotFound => Error::NotFound,
BluetoothError::NotSupported => Error::NotSupported,
BluetoothError::Security => Error::Security,
BluetoothError::InvalidState => Error::InvalidState,
}
}
}
impl BluetoothMethods for Bluetooth {
fn RequestDevice(
&self,
option: &RequestDeviceOptions,
comp: InRealm,
can_gc: CanGc,
) -> Rc<Promise> {
let p = Promise::new_in_current_realm(comp, can_gc);
if (option.filters.is_some() && option.acceptAllDevices) ||
(option.filters.is_none() && !option.acceptAllDevices)
{
p.reject_error(Error::Type(OPTIONS_ERROR.to_owned()));
return p;
}
let sender = response_async(&p, self);
self.request_bluetooth_devices(&p, &option.filters, &option.optionalServices, sender);
p
}
fn GetAvailability(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
let p = Promise::new_in_current_realm(comp, can_gc);
let sender = response_async(&p, self);
self.get_bluetooth_thread()
.send(BluetoothRequest::GetAvailability(sender))
.unwrap();
p
}
event_handler!(
availabilitychanged,
GetOnavailabilitychanged,
SetOnavailabilitychanged
);
}
impl AsyncBluetoothListener for Bluetooth {
fn handle_response(&self, response: BluetoothResponse, promise: &Rc<Promise>, _can_gc: CanGc) {
match response {
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()) {
return promise.resolve_native(&**existing_device);
}
let bt_device = BluetoothDevice::new(
&self.global(),
DOMString::from(device.id.clone()),
device.name.map(DOMString::from),
self,
);
device_instance_map.insert(device.id.clone(), Dom::from_ref(&bt_device));
self.global()
.as_window()
.bluetooth_extra_permission_data()
.add_new_allowed_device(AllowedBluetoothDevice {
deviceId: DOMString::from(device.id),
mayUseGATT: true,
});
promise.resolve_native(&bt_device);
},
BluetoothResponse::GetAvailability(is_available) => {
promise.resolve_native(&is_available);
},
_ => promise.reject_error(Error::Type("Something went wrong...".to_owned())),
}
}
}
impl PermissionAlgorithm for Bluetooth {
type Descriptor = BluetoothPermissionDescriptor;
type Status = BluetoothPermissionResult;
fn create_descriptor(
cx: JSContext,
permission_descriptor_obj: *mut JSObject,
) -> Result<BluetoothPermissionDescriptor, Error> {
rooted!(in(*cx) let mut property = UndefinedValue());
property
.handle_mut()
.set(ObjectValue(permission_descriptor_obj));
match BluetoothPermissionDescriptor::new(cx, property.handle()) {
Ok(ConversionResult::Success(descriptor)) => Ok(descriptor),
Ok(ConversionResult::Failure(error)) => Err(Error::Type(error.into_owned())),
Err(_) => Err(Error::Type(String::from(BT_DESC_CONVERSION_ERROR))),
}
}
fn permission_query(
_cx: JSContext,
promise: &Rc<Promise>,
descriptor: &BluetoothPermissionDescriptor,
status: &BluetoothPermissionResult,
) {
status.set_state(get_descriptor_permission_state(status.get_query(), None));
if let PermissionState::Denied = status.get_state() {
status.set_devices(Vec::new());
return promise.resolve_native(status);
}
rooted_vec!(let mut matching_devices);
let global = status.global();
let allowed_devices = global
.as_window()
.bluetooth_extra_permission_data()
.get_allowed_devices();
let bluetooth = status.get_bluetooth();
let device_map = bluetooth.get_device_map().borrow();
for allowed_device in allowed_devices.iter() {
if let Some(ref id) = descriptor.deviceId {
if &allowed_device.deviceId != id {
continue;
}
}
let device_id = String::from(allowed_device.deviceId.as_ref());
if let Some(ref filters) = descriptor.filters {
let mut scan_filters: Vec<BluetoothScanfilter> = Vec::new();
for filter in filters {
match canonicalize_filter(filter) {
Ok(f) => scan_filters.push(f),
Err(error) => return promise.reject_error(error),
}
}
let (sender, receiver) =
ProfiledIpc::channel(global.time_profiler_chan().clone()).unwrap();
status
.get_bluetooth_thread()
.send(BluetoothRequest::MatchesFilter(
device_id.clone(),
BluetoothScanfilterSequence::new(scan_filters),
sender,
))
.unwrap();
match receiver.recv().unwrap() {
Ok(true) => (),
Ok(false) => continue,
Err(error) => return promise.reject_error(Error::from(error)),
};
}
if let Some(device) = device_map.get(&device_id) {
matching_devices.push(Dom::from_ref(&**device));
}
}
status.set_devices(matching_devices.drain(..).collect());
promise.resolve_native(status);
}
fn permission_request(
_cx: JSContext,
promise: &Rc<Promise>,
descriptor: &BluetoothPermissionDescriptor,
status: &BluetoothPermissionResult,
) {
if descriptor.filters.is_some() == descriptor.acceptAllDevices {
return promise.reject_error(Error::Type(OPTIONS_ERROR.to_owned()));
}
let sender = response_async(promise, status);
let bluetooth = status.get_bluetooth();
bluetooth.request_bluetooth_devices(
promise,
&descriptor.filters,
&descriptor.optionalServices,
sender,
);
}
#[allow(crown::unrooted_must_root)]
fn permission_revoke(
_descriptor: &BluetoothPermissionDescriptor,
status: &BluetoothPermissionResult,
can_gc: CanGc,
) {
let global = status.global();
let allowed_devices = global
.as_window()
.bluetooth_extra_permission_data()
.get_allowed_devices();
let bluetooth = status.get_bluetooth();
let device_map = bluetooth.get_device_map().borrow();
for (id, device) in device_map.iter() {
let id = DOMString::from(id.clone());
if allowed_devices.iter().any(|d| d.deviceId == id) &&
!device.is_represented_device_null()
{
continue;
}
let _ = device.get_gatt().Disconnect(can_gc);
}
}
}