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