script/dom/
servointernals.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
5use std::rc::Rc;
6
7use constellation_traits::ScriptToConstellationMessage;
8use dom_struct::dom_struct;
9use js::rust::HandleObject;
10use profile_traits::mem::MemoryReportResult;
11use script_bindings::error::{Error, Fallible};
12use script_bindings::interfaces::ServoInternalsHelpers;
13use script_bindings::script_runtime::JSContext;
14use script_bindings::str::USVString;
15use servo_config::prefs::{self, PrefValue};
16
17use crate::dom::bindings::codegen::Bindings::ServoInternalsBinding::ServoInternalsMethods;
18use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object};
19use crate::dom::bindings::root::DomRoot;
20use crate::dom::globalscope::GlobalScope;
21use crate::dom::promise::Promise;
22use crate::realms::{AlreadyInRealm, InRealm};
23use crate::routed_promise::{RoutedPromiseListener, route_promise};
24use crate::script_runtime::CanGc;
25use crate::script_thread::ScriptThread;
26
27#[dom_struct]
28pub(crate) struct ServoInternals {
29    reflector_: Reflector,
30}
31
32impl ServoInternals {
33    pub fn new_inherited() -> ServoInternals {
34        ServoInternals {
35            reflector_: Reflector::new(),
36        }
37    }
38
39    pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<ServoInternals> {
40        reflect_dom_object(Box::new(ServoInternals::new_inherited()), global, can_gc)
41    }
42}
43
44impl ServoInternalsMethods<crate::DomTypeHolder> for ServoInternals {
45    /// <https://servo.org/internal-no-spec>
46    fn ReportMemory(&self, comp: InRealm, can_gc: CanGc) -> Rc<Promise> {
47        let global = &self.global();
48        let promise = Promise::new_in_current_realm(comp, can_gc);
49        let task_source = global.task_manager().dom_manipulation_task_source();
50        let sender = route_promise(&promise, self, task_source);
51
52        let script_to_constellation_chan = global.script_to_constellation_chan();
53        if script_to_constellation_chan
54            .send(ScriptToConstellationMessage::ReportMemory(sender))
55            .is_err()
56        {
57            promise.reject_error(Error::Operation, can_gc);
58        }
59        promise
60    }
61
62    /// <https://servo.org/internal-no-spec>
63    fn GetBoolPreference(&self, name: USVString) -> Fallible<bool> {
64        if let PrefValue::Bool(b) = prefs::get().get_value(&name) {
65            return Ok(b);
66        }
67        Err(Error::TypeMismatch)
68    }
69
70    /// <https://servo.org/internal-no-spec>
71    fn GetIntPreference(&self, name: USVString) -> Fallible<i64> {
72        if let PrefValue::Int(i) = prefs::get().get_value(&name) {
73            return Ok(i);
74        }
75        Err(Error::TypeMismatch)
76    }
77
78    /// <https://servo.org/internal-no-spec>
79    fn GetStringPreference(&self, name: USVString) -> Fallible<USVString> {
80        if let PrefValue::Str(s) = prefs::get().get_value(&name) {
81            return Ok(s.into());
82        }
83        Err(Error::TypeMismatch)
84    }
85
86    /// <https://servo.org/internal-no-spec>
87    fn SetBoolPreference(&self, name: USVString, value: bool) {
88        let mut current_prefs = prefs::get().clone();
89        current_prefs.set_value(&name, value.into());
90        prefs::set(current_prefs);
91    }
92
93    /// <https://servo.org/internal-no-spec>
94    fn SetIntPreference(&self, name: USVString, value: i64) {
95        let mut current_prefs = prefs::get().clone();
96        current_prefs.set_value(&name, value.into());
97        prefs::set(current_prefs);
98    }
99
100    /// <https://servo.org/internal-no-spec>
101    fn SetStringPreference(&self, name: USVString, value: USVString) {
102        let mut current_prefs = prefs::get().clone();
103        current_prefs.set_value(&name, value.0.into());
104        prefs::set(current_prefs);
105    }
106}
107
108impl RoutedPromiseListener<MemoryReportResult> for ServoInternals {
109    fn handle_response(&self, response: MemoryReportResult, promise: &Rc<Promise>, can_gc: CanGc) {
110        let stringified = serde_json::to_string(&response.results)
111            .unwrap_or_else(|_| "{ error: \"failed to create memory report\"}".to_owned());
112        promise.resolve_native(&stringified, can_gc);
113    }
114}
115
116impl ServoInternalsHelpers for ServoInternals {
117    /// The navigator.servo api is exposed to about: pages except about:blank, as
118    /// well as any URLs provided by embedders that register new protocol handlers.
119    #[allow(unsafe_code)]
120    fn is_servo_internal(cx: JSContext, _global: HandleObject) -> bool {
121        unsafe {
122            let in_realm_proof = AlreadyInRealm::assert_for_cx(cx);
123            let global_scope = GlobalScope::from_context(*cx, InRealm::Already(&in_realm_proof));
124            let url = global_scope.get_url();
125            (url.scheme() == "about" && url.as_str() != "about:blank") ||
126                ScriptThread::is_servo_privileged(url)
127        }
128    }
129}