forked from mirrors/gecko-dev
<!-- Please describe your changes on the following line: --> This implements the [Permissions API](https://w3c.github.io/permissions/) spec. Also includes the WebBluetooth related implementation for this. There are some know issues: - [ ] If the descriptor name is invalid [this](https://gist.github.com/dati91/7a6a0a563d90f49ba5a351e48c5b626b#file-permissionstatusbindings-rs-L323) will throw an error, rather that return it and we could handle it. - [x] The [environment settings object](https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object) is not implemented in servo and the spec rely on it. - [x] There is a popup in the implementation which prevent us to add wpt test, we should figure out a way to make it work - [ ] The allowedDevice's allowed_services attribute is not used in our implementation, because we store these in the lower level, not in the dom side. - [ ] We think the bluetooth revoke function will need some more work, but the problem is the spec needs clarifications on that part. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: a537cf48b18d9bba3453b924a4453f5e19dea4ed --HG-- extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear extra : subtree_revision : 50bc944e75966879ab6aca2a6cc229212e733d64
124 lines
5.4 KiB
Rust
124 lines
5.4 KiB
Rust
/* 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 bluetooth_traits::{BluetoothRequest, BluetoothResponse};
|
|
use dom::bindings::cell::DOMRefCell;
|
|
use dom::bindings::codegen::Bindings::BluetoothPermissionResultBinding::{self, BluetoothPermissionResultMethods};
|
|
use dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorBinding::NavigatorMethods;
|
|
use dom::bindings::codegen::Bindings::PermissionStatusBinding::{PermissionName, PermissionState};
|
|
use dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionStatusBinding::PermissionStatusMethods;
|
|
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
|
|
use dom::bindings::error::Error;
|
|
use dom::bindings::js::{JS, Root};
|
|
use dom::bindings::reflector::{DomObject, reflect_dom_object};
|
|
use dom::bindings::str::DOMString;
|
|
use dom::bluetooth::{AsyncBluetoothListener, Bluetooth, AllowedBluetoothDevice};
|
|
use dom::bluetoothdevice::BluetoothDevice;
|
|
use dom::globalscope::GlobalScope;
|
|
use dom::permissionstatus::PermissionStatus;
|
|
use dom::promise::Promise;
|
|
use ipc_channel::ipc::IpcSender;
|
|
use js::jsapi::JSContext;
|
|
use std::rc::Rc;
|
|
|
|
// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothpermissionresult
|
|
#[dom_struct]
|
|
pub struct BluetoothPermissionResult {
|
|
status: PermissionStatus,
|
|
devices: DOMRefCell<Vec<JS<BluetoothDevice>>>,
|
|
}
|
|
|
|
impl BluetoothPermissionResult {
|
|
#[allow(unrooted_must_root)]
|
|
fn new_inherited(status: &PermissionStatus) -> BluetoothPermissionResult {
|
|
let result = BluetoothPermissionResult {
|
|
status: PermissionStatus::new_inherited(status.get_query()),
|
|
devices: DOMRefCell::new(Vec::new()),
|
|
};
|
|
result.status.set_state(status.State());
|
|
result
|
|
}
|
|
|
|
pub fn new(global: &GlobalScope, status: &PermissionStatus) -> Root<BluetoothPermissionResult> {
|
|
reflect_dom_object(box BluetoothPermissionResult::new_inherited(status),
|
|
global,
|
|
BluetoothPermissionResultBinding::Wrap)
|
|
}
|
|
|
|
pub fn get_bluetooth(&self) -> Root<Bluetooth> {
|
|
self.global().as_window().Navigator().Bluetooth()
|
|
}
|
|
|
|
pub fn get_bluetooth_thread(&self) -> IpcSender<BluetoothRequest> {
|
|
self.global().as_window().bluetooth_thread()
|
|
}
|
|
|
|
pub fn get_query(&self) -> PermissionName {
|
|
self.status.get_query()
|
|
}
|
|
|
|
pub fn set_state(&self, state: PermissionState) {
|
|
self.status.set_state(state)
|
|
}
|
|
|
|
pub fn get_state(&self) -> PermissionState {
|
|
self.status.State()
|
|
}
|
|
|
|
#[allow(unrooted_must_root)]
|
|
pub fn set_devices(&self, devices: Vec<JS<BluetoothDevice>>) {
|
|
*self.devices.borrow_mut() = devices;
|
|
}
|
|
}
|
|
|
|
impl BluetoothPermissionResultMethods for BluetoothPermissionResult {
|
|
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothpermissionresult-devices
|
|
fn Devices(&self) -> Vec<Root<BluetoothDevice>> {
|
|
let device_vec: Vec<Root<BluetoothDevice>> =
|
|
self.devices.borrow().iter().map(|d| Root::from_ref(&**d)).collect();
|
|
device_vec
|
|
}
|
|
}
|
|
|
|
impl AsyncBluetoothListener for BluetoothPermissionResult {
|
|
fn handle_response(&self, response: BluetoothResponse, promise_cx: *mut JSContext, promise: &Rc<Promise>) {
|
|
match response {
|
|
// https://webbluetoothcg.github.io/web-bluetooth/#request-bluetooth-devices
|
|
// Step 3, 11, 13 - 14.
|
|
BluetoothResponse::RequestDevice(device) => {
|
|
self.set_state(PermissionState::Granted);
|
|
let bluetooth = self.get_bluetooth();
|
|
let mut device_instance_map = bluetooth.get_device_map().borrow_mut();
|
|
if let Some(ref existing_device) = device_instance_map.get(&device.id) {
|
|
// https://webbluetoothcg.github.io/web-bluetooth/#request-the-bluetooth-permission
|
|
// Step 3.
|
|
self.set_devices(vec!(JS::from_ref(&*existing_device)));
|
|
|
|
// https://w3c.github.io/permissions/#dom-permissions-request
|
|
// Step 8.
|
|
return promise.resolve_native(promise_cx, self);
|
|
}
|
|
let bt_device = BluetoothDevice::new(&self.global(),
|
|
DOMString::from(device.id.clone()),
|
|
device.name.map(DOMString::from),
|
|
&bluetooth);
|
|
device_instance_map.insert(device.id.clone(), JS::from_ref(&bt_device));
|
|
self.global().as_window().bluetooth_extra_permission_data().add_new_allowed_device(
|
|
AllowedBluetoothDevice {
|
|
deviceId: DOMString::from(device.id),
|
|
mayUseGATT: true,
|
|
}
|
|
);
|
|
// https://webbluetoothcg.github.io/web-bluetooth/#request-the-bluetooth-permission
|
|
// Step 3.
|
|
self.set_devices(vec!(JS::from_ref(&bt_device)));
|
|
|
|
// https://w3c.github.io/permissions/#dom-permissions-request
|
|
// Step 8.
|
|
promise.resolve_native(promise_cx, self);
|
|
},
|
|
_ => promise.reject_error(promise_cx, Error::Type("Something went wrong...".to_owned())),
|
|
}
|
|
}
|
|
}
|