script/dom/
servointernals.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::conversions::ToJSValConvertible;
8use js::gc::MutableHandleValue;
9use js::jsapi::Heap;
10use js::jsval::UndefinedValue;
11use js::realm::CurrentRealm;
12use js::rust::HandleObject;
13use profile_traits::mem::MemoryReportResult;
14use script_bindings::error::{Error, Fallible};
15use script_bindings::interfaces::ServoInternalsHelpers;
16use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
17use script_bindings::str::USVString;
18use servo_config::prefs::{self, PrefValue, Preferences};
19use servo_constellation_traits::ScriptToConstellationMessage;
20
21use crate::dom::bindings::codegen::Bindings::ServoInternalsBinding::ServoInternalsMethods;
22use crate::dom::bindings::reflector::DomGlobal;
23use crate::dom::bindings::root::DomRoot;
24use crate::dom::globalscope::GlobalScope;
25use crate::dom::promise::{Promise, RootedPromise};
26use crate::event_loop::script_thread::ScriptThread;
27use crate::routed_promise::{RoutedPromiseListener, callback_promise};
28
29fn pref_to_jsval(cx: &mut js::context::JSContext, pref: &PrefValue, rval: MutableHandleValue) {
30 match pref {
31 PrefValue::Bool(b) => b.to_jsval(cx, rval),
32 PrefValue::Int(i) => i.to_jsval(cx, rval),
33 PrefValue::UInt(u) => u.to_jsval(cx, rval),
34 PrefValue::Str(s) => s.to_jsval(cx, rval),
35 PrefValue::Float(f) => f.to_jsval(cx, rval),
36 PrefValue::Array(arr) => {
37 rooted_vec!(let mut js_arr);
38 for item in arr {
39 rooted!(&in(cx) let mut js_val = UndefinedValue());
40 pref_to_jsval(cx, item, js_val.handle_mut());
41 js_arr.push(Heap::boxed(js_val.get()));
42 }
43 js_arr.to_jsval(cx, rval);
44 },
45 }
46}
47
48#[dom_struct]
49pub(crate) struct ServoInternals {
50 reflector_: Reflector,
51}
52
53impl ServoInternals {
54 pub fn new_inherited() -> ServoInternals {
55 ServoInternals {
56 reflector_: Reflector::new(),
57 }
58 }
59
60 pub(crate) fn new(cx: &mut JSContext, global: &GlobalScope) -> DomRoot<ServoInternals> {
61 reflect_dom_object_with_cx(Box::new(ServoInternals::new_inherited()), global, cx)
62 }
63}
64
65impl ServoInternalsMethods<crate::DomTypeHolder> for ServoInternals {
66 fn ReportMemory(&self, cx: &mut CurrentRealm) -> RootedPromise {
68 let promise = Promise::new_in_realm_rooted(cx);
69 let global = self.global();
70 let task_manager = global.task_manager();
71 let task_source = task_manager.dom_manipulation_task_source();
72 let callback = callback_promise(&promise, self, task_source);
73
74 let script_to_constellation_chan = global.script_to_constellation_chan();
75 if script_to_constellation_chan
76 .send(ScriptToConstellationMessage::ReportMemory(callback))
77 .is_err()
78 {
79 promise.reject_error(cx, Error::Operation(None));
80 }
81 promise
82 }
83
84 fn GarbageCollectAllContexts(&self) {
86 let global = &self.global();
87
88 let script_to_constellation_chan = global.script_to_constellation_chan();
89 let _ = script_to_constellation_chan
90 .send(ScriptToConstellationMessage::TriggerGarbageCollection);
91 }
92
93 fn PreferenceList(&self) -> Vec<USVString> {
95 Preferences::all_fields()
96 .into_iter()
97 .map(|s| USVString::from(s.to_string()))
98 .collect()
99 }
100
101 fn PreferenceType(&self, name: USVString) -> Fallible<USVString> {
103 if !Preferences::exists(&name) {
104 return Err(Error::NotFound(None));
105 }
106 let type_name = Preferences::type_of(&name).split("::").last().unwrap();
107 Ok(USVString::from(type_name.to_string()))
108 }
109
110 fn DefaultPreferenceValue(
112 &self,
113 cx: &mut JSContext,
114 name: USVString,
115 rval: MutableHandleValue,
116 ) -> Fallible<()> {
117 if !Preferences::exists(&name) {
118 return Err(Error::NotFound(None));
119 }
120 let pref = Preferences::default().get_value(&name);
121 pref_to_jsval(cx, &pref, rval);
122 Ok(())
123 }
124
125 fn GetPreference(
127 &self,
128 cx: &mut JSContext,
129 name: USVString,
130 rval: MutableHandleValue,
131 ) -> Fallible<()> {
132 if !Preferences::exists(&name) {
133 return Err(Error::NotFound(None));
134 }
135 let pref = prefs::get().get_value(&name);
136 pref_to_jsval(cx, &pref, rval);
137 Ok(())
138 }
139
140 fn GetBoolPreference(&self, name: USVString) -> Fallible<bool> {
142 if !Preferences::exists(&name) {
143 return Err(Error::NotFound(None));
144 }
145 if let PrefValue::Bool(b) = prefs::get().get_value(&name) {
146 return Ok(b);
147 }
148 Err(Error::TypeMismatch(None))
149 }
150
151 fn GetIntPreference(&self, name: USVString) -> Fallible<i64> {
153 if !Preferences::exists(&name) {
154 return Err(Error::NotFound(None));
155 }
156 if let PrefValue::Int(i) = prefs::get().get_value(&name) {
157 return Ok(i);
158 }
159 Err(Error::TypeMismatch(None))
160 }
161
162 fn GetStringPreference(&self, name: USVString) -> Fallible<USVString> {
164 if !Preferences::exists(&name) {
165 return Err(Error::NotFound(None));
166 }
167 if let PrefValue::Str(s) = prefs::get().get_value(&name) {
168 return Ok(s.into());
169 }
170 Err(Error::TypeMismatch(None))
171 }
172
173 fn SetBoolPreference(&self, name: USVString, value: bool) {
175 let mut current_prefs = prefs::get().clone();
176 current_prefs.set_value(&name, value.into());
177 prefs::set(current_prefs);
178 }
179
180 fn SetIntPreference(&self, name: USVString, value: i64) {
182 let mut current_prefs = prefs::get().clone();
183 current_prefs.set_value(&name, value.into());
184 prefs::set(current_prefs);
185 }
186
187 fn SetStringPreference(&self, name: USVString, value: USVString) {
189 let mut current_prefs = prefs::get().clone();
190 current_prefs.set_value(&name, value.0.into());
191 prefs::set(current_prefs);
192 }
193}
194
195impl RoutedPromiseListener<crate::DomTypeHolder, MemoryReportResult> for ServoInternals {
196 fn handle_response(
197 &self,
198 cx: &mut JSContext,
199 response: MemoryReportResult,
200 promise: &RootedPromise,
201 ) {
202 let stringified = serde_json::to_string(&response.results)
203 .unwrap_or_else(|_| "{ error: \"failed to create memory report\"}".to_owned());
204 promise.resolve_native(cx, &stringified);
205 }
206}
207
208impl ServoInternalsHelpers for ServoInternals {
209 fn is_servo_internal(cx: &mut JSContext, _global: HandleObject) -> bool {
212 let mut realm = CurrentRealm::assert(cx);
213 let global_scope = GlobalScope::from_current_realm(&mut realm);
214 let url = global_scope.get_url();
215 url.as_str() == "about:memory" ||
216 ScriptThread::is_servo_privileged(url) ||
217 prefs::get().expose_servointernals_globally
218 }
219}