Use MessageEventSource on MessageEvent IDL

This commit is contained in:
Kagami Sascha Rosylight 2019-11-03 23:14:23 +09:00
parent 7f77cb0bde
commit f8b61c0315
4 changed files with 44 additions and 26 deletions

View file

@ -836,6 +836,8 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
descriptorType = descriptor.nativeType
elif isArgument:
descriptorType = descriptor.argumentType
elif descriptor.interface.identifier.name == "WindowProxy":
conversionFunction = "windowproxy_from_handlevalue"
if failureCode is None:
substitutions = {
@ -2409,6 +2411,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
'crate::dom::bindings::conversions::ConversionBehavior',
'crate::dom::bindings::conversions::StringificationBehavior',
'crate::dom::bindings::conversions::root_from_handlevalue',
'crate::dom::bindings::conversions::windowproxy_from_handlevalue',
'std::ptr::NonNull',
'crate::dom::bindings::record::Record',
'crate::dom::bindings::num::Finite',
@ -2419,6 +2422,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
'crate::dom::bindings::trace::RootedTraceableBox',
'crate::dom::bindings::utils::find_enum_value',
'crate::dom::types::*',
'crate::dom::windowproxy::WindowProxy',
'crate::script_runtime::JSContext as SafeJSContext',
'js::error::throw_type_error',
'js::rust::HandleValue',

View file

@ -45,6 +45,7 @@ use crate::dom::htmlcollection::HTMLCollection;
use crate::dom::htmlformcontrolscollection::HTMLFormControlsCollection;
use crate::dom::htmloptionscollection::HTMLOptionsCollection;
use crate::dom::nodelist::NodeList;
use crate::dom::windowproxy::WindowProxy;
use js::conversions::latin1_to_string;
pub use js::conversions::ConversionBehavior;
pub use js::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
@ -55,10 +56,10 @@ use js::glue::{IsWrapper, UnwrapObjectDynamic};
use js::glue::{RUST_JSID_IS_INT, RUST_JSID_TO_INT};
use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_STRING};
use js::jsapi::{Heap, JSContext, JSObject, JSString};
use js::jsapi::{IsWindowProxy, JS_NewStringCopyN, JS_StringHasLatin1Chars};
use js::jsapi::{
JS_GetLatin1StringCharsAndLength, JS_GetTwoByteStringCharsAndLength, JS_IsExceptionPending,
};
use js::jsapi::{JS_NewStringCopyN, JS_StringHasLatin1Chars};
use js::jsval::{ObjectValue, StringValue, UndefinedValue};
use js::rust::wrappers::{JS_GetProperty, JS_HasProperty, JS_IsArrayObject};
use js::rust::{get_object_class, is_dom_class, is_dom_object, maybe_wrap_value, ToString};
@ -634,3 +635,22 @@ where
Err(()) => Err(Error::JSFailed),
}
}
/// Get a `DomRoot<T>` for a WindowProxy accessible from a `HandleValue`.
/// Caller is responsible for throwing a JS exception if needed in case of error.
pub unsafe fn windowproxy_from_handlevalue(
v: HandleValue,
_cx: *mut JSContext,
) -> Result<DomRoot<WindowProxy>, ()> {
if !v.get().is_object() {
return Err(());
}
let object = v.get().to_object();
if !IsWindowProxy(object) {
return Err(());
}
let mut value = UndefinedValue();
GetProxyReservedSlot(object, 0, &mut value);
let ptr = value.to_private() as *const WindowProxy;
Ok(DomRoot::from_ref(&*ptr))
}

View file

@ -5,9 +5,10 @@
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::MessageEventBinding;
use crate::dom::bindings::codegen::Bindings::MessageEventBinding::MessageEventMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::codegen::UnionTypes::WindowProxyOrMessagePortOrServiceWorker;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::trace::RootedTraceableBox;
@ -19,11 +20,10 @@ use crate::dom::messageport::MessagePort;
use crate::dom::windowproxy::WindowProxy;
use crate::script_runtime::JSContext;
use dom_struct::dom_struct;
use js::jsapi::{Heap, JSObject};
use js::jsapi::Heap;
use js::jsval::JSVal;
use js::rust::HandleValue;
use servo_atoms::Atom;
use std::ptr::NonNull;
#[dom_struct]
pub struct MessageEvent {
@ -94,10 +94,11 @@ impl MessageEvent {
type_: DOMString,
init: RootedTraceableBox<MessageEventBinding::MessageEventInit>,
) -> Fallible<DomRoot<MessageEvent>> {
let source = init
.source
.as_ref()
.and_then(|inner| inner.as_ref().map(|source| source.window_proxy()));
let source = match &init.source {
Some(WindowProxyOrMessagePortOrServiceWorker::WindowProxy(i)) => Some(i),
None => None,
_ => return Err(Error::NotSupported)
};
let ev = MessageEvent::new(
global,
Atom::from(type_),
@ -105,9 +106,9 @@ impl MessageEvent {
init.parent.cancelable,
init.data.handle(),
init.origin.clone(),
source.as_ref().map(|source| &**source),
source.map(|source| &**source),
init.lastEventId.clone(),
init.ports.clone().unwrap_or(vec![]),
init.ports.clone(),
);
Ok(ev)
}
@ -138,10 +139,6 @@ impl MessageEvent {
pub fn dispatch_error(target: &EventTarget, scope: &GlobalScope) {
let init = MessageEventBinding::MessageEventInit::empty();
let source = init
.source
.as_ref()
.and_then(|inner| inner.as_ref().map(|source| source.window_proxy()));
let messageevent = MessageEvent::new(
scope,
atom!("messageerror"),
@ -149,9 +146,9 @@ impl MessageEvent {
init.parent.cancelable,
init.data.handle(),
init.origin.clone(),
source.as_ref().map(|source| &**source),
None,
init.lastEventId.clone(),
init.ports.clone().unwrap_or(vec![]),
init.ports.clone(),
);
messageevent.upcast::<Event>().fire(target);
}
@ -169,10 +166,10 @@ impl MessageEventMethods for MessageEvent {
}
// https://html.spec.whatwg.org/multipage/#dom-messageevent-source
fn GetSource(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
fn GetSource(&self) -> Option<WindowProxyOrMessagePortOrServiceWorker> {
self.source
.as_ref()
.and_then(|source| NonNull::new(source.reflector().get_jsobject().get()))
.and_then(|source| Some(WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(source))))
}
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-lasteventid>

View file

@ -9,9 +9,7 @@ interface MessageEvent : Event {
readonly attribute any data;
readonly attribute DOMString origin;
readonly attribute DOMString lastEventId;
// FIXME(#22617): WindowProxy is not exposed in Worker globals
readonly attribute object? source;
//readonly attribute (WindowProxy or MessagePort)? source;
readonly attribute MessageEventSource? source;
readonly attribute /*FrozenArray<MessagePort>*/any ports;
};
@ -20,9 +18,8 @@ dictionary MessageEventInit : EventInit {
DOMString origin = "";
DOMString lastEventId = "";
//DOMString channel;
Window? source;
//(WindowProxy or MessagePort)? source;
sequence<MessagePort> ports;
MessageEventSource? source = null;
sequence<MessagePort> ports = [];
};
typedef (/*WindowProxy or */MessagePort or ServiceWorker) MessageEventSource;
typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource;