// CCF: modern Chrome-Material UI prefs - read by K-Meleon's MFC app side
// (theApp.preferences) at startup. Changes require a restart.

// URL bar background color: keep it WHITE for all security states. The
// original UI tinted the HTTPS bar cream/yellow which feels dated; modern
// browsers leave the bar neutral and signal security via the lock icon in
// the status bar. COLORREF integers are RGB packed as r | (g<<8) | (b<<16).
pref("browser.urlbar.highlight.secure", 16777215);  // RGB(255,255,255) - white
pref("browser.urlbar.highlight.broken", 14737663);  // RGB(255,224,224) - pale red (keeps the warning legible)

// Wider URL bar dropdown (autocomplete list) feels less cramped on modern displays.
pref("kmeleon.urlbar.dropdown_lines", 8);

// CCF Phase 4 (dark mode): tell page content the OS theme. When set to 1,
// CSS @media (prefers-color-scheme: dark) returns true and dark-aware sites
// (GitHub, Google, Twitter, etc.) automatically serve their dark style.
// The chrome side still reads AppsUseLightTheme at runtime to switch its own
// colors - this pref only affects what JS/CSS in pages can see.
pref("ui.systemUsesDarkTheme", 1);

// Lock toolbars by default - hides the small dotted gripper bars that
// appear at the left edge of every toolbar. They're a 2000s-era UX
// pattern and clash with the flat Material chrome. Users who want to
// re-arrange toolbars can still toggle the lock from View -> Toolbars.
pref("kmeleon.general.toolbars_locked", true);

// -----------------------------------------------------------------------
// Performance / parallelism (Level 1 - tunes the threading Goanna already
// has; doesn't add new parallelism). True multi-process per-tab would
// require porting e10s and is out of scope. See PERF.md for the analysis.
// -----------------------------------------------------------------------

// Chrome/Edge use 10 persistent connections per origin (Goanna default 6).
// On HTTP/2 servers multiplexing still wins, but for HTTP/1.1 sites and
// for parallel sub-resource fetches across origins, more is faster.
pref("network.http.max-persistent-connections-per-server", 10);
// Total simultaneous outbound connections (default already 256, raise for
// pages with hundreds of subresources from many origins).
pref("network.http.max-connections", 512);

// HTTP request pacing - groups close-spaced requests to reduce thrash on
// modern many-fetch pages. Goanna ships with these off; turn on for smoother
// fetches under load.
pref("network.http.pacing.requests.enabled", true);
pref("network.http.pacing.requests.min-parallelism", 6);
pref("network.http.pacing.requests.burst", 32);

// JS GC: smaller incremental slices = smoother frame pacing during long
// pages (less perceptible hitching when GC runs). Default is 10ms; 30ms is
// a balance (longer = more reclaimed per slice, shorter = smoother).
pref("javascript.options.mem.gc_incremental_slice_ms", 30);

// Image decode threads - -1 means "use all cores" (already default but
// pin explicitly so it survives any future Goanna pref-default flip).
pref("image.multithreaded_decoding.limit", -1);

// DNS prefetch + speculative parallel: prefetch DNS for links on the page
// so click-to-paint is faster. Already on by default but pin explicitly.
pref("network.dns.disablePrefetch", false);
pref("network.prefetch-next", true);

// -----------------------------------------------------------------------
// Performance / parallelism (Level 2 - engine-side caching + JIT tuning
// the chrome already exposes but ships with conservative defaults).
// -----------------------------------------------------------------------

// Back/forward cache - keep the last N pages "alive" in memory so Back is
// instant. Default 1 (bare minimum); 8 makes Back/Forward feel native-fast.
// Each cached page costs ~5-50MB of memory; safe on modern machines.
pref("browser.sessionhistory.max_total_viewers", 8);

// Image cache: keep more decoded images in RAM so re-displaying them
// (scroll back up, switch tabs and back) doesn't re-decode. Default ~120MB.
pref("image.mem.max_decoded_image_kb", 524288);   // 512MB

// Media playback cache - bigger = smoother long videos (less buffering
// stutter). Default 512KB is anemic for HD video.
pref("media.cache_size", 524288);                 // 512MB total
pref("media.cache_readahead_limit", 60);          // seconds of readahead

// JS engine: leave both JITs on (they're on by default but make sure
// nothing flipped them off). The IonMonkey JIT is the high-tier compiler;
// huge difference for any JS-heavy site.
pref("javascript.options.baselinejit",      true);
pref("javascript.options.ion",              true);
pref("javascript.options.asmjs",            true);

// Web Workers per domain - lets sites parallelize their JS computations.
// Default 20; 30 is harmless and helps modern apps that spawn many.
pref("dom.workers.maxPerDomain", 30);

// Disable Safe Browsing URL checks. Modern Safe Browsing on Goanna is
// either off-by-default or hits a defunct Google API; the lookup adds
// ~100ms latency per navigation either way.
pref("browser.safebrowsing.enabled",          false);
pref("browser.safebrowsing.malware.enabled",  false);

// Disable telemetry / health report / crash reporter pings. Each one
// spawns background threads and network requests we don't need.
pref("toolkit.telemetry.enabled",                  false);
pref("toolkit.telemetry.unified",                  false);
pref("datareporting.healthreport.uploadEnabled",   false);
pref("datareporting.policy.dataSubmissionEnabled", false);

// -----------------------------------------------------------------------
// Heavy-JS site responsiveness (Facebook, Twitter, Slack web, etc.)
//
// Single-process Goanna runs ALL tabs' JS on one thread, so a foreground
// tab doing heavy work freezes the chrome. We can't fix that without
// e10s, but we can:
//   * Throttle background-tab JS aggressively (frees cycles for foreground)
//   * Cap runaway scripts so they get killed before stalling the UI 30s+
//   * Tune GC so it doesn't pile up huge collections that block for seconds
//   * Cap animated images that eat CPU forever
// -----------------------------------------------------------------------

// Throttle JS timers in background tabs to 1Hz (default 1000ms). Chrome,
// Firefox, and Edge all do this; without it a hidden Facebook tab still
// runs at full speed and steals cycles from the active tab.
pref("dom.min_background_timeout_value", 1000);

// Cap how long a single script can hog the main thread before Goanna
// pops "this script is unresponsive" and offers to stop it. Default is
// usually high (10s+) - we lower to 8s so runaway scripts surface fast.
pref("dom.max_script_run_time", 8);
pref("dom.max_chrome_script_run_time", 15);

// JS GC tuning - smaller, more frequent collections instead of one big
// stop-the-world. Reduces individual pause time at slight overall cost.
pref("javascript.options.mem.gc_incremental",                     true);
pref("javascript.options.mem.gc_incremental_slice_ms",            20);
pref("javascript.options.mem.gc_low_frequency_heap_growth",       150);
pref("javascript.options.mem.gc_high_frequency_high_limit_mb",    500);
pref("javascript.options.mem.gc_high_frequency_low_limit_mb",     100);

// Animated images (GIF/WebP/APNG) - "normal" plays forever, "once" plays
// once then stops, "none" disables animation entirely. "once" is the
// sweet spot: you SEE the animation but it doesn't burn CPU forever.
pref("image.animation_mode", "once");

// Throttle iframe loads from background tabs (prevents hidden tabs from
// preloading more content while you're working in the active tab).
pref("dom.timeout.throttling_delay", 0);     // 0 = always throttle bg tabs
pref("dom.timeout.background_throttling_max_idle_rate", 2);

// Memory pressure: when total RAM under pressure, drop in-memory caches
// for inactive tabs first. Default ON but pin.
pref("browser.cache.memory.enable", true);
