@@ -178,10 +178,25 @@ 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] (
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
+ }
185
200
return S_OK;
186
201
}).Get(), nullptr));
187
202
}
@@ -260,12 +275,20 @@ public DeleteProfile(CoreWebView2Controller controller)
260
275
261
276
void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
262
277
{
263
- webView .CoreWebView2 .ProfileDeleted += CoreWebView2_ProfileDeleted ;
278
+ webView .CoreWebView2 .Closed += CoreWebView2_Closed ;
264
279
}
265
280
266
- private void CoreWebView2_ProfileDeleted (object sender , object e )
281
+ private void CoreWebView2_Closed (object sender , CoreWebView2ClosedEventArgs e )
267
282
{
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
+ }
269
292
}
270
293
```
271
294
@@ -277,9 +300,13 @@ private void CoreWebView2_ProfileDeleted(object sender, object e)
277
300
interface ICoreWebView2ControllerOptions;
278
301
interface ICoreWebView2Environment5;
279
302
interface ICoreWebView2_7;
303
+ interface ICoreWebView2_9;
280
304
interface ICoreWebView2Profile;
281
305
interface ICoreWebView2Profile2;
282
306
interface ICoreWebView2Profile3;
307
+ interface ICoreWebView2Profile4;
308
+ interface ICoreWebView2ClosedEventHandler;
309
+ interface ICoreWebView2ClosedEventArgs;
283
310
284
311
/// This interface is used to manage profile options that created by 'CreateCoreWebView2ControllerOptions'.
285
312
[uuid(C2669A3A-03A9-45E9-97EA-03CD55E5DC03), object, pointer_default(unique)]
@@ -376,15 +403,12 @@ interface ICoreWebView2Profile2 : ICoreWebView2Profile {
376
403
[uuid(2765B8BD-7C57-4B76-B8AA-1EC940FE92CC), object, pointer_default(unique)]
377
404
interface ICoreWebView2Profile4 : IUnknown {
378
405
/// 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
380
407
/// exit, if fail to delete, it will recursively try to delete at next
381
408
/// 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.
388
412
/// If create a new profile with the same name as the profile that has been
389
413
/// marked as deleted will be failure with the HRESULT:ERROR_INVALID_STATE
390
414
/// (0x8007139FL).
@@ -393,17 +417,43 @@ interface ICoreWebView2Profile4 : IUnknown {
393
417
394
418
[uuid(cc39bea3-f6f8-471b-919f-fa253e2fff03), object, pointer_default(unique)]
395
419
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,
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,
401
425
[out] EventRegistrationToken* token);
402
426
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 (
405
429
[in] EventRegistrationToken token);
406
430
}
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
+
407
457
```
408
458
409
459
## .NET and WinRT
0 commit comments