Skip to main content

script/dom/bluetooth/
bluetoothdevice.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::collections::HashMap;
7use std::rc::Rc;
8
9use dom_struct::dom_struct;
10use js::realm::CurrentRealm;
11use profile_traits::generic_channel;
12use script_bindings::cell::DomRefCell;
13use script_bindings::reflector::reflect_dom_object_with_cx;
14use servo_base::generic_channel::GenericSender;
15use servo_bluetooth_traits::{
16    BluetoothCharacteristicMsg, BluetoothDescriptorMsg, BluetoothRequest, BluetoothResponse,
17    BluetoothServiceMsg,
18};
19
20use crate::conversions::Convert;
21use crate::dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
22use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
23use crate::dom::bindings::error::{Error, ErrorResult};
24use crate::dom::bindings::inheritance::Castable;
25use crate::dom::bindings::reflector::DomGlobal;
26use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
27use crate::dom::bindings::str::DOMString;
28use crate::dom::bluetooth::{AsyncBluetoothListener, Bluetooth, response_async};
29use crate::dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
30use crate::dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
31use crate::dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
32use crate::dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
33use crate::dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
34use crate::dom::eventtarget::EventTarget;
35use crate::dom::globalscope::GlobalScope;
36use crate::dom::promise::Promise;
37use crate::script_runtime::CanGc;
38
39#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
40#[derive(JSTraceable, MallocSizeOf)]
41struct AttributeInstanceMap {
42    service_map: DomRefCell<HashMap<String, Dom<BluetoothRemoteGATTService>>>,
43    characteristic_map: DomRefCell<HashMap<String, Dom<BluetoothRemoteGATTCharacteristic>>>,
44    descriptor_map: DomRefCell<HashMap<String, Dom<BluetoothRemoteGATTDescriptor>>>,
45}
46
47// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothdevice
48#[dom_struct]
49pub(crate) struct BluetoothDevice {
50    eventtarget: EventTarget,
51    id: DOMString,
52    name: Option<DOMString>,
53    gatt: MutNullableDom<BluetoothRemoteGATTServer>,
54    context: Dom<Bluetooth>,
55    attribute_instance_map: AttributeInstanceMap,
56    watching_advertisements: Cell<bool>,
57}
58
59impl BluetoothDevice {
60    pub(crate) fn new_inherited(
61        id: DOMString,
62        name: Option<DOMString>,
63        context: &Bluetooth,
64    ) -> BluetoothDevice {
65        BluetoothDevice {
66            eventtarget: EventTarget::new_inherited(),
67            id,
68            name,
69            gatt: Default::default(),
70            context: Dom::from_ref(context),
71            attribute_instance_map: AttributeInstanceMap {
72                service_map: DomRefCell::new(HashMap::new()),
73                characteristic_map: DomRefCell::new(HashMap::new()),
74                descriptor_map: DomRefCell::new(HashMap::new()),
75            },
76            watching_advertisements: Cell::new(false),
77        }
78    }
79
80    pub(crate) fn new(
81        cx: &mut js::context::JSContext,
82        global: &GlobalScope,
83        id: DOMString,
84        name: Option<DOMString>,
85        context: &Bluetooth,
86    ) -> DomRoot<BluetoothDevice> {
87        reflect_dom_object_with_cx(
88            Box::new(BluetoothDevice::new_inherited(id, name, context)),
89            global,
90            cx,
91        )
92    }
93
94    pub(crate) fn get_gatt(
95        &self,
96        cx: &mut js::context::JSContext,
97    ) -> DomRoot<BluetoothRemoteGATTServer> {
98        self.gatt
99            .or_init(|| BluetoothRemoteGATTServer::new(cx, &self.global(), self))
100    }
101
102    fn get_context(&self) -> DomRoot<Bluetooth> {
103        DomRoot::from_ref(&self.context)
104    }
105
106    pub(crate) fn get_or_create_service(
107        &self,
108        cx: &mut js::context::JSContext,
109        service: &BluetoothServiceMsg,
110        server: &BluetoothRemoteGATTServer,
111    ) -> DomRoot<BluetoothRemoteGATTService> {
112        let service_map_ref = &self.attribute_instance_map.service_map;
113        let mut service_map = service_map_ref.borrow_mut();
114        if let Some(existing_service) = service_map.get(&service.instance_id) {
115            return DomRoot::from_ref(existing_service);
116        }
117        let bt_service = BluetoothRemoteGATTService::new(
118            cx,
119            &server.global(),
120            &server.Device(),
121            DOMString::from(service.uuid.clone()),
122            service.is_primary,
123            service.instance_id.clone(),
124        );
125        service_map.insert(service.instance_id.clone(), Dom::from_ref(&bt_service));
126        bt_service
127    }
128
129    pub(crate) fn get_or_create_characteristic(
130        &self,
131        cx: &mut js::context::JSContext,
132        characteristic: &BluetoothCharacteristicMsg,
133        service: &BluetoothRemoteGATTService,
134    ) -> DomRoot<BluetoothRemoteGATTCharacteristic> {
135        let characteristic_map_ref = &self.attribute_instance_map.characteristic_map;
136        let mut characteristic_map = characteristic_map_ref.borrow_mut();
137        if let Some(existing_characteristic) = characteristic_map.get(&characteristic.instance_id) {
138            return DomRoot::from_ref(existing_characteristic);
139        }
140        let properties = BluetoothCharacteristicProperties::new(
141            cx,
142            &service.global(),
143            characteristic.broadcast,
144            characteristic.read,
145            characteristic.write_without_response,
146            characteristic.write,
147            characteristic.notify,
148            characteristic.indicate,
149            characteristic.authenticated_signed_writes,
150            characteristic.reliable_write,
151            characteristic.writable_auxiliaries,
152        );
153        let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(
154            cx,
155            &service.global(),
156            service,
157            DOMString::from(characteristic.uuid.clone()),
158            &properties,
159            characteristic.instance_id.clone(),
160        );
161        characteristic_map.insert(
162            characteristic.instance_id.clone(),
163            Dom::from_ref(&bt_characteristic),
164        );
165        bt_characteristic
166    }
167
168    pub(crate) fn is_represented_device_null(&self) -> bool {
169        let (sender, receiver) =
170            generic_channel::channel(self.global().time_profiler_chan().clone()).unwrap();
171        self.get_bluetooth_thread()
172            .send(BluetoothRequest::IsRepresentedDeviceNull(
173                self.Id().to_string(),
174                sender,
175            ))
176            .unwrap();
177        receiver.recv().unwrap()
178    }
179
180    pub(crate) fn get_or_create_descriptor(
181        &self,
182        cx: &mut js::context::JSContext,
183        descriptor: &BluetoothDescriptorMsg,
184        characteristic: &BluetoothRemoteGATTCharacteristic,
185    ) -> DomRoot<BluetoothRemoteGATTDescriptor> {
186        let descriptor_map_ref = &self.attribute_instance_map.descriptor_map;
187        let mut descriptor_map = descriptor_map_ref.borrow_mut();
188        if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) {
189            return DomRoot::from_ref(existing_descriptor);
190        }
191        let bt_descriptor = BluetoothRemoteGATTDescriptor::new(
192            cx,
193            &characteristic.global(),
194            characteristic,
195            DOMString::from(descriptor.uuid.clone()),
196            descriptor.instance_id.clone(),
197        );
198        descriptor_map.insert(
199            descriptor.instance_id.clone(),
200            Dom::from_ref(&bt_descriptor),
201        );
202        bt_descriptor
203    }
204
205    fn get_bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
206        self.global().as_window().bluetooth_thread()
207    }
208
209    // https://webbluetoothcg.github.io/web-bluetooth/#clean-up-the-disconnected-device
210    pub(crate) fn clean_up_disconnected_device(&self, cx: &mut js::context::JSContext) {
211        // Step 1.
212        self.get_gatt(cx).set_connected(false);
213
214        // TODO: Step 2: Implement activeAlgorithms internal slot for BluetoothRemoteGATTServer.
215
216        // Step 3: We don't need `context`, we get the attributeInstanceMap from the device.
217        // https://github.com/WebBluetoothCG/web-bluetooth/issues/330
218
219        // Step 4.
220        let mut service_map = self.attribute_instance_map.service_map.borrow_mut();
221        let service_ids = service_map.drain().map(|(id, _)| id).collect();
222
223        let mut characteristic_map = self.attribute_instance_map.characteristic_map.borrow_mut();
224        let characteristic_ids = characteristic_map.drain().map(|(id, _)| id).collect();
225
226        let mut descriptor_map = self.attribute_instance_map.descriptor_map.borrow_mut();
227        let descriptor_ids = descriptor_map.drain().map(|(id, _)| id).collect();
228
229        // Step 5, 6.4, 7.
230        // TODO: Step 6: Implement `active notification context set` for BluetoothRemoteGATTCharacteristic.
231        let _ = self
232            .get_bluetooth_thread()
233            .send(BluetoothRequest::SetRepresentedToNull(
234                service_ids,
235                characteristic_ids,
236                descriptor_ids,
237            ));
238
239        // Step 8.
240        self.upcast::<EventTarget>()
241            .fire_bubbling_event(cx, atom!("gattserverdisconnected"));
242    }
243
244    // https://webbluetoothcg.github.io/web-bluetooth/#garbage-collect-the-connection
245    pub(crate) fn garbage_collect_the_connection(
246        &self,
247        cx: &mut js::context::JSContext,
248    ) -> ErrorResult {
249        // Step 1: TODO: Check if other systems using this device.
250
251        // Step 2.
252        let context = self.get_context();
253        for (id, device) in context.get_device_map().borrow().iter() {
254            // Step 2.1 - 2.2.
255            if id == &self.Id().to_string() && device.get_gatt(cx).Connected() {
256                return Ok(());
257            }
258        }
259
260        // Step 3.
261        let (sender, receiver) =
262            generic_channel::channel(self.global().time_profiler_chan().clone()).unwrap();
263        self.get_bluetooth_thread()
264            .send(BluetoothRequest::GATTServerDisconnect(
265                String::from(self.Id()),
266                sender,
267            ))
268            .unwrap();
269        receiver.recv().unwrap().map_err(Convert::convert)
270    }
271}
272
273impl BluetoothDeviceMethods<crate::DomTypeHolder> for BluetoothDevice {
274    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-id>
275    fn Id(&self) -> DOMString {
276        self.id.clone()
277    }
278
279    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-name>
280    fn GetName(&self) -> Option<DOMString> {
281        self.name.clone()
282    }
283
284    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-gatt>
285    fn GetGatt(
286        &self,
287        cx: &mut js::context::JSContext,
288    ) -> Option<DomRoot<BluetoothRemoteGATTServer>> {
289        // Step 1.
290        if self
291            .global()
292            .as_window()
293            .bluetooth_extra_permission_data()
294            .allowed_devices_contains_id(self.id.clone()) &&
295            !self.is_represented_device_null()
296        {
297            return Some(self.get_gatt(cx));
298        }
299        // Step 2.
300        None
301    }
302
303    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-watchadvertisements>
304    fn WatchAdvertisements(&self, cx: &mut CurrentRealm) -> Rc<Promise> {
305        let p = Promise::new_in_realm(cx);
306        let sender = response_async(&p, self);
307        // TODO: Step 1.
308        // Note: Steps 2 - 3 are implemented in components/bluetooth/lib.rs in watch_advertisements function
309        // and in handle_response function.
310        self.get_bluetooth_thread()
311            .send(BluetoothRequest::WatchAdvertisements(
312                String::from(self.Id()),
313                sender,
314            ))
315            .unwrap();
316        p
317    }
318
319    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-unwatchadvertisements>
320    fn UnwatchAdvertisements(&self) {
321        // Step 1.
322        self.watching_advertisements.set(false)
323        // TODO: Step 2.
324    }
325
326    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-watchingadvertisements>
327    fn WatchingAdvertisements(&self) -> bool {
328        self.watching_advertisements.get()
329    }
330
331    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdeviceeventhandlers-ongattserverdisconnected
332    event_handler!(
333        gattserverdisconnected,
334        GetOngattserverdisconnected,
335        SetOngattserverdisconnected
336    );
337}
338
339impl AsyncBluetoothListener for BluetoothDevice {
340    fn handle_response(
341        &self,
342        cx: &mut js::context::JSContext,
343        response: BluetoothResponse,
344        promise: &Rc<Promise>,
345    ) {
346        match response {
347            // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-unwatchadvertisements
348            BluetoothResponse::WatchAdvertisements(_result) => {
349                // Step 3.1.
350                self.watching_advertisements.set(true);
351                // Step 3.2.
352                promise.resolve_native(&(), CanGc::from_cx(cx));
353            },
354            _ => promise.reject_error(
355                Error::Type(c"Something went wrong...".to_owned()),
356                CanGc::from_cx(cx),
357            ),
358        }
359    }
360}