Skip to content

Commit

Permalink
Rename Promise::new to Promise::new_in_current_compartment
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Apr 3, 2019
1 parent 6fa1853 commit 782b585
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 51 deletions.
3 changes: 2 additions & 1 deletion components/script/body.rs
Expand Up @@ -49,8 +49,9 @@ 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 = Promise::new(&object.global());
let promise = unsafe { Promise::new_in_current_compartment(&object.global()) };

// Step 1
if object.get_body_used() || object.is_locked() {
Expand Down
6 changes: 4 additions & 2 deletions components/script/dom/audiocontext.rs
Expand Up @@ -107,9 +107,10 @@ 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 = Promise::new(&self.global());
let promise = unsafe {Promise::new_in_current_compartment(&self.global()) };

// Step 2.
if self.context.control_thread_state() == ProcessingState::Closed {
Expand Down Expand Up @@ -168,9 +169,10 @@ 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 = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };

// Step 2.
if self.context.control_thread_state() == ProcessingState::Closed {
Expand Down
6 changes: 4 additions & 2 deletions components/script/dom/baseaudiocontext.rs
Expand Up @@ -273,9 +273,10 @@ 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 = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };

// Step 2.
if self.audio_context_impl.state() == ProcessingState::Closed {
Expand Down Expand Up @@ -405,14 +406,15 @@ 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 = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let global = self.global();
let window = global.as_window();

Expand Down
9 changes: 6 additions & 3 deletions components/script/dom/bluetooth.rs
Expand Up @@ -278,6 +278,7 @@ 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 @@ -291,7 +292,7 @@ where
T: AsyncBluetoothListener + DomObject + 'static,
F: FnOnce(StringOrUnsignedLong) -> Fallible<UUID>,
{
let p = Promise::new(&attribute.global());
let p = unsafe { Promise::new_in_current_compartment(&attribute.global()) };

let result_uuid = if let Some(u) = uuid {
// Step 1.
Expand Down Expand Up @@ -530,8 +531,9 @@ 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 = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
// Step 1.
if (option.filters.is_some() && option.acceptAllDevices) ||
(option.filters.is_none() && !option.acceptAllDevices)
Expand All @@ -548,8 +550,9 @@ impl BluetoothMethods for Bluetooth {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-getavailability
#[allow(unsafe_code)]
fn GetAvailability(&self) -> Rc<Promise> {
let p = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
// Step 1. We did not override the method
// Step 2 - 3. in handle_response
let sender = response_async(&p, self);
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/bluetoothdevice.rs
Expand Up @@ -277,8 +277,9 @@ impl BluetoothDeviceMethods for BluetoothDevice {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-watchadvertisements
#[allow(unsafe_code)]
fn WatchAdvertisements(&self) -> Rc<Promise> {
let p = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
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
12 changes: 8 additions & 4 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Expand Up @@ -134,8 +134,9 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

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

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) {
Expand Down Expand Up @@ -167,8 +168,9 @@ 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 = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };

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

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

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

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

// TODO: Step 3 - 4: Implement `active notification context set` for BluetoothRemoteGATTCharacteristic,
Expand Down
6 changes: 4 additions & 2 deletions components/script/dom/bluetoothremotegattdescriptor.rs
Expand Up @@ -93,8 +93,9 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}

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

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Reads) {
Expand Down Expand Up @@ -125,8 +126,9 @@ 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 = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };

// Step 1.
if uuid_is_blocklisted(self.uuid.as_ref(), Blocklist::Writes) {
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/bluetoothremotegattserver.rs
Expand Up @@ -69,9 +69,10 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
#[allow(unsafe_code)]
fn Connect(&self) -> Rc<Promise> {
// Step 1.
let p = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let sender = response_async(&p, self);

// TODO: Step 3: Check if the UA is currently using the Bluetooth system.
Expand Down
7 changes: 4 additions & 3 deletions components/script/dom/customelementregistry.rs
Expand Up @@ -399,20 +399,21 @@ 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 = Promise::new(global_scope);
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
promise.reject_native(&DOMException::new(global_scope, DOMErrorName::SyntaxError));
return promise;
}

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

// Steps 4, 5
let promise = map.get(&name).cloned().unwrap_or_else(|| {
let promise = Promise::new(global_scope);
let promise = unsafe { Promise::new_in_current_compartment(global_scope) };
map.insert(name, promise.clone());
promise
});
Expand Down
6 changes: 4 additions & 2 deletions components/script/dom/document.rs
Expand Up @@ -3129,9 +3129,10 @@ impl Document {
}

// https://fullscreen.spec.whatwg.org/#dom-element-requestfullscreen
#[allow(unsafe_code)]
pub fn enter_fullscreen(&self, pending: &Element) -> Rc<Promise> {
// Step 1
let promise = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let mut error = false;

// Step 4
Expand Down Expand Up @@ -3195,10 +3196,11 @@ impl Document {
}

// https://fullscreen.spec.whatwg.org/#exit-fullscreen
#[allow(unsafe_code)]
pub fn exit_fullscreen(&self) -> Rc<Promise> {
let global = self.global();
// Step 1
let promise = Promise::new(&global);
let promise = unsafe { Promise::new_in_current_compartment(&global) };
// Step 2
if self.fullscreen_element.get().is_none() {
promise.reject_error(Error::Type(String::from("fullscreen is null")));
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/htmlmediaelement.rs
Expand Up @@ -1651,8 +1651,9 @@ impl HTMLMediaElementMethods for HTMLMediaElement {
}

// https://html.spec.whatwg.org/multipage/#dom-media-play
#[allow(unsafe_code)]
fn Play(&self) -> Rc<Promise> {
let promise = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
// Step 1.
// FIXME(nox): Reject promise if not allowed to play.

Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/mediadevices.rs
Expand Up @@ -44,8 +44,9 @@ impl MediaDevices {

impl MediaDevicesMethods for MediaDevices {
/// https://w3c.github.io/mediacapture-main/#dom-mediadevices-getusermedia
#[allow(unsafe_code)]
fn GetUserMedia(&self, constraints: &MediaStreamConstraints) -> Rc<Promise> {
let p = Promise::new(&self.global());
let p = unsafe { Promise::new_in_current_compartment(&self.global()) };
let media = ServoMedia::get().unwrap();
let mut tracks = vec![];
if let Some(constraints) = convert_constraints(&constraints.audio) {
Expand Down
12 changes: 8 additions & 4 deletions components/script/dom/navigationpreloadmanager.rs
Expand Up @@ -43,8 +43,9 @@ impl NavigationPreloadManager {

impl NavigationPreloadManagerMethods for NavigationPreloadManager {
// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-enable
#[allow(unsafe_code)]
fn Enable(&self) -> Rc<Promise> {
let promise = Promise::new(&*self.global());
let promise = unsafe { Promise::new_in_current_compartment(&*self.global()) };

// 2.
if self.serviceworker_registration.active().is_none() {
Expand All @@ -65,8 +66,9 @@ impl NavigationPreloadManagerMethods for NavigationPreloadManager {
}

// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-disable
#[allow(unsafe_code)]
fn Disable(&self) -> Rc<Promise> {
let promise = Promise::new(&*self.global());
let promise = unsafe { Promise::new_in_current_compartment(&*self.global()) };

// 2.
if self.serviceworker_registration.active().is_none() {
Expand All @@ -87,8 +89,9 @@ impl NavigationPreloadManagerMethods for NavigationPreloadManager {
}

// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-setheadervalue
#[allow(unsafe_code)]
fn SetHeaderValue(&self, value: ByteString) -> Rc<Promise> {
let promise = Promise::new(&*self.global());
let promise = unsafe { Promise::new_in_current_compartment(&*self.global()) };

// 2.
if self.serviceworker_registration.active().is_none() {
Expand All @@ -109,8 +112,9 @@ impl NavigationPreloadManagerMethods for NavigationPreloadManager {
}

// https://w3c.github.io/ServiceWorker/#navigation-preload-manager-getstate
#[allow(unsafe_code)]
fn GetState(&self) -> Rc<Promise> {
let promise = Promise::new(&*self.global());
let promise = unsafe { Promise::new_in_current_compartment(&*self.global()) };
// 2.
let mut state = NavigationPreloadState::empty();

Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/navigator.rs
Expand Up @@ -150,8 +150,9 @@ impl NavigatorMethods for Navigator {
}

// https://w3c.github.io/webvr/spec/1.1/#navigator-getvrdisplays-attribute
#[allow(unsafe_code)]
fn GetVRDisplays(&self) -> Rc<Promise> {
let promise = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
let displays = self.Xr().get_displays();
match displays {
Ok(displays) => promise.resolve_native(&displays),
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/offlineaudiocontext.rs
Expand Up @@ -113,8 +113,9 @@ impl OfflineAudioContextMethods for OfflineAudioContext {
}

// https://webaudio.github.io/web-audio-api/#dom-offlineaudiocontext-startrendering
#[allow(unsafe_code)]
fn StartRendering(&self) -> Rc<Promise> {
let promise = Promise::new(&self.global());
let promise = unsafe { Promise::new_in_current_compartment(&self.global()) };
if self.rendering_started.get() {
promise.reject_error(Error::InvalidState);
return promise;
Expand Down
3 changes: 2 additions & 1 deletion components/script/dom/permissions.rs
Expand Up @@ -87,6 +87,7 @@ impl Permissions {
// https://w3c.github.io/permissions/#dom-permissions-query
// https://w3c.github.io/permissions/#dom-permissions-request
// https://w3c.github.io/permissions/#dom-permissions-revoke
#[allow(unsafe_code)]
fn manipulate(
&self,
op: Operation,
Expand All @@ -97,7 +98,7 @@ impl Permissions {
// (Query, Request) Step 3.
let p = match promise {
Some(promise) => promise,
None => Promise::new(&self.global()),
None => unsafe { Promise::new_in_current_compartment(&self.global()) },
};

// (Query, Request, Revoke) Step 1.
Expand Down
8 changes: 3 additions & 5 deletions components/script/dom/promise.rs
Expand Up @@ -80,13 +80,11 @@ impl Drop for Promise {

impl Promise {
#[allow(unsafe_code)]
pub fn new(global: &GlobalScope) -> Rc<Promise> {
pub unsafe fn new_in_current_compartment(global: &GlobalScope) -> Rc<Promise> {
let cx = global.get_cx();
rooted!(in(cx) let mut obj = ptr::null_mut::<JSObject>());
unsafe {
Promise::create_js_promise(cx, HandleObject::null(), obj.handle_mut());
Promise::new_with_js_promise(obj.handle(), cx)
}
Promise::create_js_promise(cx, HandleObject::null(), obj.handle_mut());
Promise::new_with_js_promise(obj.handle(), cx)
}

#[allow(unsafe_code)]
Expand Down

0 comments on commit 782b585

Please sign in to comment.