From f8b61c031549cf9897451cadf3fb51804eaf98f7 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sun, 3 Nov 2019 23:14:23 +0900 Subject: [PATCH 1/2] Use MessageEventSource on MessageEvent IDL --- .../dom/bindings/codegen/CodegenRust.py | 4 +++ components/script/dom/bindings/conversions.rs | 22 ++++++++++++- components/script/dom/messageevent.rs | 33 +++++++++---------- .../script/dom/webidls/MessageEvent.webidl | 11 +++---- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index 86ee03d7ddd..7841fb48b7d 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -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', diff --git a/components/script/dom/bindings/conversions.rs b/components/script/dom/bindings/conversions.rs index c84adf64dbb..863c84c0512 100644 --- a/components/script/dom/bindings/conversions.rs +++ b/components/script/dom/bindings/conversions.rs @@ -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` 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, ()> { + 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)) +} diff --git a/components/script/dom/messageevent.rs b/components/script/dom/messageevent.rs index cb8ebaaebd1..9c0e660f4d3 100644 --- a/components/script/dom/messageevent.rs +++ b/components/script/dom/messageevent.rs @@ -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, ) -> Fallible> { - 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::().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> { + fn GetSource(&self) -> Option { self.source .as_ref() - .and_then(|source| NonNull::new(source.reflector().get_jsobject().get())) + .and_then(|source| Some(WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(source)))) } /// diff --git a/components/script/dom/webidls/MessageEvent.webidl b/components/script/dom/webidls/MessageEvent.webidl index 63dd9019cc9..1d4699cbe3c 100644 --- a/components/script/dom/webidls/MessageEvent.webidl +++ b/components/script/dom/webidls/MessageEvent.webidl @@ -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*/any ports; }; @@ -20,9 +18,8 @@ dictionary MessageEventInit : EventInit { DOMString origin = ""; DOMString lastEventId = ""; //DOMString channel; - Window? source; - //(WindowProxy or MessagePort)? source; - sequence ports; + MessageEventSource? source = null; + sequence ports = []; }; -typedef (/*WindowProxy or */MessagePort or ServiceWorker) MessageEventSource; +typedef (WindowProxy or MessagePort or ServiceWorker) MessageEventSource; From cf46a9a6dc1b57874a6f4722660b1a57b737b02e Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 4 Nov 2019 19:24:16 +0900 Subject: [PATCH 2/2] Store source as an enum --- components/script/dom/messageevent.rs | 66 ++++++++++++++++++++------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/components/script/dom/messageevent.rs b/components/script/dom/messageevent.rs index 9c0e660f4d3..3e9a7079605 100644 --- a/components/script/dom/messageevent.rs +++ b/components/script/dom/messageevent.rs @@ -6,7 +6,7 @@ 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::{Error, Fallible}; +use crate::dom::bindings::error::Fallible; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::reflect_dom_object; use crate::dom::bindings::root::{Dom, DomRoot}; @@ -17,6 +17,7 @@ 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; @@ -25,13 +26,38 @@ use js::jsval::JSVal; use js::rust::HandleValue; use servo_atoms::Atom; +#[must_root] +#[derive(JSTraceable, MallocSizeOf)] +enum SrcObject { + WindowProxy(Dom), + MessagePort(Dom), + ServiceWorker(Dom), +} + +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 { event: Event, #[ignore_malloc_size_of = "mozjs"] data: Heap, origin: DOMString, - source: Option>, + source: Option, lastEventId: DOMString, ports: Vec>, } @@ -52,14 +78,14 @@ impl MessageEvent { global: &GlobalScope, data: HandleValue, origin: DOMString, - source: Option<&WindowProxy>, + source: Option<&WindowProxyOrMessagePortOrServiceWorker>, lastEventId: DOMString, ports: Vec>, ) -> DomRoot { 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 { @@ -94,11 +120,6 @@ impl MessageEvent { type_: DOMString, init: RootedTraceableBox, ) -> Fallible> { - 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_), @@ -106,7 +127,7 @@ impl MessageEvent { init.parent.cancelable, init.data.handle(), init.origin.clone(), - source.map(|source| &**source), + init.source.as_ref(), init.lastEventId.clone(), init.ports.clone(), ); @@ -130,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, ); @@ -146,7 +171,7 @@ impl MessageEvent { init.parent.cancelable, init.data.handle(), init.origin.clone(), - None, + init.source.as_ref(), init.lastEventId.clone(), init.ports.clone(), ); @@ -167,9 +192,18 @@ impl MessageEventMethods for MessageEvent { // https://html.spec.whatwg.org/multipage/#dom-messageevent-source fn GetSource(&self) -> Option { - self.source - .as_ref() - .and_then(|source| Some(WindowProxyOrMessagePortOrServiceWorker::WindowProxy(DomRoot::from_ref(source)))) + 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, + } } ///