@@ -53,6 +53,18 @@ class AppWindow {
5353
5454 wil::com_ptr<ICoreWebView2Controller > m_controller;
5555 EventRegistrationToken m_browserExitedEventToken = {};
56+ UINT32 m_newestBrowserPid = 0;
57+ }
58+
59+ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICoreWebView2Controller* controller)
60+ {
61+ // ...
62+
63+ // Save PID of the browser process serving last WebView created from our
64+ // CoreWebView2Environment.
65+ CHECK_FAILURE (m_webView->get_BrowserProcessId(&m_newestBrowserPid));
66+
67+ // ...
5668}
5769
5870void AppWindow::CloseWebView(/* ... * /) {
@@ -66,19 +78,33 @@ void AppWindow::CloseWebView(/* ... */) {
6678 ICoreWebView2Environment* sender,
6779 ICoreWebView2BrowserProcessExitedEventArgs* args) {
6880 COREWEBVIEW2_BROWSER_PROCESS_EXIT_KIND kind;
81+ UINT32 pid;
6982 CHECK_FAILURE(args->get_BrowserProcessExitKind(&kind));
70-
71- // Watch for graceful browser process exit. Let ProcessFailed event
72- // handler take care of failed browser process termination.
73- if (kind == COREWEBVIEW2_BROWSER_PROCESS_EXIT_KIND_NORMAL)
83+ CHECK_FAILURE(args->get_BrowserProcessId(&pid));
84+
85+ // If a new WebView is created from this CoreWebView2Environment after
86+ // the browser has exited but before our handler gets to run, a new
87+ // browser process will be created and lock the user data folder
88+ // again. Do not attempt to cleanup the user data folder in these
89+ // cases. We check the PID of the exited browser process against the
90+ // PID of the browser process to which our last CoreWebView2 attached.
91+ if (pid == m_newestBrowserPid)
7492 {
75- CHECK_FAILURE(
76- m_webViewEnvironment->remove_BrowserProcessExited(m_browserExitedEventToken));
77- // Release the environment only after the handler is invoked.
78- // Otherwise, there will be no environment to raise the event when
79- // the collection of WebView2 Runtime processes exit.
80- m_webViewEnvironment = nullptr;
81- CleanupUserDataFolder();
93+ // Watch for graceful browser process exit. Let ProcessFailed event
94+ // handler take care of failed browser process termination.
95+ if (kind == COREWEBVIEW2_BROWSER_PROCESS_EXIT_KIND_NORMAL)
96+ {
97+ CHECK_FAILURE(
98+ m_webViewEnvironment->remove_BrowserProcessExited(m_browserExitedEventToken));
99+ // Release the environment only after the handler is invoked.
100+ // Otherwise, there will be no environment to raise the event when
101+ // the collection of WebView2 Runtime processes exit.
102+ m_webViewEnvironment = nullptr;
103+ CleanupUserDataFolder();
104+ }
105+ } else {
106+ MessageBox(m_mainWindow, L"A new browser process prevented cleanup of "
107+ L"the user data folder.", L"Cleanup User Data Folder", MB_OK);
82108 }
83109
84110 return S_OK;
@@ -324,6 +350,8 @@ interface ICoreWebView2BrowserProcessExitedEventArgs : IUnknown
324350 /// The kind of browser process exit that has occurred.
325351 [ propget] HRESULT BrowserProcessExitKind(
326352 [ out, retval] COREWEBVIEW2_BROWSER_PROCESS_EXIT_KIND* browserProcessExitKind);
353+ /// The process ID of the browser process that has exited.
354+ [ propget] HRESULT BrowserProcessId([ out, retval] UINT32* value);
327355}
328356```
329357
@@ -387,6 +415,9 @@ namespace Microsoft.Web.WebView2.Core
387415 {
388416 /// The kind of browser process exit that has occurred.
389417 CoreWebView2BrowserProcessExitKind BrowserProcessExitKind { get ; };
418+
419+ /// The process ID of the browser process that has exited.
420+ UInt32 BrowserProcessId { get ; };
390421 }
391422}
392423```
0 commit comments