mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
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:
commit
df9065afb6
4 changed files with 84 additions and 32 deletions
|
@ -836,6 +836,8 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
|
||||||
descriptorType = descriptor.nativeType
|
descriptorType = descriptor.nativeType
|
||||||
elif isArgument:
|
elif isArgument:
|
||||||
descriptorType = descriptor.argumentType
|
descriptorType = descriptor.argumentType
|
||||||
|
elif descriptor.interface.identifier.name == "WindowProxy":
|
||||||
|
conversionFunction = "windowproxy_from_handlevalue"
|
||||||
|
|
||||||
if failureCode is None:
|
if failureCode is None:
|
||||||
substitutions = {
|
substitutions = {
|
||||||
|
@ -2409,6 +2411,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
|
||||||
'crate::dom::bindings::conversions::ConversionBehavior',
|
'crate::dom::bindings::conversions::ConversionBehavior',
|
||||||
'crate::dom::bindings::conversions::StringificationBehavior',
|
'crate::dom::bindings::conversions::StringificationBehavior',
|
||||||
'crate::dom::bindings::conversions::root_from_handlevalue',
|
'crate::dom::bindings::conversions::root_from_handlevalue',
|
||||||
|
'crate::dom::bindings::conversions::windowproxy_from_handlevalue',
|
||||||
'std::ptr::NonNull',
|
'std::ptr::NonNull',
|
||||||
'crate::dom::bindings::record::Record',
|
'crate::dom::bindings::record::Record',
|
||||||
'crate::dom::bindings::num::Finite',
|
'crate::dom::bindings::num::Finite',
|
||||||
|
@ -2419,6 +2422,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, typedefs, config):
|
||||||
'crate::dom::bindings::trace::RootedTraceableBox',
|
'crate::dom::bindings::trace::RootedTraceableBox',
|
||||||
'crate::dom::bindings::utils::find_enum_value',
|
'crate::dom::bindings::utils::find_enum_value',
|
||||||
'crate::dom::types::*',
|
'crate::dom::types::*',
|
||||||
|
'crate::dom::windowproxy::WindowProxy',
|
||||||
'crate::script_runtime::JSContext as SafeJSContext',
|
'crate::script_runtime::JSContext as SafeJSContext',
|
||||||
'js::error::throw_type_error',
|
'js::error::throw_type_error',
|
||||||
'js::rust::HandleValue',
|
'js::rust::HandleValue',
|
||||||
|
|
|
@ -45,6 +45,7 @@ use crate::dom::htmlcollection::HTMLCollection;
|
||||||
use crate::dom::htmlformcontrolscollection::HTMLFormControlsCollection;
|
use crate::dom::htmlformcontrolscollection::HTMLFormControlsCollection;
|
||||||
use crate::dom::htmloptionscollection::HTMLOptionsCollection;
|
use crate::dom::htmloptionscollection::HTMLOptionsCollection;
|
||||||
use crate::dom::nodelist::NodeList;
|
use crate::dom::nodelist::NodeList;
|
||||||
|
use crate::dom::windowproxy::WindowProxy;
|
||||||
use js::conversions::latin1_to_string;
|
use js::conversions::latin1_to_string;
|
||||||
pub use js::conversions::ConversionBehavior;
|
pub use js::conversions::ConversionBehavior;
|
||||||
pub use js::conversions::{ConversionResult, FromJSValConvertible, ToJSValConvertible};
|
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_INT, RUST_JSID_TO_INT};
|
||||||
use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_STRING};
|
use js::glue::{RUST_JSID_IS_STRING, RUST_JSID_TO_STRING};
|
||||||
use js::jsapi::{Heap, JSContext, JSObject, JSString};
|
use js::jsapi::{Heap, JSContext, JSObject, JSString};
|
||||||
|
use js::jsapi::{IsWindowProxy, JS_NewStringCopyN, JS_StringHasLatin1Chars};
|
||||||
use js::jsapi::{
|
use js::jsapi::{
|
||||||
JS_GetLatin1StringCharsAndLength, JS_GetTwoByteStringCharsAndLength, JS_IsExceptionPending,
|
JS_GetLatin1StringCharsAndLength, JS_GetTwoByteStringCharsAndLength, JS_IsExceptionPending,
|
||||||
};
|
};
|
||||||
use js::jsapi::{JS_NewStringCopyN, JS_StringHasLatin1Chars};
|
|
||||||
use js::jsval::{ObjectValue, StringValue, UndefinedValue};
|
use js::jsval::{ObjectValue, StringValue, UndefinedValue};
|
||||||
use js::rust::wrappers::{JS_GetProperty, JS_HasProperty, JS_IsArrayObject};
|
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};
|
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),
|
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))
|
||||||
|
}
|
||||||
|
|
|
@ -5,9 +5,10 @@
|
||||||
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
|
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
|
||||||
use crate::dom::bindings::codegen::Bindings::MessageEventBinding;
|
use crate::dom::bindings::codegen::Bindings::MessageEventBinding;
|
||||||
use crate::dom::bindings::codegen::Bindings::MessageEventBinding::MessageEventMethods;
|
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::error::Fallible;
|
||||||
use crate::dom::bindings::inheritance::Castable;
|
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::root::{Dom, DomRoot};
|
||||||
use crate::dom::bindings::str::DOMString;
|
use crate::dom::bindings::str::DOMString;
|
||||||
use crate::dom::bindings::trace::RootedTraceableBox;
|
use crate::dom::bindings::trace::RootedTraceableBox;
|
||||||
|
@ -16,14 +17,39 @@ use crate::dom::event::Event;
|
||||||
use crate::dom::eventtarget::EventTarget;
|
use crate::dom::eventtarget::EventTarget;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::messageport::MessagePort;
|
use crate::dom::messageport::MessagePort;
|
||||||
|
use crate::dom::serviceworker::ServiceWorker;
|
||||||
use crate::dom::windowproxy::WindowProxy;
|
use crate::dom::windowproxy::WindowProxy;
|
||||||
use crate::script_runtime::JSContext;
|
use crate::script_runtime::JSContext;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use js::jsapi::{Heap, JSObject};
|
use js::jsapi::Heap;
|
||||||
use js::jsval::JSVal;
|
use js::jsval::JSVal;
|
||||||
use js::rust::HandleValue;
|
use js::rust::HandleValue;
|
||||||
use servo_atoms::Atom;
|
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]
|
#[dom_struct]
|
||||||
pub struct MessageEvent {
|
pub struct MessageEvent {
|
||||||
|
@ -31,7 +57,7 @@ pub struct MessageEvent {
|
||||||
#[ignore_malloc_size_of = "mozjs"]
|
#[ignore_malloc_size_of = "mozjs"]
|
||||||
data: Heap<JSVal>,
|
data: Heap<JSVal>,
|
||||||
origin: DOMString,
|
origin: DOMString,
|
||||||
source: Option<Dom<WindowProxy>>,
|
source: Option<SrcObject>,
|
||||||
lastEventId: DOMString,
|
lastEventId: DOMString,
|
||||||
ports: Vec<DomRoot<MessagePort>>,
|
ports: Vec<DomRoot<MessagePort>>,
|
||||||
}
|
}
|
||||||
|
@ -52,14 +78,14 @@ impl MessageEvent {
|
||||||
global: &GlobalScope,
|
global: &GlobalScope,
|
||||||
data: HandleValue,
|
data: HandleValue,
|
||||||
origin: DOMString,
|
origin: DOMString,
|
||||||
source: Option<&WindowProxy>,
|
source: Option<&WindowProxyOrMessagePortOrServiceWorker>,
|
||||||
lastEventId: DOMString,
|
lastEventId: DOMString,
|
||||||
ports: Vec<DomRoot<MessagePort>>,
|
ports: Vec<DomRoot<MessagePort>>,
|
||||||
) -> DomRoot<MessageEvent> {
|
) -> DomRoot<MessageEvent> {
|
||||||
let ev = Box::new(MessageEvent {
|
let ev = Box::new(MessageEvent {
|
||||||
event: Event::new_inherited(),
|
event: Event::new_inherited(),
|
||||||
data: Heap::default(),
|
data: Heap::default(),
|
||||||
source: source.map(Dom::from_ref),
|
source: source.map(|source| source.into()),
|
||||||
origin,
|
origin,
|
||||||
lastEventId,
|
lastEventId,
|
||||||
ports,
|
ports,
|
||||||
|
@ -77,7 +103,7 @@ impl MessageEvent {
|
||||||
cancelable: bool,
|
cancelable: bool,
|
||||||
data: HandleValue,
|
data: HandleValue,
|
||||||
origin: DOMString,
|
origin: DOMString,
|
||||||
source: Option<&WindowProxy>,
|
source: Option<&WindowProxyOrMessagePortOrServiceWorker>,
|
||||||
lastEventId: DOMString,
|
lastEventId: DOMString,
|
||||||
ports: Vec<DomRoot<MessagePort>>,
|
ports: Vec<DomRoot<MessagePort>>,
|
||||||
) -> DomRoot<MessageEvent> {
|
) -> DomRoot<MessageEvent> {
|
||||||
|
@ -94,10 +120,6 @@ impl MessageEvent {
|
||||||
type_: DOMString,
|
type_: DOMString,
|
||||||
init: RootedTraceableBox<MessageEventBinding::MessageEventInit>,
|
init: RootedTraceableBox<MessageEventBinding::MessageEventInit>,
|
||||||
) -> Fallible<DomRoot<MessageEvent>> {
|
) -> Fallible<DomRoot<MessageEvent>> {
|
||||||
let source = init
|
|
||||||
.source
|
|
||||||
.as_ref()
|
|
||||||
.and_then(|inner| inner.as_ref().map(|source| source.window_proxy()));
|
|
||||||
let ev = MessageEvent::new(
|
let ev = MessageEvent::new(
|
||||||
global,
|
global,
|
||||||
Atom::from(type_),
|
Atom::from(type_),
|
||||||
|
@ -105,9 +127,9 @@ impl MessageEvent {
|
||||||
init.parent.cancelable,
|
init.parent.cancelable,
|
||||||
init.data.handle(),
|
init.data.handle(),
|
||||||
init.origin.clone(),
|
init.origin.clone(),
|
||||||
source.as_ref().map(|source| &**source),
|
init.source.as_ref(),
|
||||||
init.lastEventId.clone(),
|
init.lastEventId.clone(),
|
||||||
init.ports.clone().unwrap_or(vec![]),
|
init.ports.clone(),
|
||||||
);
|
);
|
||||||
Ok(ev)
|
Ok(ev)
|
||||||
}
|
}
|
||||||
|
@ -129,7 +151,11 @@ impl MessageEvent {
|
||||||
false,
|
false,
|
||||||
message,
|
message,
|
||||||
DOMString::from(origin.unwrap_or("")),
|
DOMString::from(origin.unwrap_or("")),
|
||||||
source,
|
source
|
||||||
|
.map(|source| {
|
||||||
|
WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(source))
|
||||||
|
})
|
||||||
|
.as_ref(),
|
||||||
DOMString::new(),
|
DOMString::new(),
|
||||||
ports,
|
ports,
|
||||||
);
|
);
|
||||||
|
@ -138,10 +164,6 @@ impl MessageEvent {
|
||||||
|
|
||||||
pub fn dispatch_error(target: &EventTarget, scope: &GlobalScope) {
|
pub fn dispatch_error(target: &EventTarget, scope: &GlobalScope) {
|
||||||
let init = MessageEventBinding::MessageEventInit::empty();
|
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(
|
let messageevent = MessageEvent::new(
|
||||||
scope,
|
scope,
|
||||||
atom!("messageerror"),
|
atom!("messageerror"),
|
||||||
|
@ -149,9 +171,9 @@ impl MessageEvent {
|
||||||
init.parent.cancelable,
|
init.parent.cancelable,
|
||||||
init.data.handle(),
|
init.data.handle(),
|
||||||
init.origin.clone(),
|
init.origin.clone(),
|
||||||
source.as_ref().map(|source| &**source),
|
init.source.as_ref(),
|
||||||
init.lastEventId.clone(),
|
init.lastEventId.clone(),
|
||||||
init.ports.clone().unwrap_or(vec![]),
|
init.ports.clone(),
|
||||||
);
|
);
|
||||||
messageevent.upcast::<Event>().fire(target);
|
messageevent.upcast::<Event>().fire(target);
|
||||||
}
|
}
|
||||||
|
@ -169,10 +191,19 @@ impl MessageEventMethods for MessageEvent {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-messageevent-source
|
// https://html.spec.whatwg.org/multipage/#dom-messageevent-source
|
||||||
fn GetSource(&self, _cx: JSContext) -> Option<NonNull<JSObject>> {
|
fn GetSource(&self) -> Option<WindowProxyOrMessagePortOrServiceWorker> {
|
||||||
self.source
|
match &self.source {
|
||||||
.as_ref()
|
Some(SrcObject::WindowProxy(i)) => Some(
|
||||||
.and_then(|source| NonNull::new(source.reflector().get_jsobject().get()))
|
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>
|
/// <https://html.spec.whatwg.org/multipage/#dom-messageevent-lasteventid>
|
||||||
|
|
|
@ -9,9 +9,7 @@ interface MessageEvent : Event {
|
||||||
readonly attribute any data;
|
readonly attribute any data;
|
||||||
readonly attribute DOMString origin;
|
readonly attribute DOMString origin;
|
||||||
readonly attribute DOMString lastEventId;
|
readonly attribute DOMString lastEventId;
|
||||||
// FIXME(#22617): WindowProxy is not exposed in Worker globals
|
readonly attribute MessageEventSource? source;
|
||||||
readonly attribute object? source;
|
|
||||||
//readonly attribute (WindowProxy or MessagePort)? source;
|
|
||||||
readonly attribute /*FrozenArray<MessagePort>*/any ports;
|
readonly attribute /*FrozenArray<MessagePort>*/any ports;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -20,9 +18,8 @@ dictionary MessageEventInit : EventInit {
|
||||||
DOMString origin = "";
|
DOMString origin = "";
|
||||||
DOMString lastEventId = "";
|
DOMString lastEventId = "";
|
||||||
//DOMString channel;
|
//DOMString channel;
|
||||||
Window? source;
|
MessageEventSource? source = null;
|
||||||
//(WindowProxy or MessagePort)? source;
|
sequence<MessagePort> ports = [];
|
||||||
sequence<MessagePort> ports;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef (/*WindowProxy or */MessagePort or ServiceWorker) MessageEventSource;
|
typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue