@@ -169,20 +169,37 @@ 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+ DCHECK(webView2);
175+ wil::com_ptr<ICoreWebView2Profile > profile;
176+ CHECK_FAILURE(webView2->get_Profile(&profile));
177+ CHECK_FAILURE(profile2->Delete());
178+ }
179+
180+ void AppWindow::RegisterProfileDeletedEventHandlers(ICoreWebView2* webView2)
181+ {
182+ wil::com_ptr<ICoreWebView2Profile > profile;
183+ CHECK_FAILURE(webView2->get_Profile(&profile));
184+ if (profile)
178185 {
179- 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- }
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));
186203 }
187204}
188205```
@@ -249,13 +266,25 @@ void DeleteAllCookies()
249266```
250267
251268``` csharp
252- public DeleteProfile (CoreWebView2Controller controller )
269+ public DeleteProfile (CoreWebView2 coreWebView2 )
253270{
254- // Get the profile object.
255- CoreWebView2Profile profile = controller .CoreWebView2 .Profile ;
256-
257271 // Delete current profile.
258- profile .Delete ();
272+ CoreWebView2 .Profile .Delete ();
273+ }
274+
275+ void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
276+ {
277+ WebViewProfile .Deleted += WebViewProfile_Deleted ;
278+ }
279+
280+ private void WebViewProfile_Deleted (object sender , object e )
281+ {
282+ this .Dispatcher .InvokeAsync (() =>
283+ {
284+ String message = " The webview2 has been closed and the reason is profile has been marked as deleted." ;
285+ MessageBox .Show (message );
286+ Close ();
287+ });
259288}
260289```
261290
@@ -270,6 +299,8 @@ interface ICoreWebView2_7;
270299interface ICoreWebView2Profile;
271300interface ICoreWebView2Profile2;
272301interface ICoreWebView2Profile3;
302+ interface ICoreWebView2StagingProfile7;
303+ interface ICoreWebView2StagingProfileDeletedEventHandler;
273304
274305/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
275306[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -363,16 +394,42 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
363394 [propget] HRESULT CookieManager([out, retval] ICoreWebView2CookieManager** cookieManager);
364395}
365396
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();
397+ [uuid(2765B8BD-7C57-4B76-B8CC-1EC940FF92CC), object, pointer_default(unique)]
398+ interface ICoreWebView2StagingProfile7 : IUnknown {
399+ /// After the API is called, the profile will be marked for deletion. The
400+ /// local profile's directory will be deleted at browser process exit. If it
401+ /// 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.
404+ /// The corresponding CoreWebView2s will be closed and the
405+ /// CoreWebView2Profile.Deleted event will be raised. See
406+ /// `CoreWebView2Profile.Deleted` for more information.
407+ /// If you try to create a new profile with the same name as an existing
408+ /// 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).
410+ HRESULT Delete();
411+
412+ /// Add an event handler for the `Deleted` event. The `Deleted` event is
413+ /// 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.
416+ HRESULT add_Deleted(
417+ [in] ICoreWebView2StagingProfileDeletedEventHandler* eventHandler,
418+ [out] EventRegistrationToken* token);
419+
420+ /// Remove an event handler previously added with `add_Deleted`.
421+ HRESULT remove_Deleted(
422+ [in] EventRegistrationToken token);
423+ }
424+
425+ [uuid(e5dea648-79c9-4caa-8314-dd71de62ad49), object, pointer_default(unique)]
426+ interface ICoreWebView2StagingProfileDeletedEventHandler: IUnknown {
427+ /// Called to provide the implementer with the event args for the
428+ /// profile deleted event. No event args exist and the `args`
429+ /// parameter is set to `null`.
430+ HRESULT Invoke(
431+ [in] ICoreWebView2Profile* sender,
432+ [in] IUnknown* args);
376433}
377434```
378435
@@ -415,7 +472,7 @@ namespace Microsoft.Web.WebView2.Core
415472 // ...
416473 CoreWebView2Profile Profile { get ; };
417474 }
418-
475+
419476 runtimeclass CoreWebView2Profile
420477 {
421478 String ProfileName { get ; };
@@ -426,10 +483,10 @@ namespace Microsoft.Web.WebView2.Core
426483
427484 CoreWebView2CookieManager CookieManager { get ; };
428485
429- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile3 " )]
486+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile7 " )]
430487 {
431- // ICoreWebView2Profile3 members
432488 void Delete ();
489+ event Windows .Foundation .TypedEventHandler < CoreWebView2Profile , Object > Deleted ;
433490 }
434491 }
435492}
0 commit comments