[WebView] How to clear all cached data?
ppaneksamsung opened this issue · comments
I develop a Xamarin.Forms application for Tizen TV (6.5), which uses Xamarin.Forms's WebView based on Tizen.WebView. I noticed that some data (cookies, storage data) is cached by the browser for consecutive WebView's launches. I need to clear all cached data in order to make sure that my app uses a 'clear' WebView every time it's launched.
I tried to call following API:
WebView.GetContext().ClearResourceCache();
WebView.GetContext().GetCookieManager().ClearCookies();
but this doesn't work.
Additionally, I tried to manually clear 'chromium-efl' storage:
var dataPath = Application.Current.DirectoryInfo.Data;
Directory.Delete(Path.Combine(dataPath, "chromium-efl"), true);
and it works partially, because cached data is removed, but I have to restart my app. It seems that WebView keeps data in memory.
Finally, I tried to call:
Tizen.WebView.Chromium.Shutdown();
but for some reason, it causes my app to hang.
Questions:
- How to clear all WebView's cached data?
- How to properly dispose WebView? WebViewRenderer from Xamarin.Forms only calls
StopLoading
, see https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs#L39
For my particular case, calling ewk_context_web_storage_delete_all
fixes the issue. I used following extension method:
internal static partial class Interop
{
private static class Libraries
{
internal const string ChromiumEwk = "libchromium-ewk.so";
}
internal static partial class ChromiumEwk
{
[DllImport(Libraries.ChromiumEwk)]
internal static extern bool ewk_context_web_storage_delete_all(IntPtr context);
}
}
public static class ContextExtensions
{
private static IntPtr GetHandle(this Context context)
{
var contextType = context.GetType();
var handleField = contextType.GetField(
"_handle",
BindingFlags.Instance | BindingFlags.NonPublic);
if (handleField == null)
throw new NotSupportedException("Context doesn't have a _handle field");
if (handleField.FieldType != typeof(IntPtr))
throw new NotSupportedException(
$"Unexpected field type. Expected {typeof(IntPtr)}, got {handleField.FieldType}");
return (IntPtr) handleField.GetValue(context);
}
public static bool DeleteWebStorage(this Context context)
{
var handle = context.GetHandle();
return Interop.ChromiumEwk.ewk_context_web_storage_delete_all(handle);
}
}
Consider to expose this CAPI. Another possible solution for my problem would be to launch a WebView in incognito mode, but I'm not sure if it's exposed now.
The problem seems to be a little bit more complex. It looks like Tizen.WebView
was designed to be used as a 'singleton'. I use the AddJavaScriptMessageHandler
method (https://github.com/Samsung/TizenFX/blob/master/src/Tizen.WebView/Tizen.WebView/WebView.cs#L432) and this method adds a handler
to static _javaScriptMessageHandlerMap
dictionary. I don't see any option to remove already registered JS message handler. This is a problem in following case:
- create 1st WebView
- register JS message handler with some name
- destroy 1st WebView
- create 2nd WebView
- register JS message handler with the same name as for 1st WebView
Second AddJavaScriptMessageHandler call fails due to:
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days