-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: macOS: add
wry::fetch_all_data_store_identifiers
and `wry::re…
…move_data_store`
- Loading branch information
1 parent
43d1fad
commit 352fa40
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters