clearAll static method

Future<void> clearAll({
  1. String userDataFolderWindows = 'webview_window_WebView2',
})

Clear all cookies and storage.

Implementation

static Future<void> clearAll({
  String userDataFolderWindows = 'webview_window_WebView2',
}) async {
  await _channel.invokeMethod('clearAll');

  // FIXME(boyan01) Move the logic to windows platform if WebView2 provider a way to clean caches.
  // https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/user-data-folder#create-user-data-folders
  if (Platform.isWindows) {
    final Directory webview2Dir;
    if (p.isAbsolute(userDataFolderWindows)) {
      webview2Dir = Directory(userDataFolderWindows);
    } else {
      webview2Dir = Directory(p.join(
          p.dirname(Platform.resolvedExecutable), userDataFolderWindows));
    }

    if (await (webview2Dir.exists())) {
      for (var i = 0; i <= 4; i++) {
        try {
          await webview2Dir.delete(recursive: true);
          break;
        } catch (e) {
          debugPrint("delete cache failed. retring.... $e");
        }
        // wait to ensure all web window has been closed and file handle has been release.
        await Future.delayed(const Duration(seconds: 1));
      }
    }
  }
}