Auto merge of #24636 - saschanaz:windowproxy, r=nox,jdm

Use MessageEventSource on MessageEvent IDL

<!-- Please describe your changes on the following line: -->

---
<!-- 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
- [x] These changes fix #22617

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2019-11-07 22:35:43 -05:00 committed by GitHub
commit df9065afb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 32 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::codegen::UnionTypes::WindowProxyOrMessagePortOrServiceWorker;
use crate::dom::bindings::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;
@ -16,14 +17,39 @@ use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::globalscope::GlobalScope;
use crate::dom::messageport::MessagePort;
use crate::dom::serviceworker::ServiceWorker;
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;
#[must_root]
#[derive(JSTraceable, MallocSizeOf)]
enum SrcObject {
WindowProxy(Dom<WindowProxy>),
MessagePort(Dom<MessagePort>),
ServiceWorker(Dom<ServiceWorker>),
}
impl From<&WindowProxyOrMessagePortOrServiceWorker> for SrcObject {
#[allow(unrooted_must_root)]
fn from(src_object: &WindowProxyOrMessagePortOrServiceWorker) -> SrcObject {
match src_object {
WindowProxyOrMessagePortOrServiceWorker::WindowProxy(blob) => {
SrcObject::WindowProxy(Dom::from_ref(&*blob))
},
WindowProxyOrMessagePortOrServiceWorker::MessagePort(stream) => {
SrcObject::MessagePort(Dom::from_ref(&*stream))
},
WindowProxyOrMessagePortOrServiceWorker::ServiceWorker(stream) => {
SrcObject::ServiceWorker(Dom::from_ref(&*stream))
},
}
}
}
#[dom_struct]
pub struct MessageEvent {
@ -31,7 +57,7 @@ pub struct MessageEvent {
#[ignore_malloc_size_of = "mozjs"]
data: Heap<JSVal>,
origin: DOMString,
source: Option<Dom<WindowProxy>>,
source: Option<SrcObject>,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
}
@ -52,14 +78,14 @@ impl MessageEvent {
global: &GlobalScope,
data: HandleValue,
origin: DOMString,
source: Option<&WindowProxy>,
source: Option<&WindowProxyOrMessagePortOrServiceWorker>,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> DomRoot<MessageEvent> {
let ev = Box::new(MessageEvent {
event: Event::new_inherited(),
data: Heap::default(),
source: source.map(Dom::from_ref),
source: source.map(|source| source.into()),
origin,
lastEventId,
ports,
@ -77,7 +103,7 @@ impl MessageEvent {
cancelable: bool,
data: HandleValue,
origin: DOMString,
source: Option<&WindowProxy>,
source: Option<&WindowProxyOrMessagePortOrServiceWorker>,
lastEventId: DOMString,
ports: Vec<DomRoot<MessagePort>>,
) -> DomRoot<MessageEvent> {
@ -94,10 +120,6 @@ 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 ev = MessageEvent::new(
global,
Atom::from(type_),
@ -105,9 +127,9 @@ impl MessageEvent {
init.parent.cancelable,
init.data.handle(),
init.origin.clone(),
source.as_ref().map(|source| &**source),
init.source.as_ref(),
init.lastEventId.clone(),
init.ports.clone().unwrap_or(vec![]),
init.ports.clone(),
);
Ok(ev)
}
@ -129,7 +151,11 @@ impl MessageEvent {
false,
message,
DOMString::from(origin.unwrap_or("")),
source,
source
.map(|source| {
WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(source))
})
.as_ref(),
DOMString::new(),
ports,
);
@ -138,10 +164,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 +171,9 @@ impl MessageEvent {
init.parent.cancelable,
init.data.handle(),
init.origin.clone(),
source.as_ref().map(|source| &**source),
init.source.as_ref(),
init.lastEventId.clone(),
init.ports.clone().unwrap_or(vec![]),
init.ports.clone(),
);
messageevent.upcast::<Event>().fire(target);
}
@ -169,10 +191,19 @@ impl MessageEventMethods for MessageEvent {
}
// https://html.spec.whatwg.org/multipage/#dom-messageevent-source
fn GetSource(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
self.source
.as_ref()
.and_then(|source| NonNull::new(source.reflector().get_jsobject().get()))
fn GetSource(&self) -> Option<WindowProxyOrMessagePortOrServiceWorker> {
match &self.source {
Some(SrcObject::WindowProxy(i)) => Some(
WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(&*i)),
),
Some(SrcObject::MessagePort(i)) => Some(
WindowProxyOrMessagePortOrServiceWorker::MessagePort(DomRoot::from_ref(&*i)),
),
Some(SrcObject::ServiceWorker(i)) => Some(
WindowProxyOrMessagePortOrServiceWorker::ServiceWorker(DomRoot::from_ref(&*i)),
),
None => None,
}
}
/// <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;