Skip to content

WebView2Samples update for 1.0.3230-prerelease #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -105,6 +105,30 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
}
}
}
if (wcscmp(extension_lower.c_str(), L".emlx") == 0)
{
wil::com_ptr<ICoreWebView2Deferral> 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(),
Expand Down
8 changes: 4 additions & 4 deletions SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,12 +1249,12 @@ void ScenarioWebViewEventMonitor::InitializeFrameEventView(
.Get(),
NULL);
}
auto experimental_frame8 = webviewFrame.try_query<ICoreWebView2ExperimentalFrame8>();
if (experimental_frame8)
auto frame7 = webviewFrame.try_query<ICoreWebView2Frame7>();
if (frame7)
{
//! [FrameCreated]
experimental_frame8->add_FrameCreated(
Callback<ICoreWebView2ExperimentalFrameChildFrameCreatedEventHandler>(
frame7->add_FrameCreated(
Callback<ICoreWebView2FrameChildFrameCreatedEventHandler>(
[this, depth](
ICoreWebView2Frame* sender,
ICoreWebView2FrameCreatedEventArgs* args) noexcept -> HRESULT
Expand Down
10 changes: 8 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@
<CopyFileToFolders Include="assets/AppStartPageBackground.png">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/dedicated_worker.js">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/DemoWorker.js">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
Expand Down Expand Up @@ -365,6 +368,9 @@
<CopyFileToFolders Include="assets/ScenarioCustomScheme.json">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioDedicatedWorkerPostMessage.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioDragDrop.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
Expand Down Expand Up @@ -496,13 +502,13 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.3171-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.3171-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.3230-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.3230-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.3171-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.3171-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.3230-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.3230-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>

<head>
<title>ScenarioDedicatedWorkerPostMessage</title>
<script>
"use strict";
const worker = new Worker("dedicated_worker.js");
</script>
</head>

<body>
<div>
<h1>DedicatedWorker PostMessage Sample Page</h1>
<p>This page demonstrates basic interaction between the host app and dedicated workers
by means of Web Messages.</p>
</div>

<h2>Posting and receiving messages</h2>
<p>Messages can be posted from the host app to the dedicated worker on this document using
the APIs <code>ICoreWebView2DedicatedWorker::PostWebMessageAsJson</code> and
<code>ICoreWebView2DedicatedWorker::PostWebMessageAsString</code>.
</p>

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

<h2>How to use the sample?</h2>
<p>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."
</p>
<p>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:
<code>{"command":"ADD","first":2,"second":3}</code> will send a reply back with 5.
</p>

</body>

</html>
23 changes: 19 additions & 4 deletions SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,35 @@ <h1>File Type Policy API Demo Page</h1>
<p>Two customized example rules in this demo:</p>
<p>1. Smoothly save *.eml file without file extension warning</p>
<p>2. Intentionally block save *.iso file</p>
<p>3. Block or bypass *.exe file based on configuration</p>
<p>4. Block or bypass *.exe file based on popup response</p>
<p>
<button onclick="allow_exe()">Allow exe</button>
</p>
<p>
<button onclick="block_exe()">Block exe</button>
</p>
<p>
<button onclick="clear_exe_policy()">Clear exe policy</button>
</p>
<h2>File Type Policy API for Save File</h2>
<p>
Please enter a file extension: <input type="text" id="extensionText" placeholder="try eml or iso" />
<button id="showSaveFilePickerButton">save</button>
</p>
<br>
<hr />
<h2>File Type Policy API for download</h2>
<button onclick="allow_exe()">Allow exe</button>
<button onclick="block_exe()">Block exe</button>
<button onclick="clear_exe_policy()">Clear exe policy</button>

<br />
<button onclick="enable_smartscreen()">Enable Smartscreen</button>
<br />
<br />
<button onclick="disable_smartscreen()">Disable Smartscreen</button>
<br />
<a href="https://appassets.example/bad.exe" id="download_elem" download>Download Flagged file</a>
<a href="https://appassets.example/sample.exe" id="download_exe" download>Download exe file</a>
<br />
<a href="https://appassets.example/sample.emlx" id="download_emlx" download>Download emlx file</a>

</body>
</html>
41 changes: 41 additions & 0 deletions SampleApps/WebView2APISample/assets/dedicated_worker.js
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.WebView2" version="1.0.3171-prerelease" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="1.0.3230-prerelease" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220201.1" targetFramework="native" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3230-prerelease" />
</ItemGroup>
<ItemGroup>
<Folder Include="assets\" />
Expand Down
10 changes: 9 additions & 1 deletion SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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()
{
}
Expand Down
2 changes: 1 addition & 1 deletion SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3230-prerelease" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
Loading