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;
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;
22use crate::script_runtime::CanGc;
23
24// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothadvertisingevent
25#[dom_struct]
26pub(crate) struct BluetoothAdvertisingEvent {
27    event: Event,
28    device: Dom<BluetoothDevice>,
29    name: Option<DOMString>,
30    appearance: Option<u16>,
31    tx_power: Option<i8>,
32    rssi: Option<i8>,
33}
34
35#[allow(non_snake_case)]
36impl BluetoothAdvertisingEvent {
37    pub(crate) fn new_inherited(
38        device: &BluetoothDevice,
39        name: Option<DOMString>,
40        appearance: Option<u16>,
41        tx_power: Option<i8>,
42        rssi: Option<i8>,
43    ) -> BluetoothAdvertisingEvent {
44        BluetoothAdvertisingEvent {
45            event: Event::new_inherited(),
46            device: Dom::from_ref(device),
47            name,
48            appearance,
49            tx_power,
50            rssi,
51        }
52    }
53
54    #[allow(clippy::too_many_arguments)]
55    fn new(
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        can_gc: CanGc,
67    ) -> DomRoot<BluetoothAdvertisingEvent> {
68        let ev = reflect_dom_object_with_proto(
69            Box::new(BluetoothAdvertisingEvent::new_inherited(
70                device, name, appearance, txPower, rssi,
71            )),
72            global,
73            proto,
74            can_gc,
75        );
76        {
77            let event = ev.upcast::<Event>();
78            event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
79        }
80        ev
81    }
82}
83
84impl BluetoothAdvertisingEventMethods<crate::DomTypeHolder> for BluetoothAdvertisingEvent {
85    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-bluetoothadvertisingevent
86    #[allow(non_snake_case)]
87    fn Constructor(
88        window: &Window,
89        proto: Option<HandleObject>,
90        can_gc: CanGc,
91        type_: DOMString,
92        init: &BluetoothAdvertisingEventInit,
93    ) -> Fallible<DomRoot<BluetoothAdvertisingEvent>> {
94        let name = init.name.clone();
95        let appearance = init.appearance;
96        let txPower = init.txPower;
97        let rssi = init.rssi;
98        let bubbles = EventBubbles::from(init.parent.bubbles);
99        let cancelable = EventCancelable::from(init.parent.cancelable);
100        Ok(BluetoothAdvertisingEvent::new(
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            can_gc,
112        ))
113    }
114
115    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-device
116    fn Device(&self) -> DomRoot<BluetoothDevice> {
117        DomRoot::from_ref(&*self.device)
118    }
119
120    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-name
121    fn GetName(&self) -> Option<DOMString> {
122        self.name.clone()
123    }
124
125    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-appearance
126    fn GetAppearance(&self) -> Option<u16> {
127        self.appearance
128    }
129
130    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-txpower
131    fn GetTxPower(&self) -> Option<i8> {
132        self.tx_power
133    }
134
135    // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-rssi
136    fn GetRssi(&self) -> Option<i8> {
137        self.rssi
138    }
139
140    // https://dom.spec.whatwg.org/#dom-event-istrusted
141    fn IsTrusted(&self) -> bool {
142        self.event.IsTrusted()
143    }
144}