Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Webview::clear_browsing_data #915

Merged
merged 8 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/clear-browsing-data-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

Add `Webview::clear_browsing_data` method.
12 changes: 12 additions & 0 deletions src/webview/android/kotlin/RustWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class RustWebView(context: Context): WebView(context) {
}
}

fun clearAllBrowsingData() {
try {
super.context.deleteDatabase("webviewCache.db");
super.context.deleteDatabase("webview.db");
super.clearCache(true);
super.clearHistory();
super.clearFormData();
} catch (ignore: Exception) {
Logger.error("Unable to create temporary media capture file: " + ex.message)
}
}

fun setAutoPlay(enable: Boolean) {
post {
val settings = super.getSettings()
Expand Down
6 changes: 6 additions & 0 deletions src/webview/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ impl MainPipe<'_> {
load_url(env, webview.as_obj(), url, headers, false)?;
}
}
WebViewMessage::ClearAllBrowsingData => {
if let Some(webview) = &self.webview {
env.call_method(webview, "clearAllBrowsingData", "()V", &[])?;
}
}
}
}
Ok(())
Expand Down Expand Up @@ -273,6 +278,7 @@ pub(crate) enum WebViewMessage {
GetUrl(Sender<String>),
Jni(Box<dyn FnOnce(JNIEnv, JObject, JObject) + Send>),
LoadUrl(String, Option<http::HeaderMap>),
ClearAllBrowsingData,
}

pub(crate) struct CreateWebViewAttributes {
Expand Down
5 changes: 5 additions & 0 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ impl InnerWebView {
pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string(), Some(headers)));
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
MainPipe::send(WebViewMessage::ClearAllBrowsingData);
Ok(())
}
}

#[derive(Clone, Copy)]
Expand Down
7 changes: 7 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,13 +926,20 @@ impl WebView {
self.webview.set_background_color(background_color)
}

/// Navigate to the specified url
pub fn load_url(&self, url: &str) {
self.webview.load_url(url)
}

/// Navigate to the specified url using the specified headers
pub fn load_url_with_headers(&self, url: &str, headers: http::HeaderMap) {
self.webview.load_url_with_headers(url, headers)
}

/// Clear all browsing data
pub fn clear_all_browsing_data(&self) -> Result<()> {
self.webview.clear_all_browsing_data()
}
}

/// An event enumeration sent to [`FileDropHandler`].
Expand Down
17 changes: 17 additions & 0 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,23 @@ impl InnerWebView {

self.webview.load_request(&req);
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
if let Some(context) = WebViewExt::context(&*self.webview) {
use webkit2gtk::WebContextExt;
if let Some(data_manger) = context.website_data_manager() {
webkit2gtk::WebsiteDataManagerExtManual::clear(
&data_manger,
webkit2gtk::WebsiteDataTypes::ALL,
glib::TimeSpan::from_seconds(0),
None::<&Cancellable>,
|_| {},
);
}
}

Ok(())
}
}

pub fn platform_webview_version() -> Result<String> {
Expand Down
16 changes: 16 additions & 0 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,22 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
load_url_with_headers(&self.webview, &self.env, url, headers);
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
let handler = ClearBrowsingDataCompletedHandler::create(Box::new(move |_| Ok(())));
unsafe {
self
.webview
.cast::<ICoreWebView2_13>()
.map_err(|e| Error::WebView2Error(webview2_com::Error::WindowsError(e)))?
.Profile()
.map_err(|e| Error::WebView2Error(webview2_com::Error::WindowsError(e)))?
.cast::<ICoreWebView2Profile2>()
.map_err(|e| Error::WebView2Error(webview2_com::Error::WindowsError(e)))?
.ClearBrowsingDataAll(&handler)
.map_err(|e| Error::WebView2Error(webview2_com::Error::WindowsError(e)))
}
}

pub fn set_theme(&self, theme: Theme) {
set_theme(&self.webview, theme);
}
Expand Down
12 changes: 12 additions & 0 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,18 @@ r#"Object.defineProperty(window, 'ipc', {
self.navigate_to_url(url, Some(headers))
}

pub fn clear_all_browsing_data(&self) -> Result<()> {
unsafe {
let config: id = msg_send![self.webview, configuration];
let store: id = msg_send![config, websiteDataStore];
let all_data_types: id = msg_send![class!(WKWebsiteDataStore), allWebsiteDataTypes];
let date: id = msg_send![class!(NSDate), dateWithTimeIntervalSince1970: 0.0];
let handler = block::ConcreteBlock::new(|| {});
let _: () = msg_send![store, removeDataOfTypes:all_data_types modifiedSince:date completionHandler:handler];
}
Ok(())
}

fn navigate_to_url(&self, url: &str, headers: Option<http::HeaderMap>) {
// Safety: objc runtime calls are unsafe
unsafe {
Expand Down