S049 (WIP): ad blocker in WebKit content filters, web process extension for headers, stand-ins and blocked counts

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-13 14:33:18 -06:00
commit bb1c1a5ed7

View file

@ -24,7 +24,8 @@ src/hub.rs Servo delegates; everything Servo tells us becomes an Event i
src/tabs.rs Tab (one WebView or an internal page), per-window tab list
src/keys.rs winit -> Servo key conversion, shortcut table (rebindable)
src/inject.rs JavaScript injected into pages (bridge, autofill, find, media, PiP, cosmetic filters)
src/adblock.rs adblock-rust engine, lists, updates, counters
src/adblock.rs adblock-rust: lists, updates, cosmetic engine, conversion to WebKit content filters
webext/ web process extension (cdylib): headers, stand-ins, blocked counts (D-036)
src/discord.rs Discord Rich Presence over the local IPC socket
src/media.rs media state gathered from pages; feeds the media button, Discord, MPRIS later
src/importer.rs import from Chromium- and Firefox-family browsers
@ -399,3 +400,49 @@ full-window `default_size` it centres itself in the whole height and stretches t
## Feature checklist
See `docs/FEATURES.md`; every row names the file that implements it and its review state.
## Ad blocker on WebKit (D-036, S049)
- **Network rules** → WebKit content filters. `adblock::convert` parses every enabled list (network rules only) with
adblock-rust's `content-blocking` conversion; blocks first, then exceptions (`ignore-previous-rules`), then the
"first-party documents pass" rule; identical rules are dropped. WebKit refuses a filter of more than 150 000 rules,
so blocks are shared out over several filters and **each filter repeats every exception** (an exception only
reaches rules of its own filter). JSON at `<cache>/adblock-engine.webkit<N>.json`, compiled by
`UserContentFilterStore` (`<cache>/content-filters`, id `nacre-adblock-<list fingerprint>-<N>`); a restart with the
same lists loads the compiled filter (ms). First compile of the seed lists: ~7–15 s inside WebKit, off the main
thread; pages loaded meanwhile aren't network-blocked (cosmetic hiding still runs). `Engine::set_content_filter`
installs a build only when all its filters are ready and drops stale builds.
- **Cosmetic rules** stay in the adblock-rust engine (now built cosmetic-only) and page scripts, as before.
- **Per-site pause / ad blocker off**: the view's filters are detached in `decide-policy` before a main-frame load
of a paused host (`attach_filters`), and re-checked on every settings change.
- **Coverage, seed lists (10 lists, 2026-11-13)**: 152 610 network rules, 151 179 converted (99.1 %) into 149 902
WebKit rules (1 filter) and 268 redirects. Left out:
| Reason | Rules | What it means |
|---|---|---|
| `NetworkGenerichideUnsupported` | 590 | `$generichide` (the cosmetic engine still honours it) |
| `FullRegexUnsupported` | 248 | `/regex/` rules beyond WebKit's regex dialect |
| `NetworkRemoveparamUnsupported` | 215 | `$removeparam` (Nacre's own tracking-param strip covers the common ones) |
| `RedirectRuleOnly` | 151 | `$redirect-rule=`: a stand-in only for what another rule blocks; the filter's block wins |
| `FromNotSupported` | 121 | `$from=` / `$to=` domain options |
| `NoSupportedNetworkOptions` | 77 | only types WebKit has no name for (`$ping`, `$websocket`, …) |
| `NetworkCspUnsupported` | 28 | `$csp=` header injection |
| `UnlessAndIfDomainTogetherUnsupported` | 1 | `$domain=a.com\|~b.a.com` |
- **Redirect rules** (`$redirect=<name>`): when Nacre has the stand-in (`adblock::resource`: noopjs, noop.txt,
noop.css, noopjson, noopframe, transparent images) the rule goes to the extension, which rewrites the request to
`nacre-resource:<name>` (URI scheme served by the browser, registered secure + CORS); stand-ins Nacre lacks
(google-ima.js, media files, …) become plain blocks, as before with Servo. Limits: an exception (`@@`) doesn't stop
an extension redirect; the extension can't see the resource type; third-party is "last two host labels differ".
- **Web process extension** (`webext/`, `libnacre_webext.so`, ~0.8 MB, minimal FFI — no maintained Rust bindings
for WebKit's web process extension API exist): config JSON (`net::rules::Config`: headers, strip, ad blocker
on, paused hosts, redirects) as initialization user data and `nacre-config` user messages. `send-request` sets
DNT/Sec-GPC/Save-Data, applies redirects, strips tracking parameters from sub-resources (`net::rules`, the one
copy, shared with the browser by `#[path]`). Loaded from `NACRE_WEBEXT_DIR`, else `<exe>/../lib/nacre` (packages:
`$APPDIR/usr/lib/nacre`), else the exe's folder (cargo builds it next to `target/release/nacre`).
- **Blocked counts**: a content-filter block never reaches `send-request` or the UI process. WebKit logs each one to
the page console ("Content blocker prevented frame displaying <page> from loading a resource from <url>"); the
extension's `console-message-sent` handler sends a `nacre-blocked` message to the page's view, and the UI adds it
to `Hub::blocked` (reset in the `load-changed` Started handler). Limits: depends on WebKit's console wording;
WebKit may not log blocks for every load type (checked: script, image, fetch, module, async/defer, dynamic import);
a block logged just after a new page starts may count for the new page.