From 6bf4af525a628f5818b71080a925c5e72a3e698f Mon Sep 17 00:00:00 2001 From: Jessica Chen <55165503+peiche-jessica@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:41:32 -0700 Subject: [PATCH] Update projects to use latest WebView2 SDK 1.0.2895-prerelease (#256) * Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 131.0.2895.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.2895-prerelease --------- Co-authored-by: WebView2 Github Bot --- SampleApps/WebView2APISample/AppWindow.cpp | 60 +++++++ .../ScenarioCustomScheme.cpp | 7 +- .../WebView2APISample/ScenarioCustomScheme.h | 2 + .../ScenarioCustomSchemeNavigate.cpp | 7 +- .../ScenarioCustomSchemeNavigate.h | 2 + .../ScenarioFileTypePolicy.cpp | 20 +-- .../ScenarioFileTypePolicy.h | 2 +- .../ScenarioScreenCapture.cpp | 36 ++--- .../WebView2APISample/ScenarioScreenCapture.h | 4 +- .../ScenarioWebViewEventMonitor.cpp | 3 - .../WebView2APISample/SettingsComponent.cpp | 13 +- .../WebView2APISample/SettingsComponent.h | 1 + .../WebView2APISample.vcxproj | 10 +- ...oServiceWorkerSyncRegistrationManager.html | 151 ++++++++++++++++++ ...kerSyncRegistrationManagerServiceWorker.js | 12 ++ SampleApps/WebView2APISample/packages.config | 2 +- SampleApps/WebView2APISample/stdafx.h | 2 +- .../BrowserForm.cs | 20 +-- .../WebView2WindowsFormsBrowser.csproj | 2 +- .../WebView2WpfBrowser/MainWindow.xaml.cs | 8 - .../WebView2WpfBrowser.csproj | 2 +- .../WebView2_WinUI3_Sample/MainWindow.xaml.cs | 20 +++ 22 files changed, 318 insertions(+), 68 deletions(-) create mode 100644 SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManager.html create mode 100644 SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js diff --git a/SampleApps/WebView2APISample/AppWindow.cpp b/SampleApps/WebView2APISample/AppWindow.cpp index c2c35735..463ecdd6 100644 --- a/SampleApps/WebView2APISample/AppWindow.cpp +++ b/SampleApps/WebView2APISample/AppWindow.cpp @@ -1943,6 +1943,66 @@ void AppWindow::RegisterEventHandlers() nullptr)); //! [NewBrowserVersionAvailable] + //! [RestartRequested] + // After the environment is successfully created, + // register a handler for + auto exp_env15 = m_webViewEnvironment.try_query(); + CHECK_FAILURE(exp_env15->add_RestartRequested( + Callback( + [this]( + ICoreWebView2Environment* sender, + ICoreWebView2ExperimentalRestartRequestedEventArgs* args) -> HRESULT + { + COREWEBVIEW2_RESTART_REQUESTED_PRIORITY priority; + args->get_Priority(&priority); + if (priority == COREWEBVIEW2_RESTART_REQUESTED_PRIORITY_NORMAL) + { + // Remaind user to restart the app when they get a chance. + // Don't force user to restart. + MessageBox( + m_mainWindow, L"Please restart your app when you get a chance", + L"WebView Restart Requested", MB_OK); + } + else if (priority == COREWEBVIEW2_RESTART_REQUESTED_PRIORITY_HIGH) + { + // Don't block the event handler with a message box + RunAsync( + [this]() + { + std::wstring message = + L"We detected there is a critical update for WebView2 runtime."; + if (m_webView) + { + message += L"Do you want to restart the app? \n\n"; + message += + L"Click No if you only want to re-create the webviews. \n"; + message += L"Click Cancel for no action. \n"; + } + int response = MessageBox( + m_mainWindow, message.c_str(), L"Critical Update Avaliable", + m_webView ? MB_YESNOCANCEL : MB_OK); + + if (response == IDYES) + { + RestartApp(); + } + else if (response == IDNO) + { + ReinitializeWebViewWithNewBrowser(); + } + else + { + // do nothing + } + }); + } + + return S_OK; + }) + .Get(), + nullptr)); + //! [RestartRequested] + //! [ProfileDeleted] auto webView2_13 = m_webView.try_query(); CHECK_FEATURE_RETURN_EMPTY(webView2_13); diff --git a/SampleApps/WebView2APISample/ScenarioCustomScheme.cpp b/SampleApps/WebView2APISample/ScenarioCustomScheme.cpp index 2c6d29ec..0afdf737 100644 --- a/SampleApps/WebView2APISample/ScenarioCustomScheme.cpp +++ b/SampleApps/WebView2APISample/ScenarioCustomScheme.cpp @@ -14,8 +14,11 @@ using namespace Microsoft::WRL; ScenarioCustomScheme::ScenarioCustomScheme(AppWindow* appWindow) : m_appWindow(appWindow) { - CHECK_FAILURE(m_appWindow->GetWebView()->AddWebResourceRequestedFilter( - L"custom-scheme*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)); + m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webView2_22)); + CHECK_FEATURE_RETURN_EMPTY(m_webView2_22); + CHECK_FAILURE(m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds( + L"custom-scheme*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT)); CHECK_FAILURE(m_appWindow->GetWebView()->add_WebResourceRequested( Callback( [this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) diff --git a/SampleApps/WebView2APISample/ScenarioCustomScheme.h b/SampleApps/WebView2APISample/ScenarioCustomScheme.h index ad101d23..1b617c41 100644 --- a/SampleApps/WebView2APISample/ScenarioCustomScheme.h +++ b/SampleApps/WebView2APISample/ScenarioCustomScheme.h @@ -20,5 +20,7 @@ class ScenarioCustomScheme : public ComponentBase EventRegistrationToken m_webResourceRequestedToken = {}; EventRegistrationToken m_navigationCompletedToken = {}; + wil::com_ptr m_webView2_22; + AppWindow* m_appWindow = nullptr; }; diff --git a/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.cpp b/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.cpp index e6a05900..ce911d3d 100644 --- a/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.cpp +++ b/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.cpp @@ -15,8 +15,11 @@ using namespace Microsoft::WRL; ScenarioCustomSchemeNavigate::ScenarioCustomSchemeNavigate(AppWindow* appWindow) : m_appWindow(appWindow) { - CHECK_FAILURE(m_appWindow->GetWebView()->AddWebResourceRequestedFilter( - L"wv2rocks*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)); + m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webView2_22)); + CHECK_FEATURE_RETURN_EMPTY(m_webView2_22); + CHECK_FAILURE(m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds( + L"wv2rocks*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT)); CHECK_FAILURE(m_appWindow->GetWebView()->add_WebResourceRequested( Callback( [this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) diff --git a/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.h b/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.h index 90140a86..7eec6232 100644 --- a/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.h +++ b/SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.h @@ -21,4 +21,6 @@ class ScenarioCustomSchemeNavigate : public ComponentBase EventRegistrationToken m_navigationCompletedToken = {}; AppWindow* m_appWindow = nullptr; + + wil::com_ptr m_webView2_22; }; diff --git a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp index debcb19e..2832399d 100644 --- a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp +++ b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp @@ -17,7 +17,6 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow) { if (m_webView2) { - m_webView2Experimental27 = m_webView2.try_query(); m_webView2_2 = m_webView2.try_query(); m_sampleUri = m_appWindow->GetLocalUri(c_samplePath); @@ -44,18 +43,19 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow) //! [SuppressPolicyForExtension] // This example will register the event with two custom rules. // 1. Suppressing file type policy, security dialog, and allows saving ".eml" files -// directly. -// 2. When the URI is trusted.- Showing customized warning UI when saving ".iso" -// files. It allows to block the saving directly. +// directly; when the URI is trusted. +// 2. Showing customized warning UI when saving ".iso" files. It allows to block +// the saving directly. bool ScenarioFileTypePolicy::SuppressPolicyForExtension() { - if (!m_webView2Experimental27) + m_webView2_26 = m_webView2.try_query(); + if (!m_webView2_26) return false; - m_webView2Experimental27->add_SaveFileSecurityCheckStarting( - Callback( + m_webView2_26->add_SaveFileSecurityCheckStarting( + Callback( [this]( ICoreWebView2* sender, - ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventArgs* args) + ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT { // Get the file extension for file to be saved. @@ -107,9 +107,9 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() ScenarioFileTypePolicy::~ScenarioFileTypePolicy() { - if (m_webView2Experimental27) + if (m_webView2_26) { - CHECK_FAILURE(m_webView2Experimental27->remove_SaveFileSecurityCheckStarting( + CHECK_FAILURE(m_webView2_26->remove_SaveFileSecurityCheckStarting( m_saveFileSecurityCheckStartingToken)); } CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken)); diff --git a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h index 4933fa59..b8a4ad37 100644 --- a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h +++ b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h @@ -21,7 +21,7 @@ class ScenarioFileTypePolicy : public ComponentBase AppWindow* m_appWindow; wil::com_ptr m_webView2; wil::com_ptr m_webView2_2; - wil::com_ptr m_webView2Experimental27; + wil::com_ptr m_webView2_26; EventRegistrationToken m_saveFileSecurityCheckStartingToken = {}; EventRegistrationToken m_DOMcontentLoadedToken = {}; std::wstring m_sampleUri; diff --git a/SampleApps/WebView2APISample/ScenarioScreenCapture.cpp b/SampleApps/WebView2APISample/ScenarioScreenCapture.cpp index bd54afb1..f65fd001 100644 --- a/SampleApps/WebView2APISample/ScenarioScreenCapture.cpp +++ b/SampleApps/WebView2APISample/ScenarioScreenCapture.cpp @@ -19,14 +19,13 @@ ScenarioScreenCapture::ScenarioScreenCapture(AppWindow* appWindow) m_sampleUri = m_appWindow->GetLocalUri(c_samplePath); //! [ScreenCaptureStarting0] - m_webViewExperimental26 = m_webView.try_query(); - if (m_webViewExperimental26) + m_webView2_27 = m_webView.try_query(); + if (m_webView2_27) { - m_webViewExperimental26->add_ScreenCaptureStarting( - Callback( - [this]( - ICoreWebView2* sender, - ICoreWebView2ExperimentalScreenCaptureStartingEventArgs* args) -> HRESULT + m_webView2_27->add_ScreenCaptureStarting( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2ScreenCaptureStartingEventArgs* args) + -> HRESULT { // Get Frame Info wil::com_ptr frameInfo; @@ -121,16 +120,13 @@ ScenarioScreenCapture::ScenarioScreenCapture(AppWindow* appWindow) .Get(), nullptr)); - m_experimentalFrame6 = - webviewFrame.try_query(); + m_frame6 = webviewFrame.try_query(); - m_experimentalFrame6->add_ScreenCaptureStarting( - Callback< - ICoreWebView2ExperimentalFrameScreenCaptureStartingEventHandler>( + m_frame6->add_ScreenCaptureStarting( + Callback( [this]( ICoreWebView2Frame* sender, - ICoreWebView2ExperimentalScreenCaptureStartingEventArgs* args) - -> HRESULT + ICoreWebView2ScreenCaptureStartingEventArgs* args) -> HRESULT { args->put_Handled(TRUE); @@ -243,15 +239,15 @@ ScenarioScreenCapture::~ScenarioScreenCapture() { m_webView->remove_ContentLoading(m_contentLoadingToken); m_webView->remove_WebMessageReceived(m_webMessageReceivedToken); - if (m_webViewExperimental26) + if (m_webView2_27) { - CHECK_FAILURE(m_webViewExperimental26->remove_ScreenCaptureStarting( - m_screenCaptureStartingToken)); + CHECK_FAILURE( + m_webView2_27->remove_ScreenCaptureStarting(m_screenCaptureStartingToken)); } - if (m_experimentalFrame6) + if (m_frame6) { - CHECK_FAILURE(m_experimentalFrame6->remove_ScreenCaptureStarting( - m_frameScreenCaptureStartingToken)); + CHECK_FAILURE( + m_frame6->remove_ScreenCaptureStarting(m_frameScreenCaptureStartingToken)); } if (m_webView4) { diff --git a/SampleApps/WebView2APISample/ScenarioScreenCapture.h b/SampleApps/WebView2APISample/ScenarioScreenCapture.h index 529fc499..42f18ac9 100644 --- a/SampleApps/WebView2APISample/ScenarioScreenCapture.h +++ b/SampleApps/WebView2APISample/ScenarioScreenCapture.h @@ -20,8 +20,8 @@ class ScenarioScreenCapture : public ComponentBase AppWindow* m_appWindow = nullptr; wil::com_ptr m_webView; wil::com_ptr m_webView4; - wil::com_ptr m_webViewExperimental26; - wil::com_ptr m_experimentalFrame6; + wil::com_ptr m_webView2_27; + wil::com_ptr m_frame6; std::wstring m_sampleUri; std::map m_screenCaptureFrameIdPermission; BOOL m_mainFramePermission = TRUE; diff --git a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp index 099e44b2..11a7d544 100644 --- a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp +++ b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp @@ -503,9 +503,6 @@ void ScenarioWebViewEventMonitor::EnableWebResourceRequestedEvent(bool enable) } else if (enable && m_webResourceRequestedToken.value == 0) { - m_webviewEventSource->AddWebResourceRequestedFilter( - L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL); - auto webView2_22 = m_webviewEventSource.try_query(); if (webView2_22) { diff --git a/SampleApps/WebView2APISample/SettingsComponent.cpp b/SampleApps/WebView2APISample/SettingsComponent.cpp index 5f658aa2..5e9d2cd7 100644 --- a/SampleApps/WebView2APISample/SettingsComponent.cpp +++ b/SampleApps/WebView2APISample/SettingsComponent.cpp @@ -40,6 +40,7 @@ SettingsComponent::SettingsComponent( m_webView2_14 = m_webView.try_query(); m_webView2_15 = m_webView.try_query(); m_webView2_18 = m_webView.try_query(); + m_webView2_22 = m_webView.try_query(); // Copy old settings if desired if (old) @@ -1510,8 +1511,10 @@ void SettingsComponent::SetBlockImages(bool blockImages) //! [WebResourceRequested0] if (m_blockImages) { - m_webView->AddWebResourceRequestedFilter( - L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE); + CHECK_FEATURE_RETURN_EMPTY(m_webView2_22); + m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds( + L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT); CHECK_FAILURE(m_webView->add_WebResourceRequested( Callback( [this]( @@ -1561,8 +1564,10 @@ void SettingsComponent::SetReplaceImages(bool replaceImages) //! [WebResourceRequested1] if (m_replaceImages) { - m_webView->AddWebResourceRequestedFilter( - L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE); + CHECK_FEATURE_RETURN_EMPTY(m_webView2_22); + m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds( + L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE, + COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT); CHECK_FAILURE(m_webView->add_WebResourceRequested( Callback( [this]( diff --git a/SampleApps/WebView2APISample/SettingsComponent.h b/SampleApps/WebView2APISample/SettingsComponent.h index 0dbae70e..6946d0f1 100644 --- a/SampleApps/WebView2APISample/SettingsComponent.h +++ b/SampleApps/WebView2APISample/SettingsComponent.h @@ -60,6 +60,7 @@ class SettingsComponent : public ComponentBase wil::com_ptr m_webView2_14; wil::com_ptr m_webView2_15; wil::com_ptr m_webView2_18; + wil::com_ptr m_webView2_22; wil::com_ptr m_settings; wil::com_ptr m_settings2; wil::com_ptr m_settings3; diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index 44ef1b1c..a972b9a4 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -391,6 +391,12 @@ $(OutDir)\assets + + $(OutDir)\assets + + + $(OutDir)\assets + $(OutDir)\assets @@ -480,13 +486,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManager.html b/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManager.html new file mode 100644 index 00000000..00aa0959 --- /dev/null +++ b/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManager.html @@ -0,0 +1,151 @@ + + + + ScenarioServiceWorkerSyncRegistrationManager + + + +

Periodic Background Sync Example

+

Registrations

+

+
+ + + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ +

Background Sync Example

+

Registrations

+

+
+ + +
+ +
+ + +
+
+ +
+ + + + diff --git a/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js b/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js new file mode 100644 index 00000000..4e5fbb2c --- /dev/null +++ b/SampleApps/WebView2APISample/assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js @@ -0,0 +1,12 @@ +function fetchAndCacheLatestNews() { + console.log("Fetched news from a server"); + } + + self.addEventListener("periodicsync", (event) => { + console.log("Periodic Sync Task Tag: " + event.tag + " executed"); + event.waitUntil(fetchAndCacheLatestNews()); + }); + + self.addEventListener("sync", (event) => { + console.log("Background Sync Task Tag: " + event.tag + " executed"); + }); \ No newline at end of file diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index 2bbd0a79..de9ae640 100644 --- a/SampleApps/WebView2APISample/packages.config +++ b/SampleApps/WebView2APISample/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/stdafx.h b/SampleApps/WebView2APISample/stdafx.h index 4c6bb77e..4509b7a4 100644 --- a/SampleApps/WebView2APISample/stdafx.h +++ b/SampleApps/WebView2APISample/stdafx.h @@ -17,5 +17,5 @@ #include #include -#include "webview2experimental.h" +#include "WebView2Experimental.h" #include "WebView2ExperimentalEnvironmentOptions.h" diff --git a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs index dcc87553..68368ee7 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs +++ b/SampleApps/WebView2WindowsFormsBrowser/BrowserForm.cs @@ -180,7 +180,7 @@ private void WebView2Control_CoreWebView2InitializationCompleted(object sender, this.webView2Control.CoreWebView2.SourceChanged += CoreWebView2_SourceChanged; this.webView2Control.CoreWebView2.HistoryChanged += CoreWebView2_HistoryChanged; this.webView2Control.CoreWebView2.DocumentTitleChanged += CoreWebView2_DocumentTitleChanged; - this.webView2Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Image); + this.webView2Control.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.Image, CoreWebView2WebResourceRequestSourceKinds.Document); this.webView2Control.CoreWebView2.ProcessFailed += CoreWebView2_ProcessFailed; this.webView2Control.CoreWebView2.FrameCreated += WebView_HandleIFrames; @@ -583,9 +583,9 @@ private void toggleDefaultScriptDialogsMenuItem_Click(object sender, EventArgs e MessageBox.Show("Default script dialogs will be " + (WebViewSettings.AreDefaultScriptDialogsEnabled ? "enabled" : "disabled"), "after the next navigation."); } - private void addRemoteObjectMenuItem_Click(object sender, EventArgs e) - { - try + private void addRemoteObjectMenuItem_Click(object sender, EventArgs e) + { + try { this.webView2Control.CoreWebView2.AddHostObjectToScript("bridge", new BridgeAddRemoteObject()); } @@ -721,13 +721,13 @@ void HandleWebMessage(CoreWebView2WebMessageReceivedEventArgs args, CoreWebView2 "\\nRight:" + this.webView2Control.Width + "\\nBottom:" + this.webView2Control.Height + "\"}"; - if (frame != null) - { - frame.PostWebMessageAsJson(reply); + if (frame != null) + { + frame.PostWebMessageAsJson(reply); } - else - { - this.webView2Control.CoreWebView2.PostWebMessageAsJson(reply); + else + { + this.webView2Control.CoreWebView2.PostWebMessageAsJson(reply); } } else diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index a4f2bc8b..9f02b96f 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index 386b49cb..6c106371 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -2194,9 +2194,7 @@ void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2Init webView.CoreWebView2.DOMContentLoaded += WebView_PermissionManager_DOMContentLoaded; webView.CoreWebView2.WebMessageReceived += WebView_PermissionManager_WebMessageReceived; -#if USE_WEBVIEW2_EXPERIMENTAL webView.CoreWebView2.ScreenCaptureStarting += WebView_ScreenCaptureStarting; -#endif // The CoreWebView2Environment instance is reused when re-assigning CoreWebView2CreationProperties // to the replacement control. We don't need to re-attach the event handlers unless the environment @@ -3731,7 +3729,6 @@ void ShowWebView() #endif // -#if USE_WEBVIEW2_EXPERIMENTAL private bool isScreenCaptureEnabled = true; void WebView_ScreenCaptureStarting(object sender, CoreWebView2ScreenCaptureStartingEventArgs args) @@ -3746,7 +3743,6 @@ void WebView_ScreenCaptureStarting(object sender, CoreWebView2ScreenCaptureStart args.Cancel = !isScreenCaptureEnabled; } -#endif void TogglScreenCaptureEnabledCmdExecuted(object target, ExecutedRoutedEventArgs e) { @@ -3760,7 +3756,6 @@ void TogglScreenCaptureEnabledCmdExecuted(object target, ExecutedRoutedEventArgs // void FileTypePolicyExecuted(object target, ExecutedRoutedEventArgs e) { -#if USE_WEBVIEW2_EXPERIMENTAL _iWebView2.CoreWebView2.SaveFileSecurityCheckStarting += WebView_SaveFileSecurityCheckStarting; _iWebView2.CoreWebView2.DOMContentLoaded += WebView_FileTypePolicy_DOMContentLoaded; _iWebView2.CoreWebView2.SetVirtualHostNameToFolderMapping( @@ -3768,11 +3763,9 @@ void FileTypePolicyExecuted(object target, ExecutedRoutedEventArgs e) _iWebView2.CoreWebView2.Navigate("https://appassets.example/SecnarioFileTypePolicy.html"); MessageBox.Show("Example rules of Dangerous File Security Policy has been applied in this demo page", "Info"); -#endif } // -#if USE_WEBVIEW2_EXPERIMENTAL // void WebView_SaveFileSecurityCheckStarting(object sender, CoreWebView2SaveFileSecurityCheckStartingEventArgs args) { @@ -3811,7 +3804,6 @@ void WebView_FileTypePolicy_DOMContentLoaded(object sender, CoreWebView2DOMConte } } -#endif void DedicatedWorkerCreatedExecuted(object target, ExecutedRoutedEventArgs e) { diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index dca596c4..b0575eaf 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - + diff --git a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs index 441a6a01..c9d8ada5 100644 --- a/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs +++ b/SampleApps/WebView2_WinUI3_Sample/WebView2_WinUI3_Sample/MainWindow.xaml.cs @@ -17,6 +17,26 @@ public sealed partial class MainWindow : Window public MainWindow() { this.InitializeComponent(); + + Closed += (obj, eventArgs) => + { + if (WebView2 != null) + { + // Ensure that WebView2 resources are released when + // the MainWindow is closed. This fixes an issue where + // the sample app was not properly shutting down when run + // in debug mode from within Visual Studio due to an + // an expected winrt::hresult_error exception in the WinUI3 + // WebView2::CoreWebView2HasInvalidState method. + // (https://github.com/microsoft/microsoft-ui-xaml/blob/main/src/controls/dev/WebView2/WebView2.cpp) + // + // See here for more details regarding the WebView2 + // lifecycle in WinUI3 and the Close() method. + // https://github.com/microsoft/microsoft-ui-xaml/issues/4752#issuecomment-819687363 + WebView2.Close(); + } + }; + AddressBar.Text = "https://developer.microsoft.com/en-us/microsoft-edge/webview2/"; WebView2.NavigationCompleted += WebView2_NavigationCompleted;