diff options
Diffstat (limited to 'components/script/dom/bindings/guard.rs')
-rw-r--r-- | components/script/dom/bindings/guard.rs | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/components/script/dom/bindings/guard.rs b/components/script/dom/bindings/guard.rs index 25a52fbe84c..17f3a3d20bc 100644 --- a/components/script/dom/bindings/guard.rs +++ b/components/script/dom/bindings/guard.rs @@ -1,11 +1,17 @@ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! Machinery to conditionally expose things. -use js::jsapi::{HandleObject, JSContext}; -use servo_config::prefs::PREFS; +use crate::dom::bindings::codegen::InterfaceObjectMap; +use crate::dom::bindings::interface::is_exposed_in; +use crate::dom::globalscope::GlobalScope; +use crate::realms::AlreadyInRealm; +use crate::realms::InRealm; +use crate::script_runtime::JSContext; +use js::rust::HandleObject; +use servo_config::prefs; /// A container with a condition. pub struct Guard<T: Clone + Copy> { @@ -25,8 +31,8 @@ impl<T: Clone + Copy> Guard<T> { /// Expose the value if the condition is satisfied. /// /// The passed handle is the object on which the value may be exposed. - pub unsafe fn expose(&self, cx: *mut JSContext, obj: HandleObject) -> Option<T> { - if self.condition.is_satisfied(cx, obj) { + pub fn expose(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> Option<T> { + if self.condition.is_satisfied(cx, obj, global) { Some(self.value) } else { None @@ -37,18 +43,30 @@ impl<T: Clone + Copy> Guard<T> { /// A condition to expose things. pub enum Condition { /// The condition is satisfied if the function returns true. - Func(unsafe fn(*mut JSContext, HandleObject) -> bool), + Func(fn(JSContext, HandleObject) -> bool), /// The condition is satisfied if the preference is set. Pref(&'static str), + // The condition is satisfied if the interface is exposed in the global. + Exposed(InterfaceObjectMap::Globals), + SecureContext(), /// The condition is always satisfied. 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 { - unsafe fn is_satisfied(&self, cx: *mut JSContext, obj: HandleObject) -> bool { + pub fn is_satisfied(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> bool { match *self { - Condition::Pref(name) => PREFS.get(name).as_boolean().unwrap_or(false), + 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, } } |