diff --git a/SampleApps/WebView2APISample/AppWindow.cpp b/SampleApps/WebView2APISample/AppWindow.cpp index 4d47daf2..f692d5ce 100644 --- a/SampleApps/WebView2APISample/AppWindow.cpp +++ b/SampleApps/WebView2APISample/AppWindow.cpp @@ -62,6 +62,7 @@ #include "ScenarioVirtualHostMappingForPopUpWindow.h" #include "ScenarioVirtualHostMappingForSW.h" #include "ScenarioWebMessage.h" +#include "ScenarioWebRtcUdpPortConfiguration.h" #include "ScenarioWebViewEventMonitor.h" #include "ScenarioWindowControlsOverlay.h" #include "ScriptComponent.h" @@ -531,6 +532,11 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam) NewComponent(this); return true; } + case IDM_SCENARIO_WEBRTC_UDP_PORT_CONFIGURATION: + { + NewComponent(this); + return true; + } case IDM_SCENARIO_ADD_HOST_OBJECT: { NewComponent(this); @@ -597,6 +603,21 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam) NewComponent(this, true); return true; } + case IDM_SCENARIO_DRAG_DROP_OVERRIDE: + { + if (m_dcompDevice || m_wincompCompositor) + { + NewComponent(this); + } + else + { + MessageBox( + m_mainWindow, + L"Drag and Drop Override is only supported in visual hosting mode", + L"Drag and Drop Override", MB_OK); + } + return true; + } case IDM_SCENARIO_CUSTOM_SCHEME: { NewComponent(this); @@ -1777,7 +1798,7 @@ void AppWindow::InitializeWebView() args.append( L"--enable-features=ThirdPartyStoragePartitioning,PartitionedCookies," L"msPageInteractionManagerWebview2"); - auto options = Microsoft::WRL::Make(); + auto options = Microsoft::WRL::Make(); options->put_AdditionalBrowserArguments(args.c_str()); CHECK_FAILURE( options->put_AllowSingleSignOnUsingOSPrimaryAccount(m_AADSSOEnabled ? TRUE : FALSE)); @@ -1833,6 +1854,24 @@ void AppWindow::InitializeWebView() CHECK_FAILURE(options8->put_ScrollBarStyle(style)); } + Microsoft::WRL::ComPtr optionsExperimental; + if (options.As(&optionsExperimental) == S_OK) + { + // Configure port ranges for WebRTC UDP traffic to work within enterprise firewalls + // Set UDP port range (example: 50000-55000 for enterprise environments) + const INT32 udpMin = 50000, udpMax = 55000; + + CHECK_FAILURE(optionsExperimental->SetAllowedPortRange( + COREWEBVIEW2_ALLOWED_PORT_RANGE_SCOPE_WEB_RTC, + COREWEBVIEW2_TRANSPORT_PROTOCOL_KIND_UDP, udpMin, udpMax)); + + // Get the configured port range + CHECK_FAILURE(optionsExperimental->GetEffectiveAllowedPortRange( + COREWEBVIEW2_ALLOWED_PORT_RANGE_SCOPE_WEB_RTC, + COREWEBVIEW2_TRANSPORT_PROTOCOL_KIND_UDP, &m_udpPortRange.minPort, + &m_udpPortRange.maxPort)); + } + HRESULT hr = CreateCoreWebView2EnvironmentWithOptions( subFolder, m_userDataFolder.c_str(), options.Get(), Callback( diff --git a/SampleApps/WebView2APISample/AppWindow.h b/SampleApps/WebView2APISample/AppWindow.h index ddfc84cb..92d50a12 100644 --- a/SampleApps/WebView2APISample/AppWindow.h +++ b/SampleApps/WebView2APISample/AppWindow.h @@ -89,6 +89,13 @@ struct SamplePrintSettings std::wstring FooterUri = L""; }; +// Port range configuration +struct PortRangeConfig +{ + INT32 minPort = 0; + INT32 maxPort = 0; +}; + class AppWindow { public: @@ -172,6 +179,12 @@ class AppWindow { return m_webviewOption; } + + const PortRangeConfig& GetUdpPortRange() const + { + return m_udpPortRange; + } + private: static PCWSTR GetWindowClass(); @@ -290,6 +303,7 @@ class AppWindow bool m_CustomCrashReportingEnabled = false; bool m_TrackingPreventionEnabled = true; + PortRangeConfig m_udpPortRange; private: // Fullscreen related code RECT m_previousWindowRect; diff --git a/SampleApps/WebView2APISample/ScenarioDragDropOverride.cpp b/SampleApps/WebView2APISample/ScenarioDragDropOverride.cpp index 12de19a3..7215f5b6 100644 --- a/SampleApps/WebView2APISample/ScenarioDragDropOverride.cpp +++ b/SampleApps/WebView2APISample/ScenarioDragDropOverride.cpp @@ -26,9 +26,8 @@ ScenarioDragDropOverride::ScenarioDragDropOverride(AppWindow* appWindow) return; } - m_compControllerExperimental6 = - compController.try_query(); - if (!m_compControllerExperimental6) + m_compController5 = compController.try_query(); + if (!m_compController5) { return; } @@ -70,11 +69,11 @@ ScenarioDragDropOverride::ScenarioDragDropOverride(AppWindow* appWindow) //! [DragStarting] // Using DragStarting to simply make a synchronous DoDragDrop call instead of // having WebView2 do it. - CHECK_FAILURE(m_compControllerExperimental6->add_DragStarting( - Callback( + CHECK_FAILURE(m_compController5->add_DragStarting( + Callback( [this]( ICoreWebView2CompositionController* sender, - ICoreWebView2ExperimentalDragStartingEventArgs* args) + ICoreWebView2DragStartingEventArgs* args) { if (m_dragOverrideMode != DragOverrideMode::OVERRIDE) { @@ -126,9 +125,9 @@ ScenarioDragDropOverride::ScenarioDragDropOverride(AppWindow* appWindow) ScenarioDragDropOverride::~ScenarioDragDropOverride() { - if (m_compControllerExperimental6) + if (m_compController5) { - CHECK_FAILURE(m_compControllerExperimental6->remove_DragStarting(m_dragStartingToken)); + CHECK_FAILURE(m_compController5->remove_DragStarting(m_dragStartingToken)); } CHECK_FAILURE(m_webView->remove_WebMessageReceived(m_webMessageReceivedToken)); CHECK_FAILURE(m_webView->remove_ContentLoading(m_contentLoadingToken)); diff --git a/SampleApps/WebView2APISample/ScenarioDragDropOverride.h b/SampleApps/WebView2APISample/ScenarioDragDropOverride.h index ff8e1c64..564033df 100644 --- a/SampleApps/WebView2APISample/ScenarioDragDropOverride.h +++ b/SampleApps/WebView2APISample/ScenarioDragDropOverride.h @@ -55,7 +55,7 @@ class ScenarioDragDropOverride : public ComponentBase AppWindow* m_appWindow = nullptr; std::wstring m_sampleUri; wil::com_ptr m_webView = nullptr; - wil::com_ptr m_compControllerExperimental6; + wil::com_ptr m_compController5; wil::com_ptr m_dropSource; DragOverrideMode m_dragOverrideMode = DragOverrideMode::DEFAULT; }; \ No newline at end of file diff --git a/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.cpp b/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.cpp new file mode 100644 index 00000000..3b2809e2 --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.cpp @@ -0,0 +1,48 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "stdafx.h" + +#include "CheckFailure.h" +#include "TextInputDialog.h" + +#include "ScenarioWebRtcUdpPortConfiguration.h" + +using namespace Microsoft::WRL; + +static constexpr WCHAR c_samplePath[] = L"ScenarioWebRtcUdpPortConfiguration.html"; + +ScenarioWebRtcUdpPortConfiguration::ScenarioWebRtcUdpPortConfiguration(AppWindow* appWindow) + : m_appWindow(appWindow), m_webView(appWindow->GetWebView()) +{ + // Demonstrates how to configure a custom WebRTC UDP port range using the + // new ICoreWebView2WebRtcPortConfiguration exposed via + // ICoreWebView2ExperimentalEnvironmentOptions. + + // Navigate to a demo page that will trigger WebRTC usage. + m_sampleUri = m_appWindow->GetLocalUri(c_samplePath); + CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str())); + + // If we navigate away from the demo page, turn off this scenario. + CHECK_FAILURE(m_webView->add_ContentLoading( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* /*args*/) + -> HRESULT + { + wil::unique_cotaskmem_string uri; + CHECK_FAILURE(sender->get_Source(&uri)); + if (uri.get() != m_sampleUri) + { + m_appWindow->DeleteComponent(this); + } + return S_OK; + }) + .Get(), + &m_contentLoadingToken)); +} + +ScenarioWebRtcUdpPortConfiguration::~ScenarioWebRtcUdpPortConfiguration() +{ + CHECK_FAILURE(m_webView->remove_ContentLoading(m_contentLoadingToken)); +} \ No newline at end of file diff --git a/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.h b/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.h new file mode 100644 index 00000000..99dcef1f --- /dev/null +++ b/SampleApps/WebView2APISample/ScenarioWebRtcUdpPortConfiguration.h @@ -0,0 +1,22 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "stdafx.h" + +#include "AppWindow.h" +#include "ComponentBase.h" + +// Demonstrates configuring WebRTC UDP port ranges via WebView2 environment options. +class ScenarioWebRtcUdpPortConfiguration : public ComponentBase +{ +public: + ScenarioWebRtcUdpPortConfiguration(AppWindow* appWindow); + ~ScenarioWebRtcUdpPortConfiguration() override; + +private: + AppWindow* m_appWindow = nullptr; + wil::com_ptr m_webView; + EventRegistrationToken m_contentLoadingToken = {}; + std::wstring m_sampleUri; +}; diff --git a/SampleApps/WebView2APISample/WebView2APISample.rc b/SampleApps/WebView2APISample/WebView2APISample.rc index 15d05621..c3d206cb 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.rc +++ b/SampleApps/WebView2APISample/WebView2APISample.rc @@ -268,6 +268,7 @@ BEGIN MENUITEM "Service worker WebResourceRequested", IDM_SCENARIO_SERVICE_WORKER_WRR MENUITEM "Shared worker WebResourceRequested", IDM_SCENARIO_SHARED_WORKER END + MENUITEM "WebRTC UDP Port Configuration", IDM_SCENARIO_WEBRTC_UDP_PORT_CONFIGURATION POPUP "Custom Download Experience" BEGIN MENUITEM "Use Deferred Download Dialog", IDM_SCENARIO_USE_DEFERRED_DOWNLOAD @@ -301,6 +302,7 @@ BEGIN MENUITEM "Web Messaging", IDM_SCENARIO_POST_WEB_MESSAGE MENUITEM "WebView Event Monitor", IDM_SCENARIO_WEB_VIEW_EVENT_MONITOR MENUITEM "WebView Window Controls Overlay", IDM_SCENARIO_WINDOW_CONTROLS_OVERLAY + MENUITEM "Dropped file path", IDM_SCENARIO_DROPPED_FILE_PATH POPUP "Save As" BEGIN @@ -328,6 +330,7 @@ BEGIN MENUITEM "Listen to new Shared Worker Creations", IDM_SCENARIO_SHARED_WORKER_MANAGER MENUITEM "Get Shared Workers", IDM_SCENARIO_GET_SHARED_WORKERS END + MENUITEM "Drag Drop Override", IDM_SCENARIO_DRAG_DROP_OVERRIDE POPUP "Start Find" BEGIN MENUITEM "Start Find on Page Dialog", IDM_START_FIND diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index b088b82d..8f85c4e3 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -261,6 +261,7 @@ + @@ -324,6 +325,7 @@ + @@ -530,13 +532,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/packages.config b/SampleApps/WebView2APISample/packages.config index 8117e656..69420a64 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/resource.h b/SampleApps/WebView2APISample/resource.h index 3da8309d..89dd78b8 100644 --- a/SampleApps/WebView2APISample/resource.h +++ b/SampleApps/WebView2APISample/resource.h @@ -200,6 +200,7 @@ #define IDM_SCENARIO_GET_SERVICE_WORKER_REGISTERED_FOR_SCOPE 2054 #define IDM_SCENARIO_GET_SHARED_WORKERS 2055 #define IDM_SCENARIO_WINDOW_CONTROLS_OVERLAY 2057 +#define IDM_SCENARIO_DRAG_DROP_OVERRIDE 2058 #define IDC_FIND_TERM 2059 #define IDC_IS_CASE_SENSITIVE 2060 #define IDC_SHOULD_MATCH_WHOLE_WORD 2061 @@ -217,6 +218,7 @@ #define IDM_CREATION_MODE_HOST_INPUT_PROCESSING 3006 #define IDM_SCENARIO_DEDICATED_WORKER_POST_MESSAGE 3009 #define IDM_SCENARIO_SERVICE_WORKER_POST_MESSAGE 3010 +#define IDM_SCENARIO_WEBRTC_UDP_PORT_CONFIGURATION 3011 #define ID_BLOCKEDSITES 32773 #define ID_SETTINGS_NAVIGATETOSTRING 32774 #define ID_ADD_INITIALIZE_SCRIPT 32775 diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index 733822da..a53e9618 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml b/SampleApps/WebView2WpfBrowser/MainWindow.xaml index e20f7281..a36444fe 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml @@ -116,6 +116,7 @@ found in the LICENSE file. + @@ -246,6 +247,9 @@ found in the LICENSE file. + + + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index d1e36e4b..a7f31cda 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -310,7 +310,41 @@ async Task InitializeWebView(IWebView2 webView2) AttachControlEventHandlers(webView2); // Set background transparent webView2.DefaultBackgroundColor = System.Drawing.Color.Transparent; + + // Create environment with options and configure WebRTC UDP port range +#if USE_WEBVIEW2_EXPERIMENTAL + // + CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions(); + + try + { + // Set allowed port range for WebRTC UDP traffic (example: ports 10000-20000) + options.SetAllowedPortRange( + CoreWebView2AllowedPortRangeScope.WebRtc, + CoreWebView2TransportProtocolKind.Udp, + 10000, + 20000); + + } + catch (Exception ex) + { + // Handle any errors setting the port range + System.Diagnostics.Debug.WriteLine($"Failed to set WebRTC UDP port range: {ex.Message}"); + } + string browserExecutableFolder = null; + if (webView2.CreationProperties?.BrowserExecutableFolder != null) + { + browserExecutableFolder = webView2.CreationProperties.BrowserExecutableFolder; + } + + CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(browserExecutableFolder, null, options); + + // Configure WebRTC UDP port range if experimental API is available + await webView2.EnsureCoreWebView2Async(environment); + // +#else await webView2.EnsureCoreWebView2Async(); +#endif } // In general, re-initializing a WebView2 involves creating and initializing a new WebView2, and then @@ -1006,8 +1040,21 @@ void EnhancedSecurityModeSetEnforceListCommandExecuted(object target, ExecutedRo } void WebRtcUdpPortConfigCommandExecuted(object target, ExecutedRoutedEventArgs e) { - MessageBox.Show("WebRTC UDP Port Configuration is only available in staging builds", +#if USE_WEBVIEW2_EXPERIMENTAL + if (_iWebView2?.CoreWebView2 != null) + { + // Navigate to the WebRTC test page to demonstrate port configuration + _iWebView2.CoreWebView2.Navigate("https://appassets.example/ScenarioWebrtcUdpPortConfiguration.html"); + + } + else + { + MessageBox.Show("WebView2 is not initialized.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); + } +#else + MessageBox.Show("WebRTC UDP Port Configuration is only available in prerelease builds", "Feature Not Available", MessageBoxButton.OK, MessageBoxImage.Information); +#endif } async void GetCookiesCmdExecuted(object target, ExecutedRoutedEventArgs e) diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index f2764d52..9e94ff27 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - +