@@ -178,26 +178,21 @@ HRESULT AppWindow::DeleteProfile(ICoreWebView2* webView2)
178178
179179void AppWindow::RegisterEventHandlers()
180180{
181- CHECK_FAILURE(m_webView->add_Closed(
182- Microsoft::WRL::Callback< ICoreWebView2ClosedEventHandler >(
183- [ this ] (ICoreWebView2_20 * sender, ICoreWebView2EventArgs * args )
184- {
185- COREWEBVIEW2_CLOSED_REASON reason;
186- CHECK_FAILURE(args->get_Reason(&reason));
187- if (reason == COREWEBVIEW2_CLOSED_REASON::COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED )
181+ wil::com_ptr< ICoreWebView2Profile > webView2Profile;
182+ webView2_13->get_Profile(&webView2Profile);
183+ if (webView2Profile )
184+ {
185+ webView2Profile->add_ProfileDeletionStarted(
186+ Microsoft::WRL::Callback< ICoreWebView2ProfileDeletionStartedEventHandler >(
187+ [ this ] (ICoreWebView2Profile7 * sender, IUnknown * args )
188188 {
189189 RunAsync( [ this] ( )
190190 {
191- std::wstring message =
192- L"The webview2 has been closed and the reason is profile "
193- L"has been marked as deleted.";
194- MessageBox(
195- m_mainWindow, message.c_str(), L"webview2 closed", MB_OK);
196191 CloseAppWindow();
197- });
198- }
199- return S_OK ;
200- }).Get(), nullptr));
192+ }
193+ return S_OK;
194+ }).Get(),nullptr) ;
195+ }
201196}
202197```
203198
@@ -274,20 +269,15 @@ public DeleteProfile(CoreWebView2Controller controller)
274269
275270void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
276271{
277- webView . CoreWebView2 . Closed += CoreWebView2_Closed ;
272+ WebViewProfile . ProfileDeletionStarted += CoreWebView2_ProfileDeletionStarted ;
278273}
279274
280- private void CoreWebView2_Closed (object sender , CoreWebView2ClosedEventArgs e )
275+ private void CoreWebView2_ProfileDeletionStarted (object sender , object e )
281276{
282- if ( e . Reason == CoreWebView2ClosedReason . ProfileDeleted )
277+ this . Dispatcher . InvokeAsync (() =>
283278 {
284- this .Dispatcher .InvokeAsync (() =>
285- {
286- String message = " The webview2 has been closed and the reason is profile has been marked as deleted" ;
287- MessageBox .Show (message );
288- Close ();
289- });
290- }
279+ Close ();
280+ });
291281}
292282```
293283
@@ -299,13 +289,11 @@ private void CoreWebView2_Closed(object sender, CoreWebView2ClosedEventArgs e)
299289interface ICoreWebView2ControllerOptions;
300290interface ICoreWebView2Environment5;
301291interface ICoreWebView2_7;
302- interface ICoreWebView2_9;
303292interface ICoreWebView2Profile;
304293interface ICoreWebView2Profile2;
305294interface ICoreWebView2Profile3;
306295interface ICoreWebView2Profile4;
307- interface ICoreWebView2ClosedEventHandler;
308- interface ICoreWebView2ClosedEventArgs;
296+ interface ICoreWebView2ProfileDeletionStartedEventHandler;
309297
310298/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
311299[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -399,60 +387,44 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
399387 [propget] HRESULT CookieManager([out, retval] ICoreWebView2CookieManager** cookieManager);
400388}
401389
402- [uuid(2765B8BD-7C57-4B76-B8AA-1EC940FE92CC), object, pointer_default(unique)]
403- interface ICoreWebView2Profile4 : IUnknown {
404- /// After the API is called, the profile will be marked for deletion. The
405- /// local profile's directory will be tried to delete at browser process
406- /// exit, if fail to delete, it will recursively try to delete at next
407- /// browser process start until successful.
408- /// The corresponding webview2s will be auto closed and its Closed event
409- /// handle function will be triggered with the reason is
410- /// COREWEBVIEW2_CLOSED_REASON.COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED.
390+ /// Interfaces in profile for Delete.
391+ [uuid(7CB8811D-A9B6-425E-9C57-0B0E599BAC5D), object, pointer_default(unique)]
392+ interface ICoreWebView2Profile7 : IUnknown {
393+ /// After the API is called, the profile will be marked for deletion. If the
394+ /// profile has been marked for deletion, this API will return S_OK directly.
395+ /// The local profile's directory will be tried to delete at browser
396+ /// process exit, if fail to delete, it will recursively try to delete at
397+ /// next browser process start until successful.
398+ /// When the profile is marked for deletion, the corresponding
399+ /// `ProfileDeletionStarted` event handle of each profile with the same name
400+ /// in current or different process will be all triggered. See
401+ /// `add_ProfileDeletionStarted` for more information.
411402 /// If create a new profile with the same name as the profile that has been
412403 /// marked as deleted will be failure with the HRESULT:ERROR_INVALID_STATE
413404 /// (0x8007139FL).
414405 HRESULT Delete();
415- }
416406
417- [uuid(cc39bea3-f6f8-471b-919f-fa253e2fff03), object, pointer_default(unique)]
418- interface ICoreWebView2_9 : IUnknown {
419- /// Add an event handler for the `Closed` event. `Closed` enent handle runs
420- /// when the webview2 is closed passivly. When this event is raised, the
421- /// webview2 cannot be used anymore.
422- HRESULT add_Closed(
423- [in] ICoreWebView2ClosedEventHandler* eventHandler,
407+ /// The `ProfileDeletionStarted` event will be raised when profile has been
408+ /// marked as deleted. When this event has been raised, it is recommended to
409+ /// do some clean works and then close the webview2.
410+ HRESULT add_ProfileDeletionStarted (
411+ [in] ICoreWebView2ProfileDeletionStartedEventHandler* eventHandler,
424412 [out] EventRegistrationToken* token);
425413
426- /// Remove an event handler previously added with `add_Closed `.
427- HRESULT remove_Closed (
414+ /// Remove an event handler previously added with `add_ProfileDeletionStarted `.
415+ HRESULT remove_ProfileDeletionStarted (
428416 [in] EventRegistrationToken token);
429417}
430418
431- /// The reason of webview2 closed.
432- [v1_enum]
433- typedef enum COREWEBVIEW2_CLOSED_REASON {
434- /// Indicates that the reason of webview2 closed is its corresponding
435- /// Profile has been or marked as deleted.
436- COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED,
437- } COREWEBVIEW2_CLOSED_REASON;
438-
439- /// Receives the webview2 `Closed` event.
419+ /// Receives the profile `ProfileDeletionStarted` event.
440420[uuid(970BB7E0-A257-4A76-BE15-5BDEB00B5673), object, pointer_default(unique)]
441- interface ICoreWebView2ClosedEventHandler : IUnknown {
442- /// Called to provide the implementer with the event args for the
443- /// corresponding event.
444- HRESULT Invoke([in] ICoreWebView2_20* sender,
445- [in] ICoreWebView2ClosedEventArgs* args);
421+ interface ICoreWebView2ProfileDeletionStartedEventHandler : IUnknown {
422+ /// Called to provide the implementer for the ProfileDeletionStarted event.
423+ /// No event args exist and the `args` parameter is set to `null`.
424+ HRESULT Invoke(
425+ [in] ICoreWebView2Profile7* sender,
426+ [in] IUnknown* args);
446427}
447-
448- /// This is the event args interface for webview2 `Closed` event handle.
449- [uuid(0e1730c1-03df-4ad2-b847-be4d63adf777), object, pointer_default(unique)]
450- interface ICoreWebView2ClosedEventArgs : IUnknown {
451- /// webview2 closed reason.
452- [propget] HRESULT Reason([out, retval]
453- COREWEBVIEW2_CLOSED_REASON* value);
454- }
455-
456428```
457429
458430## .NET and WinRT
@@ -488,28 +460,6 @@ namespace Microsoft.Web.WebView2.Core
488460 CoreWebView2ControllerWindowReference ParentWindow ,
489461 CoreWebView2ControllerOptions options );
490462 }
491-
492- runtimeclass CoreWebView2
493- {
494- // ...
495- CoreWebView2Profile Profile { get ; };
496-
497- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2_9" )]
498- {
499- event Windows .Foundation .TypedEventHandler < CoreWebView2 , CoreWebView2ClosedEventArgs > Closed ;
500- }
501- }
502-
503- enum CoreWebView2ClosedReason
504- {
505- ProfileDeleted = 0 ,
506- };
507-
508- runtimeclass CoreWebView2ClosedEventArgs
509- {
510- CoreWebView2ClosedReason Reason { get ; };
511- }
512-
513463 runtimeclass CoreWebView2Profile
514464 {
515465 String ProfileName { get ; };
@@ -520,10 +470,10 @@ namespace Microsoft.Web.WebView2.Core
520470
521471 CoreWebView2CookieManager CookieManager { get ; };
522472
523- [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile4 " )]
473+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2Profile7 " )]
524474 {
525- // ICoreWebView2Profile4 members
526475 void Delete ();
476+ event Windows .Foundation .TypedEventHandler < CoreWebView2Profile , Object > ProfileDeletionStarted ;
527477 }
528478 }
529479}
0 commit comments