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

Attempt to clean-up WV2 data folder on window close #349

Closed
wants to merge 1 commit into from

Conversation

aluhrs13
Copy link

Taking a quick stab at the direction of cleaning up WV2 data. This will clear the browsing data when the user closes the auth window, trigger a dispose of the control, then delete the entire user data folder once the WV2 processes finish getting shutdown.

I'm not the best WPF developer, so I'm not sure doing this on Click is actually correct, but figured sharing something is better than nothing 😊. I was also able to hit some mild race condition that left the data folder by very quickly opening and closing the auth window. That's partially why there's the ClearBrowsingData call - the deleting of the UDF is "best effort" but ClearBrowsingData has a stronger guarantee that data will be deleted, just not all of it.

@Tyrrrz Tyrrrz added the enhancement New feature or request label Jul 13, 2023
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 13, 2023

Hey @aluhrs13, thanks a lot for the PR!

I tested it out, and it seems that WebView still leaves behind some files:

image

What I'm observing is that after closing the window for the first time, the user profile folder is deleted, but then next time you open and close it, at least some files in the folder remain. I don't think I'm opening and closing the window too quickly, as I wait around 3 seconds – and that time is consistently enough to clear the data on the first attempt.

@Danielx64
Copy link

Hey @Tyrrrz

It's Daniel here, I replied to your post on Twitter and your post on webview2 feedback tracker.

I have some code that does some cleaning up, it's along the same line as what the OP provided but a little more in-depth.

I'll need to do some testing to see if what I have helps it further.

Cheers

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 14, 2023

Hey @Tyrrrz

It's Daniel here, I replied to your post on Twitter and your post on webview2 feedback tracker.

I have some code that does some cleaning up, it's along the same line as what the OP provided but a little more in-depth.

I'll need to do some testing to see if what I have helps it further.

Cheers

Thanks, Daniel. Would be awesome.

@Danielx64
Copy link

Ok I'm posting the code that I have in my app when I am closing the browser. I haven't got to putting this in your app yet to see if it helps (I just got home) but will do so.

A suggestion on my side is that you create the folder in someone's appdata folder and not the same as the main program- to avoid permission issues if someone put this in their program files folder.

Here's the code that I have in my app:

		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();
		}

Application.Current.Shutdown(); can be removed so that the whole app doesn't shut down. Globals.USER_DATA_FOLDER is the path to where YoutubeDownloader.exe.WebView2 would be located.

@Tyrrrz
Copy link
Owner

Tyrrrz commented Jul 15, 2023

Ok I'm posting the code that I have in my app when I am closing the browser. I haven't got to putting this in your app yet to see if it helps (I just got home) but will do so.

A suggestion on my side is that you create the folder in someone's appdata folder and not the same as the main program- to avoid permission issues if someone put this in their program files folder.

Here's the code that I have in my app:

		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();
		}

Application.Current.Shutdown(); can be removed so that the whole app doesn't shut down. Globals.USER_DATA_FOLDER is the path to where YoutubeDownloader.exe.WebView2 would be located.

That seems more or less the same as what @aluhrs13 suggested, right?

@Danielx64
Copy link

Yes, it's pretty much the same, only difference is that I'm removing files and folders one by one and I also got logic where if the deleting failed for any reason, it won't crash the application (something that I ran into in my early days). Let's wait and see what the guys at webview2 say (if they can offer some work around in the interim).

@Tyrrrz Tyrrrz changed the title Attempt to clean-up WV2 data folder on window close. Attempt to clean-up WV2 data folder on window close Jul 15, 2023
@aluhrs13
Copy link
Author

@Danielx64 - With that logic is there a certain file the consistently doesn't get deleted?

@Danielx64
Copy link

@Danielx64 - With that logic is there a certain file the consistently doesn't get deleted?

Good question, I haven't really looked into that, the most I worry about was how big the entire folder is. I seen it go from say 50mb right down to 1 5 mb. In some cases (not always) the whole folder gets removed.

The delay I put in was designed to give it time before trying to delete files etc.

Mind you that's in my app and not this one.

@Tyrrrz Tyrrrz added invalid This doesn't seem right and removed enhancement New feature or request labels Aug 7, 2023
@Tyrrrz
Copy link
Owner

Tyrrrz commented Aug 7, 2023

Gonna close this for now as it doesn't work as well as I'd hoped (see my above comment). Hopefully, WebView2 will provide an out-of-the-box support for a fully in-memory execution mode.

MicrosoftEdge/WebView2Feedback#3637

@Tyrrrz Tyrrrz closed this Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants