Skip to main content

script/dom/bluetooth/
bluetoothadvertisingevent.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::domstring::TracedDOMString;
9use script_bindings::reflector::reflect_dom_object_with_proto;
10use stylo_atoms::Atom;
11
12use crate::dom::bindings::codegen::Bindings::BluetoothAdvertisingEventBinding::{
13    BluetoothAdvertisingEventInit, BluetoothAdvertisingEventMethods,
14};
15use crate::dom::bindings::codegen::Bindings::EventBinding::Event_Binding::EventMethods;
16use crate::dom::bindings::error::Fallible;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::bluetoothdevice::BluetoothDevice;
21use crate::dom::event::{Event, EventBubbles, EventCancelable};
22use crate::dom::globalscope::GlobalScope;
23use crate::dom::window::Window;
24
25// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothadvertisingevent
26#[dom_struct]
27pub(crate) struct BluetoothAdvertisingEvent {
28    event: Event,
29    device: Dom<BluetoothDevice>,
30    name: Option<TracedDOMString>,
31    appearance: Option<u16>,
32    tx_power: Option<i8>,
33    rssi: Option<i8>,
34}
35
36#[expect(non_snake_case)]
37impl BluetoothAdvertisingEvent {
38    pub(crate) fn new_inherited(
39        device: &BluetoothDevice,
40        name: Option<DOMString>,
41        appearance: Option<u16>,
42        tx_power: Option<i8>,
43        rssi: Option<i8>,
44    ) -> BluetoothAdvertisingEvent {
45        BluetoothAdvertisingEvent {
46            event: Event::new_inherited(),
47            device: Dom::from_ref(device),
48            name: name.map(|domstring| domstring.traced()),
49            appearance,
50            tx_power,
51            rssi,
52        }
53    }
54
55    #[expect(clippy::too_many_arguments)]
56    fn new(
57        cx: &mut JSContext,
58        global: &GlobalScope,
59        proto: Option<HandleObject>,
60        type_: Atom,
61        bubbles: EventBubbles,
62        cancelable: EventCancelable,
63        device: &BluetoothDevice,
64        name: Option<DOMString>,
65        appearance: Option<u16>,
66        txPower: Option<i8>,
67        rssi: Option<i8>,
68    ) -> DomRoot<BluetoothAdvertisingEvent> {
69        let ev = reflect_dom_object_with_proto(
70            cx,
71            Box::new(BluetoothAdvertisingEvent::new_inherited(
72                device, name, appearance, txPower, rssi,
73            )),
74            global,
75            proto,
76        );
77        {
78            let event = ev.upcast::<Event>();
79            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
80        }
81        ev
82    }
83}
84
85impl BluetoothAdvertisingEventMethods<crate::DomTypeHolder> for BluetoothAdvertisingEvent {
86    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-bluetoothadvertisingevent
87    #[expect(non_snake_case)]
88    fn Constructor(
89        cx: &mut JSContext,
90        window: &Window,
91        proto: Option<HandleObject>,
92        type_: DOMString,
93        init: &BluetoothAdvertisingEventInit,
94    ) -> Fallible<DomRoot<BluetoothAdvertisingEvent>> {
95        let name = init.name.clone();
96        let appearance = init.appearance;
97        let txPower = init.txPower;
98        let rssi = init.rssi;
99        let bubbles = EventBubbles::from(init.parent.bubbles);
100        let cancelable = EventCancelable::from(init.parent.cancelable);
101        Ok(BluetoothAdvertisingEvent::new(
102            cx,
103            window.as_global_scope(),
104            proto,
105            Atom::from(type_),
106            bubbles,
107            cancelable,
108            &init.device,
109            name,
110            appearance,
111            txPower,
112            rssi,
113        ))
114    }
115
116    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-device>
117    fn Device(&self) -> DomRoot<BluetoothDevice> {
118        DomRoot::from_ref(&*self.device)
119    }
120
121    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-name>
122    fn GetName(&self) -> Option<DOMString> {
123        self.name.clone().map(|domstring| domstring.root())
124    }
125
126    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-appearance>
127    fn GetAppearance(&self) -> Option<u16> {
128        self.appearance
129    }
130
131    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-txpower>
132    fn GetTxPower(&self) -> Option<i8> {
133        self.tx_power
134    }
135
136    /// <https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-rssi>
137    fn GetRssi(&self) -> Option<i8> {
138        self.rssi
139    }
140
141    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
142    fn IsTrusted(&self) -> bool {
143        self.event.IsTrusted()
144    }
145}