feat: add webview.clear_all_browsing_data (#11066)

* feat: add `webview.clear_all_browsing_data`

closes #6567

* fix build on iOS and android

* fix command name references

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr Bashir 2024-09-20 14:18:07 +03:00 committed by GitHub
parent 0ddfc59d67
commit 9014a3f176
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 96 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
"@tauri-apps/api": "patch:feat"
---
Add `WebviewWindow.clearAllBrowsingData` and `Webview.clearAllBrowsingData` to clear the webview browsing data.

View File

@ -0,0 +1,8 @@
---
"tauri": "patch:feat"
"tauri-runtime": "patch:feat"
"tauri-runtime-wry": "patch:feat"
---
Add `WebviewWindow::clear_all_browsing_data` and `Webview::clear_all_browsing_data` to clear the webview browsing data.

View File

@ -1239,6 +1239,7 @@ pub enum WebviewMessage {
Reparent(WindowId, Sender<Result<()>>), Reparent(WindowId, Sender<Result<()>>),
SetAutoResize(bool), SetAutoResize(bool),
SetZoom(f64), SetZoom(f64),
ClearAllBrowsingData,
// Getters // Getters
Url(Sender<Result<String>>), Url(Sender<Result<String>>),
Bounds(Sender<Result<tauri_runtime::Rect>>), Bounds(Sender<Result<tauri_runtime::Rect>>),
@ -1516,6 +1517,17 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
), ),
) )
} }
fn clear_all_browsing_data(&self) -> Result<()> {
send_user_message(
&self.context,
Message::Webview(
*self.window_id.lock().unwrap(),
self.webview_id,
WebviewMessage::ClearAllBrowsingData,
),
)
}
} }
/// The Tauri [`WindowDispatch`] for [`Wry`]. /// The Tauri [`WindowDispatch`] for [`Wry`].
@ -3157,6 +3169,11 @@ fn handle_user_message<T: UserEvent>(
log::error!("failed to set webview zoom: {e}"); log::error!("failed to set webview zoom: {e}");
} }
} }
WebviewMessage::ClearAllBrowsingData => {
if let Err(e) = webview.clear_all_browsing_data() {
log::error!("failed to clear webview browsing data: {e}");
}
}
// Getters // Getters
WebviewMessage::Url(tx) => { WebviewMessage::Url(tx) => {
tx.send( tx.send(

View File

@ -512,6 +512,9 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
/// Set the webview zoom level /// Set the webview zoom level
fn set_zoom(&self, scale_factor: f64) -> Result<()>; fn set_zoom(&self, scale_factor: f64) -> Result<()>;
/// Clear all browsing data for this webview.
fn clear_all_browsing_data(&self) -> Result<()>;
} }
/// Window dispatcher. A thread-safe handle to the window APIs. /// Window dispatcher. A thread-safe handle to the window APIs.

View File

@ -127,6 +127,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("set_webview_zoom", false), ("set_webview_zoom", false),
("print", false), ("print", false),
("reparent", false), ("reparent", false),
("clear_all_browsing_data", false),
// internal // internal
("internal_toggle_devtools", true), ("internal_toggle_devtools", true),
], ],

View File

@ -16,6 +16,32 @@ Default permissions for the plugin.
</tr> </tr>
<tr>
<td>
`core:webview:allow-clear-all-browsing-data`
</td>
<td>
Enables the clear_all_browsing_data command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`core:webview:deny-clear-all-browsing-data`
</td>
<td>
Denies the clear_all_browsing_data command without any pre-configured scope.
</td>
</tr>
<tr> <tr>
<td> <td>

File diff suppressed because one or more lines are too long

View File

@ -572,6 +572,10 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
fn set_auto_resize(&self, auto_resize: bool) -> Result<()> { fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
Ok(()) Ok(())
} }
fn clear_all_browsing_data(&self) -> Result<()> {
Ok(())
}
} }
impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher { impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {

View File

@ -1472,6 +1472,15 @@ tauri::Builder::default()
.set_zoom(scale_factor) .set_zoom(scale_factor)
.map_err(Into::into) .map_err(Into::into)
} }
/// Clear all browsing data for this webview.
pub fn clear_all_browsing_data(&self) -> crate::Result<()> {
self
.webview
.dispatcher
.clear_all_browsing_data()
.map_err(Into::into)
}
} }
impl<R: Runtime> Listener<R> for Webview<R> { impl<R: Runtime> Listener<R> for Webview<R> {

View File

@ -177,6 +177,7 @@ mod desktop_commands {
setter!(set_webview_position, set_position, Position); setter!(set_webview_position, set_position, Position);
setter!(set_webview_focus, set_focus); setter!(set_webview_focus, set_focus);
setter!(set_webview_zoom, set_zoom, f64); setter!(set_webview_zoom, set_zoom, f64);
setter!(clear_all_browsing_data, clear_all_browsing_data);
#[command(root = "crate")] #[command(root = "crate")]
pub async fn reparent<R: Runtime>( pub async fn reparent<R: Runtime>(
@ -262,6 +263,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::set_webview_zoom, desktop_commands::set_webview_zoom,
desktop_commands::print, desktop_commands::print,
desktop_commands::reparent, desktop_commands::reparent,
desktop_commands::clear_all_browsing_data,
#[cfg(any(debug_assertions, feature = "devtools"))] #[cfg(any(debug_assertions, feature = "devtools"))]
desktop_commands::internal_toggle_devtools, desktop_commands::internal_toggle_devtools,
]); ]);

View File

@ -1782,6 +1782,11 @@ impl<R: Runtime> WebviewWindow<R> {
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> { pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
self.webview.set_zoom(scale_factor) self.webview.set_zoom(scale_factor)
} }
/// Clear all browsing data for this webview window.
pub fn clear_all_browsing_data(&self) -> crate::Result<()> {
self.webview.clear_all_browsing_data()
}
} }
impl<R: Runtime> Listener<R> for WebviewWindow<R> { impl<R: Runtime> Listener<R> for WebviewWindow<R> {

View File

@ -517,6 +517,20 @@ class Webview {
}) })
} }
/**
* Clears all browsing data for this webview.
* @example
* ```typescript
* import { getCurrentWebview } from '@tauri-apps/api/webview';
* await getCurrentWebview().clearAllBrowsingData();
* ```
*
* @returns A promise indicating the success or failure of the operation.
*/
async clearAllBrowsingData(): Promise<void> {
return invoke('plugin:webview|clear_all_browsing_data')
}
// Listeners // Listeners
/** /**