script/dom/bindings/
proxyhandler.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Utilities for the implementation of JSAPI proxy handlers.
6
7#![deny(missing_docs)]
8
9use js::jsapi::{GetObjectRealmOrNull, GetRealmPrincipals, HandleObject as RawHandleObject};
10use js::rust::get_context_realm;
11use script_bindings::principals::ServoJSPrincipalsRef;
12pub(crate) use script_bindings::proxyhandler::*;
13
14use crate::script_runtime::JSContext as SafeJSContext;
15
16/// <https://html.spec.whatwg.org/multipage/#isplatformobjectsameorigin-(-o-)>
17pub(crate) unsafe fn is_platform_object_same_origin(
18    cx: SafeJSContext,
19    obj: RawHandleObject,
20) -> bool {
21    let subject_realm = unsafe { get_context_realm(*cx) };
22    let obj_realm = unsafe { GetObjectRealmOrNull(*obj) };
23    assert!(!obj_realm.is_null());
24
25    let subject_principals =
26        unsafe { ServoJSPrincipalsRef::from_raw_unchecked(GetRealmPrincipals(subject_realm)) };
27    let obj_principals =
28        unsafe { ServoJSPrincipalsRef::from_raw_unchecked(GetRealmPrincipals(obj_realm)) };
29
30    let subject_origin = subject_principals.origin();
31    let obj_origin = obj_principals.origin();
32
33    let result = subject_origin.same_origin_domain(&obj_origin);
34    log::trace!(
35        "object {:p} (realm = {:p}, principalls = {:p}, origin = {:?}) is {} \
36        with reference to the current Realm (realm = {:p}, principals = {:p}, \
37        origin = {:?})",
38        obj.get(),
39        obj_realm,
40        obj_principals.as_raw(),
41        obj_origin.immutable(),
42        ["NOT same domain-origin", "same domain-origin"][result as usize],
43        subject_realm,
44        subject_principals.as_raw(),
45        subject_origin.immutable()
46    );
47
48    result
49}