Skip to content

Commit

Permalink
Replace AdvertisingData with AdvertisingEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
dati91 committed Dec 14, 2016
1 parent a239116 commit 748b78a
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 99 deletions.
13 changes: 10 additions & 3 deletions components/bluetooth/lib.rs
Expand Up @@ -278,6 +278,9 @@ impl BluetoothManager {
BluetoothRequest::EnableNotification(id, enable, sender) => {
self.enable_notification(id, enable, sender)
},
BluetoothRequest::WatchAdvertisements(id, sender) => {
self.watch_advertisements(id, sender)
},
BluetoothRequest::Test(data_set_name, sender) => {
self.test(data_set_name, sender)
}
Expand Down Expand Up @@ -613,9 +616,6 @@ impl BluetoothManager {
let message = BluetoothDeviceMsg {
id: device_id,
name: device.get_name().ok(),
appearance: device.get_appearance().ok(),
tx_power: device.get_tx_power().ok().map(|p| p as i8),
rssi: device.get_rssi().ok().map(|p| p as i8),
};
return drop(sender.send(Ok(BluetoothResponse::RequestDevice(message))));
}
Expand Down Expand Up @@ -1094,4 +1094,11 @@ impl BluetoothManager {
None => return drop(sender.send(Err(BluetoothError::InvalidState))),
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-watchadvertisements
fn watch_advertisements(&mut self, _device_id: String, sender: IpcSender<BluetoothResponseResult>) {
// Step 2.
// TODO: Implement this when supported in lower level
return drop(sender.send(Err(BluetoothError::NotSupported)));
}
}
6 changes: 2 additions & 4 deletions components/bluetooth_traits/lib.rs
Expand Up @@ -31,10 +31,6 @@ pub struct BluetoothDeviceMsg {
// Bluetooth Device properties
pub id: String,
pub name: Option<String>,
// Advertising Data properties
pub appearance: Option<u16>,
pub tx_power: Option<i8>,
pub rssi: Option<i8>,
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -93,6 +89,7 @@ pub enum BluetoothRequest {
ReadValue(String, IpcSender<BluetoothResponseResult>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothResponseResult>),
EnableNotification(String, bool, IpcSender<BluetoothResponseResult>),
WatchAdvertisements(String, IpcSender<BluetoothResponseResult>),
Test(String, IpcSender<BluetoothResult<()>>),
Exit,
}
Expand All @@ -112,6 +109,7 @@ pub enum BluetoothResponse {
ReadValue(Vec<u8>),
WriteValue(Vec<u8>),
EnableNotification(()),
WatchAdvertisements(()),
}

pub trait BluetoothResponseListener {
Expand Down
6 changes: 0 additions & 6 deletions components/script/dom/bluetooth.rs
Expand Up @@ -19,7 +19,6 @@ use dom::bindings::js::{MutJS, Root};
use dom::bindings::refcounted::{Trusted, TrustedPromise};
use dom::bindings::reflector::{DomObject, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
use dom::eventtarget::EventTarget;
Expand Down Expand Up @@ -400,14 +399,9 @@ impl AsyncBluetoothListener for Bluetooth {
if let Some(existing_device) = device_instance_map.get(&device.id.clone()) {
return promise.resolve_native(promise_cx, &existing_device.get());
}
let ad_data = BluetoothAdvertisingData::new(&self.global(),
device.appearance,
device.tx_power,
device.rssi);
let bt_device = BluetoothDevice::new(&self.global(),
DOMString::from(device.id.clone()),
device.name.map(DOMString::from),
&ad_data,
&self);
device_instance_map.insert(device.id, MutJS::new(&bt_device));
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
Expand Down
61 changes: 0 additions & 61 deletions components/script/dom/bluetoothadvertisingdata.rs

This file was deleted.

126 changes: 126 additions & 0 deletions components/script/dom/bluetoothadvertisingevent.rs
@@ -0,0 +1,126 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BluetoothAdvertisingEventBinding::{self, BluetoothAdvertisingEventInit};
use dom::bindings::codegen::Bindings::BluetoothAdvertisingEventBinding::BluetoothAdvertisingEventMethods;
use dom::bindings::codegen::Bindings::EventBinding::EventBinding::EventMethods;
use dom::bindings::error::Fallible;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::reflector::reflect_dom_object;
use dom::bindings::str::DOMString;
use dom::bluetoothdevice::BluetoothDevice;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::globalscope::GlobalScope;
use dom::window::Window;
use servo_atoms::Atom;

// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothadvertisingevent
#[dom_struct]
pub struct BluetoothAdvertisingEvent {
event: Event,
device: JS<BluetoothDevice>,
name: Option<DOMString>,
appearance: Option<u16>,
tx_power: Option<i8>,
rssi: Option<i8>,
}

impl BluetoothAdvertisingEvent {
pub fn new_inherited(device: &BluetoothDevice,
name: Option<DOMString>,
appearance: Option<u16>,
tx_power: Option<i8>,
rssi: Option<i8>)
-> BluetoothAdvertisingEvent {
BluetoothAdvertisingEvent {
event: Event::new_inherited(),
device: JS::from_ref(device),
name: name,
appearance: appearance,
tx_power: tx_power,
rssi: rssi,
}
}

pub fn new(global: &GlobalScope,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
device: &BluetoothDevice,
name: Option<DOMString>,
appearance: Option<u16>,
txPower: Option<i8>,
rssi: Option<i8>)
-> Root<BluetoothAdvertisingEvent> {
let ev = reflect_dom_object(box BluetoothAdvertisingEvent::new_inherited(device,
name,
appearance,
txPower,
rssi),
global,
BluetoothAdvertisingEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
}
ev
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-bluetoothadvertisingevent
pub fn Constructor(window: &Window,
type_: DOMString,
init: &BluetoothAdvertisingEventInit)
-> Fallible<Root<BluetoothAdvertisingEvent>> {
let global = window.upcast::<GlobalScope>();
let device = init.device.r();
let name = init.name.clone();
let appearance = init.appearance.clone();
let txPower = init.txPower.clone();
let rssi = init.rssi.clone();
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
Ok(BluetoothAdvertisingEvent::new(global,
Atom::from(type_),
bubbles,
cancelable,
device,
name,
appearance,
txPower,
rssi))
}
}

impl BluetoothAdvertisingEventMethods for BluetoothAdvertisingEvent {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-device
fn Device(&self) -> Root<BluetoothDevice> {
Root::from_ref(&*self.device)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-name
fn GetName(&self) -> Option<DOMString> {
self.name.clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-appearance
fn GetAppearance(&self) -> Option<u16> {
self.appearance
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-txpower
fn GetTxPower(&self) -> Option<i8> {
self.tx_power
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingevent-rssi
fn GetRssi(&self) -> Option<i8> {
self.rssi
}

// https://dom.spec.whatwg.org/#dom-event-istrusted
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}

0 comments on commit 748b78a

Please sign in to comment.