@@ -169,20 +169,50 @@ void ScenarioCookieManagement::DeleteAllCookies()
169169### Delete profile
170170
171171``` cpp
172- HRESULT AppWindow::DeleteProfile (ICoreWebView2Controller * controller )
172+ HRESULT AppWindow::DeleteProfile (ICoreWebView2 * webView2 )
173173{
174- wil::com_ptr<ICoreWebView2 > coreWebView2;
175- CHECK_FAILURE(controller->get_CoreWebView2(&coreWebView2));
176- auto webview7 = coreWebView2.try_query<ICoreWebView2_7>();
177- if (webview7)
174+ auto webview13 = wil::try_com_query<ICoreWebView2_13>(webview2);
175+ if (webview13)
178176 {
179177 wil::com_ptr<ICoreWebView2Profile > profile;
180- CHECK_FAILURE(webview7->get_Profile(&profile));
181- auto profile2 = profile.try_query<ICoreWebView2StagingProfile4 >();
182- if (profile2)
183- {
184- CHECK_FAILURE(profile2->Delete());
185- }
178+ CHECK_FAILURE(webview13->get_Profile(&profile));
179+ CHECK_FAILURE(profile->Delete());
180+ }
181+ }
182+
183+ void AppWindow::RegisterProfileDeletedEventHandlers(ICoreWebView2* webView2)
184+ {
185+ wil::com_ptr<ICoreWebView2Profile > profile;
186+ CHECK_FAILURE(webView2->get_Profile(&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))
210+ {
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();
186216 }
187217}
188218```
@@ -249,13 +279,40 @@ void DeleteAllCookies()
249279```
250280
251281``` csharp
252- public DeleteProfile (CoreWebView2Controller controller )
282+ public DeleteProfile (CoreWebView2 coreWebView2 )
253283{
254- // Get the profile object.
255- CoreWebView2Profile profile = controller .CoreWebView2 .Profile ;
256-
257284 // Delete current profile.
258- profile .Delete ();
285+ CoreWebView2 .Profile .Delete ();
286+ }
287+
288+ void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
289+ {
290+ 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 ())
300+ {
301+ new MainWindow (dialog .CreationProperties ).Show ();
302+ }
303+ Close ();
304+ return ;
305+ }
306+ }
307+
308+ private void WebViewProfile_Deleted (object sender , object e )
309+ {
310+ this .Dispatcher .InvokeAsync (() =>
311+ {
312+ String message = " The profile has been marked for deletion. Any associated webview2 objects will be closed." ;
313+ MessageBox .Show (message );
314+ Close ();
315+ });
259316}
260317```
261318
@@ -270,6 +327,8 @@ interface ICoreWebView2_7;
270327interface ICoreWebView2Profile;
271328interface ICoreWebView2Profile2;
272329interface ICoreWebView2Profile3;
330+ interface ICoreWebView2StagingProfile7;
331+ interface ICoreWebView2StagingProfileDeletedEventHandler;
273332
274333/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
275334[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -363,16 +422,42 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
363422 [propget] HRESULT CookieManager([out, retval] ICoreWebView2CookieManager** cookieManager);
364423}
365424
366- [uuid(1c1ae2cc-d5c2-ffe3-d3e7-7857035d23b7), object, pointer_default(unique)]
367- interface ICoreWebView2Profile3 : ICoreWebView2Profile2 {
368- /// All webviews on this profile will be closed, and the profile will be marked for deletion.
369- /// After the Delete() call completes, The render process of webviews on this profile will
370- /// asynchronously exit with the reason:`COREWEBVIEW2_PROCESS_FAILED_REASON_PROFILE_DELETED`.
371- /// See 'COREWEBVIEW2_PROCESS_FAILED_REASON::COREWEBVIEW2_PROCESS_FAILED_REASON_PROFILE_DELETED'
372- /// for more details. The profile directory on disk will be actually deleted when the browser
373- /// process exits. Webview2 creation will fail with the HRESULT is ERROR_INVALID_STATE(0x8007139FL)
374- /// if you create it with the same name as a profile that is being deleted.
375- HRESULT Delete();
425+ [uuid(2765B8BD-7C57-4B76-B8CC-1EC940FF92CC), object, pointer_default(unique)]
426+ interface ICoreWebView2StagingProfile7 : IUnknown {
427+ /// After the API is called, the profile will be marked for deletion. The
428+ /// local profile's directory will be deleted at browser process exit. If it
429+ /// fails to delete, because something else is holding the files open,
430+ /// WebView2 will try to delete the profile at all future browser process
431+ /// starts until successful.
432+ /// The corresponding CoreWebView2s will be closed and the
433+ /// CoreWebView2Profile.Deleted event will be raised. See
434+ /// `CoreWebView2Profile.Deleted` for more information.
435+ /// If you try to create a new profile with the same name as an existing
436+ /// profile that has been marked as deleted but hasn't yet been deleted,
437+ /// profile creation will fail with HRESULT_FROM_WIN32(ERROR_DELETE_PENDING).
438+ HRESULT Delete();
439+
440+ /// Add an event handler for the `Deleted` event. The `Deleted` event is
441+ /// raised when the profile is marked for deletion. When this event is
442+ /// raised, the CoreWebView2Profile and its corresponding CoreWebView2s have
443+ /// been closed, and cannot be used anymore.
444+ HRESULT add_Deleted(
445+ [in] ICoreWebView2StagingProfileDeletedEventHandler* eventHandler,
446+ [out] EventRegistrationToken* token);
447+
448+ /// Remove an event handler previously added with `add_Deleted`.
449+ HRESULT remove_Deleted(
450+ [in] EventRegistrationToken token);
451+ }
452+
453+ [uuid(e5dea648-79c9-4caa-8314-dd71de62ad49), object, pointer_default(unique)]
454+ interface ICoreWebView2StagingProfileDeletedEventHandler: IUnknown {
455+ /// Called to provide the implementer with the event args for the
456+ /// profile deleted event. No event args exist and the `args`
457+ /// parameter is set to `null`.
458+ HRESULT Invoke(
459+ [in] ICoreWebView2Profile* sender,
460+ [in] IUnknown* args);
376461}
377462```
378463
@@ -415,7 +500,7 @@ namespace Microsoft.Web.WebView2.Core
415500 // ...
416501 CoreWebView2Profile Profile { get ; };
417502 }
418-
503+
419504 runtimeclass CoreWebView2Profile
420505 {
421506 String ProfileName { get ; };
@@ -426,10 +511,10 @@ namespace Microsoft.Web.WebView2.Core
426511
427512 CoreWebView2CookieManager CookieManager { get ; };
428513
429- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile3 " )]
514+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile7 " )]
430515 {
431- // ICoreWebView2Profile3 members
432516 void Delete ();
517+ event Windows .Foundation .TypedEventHandler < CoreWebView2Profile , Object > Deleted ;
433518 }
434519 }
435520}
0 commit comments