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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/* 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/. */
use crate::basedir::default_config_dir;
use crate::opts;
use embedder_traits::resources::{self, Resource};
use serde_json::{self, Value};
use std::borrow::ToOwned;
use std::collections::HashMap;
use std::fs::File;
use std::io::{stderr, Read, Write};
use std::path::PathBuf;
use crate::pref_util::Preferences;
pub use crate::pref_util::{PrefError, PrefValue};
use gen::Prefs;
lazy_static! {
static ref PREFS: Preferences<'static, Prefs> = {
let def_prefs: Prefs = serde_json::from_str(&resources::read_string(Resource::Preferences))
.expect("Failed to initialize config preferences.");
Preferences::new(def_prefs, &gen::PREF_ACCESSORS)
};
}
/// A convenience macro for accessing a preference value using its static path.
/// Passing an invalid path is a compile-time error.
#[macro_export]
macro_rules! pref {
($($segment: ident).+) => {{
let values = $crate::prefs::pref_map().values();
let lock = values.read()
.map(|prefs| prefs $(.$segment)+.clone());
lock.unwrap()
}};
}
/// A convenience macro for updating a preference value using its static path.
/// Passing an invalid path is a compile-time error.
#[macro_export]
macro_rules! set_pref {
($($segment: ident).+, $value: expr) => {{
let values = $crate::prefs::pref_map().values();
let mut lock = values.write().unwrap();
lock$ (.$segment)+ = $value;
}};
}
/// Access preferences using their `String` keys. Note that the key may be different from the
/// static path because legacy keys contain hyphens, or because a preference name has been renamed.
///
/// When retrieving a preference, the value will always be a `PrefValue`. When setting a value, it
/// may be a `PrefValue` or the type that converts into the correct underlying value; one of `bool`,
/// `i64`, `f64` or `String`.
#[inline]
pub fn pref_map() -> &'static Preferences<'static, Prefs> {
&PREFS
}
pub(crate) fn add_user_prefs() {
if let Some(path) = user_prefs_path() {
init_user_prefs(path);
}
}
fn user_prefs_path() -> Option<PathBuf> {
opts::get()
.config_dir
.clone()
.or_else(|| default_config_dir())
.map(|path| path.join("prefs.json"))
.filter(|path| path.exists())
}
fn init_user_prefs(path: PathBuf) {
if let Ok(mut file) = File::open(&path) {
let mut txt = String::new();
file.read_to_string(&mut txt)
.expect("Can't read user prefs");
match read_prefs_map(&txt) {
Ok(prefs) => {
if let Err(error) = PREFS.set_all(prefs.into_iter()) {
writeln!(&mut stderr(), "Error setting preference: {:?}", error)
} else {
Ok(())
}
},
Err(error) => writeln!(&mut stderr(), "Error parsing prefs.json: {:?}", error),
}
} else {
writeln!(&mut stderr(), "Error opening user prefs from {:?}", path)
}
.expect("failed printing to stderr");
}
pub fn read_prefs_map(txt: &str) -> Result<HashMap<String, PrefValue>, PrefError> {
let prefs: HashMap<String, Value> =
serde_json::from_str(txt).map_err(|e| PrefError::JsonParseErr(e))?;
prefs
.into_iter()
.map(|(k, pref_value)| {
Ok({
let v = match &pref_value {
Value::Bool(b) => PrefValue::Bool(*b),
Value::Number(n) if n.is_i64() => PrefValue::Int(n.as_i64().unwrap()),
Value::Number(n) if n.is_f64() => PrefValue::Float(n.as_f64().unwrap()),
Value::String(s) => PrefValue::Str(s.to_owned()),
_ => {
return Err(PrefError::InvalidValue(format!(
"Invalid value: {}",
pref_value
)));
},
};
(k.to_owned(), v)
})
})
.collect()
}
mod gen {
use servo_config_plugins::build_structs;
// The number of layout threads is calculated if it is not present in `prefs.json`.
fn default_layout_threads() -> i64 {
std::cmp::max(num_cpus::get() * 3 / 4, 1) as i64
}
fn black() -> i64 {
0x000000
}
fn white() -> i64 {
0xFFFFFF
}
build_structs! {
// type of the accessors
accessor_type = crate::pref_util::Accessor::<Prefs, crate::pref_util::PrefValue>,
// name of the constant, which will hold a HashMap of preference accessors
gen_accessors = PREF_ACCESSORS,
// tree of structs to generate
gen_types = Prefs {
browser: {
display: {
#[serde(default = "white")]
background_color: i64,
#[serde(default = "black")]
foreground_color: i64,
}
},
css: {
animations: {
testing: {
#[serde(default)]
enabled: bool,
},
},
},
dom: {
webgpu: {
enabled: bool,
},
bluetooth: {
enabled: bool,
testing: {
enabled: bool,
}
},
canvas_text: {
#[serde(rename = "dom.canvas-text.enabled")]
enabled: bool,
},
composition_event: {
#[serde(rename = "dom.compositionevent.enabled")]
enabled: bool,
},
custom_elements: {
#[serde(rename = "dom.customelements.enabled")]
enabled: bool,
},
document: {
dblclick_timeout: i64,
dblclick_dist: i64,
},
forcetouch: {
enabled: bool,
},
fullscreen: {
test: bool,
},
gamepad: {
enabled: bool,
},
microdata: {
testing: {
enabled: bool,
}
},
mouse_event: {
which: {
#[serde(rename = "dom.mouseevent.which.enabled")]
enabled: bool,
}
},
mutation_observer: {
enabled: bool,
},
offscreen_canvas: {
enabled: bool,
},
permissions: {
enabled: bool,
testing: {
allowed_in_nonsecure_contexts: bool,
}
},
serviceworker: {
enabled: bool,
timeout_seconds: i64,
},
servoparser: {
async_html_tokenizer: {
enabled: bool,
}
},
shadowdom: {
enabled: bool,
},
svg: {
enabled: bool,
},
testable_crash: {
enabled: bool,
},
testbinding: {
enabled: bool,
prefcontrolled: {
#[serde(default)]
enabled: bool,
},
prefcontrolled2: {
#[serde(default)]
enabled: bool,
},
preference_value: {
#[serde(default)]
falsy: bool,
#[serde(default)]
quote_string_test: String,
#[serde(default)]
space_string_test: String,
#[serde(default)]
string_empty: String,
#[serde(default)]
string_test: String,
#[serde(default)]
truthy: bool,
},
},
testing: {
element: {
activation: {
#[serde(default)]
enabled: bool,
}
},
html_input_element: {
select_files: {
#[serde(rename = "dom.testing.htmlinputelement.select_files.enabled")]
enabled: bool,
}
},
},
testperf: {
#[serde(default)]
enabled: bool,
},
webgl: {
dom_to_texture: {
enabled: bool,
}
},
webgl2: {
enabled: bool,
},
webrtc: {
#[serde(default)]
enabled: bool,
},
webvr: {
enabled: bool,
event_polling_interval: i64,
test: bool,
},
webxr: {
#[serde(default)]
enabled: bool,
#[serde(default)]
test: bool,
#[serde(default)]
glwindow: bool,
},
worklet: {
blockingsleep: {
#[serde(default)]
enabled: bool,
},
#[serde(default)]
enabled: bool,
testing: {
#[serde(default)]
enabled: bool,
},
timeout_ms: i64,
},
},
gfx: {
subpixel_text_antialiasing: {
#[serde(rename = "gfx.subpixel-text-antialiasing.enabled")]
enabled: bool,
}
},
js: {
asmjs: {
enabled: bool,
},
asyncstack: {
enabled: bool,
},
baseline: {
enabled: bool,
unsafe_eager_compilation: {
enabled: bool,
},
},
discard_system_source: {
enabled: bool,
},
dump_stack_on_debuggee_would_run: {
enabled: bool,
},
ion: {
enabled: bool,
offthread_compilation: {
enabled: bool,
},
unsafe_eager_compilation: {
enabled: bool,
},
},
mem: {
gc: {
allocation_threshold_mb: i64,
allocation_threshold_factor: i64,
allocation_threshold_avoid_interrupt_factor: i64,
compacting: {
enabled: bool,
},
decommit_threshold_mb: i64,
dynamic_heap_growth: {
enabled: bool,
},
dynamic_mark_slice: {
enabled: bool,
},
empty_chunk_count_max: i64,
empty_chunk_count_min: i64,
high_frequency_heap_growth_max: i64,
high_frequency_heap_growth_min: i64,
high_frequency_high_limit_mb: i64,
high_frequency_low_limit_mb: i64,
high_frequency_time_limit_ms: i64,
incremental: {
enabled: bool,
slice_ms: i64,
},
low_frequency_heap_growth: i64,
per_compartment: {
enabled: bool,
},
per_zone: {
enabled: bool,
},
zeal: {
frequency: i64,
level: i64,
},
},
high_water_mark: i64,
max: i64,
},
native_regex: {
enabled: bool,
},
offthread_compilation: {
enabled: bool,
},
parallel_parsing: {
enabled: bool,
},
shared_memory: {
enabled: bool,
},
strict: {
debug: {
enabled: bool,
},
enabled: bool,
},
throw_on_asmjs_validation_failure: {
enabled: bool,
},
throw_on_debuggee_would_run: {
enabled: bool,
},
timers: {
minimum_duration: i64,
},
wasm: {
baseline: {
enabled: bool,
},
enabled: bool,
ion: {
enabled: bool,
}
},
werror: {
enabled: bool,
},
},
layout: {
animations: {
test: {
enabled: bool,
}
},
columns: {
enabled: bool,
},
#[serde(default = "default_layout_threads")]
threads: i64,
viewport: {
enabled: bool,
},
writing_mode: {
#[serde(rename = "layout.writing-mode.enabled")]
enabled: bool,
}
},
media: {
glvideo: {
enabled: bool,
},
testing: {
enabled: bool,
}
},
network: {
enforce_tls: {
enabled: bool,
localhost: bool,
onion: bool,
},
http_cache: {
#[serde(rename = "network.http-cache.disabled")]
disabled: bool,
},
mime: {
sniff: bool,
}
},
session_history: {
#[serde(rename = "session-history.max-length")]
max_length: i64,
},
shell: {
homepage: String,
keep_screen_on: {
enabled: bool,
},
#[serde(rename = "shell.native-orientation")]
native_orientation: String,
native_titlebar: {
#[serde(rename = "shell.native-titlebar.enabled")]
enabled: bool,
},
searchpage: String,
},
webgl: {
testing: {
context_creation_error: bool,
}
},
}
}
}
|