@@ -171,35 +171,48 @@ void ScenarioCookieManagement::DeleteAllCookies()
171
171
``` cpp
172
172
HRESULT AppWindow::DeleteProfile (ICoreWebView2* webView2)
173
173
{
174
- DCHECK(webView2);
175
- wil::com_ptr<ICoreWebView2Profile > profile;
176
- CHECK_FAILURE(webView2->get_Profile(&profile));
177
- CHECK_FAILURE(profile2->Delete());
174
+ auto webview13 = wil::try_com_query<ICoreWebView2_13>(webview2);
175
+ if (webview13)
176
+ {
177
+ wil::com_ptr<ICoreWebView2Profile > profile;
178
+ CHECK_FAILURE(webview13->get_Profile(&profile));
179
+ CHECK_FAILURE(profile->Delete());
180
+ }
178
181
}
179
182
180
183
void AppWindow::RegisterProfileDeletedEventHandlers(ICoreWebView2* webView2)
181
184
{
182
185
wil::com_ptr<ICoreWebView2Profile > profile;
183
186
CHECK_FAILURE(webView2->get_Profile(&profile));
184
- if (profile)
187
+ CHECK_FAILURE(profile->add_Deleted(
188
+ Microsoft::WRL::Callback<ICoreWebView2StagingProfileDeletedEventHandler >(
189
+ [ this] (ICoreWebView2Profile* sender, IUnknown* args)
190
+ {
191
+ RunAsync(
192
+ [ this] ( )
193
+ {
194
+ std::wstring message = L"The profile has been marked"
195
+ "for deletion. Any associated webview2 objects has"
196
+ " been closed.";
197
+ MessageBox(
198
+ m_mainWindow, message.c_str(), L"webview2 closed",
199
+ MB_OK);
200
+ CloseAppWindow();
201
+ });
202
+ return S_OK;
203
+ }).Get(), nullptr));
204
+ }
205
+
206
+ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(
207
+ HRESULT result, ICoreWebView2Controller* controller)
208
+ {
209
+ if (result == HRESULT_FROM_WIN32(ERROR_DELETE_PENDING))
185
210
{
186
- CHECK_FAILURE(profile->add_Deleted(
187
- Microsoft::WRL::Callback<ICoreWebView2StagingProfileDeletedEventHandler >(
188
- [ this] (ICoreWebView2Profile* sender, IUnknown* args)
189
- {
190
- RunAsync(
191
- [ this] ( )
192
- {
193
- std::wstring message = L"The webview2 has been closed and "
194
- L"the reason is profile "
195
- L"has been marked as deleted.";
196
- MessageBox(
197
- m_mainWindow, message.c_str(), L"webview2 closed",
198
- MB_OK);
199
- CloseAppWindow();
200
- });
201
- return S_OK;
202
- }).Get(), nullptr));
211
+ ShowFailure(
212
+ result, L"Failed to create webview, because the profile's name has "
213
+ "been marked as deleted, please use a different profile's name");
214
+ m_webviewOption.PopupDialog(this);
215
+ CloseAppWindow();
203
216
}
204
217
}
205
218
```
@@ -275,17 +288,38 @@ public DeleteProfile(CoreWebView2 coreWebView2)
275
288
void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
276
289
{
277
290
WebViewProfile .Deleted += WebViewProfile_Deleted ;
291
+
292
+ // ...
293
+ // ERROR_DELETE_PENDING(0x8007012f)
294
+ if (e .InitializationException .HResult == - 2147024593 )
295
+ {
296
+ MessageBox .Show ($" Failed to create webview, because the profile's name has been marked as deleted, please use a different profile's name." );
297
+ var dialog = new NewWindowOptionsDialog ();
298
+ dialog .CreationProperties = webView .CreationProperties ;
299
+ if (dialog .ShowDialog () == true )
300
+ {
301
+ new MainWindow (dialog .CreationProperties ).Show ();
302
+ }
303
+ Close ();
304
+ return ;
305
+ }
278
306
}
279
307
280
308
private void WebViewProfile_Deleted (object sender , object e )
281
309
{
282
310
this .Dispatcher .InvokeAsync (() =>
283
311
{
284
- String message = " The webview2 has been closed and the reason is profile has been marked as deleted ." ;
312
+ String message = " The profile has been marked for deletion. Any associated webview2 objects will be closed ." ;
285
313
MessageBox .Show (message );
286
314
Close ();
287
315
});
288
316
}
317
+
318
+ void WebView_CoreWebView2InitializationCompleted (object sender , CoreWebView2InitializationCompletedEventArgs e )
319
+ {
320
+
321
+ }
322
+
289
323
```
290
324
291
325
# API Details
@@ -399,20 +433,20 @@ interface ICoreWebView2StagingProfile7 : IUnknown {
399
433
/// After the API is called, the profile will be marked for deletion. The
400
434
/// local profile's directory will be deleted at browser process exit. If it
401
435
/// fails to delete, because something else is holding the files open,
402
- /// WebView2 will try to delete again during all future at next browser
403
- /// process starts until successful.
436
+ /// WebView2 will try to delete the profile at all future browser process
437
+ /// starts until successful.
404
438
/// The corresponding CoreWebView2s will be closed and the
405
439
/// CoreWebView2Profile.Deleted event will be raised. See
406
440
/// `CoreWebView2Profile.Deleted` for more information.
407
441
/// If you try to create a new profile with the same name as an existing
408
442
/// profile that has been marked as deleted but hasn't yet been deleted,
409
- /// profile creation will fail with HRESULT_FROM_WIN32(ERROR_INVALID_STATE ).
443
+ /// profile creation will fail with HRESULT_FROM_WIN32(ERROR_DELETE_PENDING ).
410
444
HRESULT Delete();
411
445
412
446
/// Add an event handler for the `Deleted` event. The `Deleted` event is
413
447
/// raised when the profile is marked for deletion. When this event is
414
- /// raised, the CoreWebView2Profile and its corresponding CoreWebView2s are
415
- /// closed, and cannot be used anymore.
448
+ /// raised, the CoreWebView2Profile and its corresponding CoreWebView2s have
449
+ /// been closed, and cannot be used anymore.
416
450
HRESULT add_Deleted(
417
451
[in] ICoreWebView2StagingProfileDeletedEventHandler* eventHandler,
418
452
[out] EventRegistrationToken* token);
0 commit comments