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::realm::CurrentRealm;
11use script_bindings::principals::ServoJSPrincipalsRef;
12pub(crate) use script_bindings::proxyhandler::*;
13
14/// <https://html.spec.whatwg.org/multipage/#isplatformobjectsameorigin-(-o-)>
15pub(crate) unsafe fn is_platform_object_same_origin(
16    realm: &CurrentRealm,
17    obj: RawHandleObject,
18) -> bool {
19    let subject_realm = realm.realm().as_ptr();
20    let obj_realm = unsafe { GetObjectRealmOrNull(*obj) };
21    assert!(!obj_realm.is_null());
22
23    let subject_principals =
24        unsafe { ServoJSPrincipalsRef::from_raw_unchecked(GetRealmPrincipals(subject_realm)) };
25    let obj_principals =
26        unsafe { ServoJSPrincipalsRef::from_raw_unchecked(GetRealmPrincipals(obj_realm)) };
27
28    let subject_origin = subject_principals.origin();
29    let obj_origin = obj_principals.origin();
30
31    let result = subject_origin.same_origin_domain(&obj_origin);
32    log::trace!(
33        "object {:p} (realm = {:p}, principalls = {:p}, origin = {:?}) is {} \
34        with reference to the current Realm (realm = {:p}, principals = {:p}, \
35        origin = {:?})",
36        obj.get(),
37        obj_realm,
38        obj_principals.as_raw(),
39        obj_origin.immutable(),
40        ["NOT same domain-origin", "same domain-origin"][result as usize],
41        subject_realm,
42        subject_principals.as_raw(),
43        subject_origin.immutable()
44    );
45
46    result
47}