1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* 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 https://mozilla.org/MPL/2.0/. */

//! Machinery to conditionally expose things.

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;

/// A container with a list of conditions.
pub struct Guard<T: Clone + Copy> {
    conditions: &'static [Condition],
    value: T,
}

impl<T: Clone + Copy> Guard<T> {
    /// Construct a new guarded value.
    pub const fn new(conditions: &'static [Condition], value: T) -> Self {
        Guard { conditions, value }
    }

    /// Expose the value if the conditions are satisfied.
    ///
    /// The passed handle is the object on which the value may be exposed.
    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
            },
            // If there are multiple Exposed conditions, we just need one of them to be 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
        }
    }
}

/// A condition to expose things.
#[derive(Clone, Copy)]
pub enum Condition {
    /// The condition is satisfied if the function returns true.
    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 {
    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,
        }
    }
}