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