From 320965bfb9b606b75f9d7d812cf2615ff58e0a82 Mon Sep 17 00:00:00 2001 From: yvt Date: Tue, 13 Jul 2021 21:45:21 +0900 Subject: [PATCH] =?UTF-8?q?refactor(script):=20move=20`crate::dom::binding?= =?UTF-8?q?s::{utils=20=E2=86=92=20principals)::ServoJSPrincipal`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/script/dom/bindings/interface.rs | 3 +- components/script/dom/bindings/mod.rs | 1 + components/script/dom/bindings/principals.rs | 44 +++++++++++++++++++ components/script/dom/bindings/utils.rs | 45 -------------------- components/script/script_runtime.rs | 7 +-- 5 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 components/script/dom/bindings/principals.rs diff --git a/components/script/dom/bindings/interface.rs b/components/script/dom/bindings/interface.rs index d9fba2c5b8d..215907bf062 100644 --- a/components/script/dom/bindings/interface.rs +++ b/components/script/dom/bindings/interface.rs @@ -9,7 +9,8 @@ use crate::dom::bindings::codegen::PrototypeList; use crate::dom::bindings::constant::{define_constants, ConstantSpec}; use crate::dom::bindings::conversions::{get_dom_class, DOM_OBJECT_SLOT}; use crate::dom::bindings::guard::Guard; -use crate::dom::bindings::utils::{ProtoOrIfaceArray, ServoJSPrincipal, DOM_PROTOTYPE_SLOT}; +use crate::dom::bindings::principals::ServoJSPrincipal; +use crate::dom::bindings::utils::{ProtoOrIfaceArray, DOM_PROTOTYPE_SLOT}; use crate::dom::window::Window; use crate::script_runtime::JSContext as SafeJSContext; use js::error::throw_type_error; diff --git a/components/script/dom/bindings/mod.rs b/components/script/dom/bindings/mod.rs index 8b3f9682544..e446af3693f 100644 --- a/components/script/dom/bindings/mod.rs +++ b/components/script/dom/bindings/mod.rs @@ -145,6 +145,7 @@ pub mod interface; pub mod iterable; pub mod namespace; pub mod num; +pub mod principals; pub mod proxyhandler; pub mod record; pub mod refcounted; diff --git a/components/script/dom/bindings/principals.rs b/components/script/dom/bindings/principals.rs new file mode 100644 index 00000000000..323bc9e8a23 --- /dev/null +++ b/components/script/dom/bindings/principals.rs @@ -0,0 +1,44 @@ +use js::glue::{ + CreateRustJSPrincipals, DestroyRustJSPrincipals, GetRustJSPrincipalsPrivate, + JSPrincipalsCallbacks, +}; +use js::jsapi::JSPrincipals; +use servo_url::MutableOrigin; + +// TODO: RAII ref-counting +pub struct ServoJSPrincipal(pub *mut JSPrincipals); + +impl ServoJSPrincipal { + pub fn new(origin: &MutableOrigin) -> Self { + let private: Box = Box::new(origin.clone()); + Self(unsafe { CreateRustJSPrincipals(&PRINCIPALS_CALLBACKS, Box::into_raw(private) as _) }) + } + + pub unsafe fn origin(&self) -> MutableOrigin { + let origin = GetRustJSPrincipalsPrivate(self.0) as *mut MutableOrigin; + (*origin).clone() + } +} + +pub unsafe extern "C" fn destroy_servo_jsprincipal(principals: *mut JSPrincipals) { + Box::from_raw(GetRustJSPrincipalsPrivate(principals) as *mut MutableOrigin); + DestroyRustJSPrincipals(principals); +} + +const PRINCIPALS_CALLBACKS: JSPrincipalsCallbacks = JSPrincipalsCallbacks { + write: None, + isSystemOrAddonPrincipal: Some(principals_is_system_or_addon_principal), +}; + +unsafe extern "C" fn principals_is_system_or_addon_principal(_: *mut JSPrincipals) -> bool { + false +} + +//TODO is same_origin_domain equivalent to subsumes for our purposes +pub unsafe extern "C" fn subsumes(obj: *mut JSPrincipals, other: *mut JSPrincipals) -> bool { + let obj = ServoJSPrincipal(obj); + let other = ServoJSPrincipal(other); + let obj_origin = obj.origin(); + let other_origin = other.origin(); + obj_origin.same_origin_domain(&other_origin) +} diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index b2e4066fca3..77192291416 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -19,17 +19,12 @@ use crate::script_runtime::JSContext as SafeJSContext; use js::conversions::ToJSValConvertible; use js::glue::JS_GetReservedSlot; use js::glue::{CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, IsWrapper}; -use js::glue::{ - CreateRustJSPrincipals, DestroyRustJSPrincipals, GetRustJSPrincipalsPrivate, - JSPrincipalsCallbacks, -}; use js::glue::{UnwrapObjectDynamic, UnwrapObjectStatic, RUST_JSID_TO_INT, RUST_JSID_TO_STRING}; use js::glue::{ RUST_FUNCTION_VALUE_TO_JITINFO, RUST_JSID_IS_INT, RUST_JSID_IS_STRING, RUST_JSID_IS_VOID, }; use js::jsapi::HandleId as RawHandleId; use js::jsapi::HandleObject as RawHandleObject; -use js::jsapi::JSPrincipals; use js::jsapi::MutableHandleIdVector as RawMutableHandleIdVector; use js::jsapi::{AtomToLinearString, GetLinearStringCharAt, GetLinearStringLength}; use js::jsapi::{CallArgs, DOMCallbacks, GetNonCCWObjectGlobal}; @@ -53,7 +48,6 @@ use js::rust::{Handle, HandleId, HandleObject, HandleValue, MutableHandleValue}; use js::typedarray::{CreateWith, Float32Array}; use js::JS_CALLEE; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; -use servo_url::MutableOrigin; use std::ffi::CString; use std::os::raw::{c_char, c_void}; use std::ptr; @@ -70,45 +64,6 @@ impl MallocSizeOf for WindowProxyHandler { } } -//TODO make principal.rs -// TODO: RAII ref-counting -pub struct ServoJSPrincipal(pub *mut JSPrincipals); - -impl ServoJSPrincipal { - pub fn new(origin: &MutableOrigin) -> Self { - let private: Box = Box::new(origin.clone()); - Self(unsafe { CreateRustJSPrincipals(&PRINCIPALS_CALLBACKS, Box::into_raw(private) as _) }) - } - - pub unsafe fn origin(&self) -> MutableOrigin { - let origin = GetRustJSPrincipalsPrivate(self.0) as *mut MutableOrigin; - (*origin).clone() - } -} - -pub unsafe extern "C" fn destroy_servo_jsprincipal(principals: *mut JSPrincipals) { - Box::from_raw(GetRustJSPrincipalsPrivate(principals) as *mut MutableOrigin); - DestroyRustJSPrincipals(principals); -} - -const PRINCIPALS_CALLBACKS: JSPrincipalsCallbacks = JSPrincipalsCallbacks { - write: None, - isSystemOrAddonPrincipal: Some(principals_is_system_or_addon_principal), -}; - -unsafe extern "C" fn principals_is_system_or_addon_principal(_: *mut JSPrincipals) -> bool { - false -} - -//TODO is same_origin_domain equivalent to subsumes for our purposes -pub unsafe extern "C" fn subsumes(obj: *mut JSPrincipals, other: *mut JSPrincipals) -> bool { - let obj = ServoJSPrincipal(obj); - let other = ServoJSPrincipal(other); - let obj_origin = obj.origin(); - let other_origin = other.origin(); - obj_origin.same_origin_domain(&other_origin) -} - #[derive(JSTraceable, MallocSizeOf)] /// Static data associated with a global object. pub struct GlobalStaticData { diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index e19526be0b1..5ce21f01366 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -16,13 +16,14 @@ use crate::dom::bindings::conversions::private_from_object; use crate::dom::bindings::conversions::root_from_handleobject; use crate::dom::bindings::error::{throw_dom_exception, Error}; use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::principals; use crate::dom::bindings::refcounted::{trace_refcounted_objects, LiveDOMReferences}; use crate::dom::bindings::refcounted::{Trusted, TrustedPromise}; use crate::dom::bindings::reflector::DomObject; use crate::dom::bindings::root::trace_roots; use crate::dom::bindings::settings_stack; use crate::dom::bindings::trace::{trace_traceables, JSTraceable}; -use crate::dom::bindings::utils::{self, DOM_CALLBACKS}; +use crate::dom::bindings::utils::DOM_CALLBACKS; use crate::dom::event::{Event, EventBubbles, EventCancelable, EventStatus}; use crate::dom::eventtarget::EventTarget; use crate::dom::globalscope::GlobalScope; @@ -101,7 +102,7 @@ static JOB_QUEUE_TRAPS: JobQueueTraps = JobQueueTraps { static SECURITY_CALLBACKS: JSSecurityCallbacks = JSSecurityCallbacks { // TODO: Content Security Policy contentSecurityPolicyAllows: None, - subsumes: Some(utils::subsumes), + subsumes: Some(principals::subsumes), }; /// Common messages used to control the event loops in both the script and the worker @@ -475,7 +476,7 @@ unsafe fn new_rt_and_cx_with_parent( JS_SetSecurityCallbacks(cx, &SECURITY_CALLBACKS); - JS_InitDestroyPrincipalsCallback(cx, Some(utils::destroy_servo_jsprincipal)); + JS_InitDestroyPrincipalsCallback(cx, Some(principals::destroy_servo_jsprincipal)); // Needed for debug assertions about whether GC is running. if cfg!(debug_assertions) {