script/dom/bluetooth/
bluetoothpermissionresult.rs1use std::rc::Rc;
6
7use base::generic_channel::GenericSender;
8use bluetooth_traits::{BluetoothRequest, BluetoothResponse};
9use dom_struct::dom_struct;
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#[dom_struct]
32pub(crate) struct BluetoothPermissionResult {
33 status: PermissionStatus,
34 devices: DomRefCell<Vec<Dom<BluetoothDevice>>>,
35}
36
37impl BluetoothPermissionResult {
38 #[cfg_attr(crown, expect(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 cx: &mut js::context::JSContext,
50 global: &GlobalScope,
51 status: &PermissionStatus,
52 ) -> DomRoot<BluetoothPermissionResult> {
53 reflect_dom_object(
54 Box::new(BluetoothPermissionResult::new_inherited(status)),
55 global,
56 CanGc::from_cx(cx),
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) -> GenericSender<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, expect(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 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(
101 &self,
102 cx: &mut js::context::JSContext,
103 response: BluetoothResponse,
104 promise: &Rc<Promise>,
105 ) {
106 match response {
107 BluetoothResponse::RequestDevice(device) => {
110 self.set_state(PermissionState::Granted);
111 let bluetooth = self.get_bluetooth();
112 let mut device_instance_map = bluetooth.get_device_map().borrow_mut();
113 if let Some(existing_device) = device_instance_map.get(&device.id) {
114 self.set_devices(vec![Dom::from_ref(existing_device)]);
117
118 return promise.resolve_native(self, CanGc::from_cx(cx));
121 }
122 let bt_device = BluetoothDevice::new(
123 cx,
124 &self.global(),
125 DOMString::from(device.id.clone()),
126 device.name.map(DOMString::from),
127 &bluetooth,
128 );
129 device_instance_map.insert(device.id.clone(), Dom::from_ref(&bt_device));
130 self.global()
131 .as_window()
132 .bluetooth_extra_permission_data()
133 .add_new_allowed_device(AllowedBluetoothDevice {
134 deviceId: DOMString::from(device.id),
135 mayUseGATT: true,
136 });
137 self.set_devices(vec![Dom::from_ref(&bt_device)]);
140
141 promise.resolve_native(self, CanGc::from_cx(cx));
144 },
145 _ => promise.reject_error(
146 Error::Type(c"Something went wrong...".to_owned()),
147 CanGc::from_cx(cx),
148 ),
149 }
150 }
151}