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