@@ -171,35 +171,48 @@ void ScenarioCookieManagement::DeleteAllCookies()
171171``` cpp
172172HRESULT AppWindow::DeleteProfile (ICoreWebView2* webView2)
173173{
174- DCHECK(webView2);
175- wil::com_ptr<ICoreWebView2Profile > profile;
176- CHECK_FAILURE(webView2->get_Profile(&profile));
177- CHECK_FAILURE(profile2->Delete());
174+ auto webview13 = wil::try_com_query<ICoreWebView2_13>(webview2);
175+ if (webview13)
176+ {
177+ wil::com_ptr<ICoreWebView2Profile > profile;
178+ CHECK_FAILURE(webview13->get_Profile(&profile));
179+ CHECK_FAILURE(profile->Delete());
180+ }
178181}
179182
180183void AppWindow::RegisterProfileDeletedEventHandlers(ICoreWebView2* webView2)
181184{
182185 wil::com_ptr<ICoreWebView2Profile > profile;
183186 CHECK_FAILURE(webView2->get_Profile(&profile));
184- if (profile)
187+ CHECK_FAILURE(profile->add_Deleted(
188+ Microsoft::WRL::Callback<ICoreWebView2StagingProfileDeletedEventHandler >(
189+ [ this] (ICoreWebView2Profile* sender, IUnknown* args)
190+ {
191+ RunAsync(
192+ [ this] ( )
193+ {
194+ std::wstring message = L"The profile has been marked"
195+ "for deletion. Any associated webview2 objects has"
196+ " been closed.";
197+ MessageBox(
198+ m_mainWindow, message.c_str(), L"webview2 closed",
199+ MB_OK);
200+ CloseAppWindow();
201+ });
202+ return S_OK;
203+ }).Get(), nullptr));
204+ }
205+
206+ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(
207+ HRESULT result, ICoreWebView2Controller* controller)
208+ {
209+ if (result == HRESULT_FROM_WIN32(ERROR_DELETE_PENDING))
185210 {
186- CHECK_FAILURE(profile->add_Deleted(
187- Microsoft::WRL::Callback<ICoreWebView2StagingProfileDeletedEventHandler >(
188- [ this] (ICoreWebView2Profile* sender, IUnknown* args)
189- {
190- RunAsync(
191- [ this] ( )
192- {
193- std::wstring message = L"The webview2 has been closed and "
194- L"the reason is profile "
195- L"has been marked as deleted.";
196- MessageBox(
197- m_mainWindow, message.c_str(), L"webview2 closed",
198- MB_OK);
199- CloseAppWindow();
200- });
201- return S_OK;
202- }).Get(), nullptr));
211+ ShowFailure(
212+ result, L"Failed to create webview, because the profile's name has "
213+ "been marked as deleted, please use a different profile's name");
214+ m_webviewOption.PopupDialog(this);
215+ CloseAppWindow();
203216 }
204217}
205218```
@@ -275,17 +288,38 @@ public DeleteProfile(CoreWebView2 coreWebView2)
275288void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
276289{
277290 WebViewProfile .Deleted += WebViewProfile_Deleted ;
291+
292+ // ...
293+ // ERROR_DELETE_PENDING(0x8007012f)
294+ if (e .InitializationException .HResult == - 2147024593 )
295+ {
296+ MessageBox .Show ($" Failed to create webview, because the profile's name has been marked as deleted, please use a different profile's name." );
297+ var dialog = new NewWindowOptionsDialog ();
298+ dialog .CreationProperties = webView .CreationProperties ;
299+ if (dialog .ShowDialog () == true )
300+ {
301+ new MainWindow (dialog .CreationProperties ).Show ();
302+ }
303+ Close ();
304+ return ;
305+ }
278306}
279307
280308private void WebViewProfile_Deleted (object sender , object e )
281309{
282310 this .Dispatcher .InvokeAsync (() =>
283311 {
284- String message = " The webview2 has been closed and the reason is profile has been marked as deleted ." ;
312+ String message = " The profile has been marked for deletion. Any associated webview2 objects will be closed ." ;
285313 MessageBox .Show (message );
286314 Close ();
287315 });
288316}
317+
318+ void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
319+ {
320+
321+ }
322+
289323```
290324
291325# API Details
@@ -399,20 +433,20 @@ interface ICoreWebView2StagingProfile7 : IUnknown {
399433 /// After the API is called, the profile will be marked for deletion. The
400434 /// local profile's directory will be deleted at browser process exit. If it
401435 /// fails to delete, because something else is holding the files open,
402- /// WebView2 will try to delete again during all future at next browser
403- /// process starts until successful.
436+ /// WebView2 will try to delete the profile at all future browser process
437+ /// starts until successful.
404438 /// The corresponding CoreWebView2s will be closed and the
405439 /// CoreWebView2Profile.Deleted event will be raised. See
406440 /// `CoreWebView2Profile.Deleted` for more information.
407441 /// If you try to create a new profile with the same name as an existing
408442 /// profile that has been marked as deleted but hasn't yet been deleted,
409- /// profile creation will fail with HRESULT_FROM_WIN32(ERROR_INVALID_STATE ).
443+ /// profile creation will fail with HRESULT_FROM_WIN32(ERROR_DELETE_PENDING ).
410444 HRESULT Delete();
411445
412446 /// Add an event handler for the `Deleted` event. The `Deleted` event is
413447 /// raised when the profile is marked for deletion. When this event is
414- /// raised, the CoreWebView2Profile and its corresponding CoreWebView2s are
415- /// closed, and cannot be used anymore.
448+ /// raised, the CoreWebView2Profile and its corresponding CoreWebView2s have
449+ /// been closed, and cannot be used anymore.
416450 HRESULT add_Deleted(
417451 [in] ICoreWebView2StagingProfileDeletedEventHandler* eventHandler,
418452 [out] EventRegistrationToken* token);
0 commit comments