script/dom/bluetooth/
bluetoothpermissionresult.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::rc::Rc;
6
7use bluetooth_traits::{BluetoothRequest, BluetoothResponse};
8use dom_struct::dom_struct;
9use ipc_channel::ipc::IpcSender;
10
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::BluetoothPermissionResultBinding::BluetoothPermissionResultMethods;
13use crate::dom::bindings::codegen::Bindings::NavigatorBinding::Navigator_Binding::NavigatorMethods;
14use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionStatus_Binding::PermissionStatusMethods;
15use crate::dom::bindings::codegen::Bindings::PermissionStatusBinding::{
16    PermissionName, PermissionState,
17};
18use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
19use crate::dom::bindings::error::Error;
20use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
21use crate::dom::bindings::root::{Dom, DomRoot};
22use crate::dom::bindings::str::DOMString;
23use crate::dom::bluetooth::{AllowedBluetoothDevice, AsyncBluetoothListener, Bluetooth};
24use crate::dom::bluetoothdevice::BluetoothDevice;
25use crate::dom::globalscope::GlobalScope;
26use crate::dom::permissionstatus::PermissionStatus;
27use crate::dom::promise::Promise;
28use crate::script_runtime::CanGc;
29
30// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothpermissionresult
31#[dom_struct]
32pub(crate) struct BluetoothPermissionResult {
33    status: PermissionStatus,
34    devices: DomRefCell<Vec<Dom<BluetoothDevice>>>,
35}
36
37impl BluetoothPermissionResult {
38    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
39    fn new_inherited(status: &PermissionStatus) -> BluetoothPermissionResult {
40        let result = BluetoothPermissionResult {
41            status: PermissionStatus::new_inherited(status.get_query()),
42            devices: DomRefCell::new(Vec::new()),
43        };
44        result.status.set_state(status.State());
45        result
46    }
47
48    pub(crate) fn new(
49        global: &GlobalScope,
50        status: &PermissionStatus,
51        can_gc: CanGc,
52    ) -> DomRoot<BluetoothPermissionResult> {
53        reflect_dom_object(
54            Box::new(BluetoothPermissionResult::new_inherited(status)),
55            global,
56            can_gc,
57        )
58    }
59
60    pub(crate) fn get_bluetooth(&self) -> DomRoot<Bluetooth> {
61        self.global().as_window().Navigator().Bluetooth()
62    }
63
64    pub(crate) fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
65        self.global().as_window().bluetooth_thread()
66    }
67
68    pub(crate) fn get_query(&self) -> PermissionName {
69        self.status.get_query()
70    }
71
72    pub(crate) fn set_state(&self, state: PermissionState) {
73        self.status.set_state(state)
74    }
75
76    pub(crate) fn get_state(&self) -> PermissionState {
77        self.status.State()
78    }
79
80    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
81    pub(crate) fn set_devices(&self, devices: Vec<Dom<BluetoothDevice>>) {
82        *self.devices.borrow_mut() = devices;
83    }
84}
85
86impl BluetoothPermissionResultMethods<crate::DomTypeHolder> for BluetoothPermissionResult {
87    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothpermissionresult-devices
88    fn Devices(&self) -> Vec<DomRoot<BluetoothDevice>> {
89        let device_vec: Vec<DomRoot<BluetoothDevice>> = self
90            .devices
91            .borrow()
92            .iter()
93            .map(|d| DomRoot::from_ref(&**d))
94            .collect();
95        device_vec
96    }
97}
98
99impl AsyncBluetoothListener for BluetoothPermissionResult {
100    fn handle_response(&self, response: BluetoothResponse, promise: &Rc<Promise>, can_gc: CanGc) {
101        match response {
102            // https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
103            // Step 3, 11, 13 - 14.
104            BluetoothResponse::RequestDevice(device) => {
105                self.set_state(PermissionState::Granted);
106                let bluetooth = self.get_bluetooth();
107                let mut device_instance_map = bluetooth.get_device_map().borrow_mut();
108                if let Some(existing_device) = device_instance_map.get(&device.id) {
109                    // https://webbluetoothcg.github.io/web-bluetooth/#request-the-bluetooth-permission
110                    // Step 3.
111                    self.set_devices(vec![Dom::from_ref(existing_device)]);
112
113                    // https://w3c.github.io/permissions/#dom-permissions-request
114                    // Step 8.
115                    return promise.resolve_native(self, can_gc);
116                }
117                let bt_device = BluetoothDevice::new(
118                    &self.global(),
119                    DOMString::from(device.id.clone()),
120                    device.name.map(DOMString::from),
121                    &bluetooth,
122                    can_gc,
123                );
124                device_instance_map.insert(device.id.clone(), Dom::from_ref(&bt_device));
125                self.global()
126                    .as_window()
127                    .bluetooth_extra_permission_data()
128                    .add_new_allowed_device(AllowedBluetoothDevice {
129                        deviceId: DOMString::from(device.id),
130                        mayUseGATT: true,
131                    });
132                // https://webbluetoothcg.github.io/web-bluetooth/#request-the-bluetooth-permission
133                // Step 3.
134                self.set_devices(vec![Dom::from_ref(&bt_device)]);
135
136                // https://w3c.github.io/permissions/#dom-permissions-request
137                // Step 8.
138                promise.resolve_native(self, can_gc);
139            },
140            _ => promise.reject_error(Error::Type("Something went wrong...".to_owned()), can_gc),
141        }
142    }
143}