In-memory or no-profile mode
Is your feature request related to a problem? Please describe.
Hello. This feature request is inspired by this Twitter thread: https://twitter.com/aluhrs13/status/1679145646706810882
I'm using WebView2 via its .NET/WPF wrapper in a desktop application. Its purpose there is very limited: I'm using it to a display a Google login page, have the user enter and confirm their credentials, and then extract the received cookies to use for further (non-webview) HTTP requests.
For context, this is used in https://github.com/Tyrrrz/YoutubeDownloader in order to download private playlists and videos. It's not possible to use OAuth or other "native" ways of authentication to achieve this, since downloading videos is not exactly a scenario supported by YouTube. So using an actual browser, as in WebView, is the only way to do it.
Because of my use case, I really don't need to persist any user data between sessions. Ideally, I want WV2 to be able to run completely in-memory and unload immediately as it becomes unneeded (i.e. when the user finishes logging in). Currently, this is not the case, as WebView2 leaves a noticeable footprint on disk, where it stores the user profile.
The closest thing to what I want appears to be the InPrivateMode switch, but even when using it, I found that WebView2 still creates a user profile and persists certain files on disk. The issue is that the user folder occupies a significant amount of space that seems to increase over time, and my limited usage scenario does not require it whatsoever.
Describe the solution you'd like and alternatives you've considered
I would propose a similar switch to InPrivateMode, except something like InMemoryMode. When it's enabled, WebView should store all user data in-memory and not persist anything on disk.
To my knowledge (and from my discussion with @aluhrs13), this does not appear to be an existing API.
Let me know if you need more context or any other information that may be useful.
@Tyrrrz Thanks for the detailed explanation of your scenario! I've added to our backlog for further evaluation.
That would be a nice feature. While my use case is very different, my use would be for a multi tabbed web browser where it is not needed to keep the user profile once the app is closed.
Currently I'm doing a manual clear and deleting the folder when the app is closing.
Also, to add some context, manual clean up of user profile is a bit clunky right now, at least using the .NET wrapper. The issue is that you can't delete the files while the browser engine is still running, and there appears to be no reliable way to gracefully shut it down.
@aluhrs13 made a PR to facilitate this cleanup in my project, but it's quite hacky and there seem to still be some problems with it: https://github.com/Tyrrrz/YoutubeDownloader/pull/349
I think the WebView2 API could improve on both of these fronts:
- Either provide an in-memory mode (which is what the OP is about)
- Or provide a clean way to shut down the browser engine, clear out user data, and then allow the browser engine to be started again if needed
The first approach would completely eliminate my issue altogether, while the second will make it more manageable.
Also, to add some context, manual clean up of user profile is a bit clunky right now, at least using the .NET wrapper. The issue is that you can't delete the files while the browser engine is still running, and there appears to be no reliable way to gracefully shut it down.
@aluhrs13 made a PR to facilitate this cleanup in my project, but it's quite hacky and there seem to still be some problems with it: Tyrrrz/YoutubeDownloader#349
I think the WebView2 API could improve on both of these fronts:
- Either provide an in-memory mode (which is what the OP is about)
- Or provide a clean way to shut down the browser engine, clear out user data, and then allow the browser engine to be started again if needed
The first approach would completely eliminate my issue altogether, while the second will make it more manageable.
The second option would be useful in several cases including where you don't want to clear out the profile, but still want to close off processes, for example you're building a multi tabbed browser, user close off tab, free up ram used by that tab etc. And if you want to clear the profile completely, you can still do that.
CEF has an in-memory cache, would be great to see the same for WebView2.
@novac42 have you got any timeline in when this will get put in? I just finished doing a major rewrite and due to how it's written it's no longer possible to use a supported method to shut down webview2 before clearing out the profile.
@Danielx64 may you help us understand whether this is a blocker issue for you and what's your expected timeline for this?
@Danielx64 may you help us understand whether this is a blocker issue for you and what's your expected timeline for this?
So, my use case is that I have a really dumb browser that has no address bar or anything like that (think Kiosk but with title bar). Right now, I have 2 different version, v1 and v2.
v1: single window, setup with AllowSingleSignOnUsingOSPrimaryAccount set to true, has custom profile directory and other settings set via "CoreWebView2.Settings.". In this version, everything I do is pretty much supported, I can clear out the profile folder with no issues with the below code:
private void CloseWindow()
{
WebView.CoreWebView2.CallDevToolsProtocolMethodAsync("Network.clearBrowserCache", "{}");
WebView.CoreWebView2.Profile.ClearBrowsingDataAsync();
WebView.Dispose();
var milliseconds = 100;
Thread.Sleep(milliseconds);
DirectoryInfo directory = new DirectoryInfo(Globals.USER_DATA_FOLDER);
foreach (FileInfo file in directory.EnumerateFiles())
{
try
{
file.Delete();
}
catch (IOException)
{
return;
}
catch (UnauthorizedAccessException)
{
return;
}
}
foreach (DirectoryInfo dir in directory.EnumerateDirectories())
{
try
{
dir.Delete(true);
}
catch (IOException)
{
return;
}
catch (UnauthorizedAccessException)
{
return;
}
}
Application.Current.Shutdown();
}
As you can see, I have something that let me close off webview2 and delete the profile folder. It may not be perfect but it does the job.
v2: This version is almost a complete rewrite of v1. Many of the functions I have in v1 remains, but the biggest difference is that I have introduced AdvancedTabControl from Actipro Software to give me chrome like tabs. Due to the way that things are written, each tab has it own instance of webview2 running (that would be expected anyways), and while I got the core of my app working, I haven't been able to figure out 2 things.
1- free up the RAM when tab is closed (maybe due to lack of c# skills as it not my regular thing) 2- do 1 and clear out the profile folder in a safe way when completely closing my app. While I have managed to come up with something, I don't feel that it safe....
private void CloseWindow()
{
foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower() == "msedgewebview2")
{
p.Kill();
}
}
var milliseconds = 100;
Thread.Sleep(milliseconds);
DirectoryInfo directory = new DirectoryInfo(Globals.USER_DATA_FOLDER);
foreach (FileInfo file in directory.EnumerateFiles())
{
try
{
file.Delete();
}
catch (IOException)
{
return;
}
catch (UnauthorizedAccessException)
{
return;
}
}
foreach (DirectoryInfo dir in directory.EnumerateDirectories())
{
try
{
dir.Delete(true);
}
catch (IOException)
{
return;
}
catch (UnauthorizedAccessException)
{
return;
}
}
Application.Current.Shutdown();
}
As you can see, I'm blindly closing off msedgewebview2 and that isn't safe.
For timeline, I don't have one yet, as I need to get approval to buy the WPF control, and I do have another option (clear the folder while launching the app) but I'm keeping my eyes open to other options.
So would solve most of my issues is having the profile loaded into ram, and then I don't need to clear out the profile folder while loading the app or being reckless and blindly closing msedgewebview2 before deleting the folder.
@Danielx64 - I'm not sure of the problems that the 3rd party tab control could be bringing to this. If the WV2s are still around when a tab is closed, that could be the problem.
I think the concrete expectation is that at some point after all the WV2s are closed and disposed of, the BrowserProcessExited event will fire and you should be able to safely delete the data folder. If you can share an example of where that doesn't occur we can take a look (probably make a separate bug).
For in-memory mode, this is fairly low on our backlog and we'll be keeping an eye on this GitHub issue for additional information to change that.
For in-memory mode, this is fairly low on our backlog and we'll be keeping an eye on this GitHub issue for additional information to change that.
Thank you for that, i'll see if I can get in touch with the vendor and see if they are able to offer any guidance.
In-memory mode would also be helpful to avoid EBWebView directory permission issues for applications that don't need a persisted profile a la https://communities.bentley.com/communities/other_communities/licensing_cloud_and_web_services/w/wiki/53329/microsoft-edge-can-t-read-and-write-to-its-data-directory
Are there any updates regarding this issue?