Skip to main content

servo_config/
prefs.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
5//! Preferences are the global configuration options that can be changed at runtime.
6
7use std::env::consts::ARCH;
8use std::sync::{RwLock, RwLockReadGuard};
9use std::time::Duration;
10
11use serde::{Deserialize, Serialize};
12use servo_config_macro::ServoPreferences;
13
14pub use crate::pref_util::PrefValue;
15
16static PREFERENCES: RwLock<Preferences> = RwLock::new(Preferences::const_default());
17
18/// A trait to be implemented by components that wish to be notified about runtime changes to the
19/// global preferences for the current process.
20pub trait PreferencesObserver: Send + Sync {
21    /// This method is called when the global preferences have been updated. The argument to the
22    /// method is an array of tuples where the first component is the name of the preference and
23    /// the second component is the new value of the preference.
24    fn prefs_changed(&self, _changes: &[(&'static str, PrefValue)]) {}
25}
26
27static OBSERVERS: RwLock<Vec<Box<dyn PreferencesObserver>>> = RwLock::new(Vec::new());
28
29#[inline]
30/// Get the current set of global preferences for Servo.
31pub fn get() -> RwLockReadGuard<'static, Preferences> {
32    PREFERENCES.read().unwrap()
33}
34
35/// Subscribe to notifications about changes to the global preferences for the current process.
36pub fn add_observer(observer: Box<dyn PreferencesObserver>) {
37    OBSERVERS.write().unwrap().push(observer);
38}
39
40/// Update the values of the global preferences for the current process. This also notifies the
41/// observers previously added using [`add_observer`].
42pub fn set(preferences: Preferences) {
43    // Get list of changes, returning early if the preferences haven't changed.
44    let changed = preferences.diff(&PREFERENCES.read().unwrap());
45    if changed.is_empty() {
46        return;
47    }
48
49    // Map between Stylo preference names and Servo preference names as the This should be
50    // kept in sync with components/script/dom/bindings/codegen/run.py which generates the
51    // DOM CSS style accessors.
52    stylo_static_prefs::set_pref!("layout.unimplemented", preferences.layout_unimplemented);
53    stylo_static_prefs::set_pref!("layout.threads", preferences.layout_threads as i32);
54    stylo_static_prefs::set_pref!("layout.columns.enabled", preferences.layout_columns_enabled);
55    stylo_static_prefs::set_pref!("layout.grid.enabled", preferences.layout_grid_enabled);
56    stylo_static_prefs::set_pref!(
57        "layout.css.attr.enabled",
58        preferences.layout_css_attr_enabled
59    );
60    stylo_static_prefs::set_pref!(
61        "layout.writing-mode.enabled",
62        preferences.layout_writing_mode_enabled
63    );
64    stylo_static_prefs::set_pref!(
65        "layout.container-queries.enabled",
66        preferences.layout_container_queries_enabled
67    );
68    stylo_static_prefs::set_pref!(
69        "layout.variable_fonts.enabled",
70        preferences.layout_variable_fonts_enabled
71    );
72
73    *PREFERENCES.write().unwrap() = preferences;
74
75    for observer in &*OBSERVERS.read().unwrap() {
76        observer.prefs_changed(&changed);
77    }
78}
79
80/// A convenience macro for accessing a preference value using its static path.
81/// Passing an invalid path is a compile-time error.
82#[macro_export]
83macro_rules! pref {
84    ($name: ident) => {
85        $crate::prefs::get().$name.clone()
86    };
87}
88
89/// The set of global preferences supported by Servo.
90///
91/// Each preference has a default value that determines its initial state. These defaults
92/// fall into roughly three categories:
93/// - **Stable**: enabled by default.
94/// - **Experimental**: disabled by default, but intended to be enabled for experimental use.
95/// - **Unstable**: disabled by default.
96///
97/// For a full overview of which preferences are experimental, see the
98/// [experimental features documentation](https://book.servo.org/design-documentation/experimental-features.html).
99#[derive(Clone, Deserialize, Serialize, ServoPreferences)]
100pub struct Preferences {
101    pub fonts_default: String,
102    pub fonts_serif: String,
103    pub fonts_sans_serif: String,
104    pub fonts_monospace: String,
105    pub fonts_default_size: i64,
106    pub fonts_default_monospace_size: i64,
107    /// The amount of time that a half cycle of a text caret blink takes in milliseconds.
108    /// If this value is less than or equal to zero, then caret blink is disabled.
109    pub editing_caret_blink_time: i64,
110    pub css_animations_testing_enabled: bool,
111    /// Start the devtools server at startup
112    pub devtools_server_enabled: bool,
113    /// The address:port the devtools server listens to, default to 127.0.0.1:7000.
114    pub devtools_server_listen_address: String,
115    // feature: WebGPU | #24706 | Web/API/WebGPU_API
116    pub dom_webgpu_enabled: bool,
117    /// List of comma-separated backends to be used by wgpu.
118    pub dom_webgpu_wgpu_backend: String,
119    // feature: AbortController | #34866 | Web/API/AbortController
120    pub dom_abort_controller_enabled: bool,
121    // feature: Adopted Stylesheet | #38132 | Web/API/Document/adoptedStyleSheets
122    pub dom_adoptedstylesheet_enabled: bool,
123    pub dom_allow_preloading_module_descendants: bool,
124    // feature: Clipboard API | #36084 | Web/API/Clipboard_API
125    pub dom_async_clipboard_enabled: bool,
126    pub dom_bluetooth_enabled: bool,
127    pub dom_bluetooth_testing_enabled: bool,
128    pub dom_allow_scripts_to_close_windows: bool,
129    // feature: Media Capture and Streams API | #26861 | Web/API/Media_Capture_and_Streams_API
130    pub dom_canvas_capture_enabled: bool,
131    pub dom_canvas_text_enabled: bool,
132    /// Selects canvas backend
133    ///
134    /// Available values:
135    /// - vello
136    /// - Everything else selects vello_cpu
137    pub dom_canvas_backend: String,
138    /// Maximum number of buffered canvas commands before an automatic flush is triggered.
139    ///
140    /// A lower value keeps the paint thread fed with work (better parallelism),
141    /// while a higher value improves batching efficiency (fewer channel operations, lower power).
142    ///
143    /// See <https://github.com/servo/servo/pull/45301> for measurements.
144    pub dom_canvas_msg_buffer_size: u64,
145    pub dom_clipboardevent_enabled: bool,
146    pub dom_composition_event_enabled: bool,
147    // feature: CookieStore | #37674 | Web/API/CookieStore
148    pub dom_cookiestore_enabled: bool,
149    // feature: Credential Management API | #38788 | Web/API/Credential_Management_API
150    pub dom_credential_management_enabled: bool,
151    // feature: WebCrypto API | #40687 | Web/API/Web_Crypto_API
152    pub dom_crypto_subtle_enabled: bool,
153    pub dom_document_dblclick_timeout: i64,
154    pub dom_document_dblclick_dist: i64,
155    // feature: File and Directory Entries API | #45653 | Web/API/File_and_Directory_Entries_API
156    pub dom_entries_api_enabled: bool,
157    // feature: Document.execCommand | #25005 | Web/API/Document/execCommand
158    pub dom_exec_command_enabled: bool,
159    // feature: CSS Font Loading API | #29376 | Web/API/CSS_Font_Loading_API
160    pub dom_fontface_enabled: bool,
161    pub dom_fullscreen_test: bool,
162    // feature: Gamepad API | #10977 | Web/API/Gamepad_API
163    pub dom_gamepad_enabled: bool,
164    // feature: Geolocation API | #38903 | Web/API/Geolocation_API
165    pub dom_geolocation_enabled: bool,
166    // feature: Screen Wake Lock API | #43615 | Web/API/Screen_Wake_Lock_API
167    pub dom_wakelock_enabled: bool,
168    // feature: IndexedDB | #6963 | Web/API/IndexedDB_API
169    pub dom_indexeddb_enabled: bool,
170    // feature: IntersectionObserver | #35767 | Web/API/Intersection_Observer_API
171    pub dom_intersection_observer_enabled: bool,
172    pub dom_microdata_testing_enabled: bool,
173    pub dom_uievent_which_enabled: bool,
174    // feature: MutationObserver | #6633 | Web/API/MutationObserver
175    pub dom_mutation_observer_enabled: bool,
176    // feature: Navigator.registerProtocolHandler() | #40615 | Web/API/Navigator/registerProtocolHandler
177    pub dom_navigator_protocol_handlers_enabled: bool,
178    // feature: Notification API | #34841 | Web/API/Notifications_API
179    pub dom_notification_enabled: bool,
180    // feature: OffscreenCanvas | #34111 | Web/API/OffscreenCanvas
181    pub dom_offscreen_canvas_enabled: bool,
182    pub dom_parallel_css_parsing_enabled: bool,
183    // feature: Permissions API | #31235 | Web/API/Permissions_API
184    pub dom_permissions_enabled: bool,
185    pub dom_permissions_testing_allowed_in_nonsecure_contexts: bool,
186    // feature: ResizeObserver | #39790 | Web/API/ResizeObserver
187    pub dom_resize_observer_enabled: bool,
188    // feature: Sanitizer API | #43948 | Web/API/HTML_Sanitizer_API
189    pub dom_sanitizer_enabled: bool,
190    pub dom_script_asynch: bool,
191    // feature: Storage API | #43976 | Web/API/Storage_API
192    pub dom_storage_manager_api_enabled: bool,
193    // feature: ServiceWorker | #36538 | Web/API/Service_Worker_API
194    pub dom_serviceworker_enabled: bool,
195    pub dom_serviceworker_timeout_seconds: i64,
196    // feature: SharedWorker | #7458 | Web/API/SharedWorker
197    pub dom_sharedworker_enabled: bool,
198    pub dom_servo_helpers_enabled: bool,
199    pub dom_servoparser_async_html_tokenizer_enabled: bool,
200    pub dom_testbinding_enabled: bool,
201    pub dom_testbinding_prefcontrolled_enabled: bool,
202    pub dom_testbinding_prefcontrolled2_enabled: bool,
203    pub dom_testbinding_preference_value_falsy: bool,
204    pub dom_testbinding_preference_value_quote_string_test: String,
205    pub dom_testbinding_preference_value_space_string_test: String,
206    pub dom_testbinding_preference_value_string_empty: String,
207    pub dom_testbinding_preference_value_string_test: String,
208    pub dom_testbinding_preference_value_truthy: bool,
209    pub dom_testing_element_activation_enabled: bool,
210    pub dom_testing_html_input_element_select_files_enabled: bool,
211    pub dom_testperf_enabled: bool,
212    // https://testutils.spec.whatwg.org#availability
213    pub dom_testutils_enabled: bool,
214    /// <https://w3c.github.io/touch-events/#conditionally-exposing-legacy-touch-event-apis>
215    pub dom_touch_events_legacy_apis_enabled: bool,
216    /// <https://html.spec.whatwg.org/multipage/#transient-activation-duration>
217    pub dom_transient_activation_duration_ms: i64,
218    // feature: Web Animations | #36950 | Web/API/Web_Animations_API
219    pub dom_web_animations_enabled: bool,
220    /// Enable WebGL2 APIs.
221    // feature: WebGL2 | #41394 | Web/API/WebGL2RenderingContext
222    pub dom_webgl2_enabled: bool,
223    // feature: WebRTC | #41396 | Web/API/WebRTC_API
224    pub dom_webrtc_enabled: bool,
225    // feature: WebRTC Transceiver | #41396 | Web/API/RTCRtpTransceiver
226    pub dom_webrtc_transceiver_enabled: bool,
227    // feature: WebVTT | #22312 | Web/API/WebVTT_API
228    pub dom_webvtt_enabled: bool,
229    pub dom_webxr_enabled: bool,
230    pub dom_webxr_test: bool,
231    pub dom_webxr_first_person_observer_view: bool,
232    pub dom_webxr_glwindow_enabled: bool,
233    pub dom_webxr_glwindow_left_right: bool,
234    pub dom_webxr_glwindow_red_cyan: bool,
235    pub dom_webxr_glwindow_spherical: bool,
236    pub dom_webxr_glwindow_cubemap: bool,
237    pub dom_webxr_hands_enabled: bool,
238    // feature: WebXR Layers | #27468 | Web/API/XRCompositionLayer
239    pub dom_webxr_layers_enabled: bool,
240    pub dom_webxr_openxr_enabled: bool,
241    pub dom_webxr_sessionavailable: bool,
242    pub dom_webxr_unsafe_assume_user_intent: bool,
243    pub dom_worklet_enabled: bool,
244    pub dom_worklet_blockingsleep_enabled: bool,
245    pub dom_worklet_testing_enabled: bool,
246    pub dom_worklet_timeout_ms: i64,
247    /// <https://drafts.csswg.org/cssom-view/#the-visualviewport-interface>
248    // feature: VisualViewport | #41341 | Web/API/VisualViewport
249    pub dom_visual_viewport_enabled: bool,
250    /// True to compile all WebRender shaders when Servo initializes. This is mostly
251    /// useful when modifying the shaders, to ensure they all compile after each change is
252    /// made.
253    pub gfx_precache_shaders: bool,
254    /// Whether or not antialiasing is enabled for text rendering.
255    pub gfx_text_antialiasing_enabled: bool,
256    /// Whether or not subpixel antialiasing is enabled for text rendering.
257    pub gfx_subpixel_text_antialiasing_enabled: bool,
258    pub gfx_texture_swizzling_enabled: bool,
259    /// The amount of image keys we request per batch for the image cache.
260    pub image_key_batch_size: i64,
261    /// Whether or not the DOM inspector should show shadow roots of user-agent shadow trees
262    pub inspector_show_servo_internal_shadow_roots: bool,
263    /// A locale tag (eg. es-ES) to use for language negotiation instead of the system locale.
264    /// An empty string represents no override.
265    /// TODO: Option<> support in PrefValue
266    pub intl_locale_override: String,
267    pub js_asmjs_enabled: bool,
268    pub js_baseline_interpreter_enabled: bool,
269    /// Whether to disable the jit within SpiderMonkey
270    pub js_disable_jit: bool,
271    pub js_baseline_jit_enabled: bool,
272    pub js_baseline_jit_unsafe_eager_compilation_enabled: bool,
273    pub js_ion_enabled: bool,
274    pub js_ion_unsafe_eager_compilation_enabled: bool,
275    pub js_mem_gc_compacting_enabled: bool,
276    pub js_mem_gc_empty_chunk_count_min: i64,
277    pub js_mem_gc_high_frequency_heap_growth_max: i64,
278    pub js_mem_gc_high_frequency_heap_growth_min: i64,
279    pub js_mem_gc_high_frequency_high_limit_mb: i64,
280    pub js_mem_gc_high_frequency_low_limit_mb: i64,
281    pub js_mem_gc_high_frequency_time_limit_ms: i64,
282    pub js_mem_gc_incremental_enabled: bool,
283    pub js_mem_gc_incremental_slice_ms: i64,
284    pub js_mem_gc_low_frequency_heap_growth: i64,
285    pub js_mem_gc_per_zone_enabled: bool,
286    pub js_mem_gc_zeal_frequency: i64,
287    pub js_mem_gc_zeal_level: i64,
288    pub js_mem_max: i64,
289    pub js_native_regex_enabled: bool,
290    pub js_offthread_compilation_enabled: bool,
291    pub js_timers_minimum_duration: i64,
292    pub js_wasm_baseline_enabled: bool,
293    pub js_wasm_enabled: bool,
294    pub js_wasm_ion_enabled: bool,
295    // feature: Largest Contentful Paint | #42000 | Web/API/LargestContentfulPaint
296    pub largest_contentful_paint_enabled: bool,
297    pub layout_animations_test_enabled: bool,
298    // feature: CSS Multicol | #22397 | Web/CSS/Guides/Multicol_layout
299    pub layout_columns_enabled: bool,
300    // feature: CSS Grid | #34479 | Web/CSS/Guides/Grid_layout
301    pub layout_grid_enabled: bool,
302    pub layout_container_queries_enabled: bool,
303    pub layout_css_attr_enabled: bool,
304    pub layout_style_sharing_cache_enabled: bool,
305    pub layout_threads: i64,
306    /// The minimum number of parallelizable jobs required before turning on parallelism
307    /// for a set of jobs.
308    ///
309    /// When deciding whether or not to parallelize layout, this is the minimum number of
310    /// jobs that must be larger than [`Self::layout_parallelism_job_size_minimum`] to
311    /// turn on parallelism. An exception is when doing box tree layout, where Servo does
312    /// not know the depth of the tree. In that case any task that has more jobs than this
313    /// value will be parallelized.
314    ///
315    /// The goal of these two values is to allow tuning Servo's parallelism for both wide
316    /// and deep trees.
317    pub layout_parallelism_job_count_minimum: u64,
318    /// The minimum size of a layout job to be considered for parallelization.
319    ///
320    /// When deciding whether or not to parallelize layout, jobs greater than this size
321    /// are counted when considering the [`Self::layout_parallelism_job_count_minimum`]
322    /// threshold for turning on parallelism. Generally the size of the job is based on
323    /// the number of tasks to process in the subtree. For instance, this might be the
324    /// number of boxes to process in a box tree subtree.
325    ///
326    /// The goal of these two values is to allow tuning Servo's parallelism for both wide
327    /// and deep trees.
328    pub layout_parallelism_job_size_minimum: u64,
329    pub layout_unimplemented: bool,
330    // feature: Variable fonts | #38800 | Web/CSS/Guides/Fonts/Variable_fonts
331    pub layout_variable_fonts_enabled: bool,
332    // feature: CSS writing modes | #2560 | Web/CSS/Guides/Writing_modes
333    pub layout_writing_mode_enabled: bool,
334    /// Enable hardware acceleration for video playback.
335    pub media_glvideo_enabled: bool,
336    /// Enable a non-standard event handler for verifying behavior of media elements during tests.
337    pub media_testing_enabled: bool,
338    /// The default timeout set for establishing a network connection in seconds. This amount
339    /// if for the entire process of connecting to an address. For instance, if a particular host is
340    /// associated with multiple IP addresses, this timeout will be divided equally among
341    /// each IP address.
342    pub network_connection_timeout: u64,
343    pub network_enforce_tls_enabled: bool,
344    pub network_enforce_tls_localhost: bool,
345    pub network_enforce_tls_onion: bool,
346    pub network_http_cache_disabled: bool,
347    /// A url for a http proxy. We treat an empty string as no proxy.
348    pub network_http_proxy_uri: String,
349    /// A url for a https proxy. We treat an empty string as no proxy.
350    pub network_https_proxy_uri: String,
351    /// The domains for which we will not have a proxy. No effect if `network_http_proxy_uri` is not set.
352    /// The exact behavior is given by
353    /// <https://docs.rs/hyper-util/latest/hyper_util/client/proxy/matcher/struct.Builder.html#method.no>
354    pub network_http_no_proxy: String,
355    /// The weight of the http memory cache
356    /// Notice that this is not equal to the number of different urls in the cache.
357    pub network_http_cache_size: u64,
358    pub network_local_directory_listing_enabled: bool,
359    /// Force the use of `rust-webpki` verification for CA roots. If this is false (the
360    /// default), then `rustls-platform-verifier` will be used, except on Android where
361    /// `rust-webpki` is always used.
362    pub network_use_webpki_roots: bool,
363    /// The length of the session history, in navigations, for each `WebView. Back-forward
364    /// cache entries that are more than `session_history_max_length` steps in the future or
365    /// `session_history_max_length` steps in the past will be discarded. Navigating forward
366    /// or backward to that entry will cause the entire page to be reloaded.
367    pub session_history_max_length: i64,
368    /// The background color of shell's viewport. This will be used by OpenGL's `glClearColor`.
369    pub shell_background_color_rgba: [f64; 4],
370    pub webgl_testing_context_creation_error: bool,
371    /// Maximum number of workers for the main thread pool
372    pub thread_pool_workers_max: u64,
373    /// Number of workers per thread pool, if we fail to detect how much
374    /// parallelism is available at runtime.
375    pub thread_pool_fallback_workers: u64,
376    /// Maximum number of workers for the asynchronous networking runtime thread pool
377    pub thread_pool_async_runtime_workers_max: u64,
378    /// Maximum number of workers for WebRender
379    pub thread_pool_webrender_workers_max: u64,
380    /// The user-agent to use for Servo. This can also be set via [`UserAgentPlatform`] in
381    /// order to set the value to the default value for the given platform.
382    pub user_agent: String,
383    /// Whether or not the viewport meta tag is enabled.
384    pub viewport_meta_enabled: bool,
385    pub log_filter: String,
386    /// Whether the accessibility code is enabled.
387    pub accessibility_enabled: bool,
388    /// Whether to run accessibility tree integrity checks, and any other expensive checks.
389    /// This should only be true in tests.
390    pub expensive_accessibility_test_assertions_enabled: bool,
391    /// Exposes internal JS API functions that are usually restricted to `about:...` pages
392    /// Useful if you want to get memory report or force GC in a test page
393    pub expose_servointernals_globally: bool,
394}
395
396impl Preferences {
397    const fn const_default() -> Self {
398        Self {
399            css_animations_testing_enabled: false,
400            editing_caret_blink_time: 600,
401            devtools_server_enabled: false,
402            devtools_server_listen_address: String::new(),
403            dom_abort_controller_enabled: true,
404            dom_adoptedstylesheet_enabled: false,
405            dom_allow_preloading_module_descendants: false,
406            dom_allow_scripts_to_close_windows: false,
407            dom_async_clipboard_enabled: false,
408            dom_bluetooth_enabled: false,
409            dom_bluetooth_testing_enabled: false,
410            dom_canvas_capture_enabled: false,
411            dom_canvas_text_enabled: true,
412            dom_canvas_backend: String::new(),
413            dom_canvas_msg_buffer_size: 16,
414            dom_clipboardevent_enabled: true,
415            dom_composition_event_enabled: false,
416            dom_cookiestore_enabled: false,
417            dom_credential_management_enabled: false,
418            dom_crypto_subtle_enabled: true,
419            dom_document_dblclick_dist: 1,
420            dom_document_dblclick_timeout: 300,
421            dom_entries_api_enabled: false,
422            dom_exec_command_enabled: false,
423            dom_fontface_enabled: false,
424            dom_fullscreen_test: false,
425            dom_gamepad_enabled: true,
426            dom_geolocation_enabled: false,
427            dom_wakelock_enabled: false,
428            dom_indexeddb_enabled: false,
429            dom_intersection_observer_enabled: false,
430            dom_microdata_testing_enabled: false,
431            dom_uievent_which_enabled: true,
432            dom_mutation_observer_enabled: true,
433            dom_navigator_protocol_handlers_enabled: false,
434            dom_notification_enabled: false,
435            dom_parallel_css_parsing_enabled: true,
436            dom_offscreen_canvas_enabled: false,
437            dom_permissions_enabled: false,
438            dom_permissions_testing_allowed_in_nonsecure_contexts: false,
439            dom_resize_observer_enabled: true,
440            dom_sanitizer_enabled: false,
441            dom_script_asynch: true,
442            dom_storage_manager_api_enabled: false,
443            dom_serviceworker_enabled: false,
444            dom_serviceworker_timeout_seconds: 60,
445            dom_sharedworker_enabled: true,
446            dom_servo_helpers_enabled: false,
447            dom_servoparser_async_html_tokenizer_enabled: false,
448            dom_testbinding_enabled: false,
449            dom_testbinding_prefcontrolled2_enabled: false,
450            dom_testbinding_prefcontrolled_enabled: false,
451            dom_testbinding_preference_value_falsy: false,
452            dom_testbinding_preference_value_quote_string_test: String::new(),
453            dom_testbinding_preference_value_space_string_test: String::new(),
454            dom_testbinding_preference_value_string_empty: String::new(),
455            dom_testbinding_preference_value_string_test: String::new(),
456            dom_testbinding_preference_value_truthy: false,
457            dom_testing_element_activation_enabled: false,
458            dom_testing_html_input_element_select_files_enabled: false,
459            dom_testperf_enabled: false,
460            dom_testutils_enabled: false,
461            // Following Firefox and Chrome, we are enabling the touch events legacy APIs for android.
462            // Additionally, enabling it in ohos for compatibility as well.
463            dom_touch_events_legacy_apis_enabled: cfg!(target_os = "android") |
464                cfg!(target_env = "ohos"),
465            dom_transient_activation_duration_ms: 5000,
466            dom_web_animations_enabled: false,
467            dom_webgl2_enabled: false,
468            dom_webgpu_enabled: false,
469            dom_webgpu_wgpu_backend: String::new(),
470            dom_webrtc_enabled: false,
471            dom_webrtc_transceiver_enabled: false,
472            dom_webvtt_enabled: false,
473            dom_webxr_enabled: true,
474            dom_webxr_first_person_observer_view: false,
475            dom_webxr_glwindow_cubemap: false,
476            dom_webxr_glwindow_enabled: true,
477            dom_webxr_glwindow_left_right: false,
478            dom_webxr_glwindow_red_cyan: false,
479            dom_webxr_glwindow_spherical: false,
480            dom_webxr_hands_enabled: true,
481            dom_webxr_layers_enabled: false,
482            dom_webxr_openxr_enabled: true,
483            dom_webxr_sessionavailable: false,
484            dom_webxr_test: false,
485            dom_webxr_unsafe_assume_user_intent: false,
486            dom_worklet_blockingsleep_enabled: false,
487            dom_worklet_enabled: false,
488            dom_worklet_testing_enabled: false,
489            dom_worklet_timeout_ms: 10,
490            dom_visual_viewport_enabled: false,
491            accessibility_enabled: false,
492            expensive_accessibility_test_assertions_enabled: false,
493            fonts_default: String::new(),
494            fonts_default_monospace_size: 13,
495            fonts_default_size: 16,
496            fonts_monospace: String::new(),
497            fonts_sans_serif: String::new(),
498            fonts_serif: String::new(),
499            gfx_precache_shaders: false,
500            gfx_text_antialiasing_enabled: true,
501            gfx_subpixel_text_antialiasing_enabled: true,
502            gfx_texture_swizzling_enabled: true,
503            image_key_batch_size: 10,
504            inspector_show_servo_internal_shadow_roots: false,
505            intl_locale_override: String::new(),
506            js_asmjs_enabled: true,
507            js_baseline_interpreter_enabled: true,
508            js_baseline_jit_enabled: true,
509            js_baseline_jit_unsafe_eager_compilation_enabled: false,
510            js_disable_jit: false,
511            js_ion_enabled: true,
512            js_ion_unsafe_eager_compilation_enabled: false,
513            js_mem_gc_compacting_enabled: true,
514            js_mem_gc_empty_chunk_count_min: 1,
515            js_mem_gc_high_frequency_heap_growth_max: 300,
516            js_mem_gc_high_frequency_heap_growth_min: 150,
517            js_mem_gc_high_frequency_high_limit_mb: 500,
518            js_mem_gc_high_frequency_low_limit_mb: 100,
519            js_mem_gc_high_frequency_time_limit_ms: 1000,
520            js_mem_gc_incremental_enabled: true,
521            js_mem_gc_incremental_slice_ms: 10,
522            js_mem_gc_low_frequency_heap_growth: 150,
523            js_mem_gc_per_zone_enabled: false,
524            js_mem_gc_zeal_frequency: 100,
525            js_mem_gc_zeal_level: 0,
526            js_mem_max: -1,
527            js_native_regex_enabled: true,
528            js_offthread_compilation_enabled: true,
529            js_timers_minimum_duration: 1000,
530            js_wasm_baseline_enabled: true,
531            js_wasm_enabled: true,
532            js_wasm_ion_enabled: true,
533            largest_contentful_paint_enabled: false,
534            layout_animations_test_enabled: false,
535            layout_columns_enabled: false,
536            layout_container_queries_enabled: false,
537            layout_css_attr_enabled: false,
538            layout_grid_enabled: false,
539            layout_style_sharing_cache_enabled: true,
540            // TODO(mrobinson): This should likely be based on the number of processors.
541            layout_threads: 3,
542            layout_parallelism_job_count_minimum: 4,
543            layout_parallelism_job_size_minimum: 16,
544            layout_unimplemented: false,
545            layout_variable_fonts_enabled: false,
546            layout_writing_mode_enabled: false,
547            media_glvideo_enabled: false,
548            media_testing_enabled: false,
549            network_connection_timeout: 15,
550            network_enforce_tls_enabled: false,
551            network_enforce_tls_localhost: false,
552            network_enforce_tls_onion: false,
553            network_http_cache_disabled: false,
554            network_http_proxy_uri: String::new(),
555            network_https_proxy_uri: String::new(),
556            network_http_no_proxy: String::new(),
557            network_http_cache_size: 5000,
558            network_local_directory_listing_enabled: true,
559            network_use_webpki_roots: false,
560            session_history_max_length: 20,
561            shell_background_color_rgba: [1.0, 1.0, 1.0, 1.0],
562            log_filter: String::new(),
563            thread_pool_workers_max: 4,
564            thread_pool_async_runtime_workers_max: 6,
565            thread_pool_fallback_workers: 3,
566            thread_pool_webrender_workers_max: 4,
567            webgl_testing_context_creation_error: false,
568            user_agent: String::new(),
569            viewport_meta_enabled: false,
570            expose_servointernals_globally: false,
571        }
572    }
573
574    /// The amount of time that a half cycle of a text caret blink takes. If blinking is disabled
575    /// this returns `None`.
576    pub fn editing_caret_blink_time(&self) -> Option<Duration> {
577        if self.editing_caret_blink_time > 0 {
578            Some(Duration::from_millis(self.editing_caret_blink_time as u64))
579        } else {
580            None
581        }
582    }
583}
584
585impl Default for Preferences {
586    fn default() -> Self {
587        let mut preferences = Self::const_default();
588        preferences.user_agent = UserAgentPlatform::default().to_user_agent_string();
589        if let Ok(proxy_uri) = std::env::var("http_proxy").or_else(|_| std::env::var("HTTP_PROXY"))
590        {
591            preferences.network_http_proxy_uri = proxy_uri;
592        }
593        if let Ok(proxy_uri) =
594            std::env::var("https_proxy").or_else(|_| std::env::var("HTTPS_PROXY"))
595        {
596            preferences.network_https_proxy_uri = proxy_uri;
597        }
598        if let Ok(no_proxy) = std::env::var("no_proxy").or_else(|_| std::env::var("NO_PROXY")) {
599            preferences.network_http_no_proxy = no_proxy
600        }
601
602        preferences
603    }
604}
605
606pub enum UserAgentPlatform {
607    Desktop,
608    Android,
609    OpenHarmony,
610    Ios,
611}
612
613impl UserAgentPlatform {
614    /// Return the default `UserAgentPlatform` for this platform. This is
615    /// not an implementation of `Default` so that it can be `const`.
616    pub const fn default() -> Self {
617        if cfg!(target_os = "android") {
618            Self::Android
619        } else if cfg!(target_env = "ohos") {
620            Self::OpenHarmony
621        } else if cfg!(target_os = "ios") {
622            Self::Ios
623        } else {
624            Self::Desktop
625        }
626    }
627}
628
629impl UserAgentPlatform {
630    /// Convert this [`UserAgentPlatform`] into its corresponding `String` value, ie the
631    /// default user-agent to use for this platform.
632    pub fn to_user_agent_string(&self) -> String {
633        const SERVO_VERSION: &str = env!("CARGO_PKG_VERSION");
634        match self {
635            UserAgentPlatform::Desktop
636                if cfg!(all(target_os = "windows", target_arch = "x86_64")) =>
637            {
638                format!(
639                    "Mozilla/5.0 (Windows NT 10.0; Win64; {ARCH}rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
640                )
641            },
642            UserAgentPlatform::Desktop if cfg!(target_os = "macos") => {
643                format!(
644                    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
645                )
646            },
647            UserAgentPlatform::Desktop => {
648                format!(
649                    "Mozilla/5.0 (X11; Linux {ARCH}; rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
650                )
651            },
652            UserAgentPlatform::Android => {
653                format!(
654                    "Mozilla/5.0 (Android 10; Mobile; rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
655                )
656            },
657            UserAgentPlatform::OpenHarmony => format!(
658                "Mozilla/5.0 (OpenHarmony; Mobile; rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
659            ),
660            UserAgentPlatform::Ios => format!(
661                "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X; rv:140.0) Servo/{SERVO_VERSION} Firefox/140.0"
662            ),
663        }
664    }
665}