@@ -178,10 +178,24 @@ HRESULT AppWindow::DeleteProfile(ICoreWebView2* webView2)
178
178
179
179
void AppWindow::RegisterEventHandlers()
180
180
{
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] (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)
188
+ {
189
+ RunAsync( [ this] ( )
190
+ {
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);
196
+ CloseAppWindow();
197
+ });
198
+ }
185
199
return S_OK;
186
200
}).Get(), nullptr));
187
201
}
@@ -260,12 +274,20 @@ public DeleteProfile(CoreWebView2Controller controller)
260
274
261
275
void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
262
276
{
263
- webView .CoreWebView2 .ProfileDeleted += CoreWebView2_ProfileDeleted ;
277
+ webView .CoreWebView2 .Closed += CoreWebView2_Closed ;
264
278
}
265
279
266
- private void CoreWebView2_ProfileDeleted (object sender , object e )
280
+ private void CoreWebView2_Closed (object sender , CoreWebView2ClosedEventArgs e )
267
281
{
268
- Close ();
282
+ if (e .Reason == CoreWebView2ClosedReason .ProfileDeleted )
283
+ {
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
+ }
269
291
}
270
292
```
271
293
@@ -277,9 +299,13 @@ private void CoreWebView2_ProfileDeleted(object sender, object e)
277
299
interface ICoreWebView2ControllerOptions;
278
300
interface ICoreWebView2Environment5;
279
301
interface ICoreWebView2_7;
302
+ interface ICoreWebView2_9;
280
303
interface ICoreWebView2Profile;
281
304
interface ICoreWebView2Profile2;
282
305
interface ICoreWebView2Profile3;
306
+ interface ICoreWebView2Profile4;
307
+ interface ICoreWebView2ClosedEventHandler;
308
+ interface ICoreWebView2ClosedEventArgs;
283
309
284
310
/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
285
311
[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -376,15 +402,12 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
376
402
[uuid(2765B8BD-7C57-4B76-B8AA-1EC940FE92CC), object, pointer_default(unique)]
377
403
interface ICoreWebView2Profile4 : IUnknown {
378
404
/// 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
405
+ /// local profile' s directory will be tried to delete at browser process
380
406
/// exit, if fail to delete, it will recursively try to delete at next
381
407
/// 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.
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.
388
411
/// If create a new profile with the same name as the profile that has been
389
412
/// marked as deleted will be failure with the HRESULT:ERROR_INVALID_STATE
390
413
/// (0x8007139FL).
@@ -393,17 +416,43 @@ interface ICoreWebView2Profile4 : IUnknown {
393
416
394
417
[uuid(cc39bea3-f6f8-471b-919f-fa253e2fff03), object, pointer_default(unique)]
395
418
interface 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,
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,
401
424
[out] EventRegistrationToken* token);
402
425
403
- /// Remove an event handler previously added with `add_ProfileDeleted `.
404
- HRESULT remove_ProfileDeleted (
426
+ /// Remove an event handler previously added with `add_Closed `.
427
+ HRESULT remove_Closed (
405
428
[in] EventRegistrationToken token);
406
429
}
430
+
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.
440
+ [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);
446
+ }
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
+
407
456
```
408
457
409
458
## .NET and WinRT
@@ -447,7 +496,7 @@ namespace Microsoft.Web.WebView2.Core
447
496
448
497
[interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2_9" )]
449
498
{
450
- event Windows .Foundation .TypedEventHandler < CoreWebView2 , Object > ProfileDeleted ;
499
+ event Windows .Foundation .TypedEventHandler < CoreWebView2 , CoreWebView2ClosedEventArgs > Closed ;
451
500
}
452
501
}
453
502
0 commit comments