script_bindings/
principals.rs1use std::marker::PhantomData;
6use std::mem::ManuallyDrop;
7use std::ops::Deref;
8use std::ptr::NonNull;
9
10use js::glue::{CreateRustJSPrincipals, GetRustJSPrincipalsPrivate};
11use js::jsapi::{JS_DropPrincipals, JS_HoldPrincipals, JSPrincipals};
12use js::rust::Runtime;
13use servo_url::MutableOrigin;
14
15use crate::DomTypes;
16use crate::interfaces::DomHelpers;
17
18#[repr(transparent)]
20pub struct ServoJSPrincipals(NonNull<JSPrincipals>);
21
22impl ServoJSPrincipals {
23 pub fn new<D: DomTypes>(origin: &MutableOrigin) -> Self {
27 unsafe {
28 let private: Box<MutableOrigin> = Box::new(origin.clone());
29 let raw = CreateRustJSPrincipals(
30 <D as DomHelpers<D>>::principals_callbacks(),
31 Box::into_raw(private) as _,
32 );
33 Self::from_raw_nonnull(NonNull::new_unchecked(raw))
36 }
37 }
38
39 #[inline]
45 pub unsafe fn from_raw_nonnull(raw: NonNull<JSPrincipals>) -> Self {
46 JS_HoldPrincipals(raw.as_ptr());
47 Self(raw)
48 }
49
50 #[inline]
51 pub fn origin(&self) -> MutableOrigin {
52 unsafe {
53 let origin = GetRustJSPrincipalsPrivate(self.0.as_ptr()) as *mut MutableOrigin;
54 (*origin).clone()
55 }
56 }
57
58 #[inline]
59 pub fn as_raw_nonnull(&self) -> NonNull<JSPrincipals> {
60 self.0
61 }
62
63 #[inline]
64 pub fn as_raw(&self) -> *mut JSPrincipals {
65 self.0.as_ptr()
66 }
67}
68
69impl Clone for ServoJSPrincipals {
70 #[inline]
71 fn clone(&self) -> Self {
72 unsafe { Self::from_raw_nonnull(self.as_raw_nonnull()) }
73 }
74}
75
76impl Drop for ServoJSPrincipals {
77 #[inline]
78 fn drop(&mut self) {
79 if let Some(cx) = Runtime::get() {
80 unsafe { JS_DropPrincipals(cx.as_ptr(), self.as_raw()) };
81 }
82 }
83}
84
85pub struct ServoJSPrincipalsRef<'a>(ManuallyDrop<ServoJSPrincipals>, PhantomData<&'a ()>);
88
89impl ServoJSPrincipalsRef<'_> {
90 #[inline]
99 pub unsafe fn from_raw_nonnull(raw: NonNull<JSPrincipals>) -> Self {
100 Self(ManuallyDrop::new(ServoJSPrincipals(raw)), PhantomData)
103 }
104
105 #[inline]
112 pub unsafe fn from_raw_unchecked(raw: *mut JSPrincipals) -> Self {
113 Self::from_raw_nonnull(NonNull::new_unchecked(raw))
114 }
115}
116
117impl Clone for ServoJSPrincipalsRef<'_> {
118 #[inline]
119 fn clone(&self) -> Self {
120 Self(ManuallyDrop::new(ServoJSPrincipals(self.0.0)), PhantomData)
121 }
122}
123
124impl Deref for ServoJSPrincipalsRef<'_> {
125 type Target = ServoJSPrincipals;
126
127 #[inline]
128 fn deref(&self) -> &Self::Target {
129 &self.0
130 }
131}