use js::rust::HandleObject;
use servo_config::prefs;
use crate::dom::bindings::codegen::InterfaceObjectMap;
use crate::dom::bindings::interface::is_exposed_in;
use crate::dom::globalscope::GlobalScope;
use crate::realms::{AlreadyInRealm, InRealm};
use crate::script_runtime::JSContext;
pub struct Guard<T: Clone + Copy> {
conditions: &'static [Condition],
value: T,
}
impl<T: Clone + Copy> Guard<T> {
pub const fn new(conditions: &'static [Condition], value: T) -> Self {
Guard { conditions, value }
}
pub fn expose(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> Option<T> {
let mut exposed_on_global = false;
let conditions_satisfied = self.conditions.iter().all(|c| match c {
Condition::Satisfied => {
exposed_on_global = true;
true
},
Condition::Exposed(globals) => {
exposed_on_global |= is_exposed_in(global, *globals);
true
},
_ => c.is_satisfied(cx, obj, global),
});
if conditions_satisfied && exposed_on_global {
Some(self.value)
} else {
None
}
}
}
#[derive(Clone, Copy)]
pub enum Condition {
Func(fn(JSContext, HandleObject) -> bool),
Pref(&'static str),
Exposed(InterfaceObjectMap::Globals),
SecureContext(),
Satisfied,
}
fn is_secure_context(cx: JSContext) -> bool {
unsafe {
let in_realm_proof = AlreadyInRealm::assert_for_cx(JSContext::from_ptr(*cx));
GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof)).is_secure_context()
}
}
impl Condition {
pub fn is_satisfied(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> bool {
match *self {
Condition::Pref(name) => prefs::pref_map().get(name).as_bool().unwrap_or(false),
Condition::Func(f) => f(cx, obj),
Condition::Exposed(globals) => is_exposed_in(global, globals),
Condition::SecureContext() => is_secure_context(cx),
Condition::Satisfied => true,
}
}
}