From c69b6df305636df231db841283bd7d2b2423711e Mon Sep 17 00:00:00 2001 From: WebView2 Github Bot Date: Tue, 1 Apr 2025 06:55:15 +0000 Subject: [PATCH 1/2] Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 136.0.3230.0 --- .../ScenarioFileTypePolicy.cpp | 28 +++++++++++- .../ScenarioWebViewEventMonitor.cpp | 8 ++-- .../WebView2APISample.vcxproj | 10 ++++- .../ScenarioDedicatedWorkerPostMessage.html | 43 +++++++++++++++++++ .../assets/SecnarioFileTypePolicy.html | 23 ++++++++-- .../assets/dedicated_worker.js | 41 ++++++++++++++++++ .../WebView2WpfBrowser/MainWindow.xaml.cs | 10 ++++- 7 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 SampleApps/WebView2APISample/assets/ScenarioDedicatedWorkerPostMessage.html create mode 100644 SampleApps/WebView2APISample/assets/dedicated_worker.js diff --git a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp index c41cb9b..2dfd4c6 100644 --- a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp +++ b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp @@ -91,11 +91,11 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() CHECK_FAILURE(deferral->Complete()); }); } - if (wcscmp(extension_lower.c_str(), L"exe") == 0) + if (wcscmp(extension_lower.c_str(), L".exe") == 0) { if (is_exe_blocked.has_value()) { - if (is_exe_blocked) + if (is_exe_blocked.value()) { args->put_CancelSave(true); } @@ -105,6 +105,30 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() } } } + if (wcscmp(extension_lower.c_str(), L".emlx") == 0) + { + wil::com_ptr deferral; + CHECK_FAILURE(args->GetDeferral(&deferral)); + m_appWindow->RunAsync( + [this, args = wil::make_com_ptr(args), deferral]() + { + // With the deferral, the cancel decision and + // message box can be replaced with a customized UI. + auto selection = MessageBox( + m_appWindow->GetMainWindow(), L"Block the download?", + L"Info", MB_OKCANCEL); + if (selection == IDOK) + { + CHECK_FAILURE(args->put_CancelSave(TRUE)); + } + else if (selection == IDCANCEL) + { + CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE)); + + } + CHECK_FAILURE(deferral->Complete()); + }); + } return S_OK; }) .Get(), diff --git a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp index 23dabe0..9a01daa 100644 --- a/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp +++ b/SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp @@ -1249,12 +1249,12 @@ void ScenarioWebViewEventMonitor::InitializeFrameEventView( .Get(), NULL); } - auto experimental_frame8 = webviewFrame.try_query(); - if (experimental_frame8) + auto frame7 = webviewFrame.try_query(); + if (frame7) { //! [FrameCreated] - experimental_frame8->add_FrameCreated( - Callback( + frame7->add_FrameCreated( + Callback( [this, depth]( ICoreWebView2Frame* sender, ICoreWebView2FrameCreatedEventArgs* args) noexcept -> HRESULT diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index 54a5e5f..78c0fb7 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -1,4 +1,4 @@ - + @@ -338,6 +338,9 @@ $(OutDir)\assets + + $(OutDir)\assets + $(OutDir)\assets @@ -365,6 +368,9 @@ $(OutDir)\assets + + $(OutDir)\assets + $(OutDir)\assets @@ -505,4 +511,4 @@ - + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/assets/ScenarioDedicatedWorkerPostMessage.html b/SampleApps/WebView2APISample/assets/ScenarioDedicatedWorkerPostMessage.html new file mode 100644 index 0000000..6f2a664 --- /dev/null +++ b/SampleApps/WebView2APISample/assets/ScenarioDedicatedWorkerPostMessage.html @@ -0,0 +1,43 @@ + + + + + ScenarioDedicatedWorkerPostMessage + + + + +
+

DedicatedWorker PostMessage Sample Page

+

This page demonstrates basic interaction between the host app and dedicated workers + by means of Web Messages.

+
+ +

Posting and receiving messages

+

Messages can be posted from the host app to the dedicated worker on this document using + the APIs ICoreWebView2DedicatedWorker::PostWebMessageAsJson and + ICoreWebView2DedicatedWorker::PostWebMessageAsString. +

+ +

Messages can be posted from the dedicated worker to the host app using + self.chrome.webview.postMessage. The host app can receive messages by + registering an event handler with + ICoreWebView2DedicatedWorker::add_WebMessageReceived. +

+ +

How to use the sample?

+

This document creates a dedicated worker that listens for messages from the host app in + the form of a command (ADD, SUB, MUL, DIV) and two numbers (first and second). It + performs the specified arithmetic operation and responds with the result." +

+

When the dedicated worker is created, a dialog will be opened to enter a message. Enter a + JSON string with a command and two numbers to receive a response from the worker. Ex: + {"command":"ADD","first":2,"second":3} will send a reply back with 5. +

+ + + + \ No newline at end of file diff --git a/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html b/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html index 30b159c..4f0c519 100644 --- a/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html +++ b/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html @@ -39,6 +39,18 @@

File Type Policy API Demo Page

Two customized example rules in this demo:

1. Smoothly save *.eml file without file extension warning

2. Intentionally block save *.iso file

+

3. Block or bypass *.exe file based on configuration

+

4. Block or bypass *.exe file based on popup response

+

+ +

+

+ +

+

+ +

+

File Type Policy API for Save File

Please enter a file extension: @@ -46,13 +58,16 @@

File Type Policy API Demo Page



File Type Policy API for download

- - - +
+
+

- Download Flagged file + Download exe file +
+ Download emlx file + diff --git a/SampleApps/WebView2APISample/assets/dedicated_worker.js b/SampleApps/WebView2APISample/assets/dedicated_worker.js new file mode 100644 index 0000000..f19caa7 --- /dev/null +++ b/SampleApps/WebView2APISample/assets/dedicated_worker.js @@ -0,0 +1,41 @@ +//! [chromeWebView] +self.chrome.webview.addEventListener('message', (e) => { + const data = e.data; + if (!data.hasOwnProperty('first') || !data.hasOwnProperty('second') || + !data.hasOwnProperty('command')) { + return; + } + + const first = data.first; + const second = data.second; + switch (data.command) { + case 'ADD': { + result = first + second; + break; + } + case 'SUB': { + result = first - second; + break; + } + case 'MUL': { + result = first * second; + break; + } + case 'DIV': { + if (second === 0) { + result = 'Error: Division by zero'; + break; + } + + result = first / second; + break; + } + default: { + result = 'Failed to process the command'; + } + } + + // Notify the app about the result. + self.chrome.webview.postMessage('Result = ' + result.toString()); +}); +//! [chromeWebView] diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index ef605ed..525aade 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -133,7 +133,9 @@ public partial class MainWindow : Window public static RoutedCommand ServiceWorkerRegisteredCommand = new RoutedCommand(); public static RoutedCommand GetServiceWorkerRegistrationsCommand = new RoutedCommand(); public static RoutedCommand GetServiceWorkerRegisteredForScopeCommand = new RoutedCommand(); + public static RoutedCommand ServiceWorkerPostMessageCommand = new RoutedCommand(); public static RoutedCommand DedicatedWorkerCreatedCommand = new RoutedCommand(); + public static RoutedCommand DedicatedWorkerPostMessageCommand = new RoutedCommand(); public static RoutedCommand SharedWorkerManagerCommand = new RoutedCommand(); public static RoutedCommand GetSharedWorkersCommand = new RoutedCommand(); public static RoutedCommand ServiceWorkerSyncManagerCommand = new RoutedCommand(); @@ -3833,6 +3835,9 @@ void DedicatedWorkerCreatedExecuted(object target, ExecutedRoutedEventArgs e) void RegisterForDedicatedWorkerCreated() { } + void DedicatedWorkerPostMessageExecuted(object target, ExecutedRoutedEventArgs e) + { + } void ServiceWorkerRegisteredExecuted(object target, ExecutedRoutedEventArgs e) { RegisterForServiceWorkerRegistered(); @@ -3859,11 +3864,14 @@ async void GetServiceWorkerRegisteredForScope() { await Task.Delay(0); } + + void ServiceWorkerPostMessageExecuted(object target, ExecutedRoutedEventArgs e) + { + } void SharedWorkerManagerExecuted(object target, ExecutedRoutedEventArgs e) { RegisterForSharedWorkerCreated(); } - void RegisterForSharedWorkerCreated() { } From 3b0e45b21c4c5f9eb9a31c1efe72a1c033d35357 Mon Sep 17 00:00:00 2001 From: WebView2 Github Bot Date: Tue, 1 Apr 2025 06:55:40 +0000 Subject: [PATCH 2/2] Updated package version for Win32, WPF and WinForms sample apps to 1.0.3230-prerelease --- SampleApps/WebView2APISample/WebView2APISample.vcxproj | 8 ++++---- SampleApps/WebView2APISample/packages.config | 2 +- .../WebView2WindowsFormsBrowser.csproj | 2 +- SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index 78c0fb7..43ecbc1 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -1,4 +1,4 @@ - + @@ -502,13 +502,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}. - + - \ No newline at end of file + diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index 25800c6..9a8ba11 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/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index 72660ac..06b36f4 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index dc7af1a..953e467 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - +