ltk_webkit/wpe/
privacy.rs

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
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Process-wide privacy settings: every [`crate::WebView`] shares one network session and one web context.

use std::ffi::{ c_char, CString };
use std::ptr;

use wpe_webkit_sys::*;

/// WebKit's Intelligent Tracking Prevention. Off by default, as in WebKit.
pub fn set_tracking_prevention( enabled: bool )
{
	// SAFETY: the session is created on first use and lives for the process.
	unsafe
	{
		let session = super::session::network_session();
		if !session.is_null() { webkit_network_session_set_itp_enabled( session, enabled as glib_sys::gboolean ); }
	}
}

/// Whether a page may set cookies for a site other than the one in the address bar.
pub fn set_third_party_cookies( allowed: bool )
{
	let policy = if allowed { WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS } else { WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY };
	// SAFETY: the cookie manager is owned by the session, which lives for the process.
	unsafe
	{
		let session = super::session::network_session();
		if session.is_null() { return; }
		let cookies = webkit_network_session_get_cookie_manager( session );
		if !cookies.is_null() { webkit_cookie_manager_set_accept_policy( cookies, policy ); }
	}
}

/// `Accept-Language` and `navigator.languages`, most preferred first; WebKit's default follows the process locale.
pub fn set_preferred_languages( languages: &[ &str ] )
{
	let owned: Vec<CString> = languages.iter().filter_map( |l| CString::new( *l ).ok() ).collect();
	if owned.is_empty() { return; }
	let mut list: Vec<*const c_char> = owned.iter().map( |l| l.as_ptr() ).collect();
	list.push( ptr::null() );
	// SAFETY: `list` is NULL-terminated and outlives the call; WebKit copies the strings.
	unsafe
	{
		let context = super::context::web_context();
		if !context.is_null() { webkit_web_context_set_preferred_languages( context, list.as_ptr() ); }
	}
}