Skip to content

Commit

Permalink
feat: macOS: add wry::fetch_all_data_store_identifiers and `wry::re…
Browse files Browse the repository at this point in the history
…move_data_store`
  • Loading branch information
Simon-Laux committed Mar 7, 2025
1 parent 43d1fad commit 352fa40
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/fetch_all_and_remove_data_store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

feat: macOS: add `wry::fetch_all_data_store_identifiers` and `wry::remove_data_store`
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ pub enum Error {
#[error(transparent)]
#[cfg(any(target_os = "macos", target_os = "ios"))]
UrlPrase(#[from] url::ParseError),
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[error(transparent)]
ObjcNS(#[from] objc2_foundation::NSError),
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,9 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> {
}
}

#[cfg(any(target_os = "macos", target_os = "ios",))]
pub use wkwebview::data_store::{fetch_all_data_store_identifiers, remove_data_store};

#[cfg(windows)]
#[derive(Clone)]
pub(crate) struct PlatformSpecificWebViewAttributes {
Expand Down
57 changes: 57 additions & 0 deletions src/wkwebview/data_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::ptr::NonNull;

use block2::RcBlock;
use objc2::MainThreadMarker;
use objc2_foundation::{NSArray, NSError, NSUUID};
use objc2_web_kit::WKWebsiteDataStore;

use crate::Error;

/// Fetches all Data Store Identifiers of this application
///
/// Needs to run on main thread and needs an event loop to run.
pub fn fetch_all_data_store_identifiers(
cb: impl Fn(Vec<[u8; 16]>) + Send + 'static,
) -> Result<(), Error> {
let block = RcBlock::new(move |stores: NonNull<NSArray<NSUUID>>| {
let uuid_list = unsafe { stores.as_ref() }
.to_vec()
.iter()
.map(|uuid| uuid.as_bytes())
.collect();
cb(uuid_list);
});

match MainThreadMarker::new() {
Some(mtn) => unsafe {
WKWebsiteDataStore::fetchAllDataStoreIdentifiers(&block, mtn);
Ok(())
},
None => Err(Error::NotMainThread),
}
}

/// Deletes a Data Store by Identifiers
///
/// Needs to run on main thread and needs an event loop to run.
pub fn remove_data_store(
uuid: &[u8; 16],
cb: impl Fn(Result<(), Error>) + Send + 'static,
) -> Result<(), Error> {
let mtm = MainThreadMarker::new().ok_or(Error::NotMainThread)?;
let identifier = NSUUID::from_bytes(uuid.to_owned());

let block = RcBlock::new(move |error: *mut NSError| {
if error.is_null() {
cb(Ok(()))
} else {
cb(Err(unsafe { error.read() }.into()));
}
});

unsafe {
WKWebsiteDataStore::removeDataStoreForIdentifier_completionHandler(&identifier, &block, mtm);
}

Ok(())
}
3 changes: 3 additions & 0 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#[cfg(target_os = "macos")]
pub(crate) mod data_store;

mod download;
#[cfg(target_os = "macos")]
mod drag_drop;
Expand Down

0 comments on commit 352fa40

Please # to comment.