Skip to content

Commit

Permalink
Add proof parameter to Promise::new_in_current_compartment
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Apr 24, 2019
1 parent 7b293ee commit 1b6949d
Show file tree
Hide file tree
Showing 25 changed files with 263 additions and 95 deletions.
8 changes: 6 additions & 2 deletions components/script/body.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::codegen::Bindings::FormDataBinding::FormDataMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::DomObject;
Expand Down Expand Up @@ -49,9 +50,12 @@ pub enum FetchedData {

// https://fetch.spec.whatwg.org/#concept-body-consume-body
#[allow(unrooted_must_root)]
#[allow(unsafe_code)]
pub fn consume_body<T: BodyOperations + DomObject>(object: &T, body_type: BodyType) -> Rc<Promise> {
let promise = unsafe { Promise::new_in_current_compartment(&object.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&object.global());
let promise = Promise::new_in_current_compartment(
&object.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1
if object.get_body_used() || object.is_locked() {
Expand Down
15 changes: 11 additions & 4 deletions components/script/dom/audiocontext.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::baseaudiocontext::{BaseAudioContext, BaseAudioContextOptions};
use crate::dom::bindings::codegen::Bindings::AudioContextBinding;
use crate::dom::bindings::codegen::Bindings::AudioContextBinding::{
Expand Down Expand Up @@ -107,10 +108,13 @@ impl AudioContextMethods for AudioContext {
}

// https://webaudio.github.io/web-audio-api/#dom-audiocontext-suspend
#[allow(unsafe_code)]
fn Suspend(&self) -> Rc<Promise> {
// Step 1.
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let promise = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 2.
if self.context.control_thread_state() == ProcessingState::Closed {
Expand Down Expand Up @@ -169,10 +173,13 @@ impl AudioContextMethods for AudioContext {
}

// https://webaudio.github.io/web-audio-api/#dom-audiocontext-close
#[allow(unsafe_code)]
fn Close(&self) -> Rc<Promise> {
// Step 1.
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let promise = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 2.
if self.context.control_thread_state() == ProcessingState::Closed {
Expand Down
15 changes: 11 additions & 4 deletions components/script/dom/baseaudiocontext.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::analysernode::AnalyserNode;
use crate::dom::audiobuffer::AudioBuffer;
use crate::dom::audiobuffersourcenode::AudioBufferSourceNode;
Expand Down Expand Up @@ -271,10 +272,13 @@ impl BaseAudioContextMethods for BaseAudioContext {
}

/// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-resume
#[allow(unsafe_code)]
fn Resume(&self) -> Rc<Promise> {
// Step 1.
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let promise = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 2.
if self.audio_context_impl.state() == ProcessingState::Closed {
Expand Down Expand Up @@ -404,15 +408,18 @@ impl BaseAudioContextMethods for BaseAudioContext {
}

// https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-decodeaudiodata
#[allow(unsafe_code)]
fn DecodeAudioData(
&self,
audio_data: CustomAutoRooterGuard<ArrayBuffer>,
decode_success_callback: Option<Rc<DecodeSuccessCallback>>,
decode_error_callback: Option<Rc<DecodeErrorCallback>>,
) -> Rc<Promise> {
// Step 1.
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let promise = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
let global = self.global();
let window = global.as_window();

Expand Down
22 changes: 16 additions & 6 deletions components/script/dom/bluetooth.rs
Expand Up @@ -7,6 +7,7 @@ use bluetooth_traits::{BluetoothResponse, BluetoothResponseResult};
use bluetooth_traits::blocklist::{Blocklist, uuid_is_blocklisted};
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothDataFilterInit};
use crate::dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothMethods, RequestDeviceOptions};
Expand Down Expand Up @@ -278,7 +279,6 @@ pub fn response_async<T: AsyncBluetoothListener + DomObject + 'static>(
}

// https://webbluetoothcg.github.io/web-bluetooth/#getgattchildren
#[allow(unsafe_code)]
pub fn get_gatt_children<T, F>(
attribute: &T,
single: bool,
Expand All @@ -292,7 +292,11 @@ where
T: AsyncBluetoothListener + DomObject + 'static,
F: FnOnce(StringOrUnsignedLong) -> Fallible<UUID>,
{
let p = unsafe { Promise::new_in_current_compartment(&attribute.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&attribute.global());
let p = Promise::new_in_current_compartment(
&attribute.global(),
&InCompartment::Already(&in_compartment_proof),
);

let result_uuid = if let Some(u) = uuid {
// Step 1.
Expand Down Expand Up @@ -531,9 +535,12 @@ impl From<BluetoothError> for Error {

impl BluetoothMethods for Bluetooth {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
#[allow(unsafe_code)]
fn RequestDevice(&self, option: &RequestDeviceOptions) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
// Step 1.
if (option.filters.is_some() && option.acceptAllDevices) ||
(option.filters.is_none() && !option.acceptAllDevices)
Expand All @@ -550,9 +557,12 @@ impl BluetoothMethods for Bluetooth {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
#[allow(unsafe_code)]
fn GetAvailability(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
// Step 1. We did not override the method
// Step 2 - 3. in handle_response
let sender = response_async(&p, self);
Expand Down
8 changes: 6 additions & 2 deletions components/script/dom/bluetoothdevice.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BluetoothDeviceBinding;
use crate::dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
Expand Down Expand Up @@ -277,9 +278,12 @@ impl BluetoothDeviceMethods for BluetoothDevice {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-watchadvertisements
#[allow(unsafe_code)]
fn WatchAdvertisements(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
let sender = response_async(&p, self);
// TODO: Step 1.
// Note: Steps 2 - 3 are implemented in components/bluetooth/lib.rs in watch_advertisements function
Expand Down
29 changes: 21 additions & 8 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::BluetoothCharacteristicPropertiesMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding;
Expand Down Expand Up @@ -134,9 +135,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-readvalue
#[allow(unsafe_code)]
fn ReadValue(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) {
Expand Down Expand Up @@ -168,9 +172,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
#[allow(unsafe_code)]
fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Writes) {
Expand Down Expand Up @@ -220,9 +227,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-startnotifications
#[allow(unsafe_code)]
fn StartNotifications(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) {
Expand Down Expand Up @@ -258,9 +268,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-stopnotifications
#[allow(unsafe_code)]
fn StopNotifications(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
let sender = response_async(&p, self);

// TODO: Step 3 - 4: Implement `active notification context set` for BluetoothRemoteGATTCharacteristic,
Expand Down
15 changes: 11 additions & 4 deletions components/script/dom/bluetoothremotegattdescriptor.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::BluetoothRemoteGATTCharacteristicMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding;
Expand Down Expand Up @@ -93,9 +94,12 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-readvalue
#[allow(unsafe_code)]
fn ReadValue(&self) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) {
Expand Down Expand Up @@ -126,9 +130,12 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
#[allow(unsafe_code)]
fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer) -> Rc<Promise> {
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Writes) {
Expand Down
7 changes: 6 additions & 1 deletion components/script/dom/bluetoothremotegattserver.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use crate::dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
Expand Down Expand Up @@ -72,7 +73,11 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
#[allow(unsafe_code)]
fn Connect(&self) -> Rc<Promise> {
// Step 1.
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let in_compartment_proof = AlreadyInCompartment::assert(&self.global());
let p = Promise::new_in_current_compartment(
&self.global(),
&InCompartment::Already(&in_compartment_proof),
);
let sender = response_async(&p, self);

// TODO: Step 3: Check if the UA is currently using the Bluetooth system.
Expand Down
22 changes: 17 additions & 5 deletions components/script/dom/customelementregistry.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::compartments::{AlreadyInCompartment, InCompartment};
use crate::dom::bindings::callback::{CallbackContainer, ExceptionHandling};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::CustomElementRegistryBinding;
Expand Down Expand Up @@ -399,21 +400,28 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
}

/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-whendefined>
#[allow(unsafe_code)]
fn WhenDefined(&self, name: DOMString) -> Rc<Promise> {
let global_scope = self.window.upcast::<GlobalScope>();
let name = LocalName::from(&*name);

// Step 1
if !is_valid_custom_element_name(&name) {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
promise.reject_native(&DOMException::new(global_scope, DOMErrorName::SyntaxError));
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
&InCompartment::Already(&in_compartment_proof),
);
promise.reject_native(&DOMException::new(&global_scope, DOMErrorName::SyntaxError));
return promise;
}

// Step 2
if self.definitions.borrow().contains_key(&name) {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
&InCompartment::Already(&in_compartment_proof),
);
promise.resolve_native(&UndefinedValue());
return promise;
}
Expand All @@ -423,7 +431,11 @@ impl CustomElementRegistryMethods for CustomElementRegistry {

// Steps 4, 5
let promise = map.get(&name).cloned().unwrap_or_else(|| {
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
let in_compartment_proof = AlreadyInCompartment::assert(&global_scope);
let promise = Promise::new_in_current_compartment(
&global_scope,
&InCompartment::Already(&in_compartment_proof),
);
map.insert(name, promise.clone());
promise
});
Expand Down

0 comments on commit 1b6949d

Please sign in to comment.