Tyrrrz / YoutubeDownloader

Downloads videos and playlists from YouTube
MIT License
8.71k stars 1.19k forks source link

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

Closed aluhrs13 closed 1 year ago

aluhrs13 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

aluhrs13 commented 1 year ago

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

Danielx64 commented 1 year ago

@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 commented 1 year ago

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.

https://github.com/MicrosoftEdge/WebView2Feedback/issues/3637