@@ -178,10 +178,25 @@ HRESULT AppWindow::DeleteProfile(ICoreWebView2* webView2)
178178
179179void AppWindow::RegisterEventHandlers()
180180{
181- CHECK_FAILURE(m_webView->add_ProfileDeleted(
182- Microsoft::WRL::Callback<ICoreWebView2ProfileDeletedEventHandler >(
183- [ this] (ICoreWebView2_9* sender, IUnknown* args) {
184- CloseAppWindow();
181+ CHECK_FAILURE(m_webView->add_Closed(
182+ Microsoft::WRL::Callback<ICoreWebView2ClosedEventHandler >(
183+ [ this] (
184+ ICoreWebView2_20* sender,
185+ ICoreWebView2EventArgs* args)
186+ {
187+ COREWEBVIEW2_CLOSED_REASON reason;
188+ CHECK_FAILURE(args->get_Reason(&reason));
189+ if (reason == COREWEBVIEW2_CLOSED_REASON::COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED)
190+ {
191+ RunAsync( [ this] ( ) {
192+ std::wstring message =
193+ L"The webview2 has been closed and the reason is profile "
194+ L"has been marked as deleted.";
195+ MessageBox(
196+ m_mainWindow, message.c_str(), L"webview2 closed", MB_OK);
197+ CloseAppWindow();
198+ });
199+ }
185200 return S_OK;
186201 }).Get(), nullptr));
187202}
@@ -260,12 +275,20 @@ public DeleteProfile(CoreWebView2Controller controller)
260275
261276void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
262277{
263- webView .CoreWebView2 .ProfileDeleted += CoreWebView2_ProfileDeleted ;
278+ webView .CoreWebView2 .Closed += CoreWebView2_Closed ;
264279}
265280
266- private void CoreWebView2_ProfileDeleted (object sender , object e )
281+ private void CoreWebView2_Closed (object sender , CoreWebView2ClosedEventArgs e )
267282{
268- Close ();
283+ if (e .Reason == CoreWebView2ClosedReason .ProfileDeleted )
284+ {
285+ this .Dispatcher .InvokeAsync (() =>
286+ {
287+ String message = " The webview2 has been closed and the reason is profile has been marked as deleted" ;
288+ MessageBox .Show (message );
289+ Close ();
290+ });
291+ }
269292}
270293```
271294
@@ -277,9 +300,13 @@ private void CoreWebView2_ProfileDeleted(object sender, object e)
277300interface ICoreWebView2ControllerOptions;
278301interface ICoreWebView2Environment5;
279302interface ICoreWebView2_7;
303+ interface ICoreWebView2_9;
280304interface ICoreWebView2Profile;
281305interface ICoreWebView2Profile2;
282306interface ICoreWebView2Profile3;
307+ interface ICoreWebView2Profile4;
308+ interface ICoreWebView2ClosedEventHandler;
309+ interface ICoreWebView2ClosedEventArgs;
283310
284311/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
285312[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -376,15 +403,12 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
376403[uuid(2765B8BD-7C57-4B76-B8AA-1EC940FE92CC), object, pointer_default(unique)]
377404interface ICoreWebView2Profile4 : IUnknown {
378405 /// After the API is called, the profile will be marked for deletion. The
379- /// local profile’ s directory will be tried to delete at browser process
406+ /// local profile' s directory will be tried to delete at browser process
380407 /// exit, if fail to delete, it will recursively try to delete at next
381408 /// browser process start until successful.
382- /// The corresponding user's `ProfileDeleted` event handle function will
383- /// be triggered. After the function is triggered, continuing to use the
384- /// profile or its corresponding webviews is an undefined behavior.
385- /// For each WebView of the same profile, the WebView will be auto closed.
386- /// If the user has registered an event handler for ProfileDeleted event,
387- /// the event handle will be invoked before WebView is closed.
409+ /// The corresponding webview2s will be auto closed and its Closed event
410+ /// handle function will be triggered with the reason is
411+ /// COREWEBVIEW2_CLOSED_REASON.COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED.
388412 /// If create a new profile with the same name as the profile that has been
389413 /// marked as deleted will be failure with the HRESULT:ERROR_INVALID_STATE
390414 /// (0x8007139FL).
@@ -393,17 +417,43 @@ interface ICoreWebView2Profile4 : IUnknown {
393417
394418[uuid(cc39bea3-f6f8-471b-919f-fa253e2fff03), object, pointer_default(unique)]
395419interface ICoreWebView2_9 : IUnknown {
396- /// The `ProfileDeleted` event is raised when its corresponding Profile's
397- /// Delete API is called . When this event has been raised, continue to use
398- /// the profile or its corresponding webviews is an undefined behavior .
399- HRESULT add_ProfileDeleted (
400- [in] ICoreWebView2ProfileDeletedEventHandler * eventHandler,
420+ /// Add an event handler for the `Closed` event. `Closed` enent handle runs
421+ /// when the webview2 is closed passivly . When this event is raised, the
422+ /// webview2 cannot be used anymore .
423+ HRESULT add_Closed (
424+ [in] ICoreWebView2ClosedEventHandler * eventHandler,
401425 [out] EventRegistrationToken* token);
402426
403- /// Remove an event handler previously added with `add_ProfileDeleted `.
404- HRESULT remove_ProfileDeleted (
427+ /// Remove an event handler previously added with `add_Closed `.
428+ HRESULT remove_Closed (
405429 [in] EventRegistrationToken token);
406430}
431+
432+ /// The reason of webview2 closed.
433+ [v1_enum]
434+ typedef enum COREWEBVIEW2_CLOSED_REASON {
435+ /// Indicates that the reason of webview2 closed is its corresponding
436+ /// Profile has been or marked as deleted.
437+ COREWEBVIEW2_CLOSED_REASON_PROFILE_DELETED,
438+ } COREWEBVIEW2_CLOSED_REASON;
439+
440+ /// Receives the webview2 `Closed` event.
441+ [uuid(970BB7E0-A257-4A76-BE15-5BDEB00B5673), object, pointer_default(unique)]
442+ interface ICoreWebView2ClosedEventHandler : IUnknown {
443+ /// Called to provide the implementer with the event args for the
444+ /// corresponding event.
445+ HRESULT Invoke([in] ICoreWebView2_20* sender,
446+ [in] ICoreWebView2ClosedEventArgs* args);
447+ }
448+
449+ /// This is the event args interface for webview2 `Closed` event handle.
450+ [uuid(0e1730c1-03df-4ad2-b847-be4d63adf777), object, pointer_default(unique)]
451+ interface ICoreWebView2ClosedEventArgs : IUnknown {
452+ /// webview2 closed reason.
453+ [propget] HRESULT Reason([out, retval]
454+ COREWEBVIEW2_CLOSED_REASON* value);
455+ }
456+
407457```
408458
409459## .NET and WinRT
0 commit comments