aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings/guard.rs
blob: 25a52fbe84c1d93abb886f54fda990cff144e18c (plain) (blame)
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
/* 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/. */

//! Machinery to conditionally expose things.

use js::jsapi::{HandleObject, JSContext};
use servo_config::prefs::PREFS;

/// A container with a condition.
pub struct Guard<T: Clone + Copy> {
    condition: Condition,
    value: T,
}

impl<T: Clone + Copy> Guard<T> {
    /// Construct a new guarded value.
    pub const fn new(condition: Condition, value: T) -> Self {
        Guard {
            condition: condition,
            value: value,
        }
    }

    /// 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) {
            Some(self.value)
        } else {
            None
        }
    }
}

/// A condition to expose things.
pub enum Condition {
    /// The condition is satisfied if the function returns true.
    Func(unsafe fn(*mut JSContext, HandleObject) -> bool),
    /// The condition is satisfied if the preference is set.
    Pref(&'static str),
    /// The condition is always satisfied.
    Satisfied,
}

impl Condition {
    unsafe fn is_satisfied(&self, cx: *mut JSContext, obj: HandleObject) -> bool {
        match *self {
            Condition::Pref(name) => PREFS.get(name).as_boolean().unwrap_or(false),
            Condition::Func(f) => f(cx, obj),
            Condition::Satisfied => true,
        }
    }
}