use std::rc::Rc;
use dom_struct::dom_struct;
use js::context::JSContext;
use js::realm::CurrentRealm;
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
use servo_base::generic_channel::GenericSender;
use servo_bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use servo_bluetooth_traits::{BluetoothRequest, BluetoothResponse};
use crate::dom::bindings::buffer_source::get_buffer_source_copy;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::BluetoothRemoteGATTCharacteristicMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use crate::dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer;
use crate::dom::bindings::error::Error::{self, InvalidModification, Network, Security};
use crate::dom::bindings::reflector::DomGlobal;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::{ByteString, DOMString};
use crate::dom::bluetooth::{AsyncBluetoothListener, response_async};
use crate::dom::bluetoothremotegattcharacteristic::{
BluetoothRemoteGATTCharacteristic, MAXIMUM_ATTRIBUTE_LENGTH,
};
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
#[dom_struct]
pub(crate) struct BluetoothRemoteGATTDescriptor {
reflector_: Reflector,
characteristic: Dom<BluetoothRemoteGATTCharacteristic>,
uuid: DOMString,
value: DomRefCell<Option<ByteString>>,
instance_id: String,
}
impl BluetoothRemoteGATTDescriptor {
pub(crate) fn new_inherited(
characteristic: &BluetoothRemoteGATTCharacteristic,
uuid: DOMString,
instance_id: String,
) -> BluetoothRemoteGATTDescriptor {
BluetoothRemoteGATTDescriptor {
reflector_: Reflector::new(),
characteristic: Dom::from_ref(characteristic),
uuid,
value: DomRefCell::new(None),
instance_id,
}
}
pub(crate) fn new(
cx: &mut JSContext,
global: &GlobalScope,
characteristic: &BluetoothRemoteGATTCharacteristic,
uuid: DOMString,
instance_id: String,
) -> DomRoot<BluetoothRemoteGATTDescriptor> {
reflect_dom_object_with_cx(
Box::new(BluetoothRemoteGATTDescriptor::new_inherited(
characteristic,
uuid,
instance_id,
)),
global,
cx,
)
}
fn get_bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
self.global().as_window().bluetooth_thread()
}
fn get_instance_id(&self) -> String {
self.instance_id.clone()
}
}
impl BluetoothRemoteGATTDescriptorMethods<crate::DomTypeHolder> for BluetoothRemoteGATTDescriptor {
fn Characteristic(&self) -> DomRoot<BluetoothRemoteGATTCharacteristic> {
DomRoot::from_ref(&self.characteristic)
}
fn Uuid(&self) -> DOMString {
self.uuid.clone()
}
fn GetValue(&self) -> Option<ByteString> {
self.value.borrow().clone()
}
fn ReadValue(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
let p = Promise::new_in_realm(cx);
if uuid_is_blocklisted(&self.uuid.str(), Blocklist::Reads) {
p.reject_error(cx, Security(None));
return p;
}
if !self
.Characteristic()
.Service()
.Device()
.get_gatt(cx)
.Connected()
{
p.reject_error(cx, Network(None));
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread()
.send(BluetoothRequest::ReadValue(self.get_instance_id(), sender))
.unwrap();
p
}
fn WriteValue(
&self,
cx: &mut CurrentRealm,
value: ArrayBufferViewOrArrayBuffer,
) -> Rc<Promise> {
let p = Promise::new_in_realm(cx);
if uuid_is_blocklisted(&self.uuid.str(), Blocklist::Writes) {
p.reject_error(cx, Security(None));
return p;
}
let vec = get_buffer_source_copy((&value).into());
if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH {
p.reject_error(cx, InvalidModification(None));
return p;
}
if !self
.Characteristic()
.Service()
.Device()
.get_gatt(cx)
.Connected()
{
p.reject_error(cx, Network(None));
return p;
}
let sender = response_async(&p, self);
self.get_bluetooth_thread()
.send(BluetoothRequest::WriteValue(
self.get_instance_id(),
vec,
sender,
))
.unwrap();
p
}
}
impl AsyncBluetoothListener for BluetoothRemoteGATTDescriptor {
fn handle_response(
&self,
cx: &mut JSContext,
response: BluetoothResponse,
promise: &Rc<Promise>,
) {
match response {
BluetoothResponse::ReadValue(result) => {
let value = ByteString::new(result);
*self.value.safe_borrow_mut(cx.no_gc()) = Some(value.clone());
promise.resolve_native(cx, &value);
},
BluetoothResponse::WriteValue(result) => {
*self.value.safe_borrow_mut(cx.no_gc()) = Some(ByteString::new(result));
promise.resolve_native(cx, &());
},
_ => promise.reject_error(cx, Error::Type(c"Something went wrong...".to_owned())),
}
}
}