S050 (WIP): Google UA quirk past WebKit's site quirks, navigation policy only for the tab's own loads, DocumentBrowser cache model (D-040), privacy-mode on WebKit

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-13 19:42:07 -06:00
commit e12d9ee9af

View file

@ -69,6 +69,27 @@ fn webext_dir() -> Option<std::path::PathBuf> {
}
/// Attach or detach the ad blocker filter for a view about to show a page of `host`; the Images filter always.
/// A navigation found to be the tab's own, for the browser to decide.
struct Navigation {
web: webkit::WebView,
hub: Rc<Hub>,
filters: Rc<RefCell<Vec<webkit::UserContentFilter>>>,
images: Rc<RefCell<Option<webkit::UserContentFilter>>>,
id: ViewId,
url: Url,
redirect: bool,
reloadable: bool,
decision: webkit::PolicyDecision,
}
impl Navigation {
fn decide(self) {
// The filter is set for the page's site before it loads (paused sites go without).
attach_filters(&self.web, &self.hub, &self.filters, &self.images, self.url.host_str().unwrap_or(""));
self.hub.push(Event::Navigation(self.id, NavigationRequest::new(self.url, self.redirect, self.reloadable, self.decision)));
}
}
fn attach_filters(web: &webkit::WebView, hub: &Hub, filters: &RefCell<Vec<webkit::UserContentFilter>>, images: &RefCell<Option<webkit::UserContentFilter>>, host: &str) {
let Some(ucm) = web.user_content_manager() else { return };
ucm.remove_all_filters();
@ -130,8 +151,9 @@ impl Engine {
}
context.set_cache_model(match env("NACRE_WK_CACHE_MODEL").as_deref() {
Some("browser") => webkit::CacheModel::WebBrowser,
Some("docbrowser") => webkit::CacheModel::DocumentBrowser,
_ => webkit::CacheModel::DocumentViewer,
Some("viewer") => webkit::CacheModel::DocumentViewer,
// DocumentViewer has no HTTP disk cache (D-040).
_ => webkit::CacheModel::DocumentBrowser,
});
let settings = webkit::Settings::new();
settings.set_enable_developer_extras(setup.developer_extras || env("NACRE_WK_INSPECTOR").is_some());
@ -377,7 +399,7 @@ impl Engine {
settings.set_user_agent(Some(ua));
return changed;
}
// A copy of the shared settings with only the user agent changed.
// A copy of the shared settings with only the user agent (and its quirks) changed.
let copy = webkit::Settings::new();
for spec in self.settings.list_properties().iter() {
let flags = spec.flags();
@ -385,6 +407,8 @@ impl Engine {
copy.set_property_from_value(spec.name(), &self.settings.property_value(spec.name()));
}
}
// WebKit's own site quirks put a Safari user agent on Google's requests over any set one.
copy.set_enable_site_specific_quirks(false);
copy.set_user_agent(Some(ua));
view.web.set_settings(&copy);
},
@ -510,7 +534,10 @@ impl Engine {
let hub = self.hub.clone();
web.connect_favicon_notify(move |_| hub.push(Event::Favicon(id)));
let hub = self.hub.clone();
let provisional = Rc::new(std::cell::Cell::new(false));
let tab_loading = provisional.clone();
web.connect_load_changed(move |w, event| {
tab_loading.set(matches!(event, webkit::LoadEvent::Started | webkit::LoadEvent::Redirected));
let state = match event {
webkit::LoadEvent::Started => LoadState::Started,
webkit::LoadEvent::Committed => LoadState::Committed,
@ -591,12 +618,37 @@ impl Engine {
if action.frame_name().is_some() {
return false;
}
let Some(url) = action.request().and_then(|r| r.uri()).and_then(|u| Url::parse(&u).ok()) else { return false };
// The filter is set for the page's site before it loads (paused sites go without).
attach_filters(web, &hub, &filters, &images, url.host_str().unwrap_or(""));
let get = action.request().and_then(|r| r.http_method()).is_none_or(|m| m.eq_ignore_ascii_case("GET"));
let Some(request) = action.request() else { return false };
let Some(uri) = request.uri() else { return false };
let Ok(url) = Url::parse(&uri) else { return false };
let get = request.http_method().is_none_or(|m| m.eq_ignore_ascii_case("GET"));
let reloadable = get && action.navigation_type() != webkit::NavigationType::BackForward;
hub.push(Event::Navigation(id, NavigationRequest::new(url, action.is_redirect(), reloadable, decision.clone())));
let redirect = action.is_redirect();
let tab = Navigation { web: web.clone(), hub: hub.clone(), filters: filters.clone(), images: images.clone(), id, url, redirect, reloadable, decision: decision.clone() };
// WebKit's GTK API doesn't say which frame a navigation is for, and the browser's decisions (HTTPS-only,
// the strip, the user agent reload) load the new URL in the tab: only the tab's own loads go to it.
// The browser's own loads come without headers; a redirect is the tab's while its load is provisional
// (frames load after the page commits); anything else is a frame's when a frame element has that
// URL. Not caught: a link followed inside a frame.
let from_browser = request.http_headers().is_none_or(|h| {
let mut any = false;
h.foreach(|_, _| any = true);
!any
});
if from_browser {
tab.decide();
} else if redirect {
if !provisional.get() {
return false;
}
tab.decide();
} else {
let script = format!("(() => {{ const u = {}; const has = d => [...d.querySelectorAll('iframe, frame')].some(f => {{ if (f.src === u) return true; try {{ return !!f.contentDocument && has(f.contentDocument); }} catch (e) {{ return false; }} }}); return has(document); }})()", serde_json::to_string(uri.as_str()).unwrap_or_default());
View::new(id, web.clone()).evaluate_javascript(script, move |frame| match frame {
Ok(serde_json::Value::Bool(true)) => tab.decision.use_(),
_ => tab.decide(),
});
}
true
},
webkit::PolicyDecisionType::Response => {