Procházet zdrojové kódy

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>
Amr Bashir před 10 měsíci
rodič
revize
9014a3f176

+ 6 - 0
.changes/clear-all-browsing-data-api.md

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

+ 8 - 0
.changes/clear-all-browsing-data.md

@@ -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 - 0
crates/tauri-runtime-wry/src/lib.rs

@@ -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>>),
@@ -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`].
@@ -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(

+ 3 - 0
crates/tauri-runtime/src/lib.rs

@@ -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.

+ 1 - 0
crates/tauri/build.rs

@@ -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),
     ],

+ 26 - 0
crates/tauri/permissions/webview/autogenerated/reference.md

@@ -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>
 

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
crates/tauri/scripts/bundle.global.js


+ 4 - 0
crates/tauri/src/test/mock_runtime.rs

@@ -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 {

+ 9 - 0
crates/tauri/src/webview/mod.rs

@@ -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> {

+ 2 - 0
crates/tauri/src/webview/plugin.rs

@@ -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>(
@@ -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,
           ]);

+ 5 - 0
crates/tauri/src/webview/webview_window.rs

@@ -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> {

+ 14 - 0
packages/api/src/webview.ts

@@ -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
 
   /**

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů