@@ -169,20 +169,36 @@ 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+ wil::com_ptr<ICoreWebView2Profile > profile;
175+ CHECK_FAILURE(webView2->get_Profile(&profile));
176+ CHECK_FAILURE(profile2->Delete());
177+ }
178+
179+ void AppWindow::RegisterEventHandlers(ICoreWebView2* webView2)
180+ {
181+ wil::com_ptr<ICoreWebView2Profile > profile;
182+ CHECK_FAILURE(webView2->get_Profile(&profile));
183+ if (profile)
178184 {
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- }
185+ CHECK_FAILURE(profile->add_Deleted(
186+ Microsoft::WRL::Callback<ICoreWebView2StagingProfileDeletedEventHandler >(
187+ [ this] (ICoreWebView2Profile* sender, IUnknown* args)
188+ {
189+ RunAsync(
190+ [ this] ( )
191+ {
192+ std::wstring message = L"The webview2 has been closed and "
193+ L"the reason is profile "
194+ L"has been marked as deleted.";
195+ MessageBox(
196+ m_mainWindow, message.c_str(), L"webview2 closed",
197+ MB_OK);
198+ CloseAppWindow();
199+ });
200+ return S_OK;
201+ }).Get(), nullptr));
186202 }
187203}
188204```
@@ -251,11 +267,23 @@ void DeleteAllCookies()
251267``` csharp
252268public DeleteProfile (CoreWebView2Controller controller )
253269{
254- // Get the profile object.
255- CoreWebView2Profile profile = controller .CoreWebView2 .Profile ;
256-
257270 // Delete current profile.
258- profile .Delete ();
271+ controller .CoreWebView2 .Profile .Delete ();
272+ }
273+
274+ void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
275+ {
276+ WebViewProfile .Deleted += WebViewProfile_Deleted ;
277+ }
278+
279+ private void WebViewProfile_Deleted (object sender , object e )
280+ {
281+ this .Dispatcher .InvokeAsync (() =>
282+ {
283+ String message = " The webview2 has been closed and the reason is profile has been marked as deleted." ;
284+ MessageBox .Show (message );
285+ Close ();
286+ });
259287}
260288```
261289
@@ -270,6 +298,8 @@ interface ICoreWebView2_7;
270298interface ICoreWebView2Profile;
271299interface ICoreWebView2Profile2;
272300interface ICoreWebView2Profile3;
301+ interface ICoreWebView2StagingProfile7;
302+ interface ICoreWebView2StagingProfileDeletedEventHandler;
273303
274304/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
275305[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -363,16 +393,40 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
363393 [propget] HRESULT CookieManager([out, retval] ICoreWebView2CookieManager** cookieManager);
364394}
365395
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();
396+ [uuid(2765B8BD-7C57-4B76-B8CC-1EC940FF92CC), object, pointer_default(unique)]
397+ interface ICoreWebView2StagingProfile7 : IUnknown {
398+ /// After the API is called, the profile will be marked for deletion. The
399+ /// local profile's directory will be tried to delete at browser process
400+ /// exit, if fail to delete, it will recursively try to delete at next
401+ /// browser process start until successful.
402+ /// The corresponding webview2s will be auto closed and profile's Deleted
403+ /// event will be raised. See `Profile.Deleted` for more information.
404+ /// If create a new profile with the same name as the profile that has been
405+ /// marked as deleted will be failure with the HRESULT:ERROR_INVALID_STATE
406+ /// (0x8007139FL).
407+ HRESULT Delete();
408+
409+ /// Add an event handler for the `Deleted` event. `Deleted` event handle runs
410+ /// when the profile's Delete API is called. When this event is raised, the
411+ /// profile and its corresponding webview2 moves to the Closed state, and
412+ /// cannot be used anymore.
413+ HRESULT add_Deleted(
414+ [in] ICoreWebView2StagingProfileDeletedEventHandler* eventHandler,
415+ [out] EventRegistrationToken* token);
416+
417+ /// Remove an event handler previously added with `add_Deleted`.
418+ HRESULT remove_Deleted(
419+ [in] EventRegistrationToken token);
420+ }
421+
422+ [uuid(e5dea648-79c9-4caa-8314-dd71de62ad49), object, pointer_default(unique)]
423+ interface ICoreWebView2StagingProfileDeletedEventHandler: IUnknown {
424+ /// Called to provide the implementer with the event args for the
425+ /// profile deleted event. No event args exist and the `args`
426+ /// parameter is set to `null`.
427+ HRESULT Invoke(
428+ [in] ICoreWebView2Profile* sender,
429+ [in] IUnknown* args);
376430}
377431```
378432
@@ -415,7 +469,7 @@ namespace Microsoft.Web.WebView2.Core
415469 // ...
416470 CoreWebView2Profile Profile { get ; };
417471 }
418-
472+
419473 runtimeclass CoreWebView2Profile
420474 {
421475 String ProfileName { get ; };
@@ -426,10 +480,10 @@ namespace Microsoft.Web.WebView2.Core
426480
427481 CoreWebView2CookieManager CookieManager { get ; };
428482
429- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile3 " )]
483+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile7 " )]
430484 {
431- // ICoreWebView2Profile3 members
432485 void Delete ();
486+ event Windows .Foundation .TypedEventHandler < CoreWebView2Profile , Object > Deleted ;
433487 }
434488 }
435489}
0 commit comments