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_all_browsing_data #11066

Merged
merged 3 commits into from
Sep 20, 2024
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
6 changes: 6 additions & 0 deletions .changes/clear-all-browsing-data-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tauri-apps/api": "patch:feat"
---

Add `WebviewWindow.clearAllBrowsingData` and `Webview.clearAllBrowsingData` to clear the webview browsing data.

8 changes: 8 additions & 0 deletions .changes/clear-all-browsing-data.md
Original file line number Diff line number Diff line change
@@ -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.

17 changes: 17 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ pub enum WebviewMessage {
Reparent(WindowId, Sender<Result<()>>),
SetAutoResize(bool),
SetZoom(f64),
ClearAllBrowsingData,
// Getters
Url(Sender<Result<String>>),
Bounds(Sender<Result<tauri_runtime::Rect>>),
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -3157,6 +3169,11 @@ fn handle_user_message<T: UserEvent>(
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
WebviewMessage::Url(tx) => {
tx.send(
Expand Down
3 changes: 3 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,9 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '

/// Set the webview zoom level
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.
Expand Down
1 change: 1 addition & 0 deletions crates/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
("set_webview_zoom", false),
("print", false),
("reparent", false),
("clear_all_browsing_data", false),
// internal
("internal_toggle_devtools", true),
],
Expand Down
26 changes: 26 additions & 0 deletions crates/tauri/permissions/webview/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ Default permissions for the plugin.
</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>
<td>

Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
Ok(())
}

fn clear_all_browsing_data(&self) -> Result<()> {
Ok(())
}
}

impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
Expand Down
9 changes: 9 additions & 0 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,15 @@ tauri::Builder::default()
.set_zoom(scale_factor)
.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> {
Expand Down
2 changes: 2 additions & 0 deletions crates/tauri/src/webview/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mod desktop_commands {
setter!(set_webview_position, set_position, Position);
setter!(set_webview_focus, set_focus);
setter!(set_webview_zoom, set_zoom, f64);
setter!(clear_all_browsing_data, clear_all_browsing_data);

#[command(root = "crate")]
pub async fn reparent<R: Runtime>(
Expand Down Expand Up @@ -262,6 +263,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
desktop_commands::set_webview_zoom,
desktop_commands::print,
desktop_commands::reparent,
desktop_commands::clear_all_browsing_data,
#[cfg(any(debug_assertions, feature = "devtools"))]
desktop_commands::internal_toggle_devtools,
]);
Expand Down
5 changes: 5 additions & 0 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,11 @@ impl<R: Runtime> WebviewWindow<R> {
pub fn set_zoom(&self, scale_factor: f64) -> crate::Result<()> {
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> {
Expand Down
14 changes: 14 additions & 0 deletions packages/api/src/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down