Skip to content
8 changes: 8 additions & 0 deletions .changes/webcontext-cache-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wry": minor
---

Breaking change: `WebContext::new` now takes a `cache_directory` argument besides `data_directory`.

This change allows users to specify a custom cache directory for the web context,
instead of using the default one [from WebKit2](https://webkitgtk.org/reference/webkit2gtk/stable/property.WebsiteDataManager.base-cache-directory.html).
18 changes: 15 additions & 3 deletions src/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::{
/// [`WebView`]: crate::WebView
#[derive(Debug)]
pub struct WebContext {
cache_directory: Option<PathBuf>,
data_directory: Option<PathBuf>,
#[allow(dead_code)] // It's not needed on Windows and macOS.
pub(crate) os: WebContextImpl,
Expand All @@ -33,12 +34,17 @@ pub struct WebContext {
impl WebContext {
/// Create a new [`WebContext`].
///
/// `cache_directory`:
/// * Whether the WebView window should have a custom cache path. This is useful in Windows
/// when a bundled application can't have the webview cache inside `Program Files`.
///
/// `data_directory`:
/// * Whether the WebView window should have a custom user data path. This is useful in Windows
/// when a bundled application can't have the webview data inside `Program Files`.
pub fn new(data_directory: Option<PathBuf>) -> Self {
pub fn new(cache_directory: Option<PathBuf>, data_directory: Option<PathBuf>) -> Self {
Self {
os: WebContextImpl::new(data_directory.as_deref()),
os: WebContextImpl::new(cache_directory.as_deref(), data_directory.as_deref()),
cache_directory,
data_directory,
custom_protocols: Default::default(),
}
Expand All @@ -47,12 +53,18 @@ impl WebContext {
#[cfg(gtk)]
pub(crate) fn new_ephemeral() -> Self {
Self {
cache_directory: None,
os: WebContextImpl::new_ephemeral(),
data_directory: None,
custom_protocols: Default::default(),
}
}

/// A reference to the cache directory the context was created with.
pub fn cache_directory(&self) -> Option<&Path> {
self.cache_directory.as_deref()
}

/// A reference to the data directory the context was created with.
pub fn data_directory(&self) -> Option<&Path> {
self.data_directory.as_deref()
Expand Down Expand Up @@ -83,7 +95,7 @@ impl WebContext {

impl Default for WebContext {
fn default() -> Self {
Self::new(None)
Self::new(None, None)
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/webkitgtk/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,29 @@ pub struct WebContextImpl {
}

impl WebContextImpl {
pub fn new(data_directory: Option<&Path>) -> Self {
pub fn new(cache_directory: Option<&Path>, data_directory: Option<&Path>) -> Self {
use webkit2gtk::{CookieManagerExt, WebsiteDataManager, WebsiteDataManagerExt};

let mut context_builder = WebContext::builder();
if let Some(data_directory) = data_directory {
let data_manager = WebsiteDataManager::builder()
.base_data_directory(data_directory.to_string_lossy())
.build();
if let Some(cookie_manager) = data_manager.cookie_manager() {

if cache_directory.is_some() || data_directory.is_some() {
let mut data_manager_builder = WebsiteDataManager::builder();

if let Some(cache_directory) = cache_directory {
data_manager_builder =
data_manager_builder.base_cache_directory(cache_directory.to_string_lossy());
}

if let Some(data_directory) = data_directory {
data_manager_builder =
data_manager_builder.base_data_directory(data_directory.to_string_lossy());
}

let data_manager = data_manager_builder.build();

if let (Some(data_directory), Some(cookie_manager)) =
(data_directory, data_manager.cookie_manager())
{
cookie_manager.set_persistent_storage(
&data_directory.join("cookies").to_string_lossy(),
CookiePersistentStorage::Text,
Expand Down