Skip to content

WebView2Samples update for 1.0.3171-prerelease #269

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
Mar 10, 2025
Merged
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
67 changes: 63 additions & 4 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
CHECK_FAILURE(m_webView2->Navigate(m_sampleUri.c_str()));
SuppressPolicyForExtension();

ListenToWebMessages();
// Turn off this scenario if we navigate away from the demo page.
CHECK_FAILURE(m_webView2_2->add_DOMContentLoaded(
Callback<ICoreWebView2DOMContentLoadedEventHandler>(
@@ -43,7 +43,7 @@ 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; when the URI is trusted.
// 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()
@@ -55,8 +55,7 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
Callback<ICoreWebView2SaveFileSecurityCheckStartingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args)
-> HRESULT
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT
{
// Get the file extension for file to be saved.
// And convert the extension to lower case for a
@@ -92,6 +91,20 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
CHECK_FAILURE(deferral->Complete());
});
}
if (wcscmp(extension_lower.c_str(), L"exe") == 0)
{
if (is_exe_blocked.has_value())
{
if (is_exe_blocked)
{
args->put_CancelSave(true);
}
else
{
args->put_SuppressDefaultPolicy(true);
}
}
}
return S_OK;
})
.Get(),
@@ -105,12 +118,58 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
}
//! [SuppressPolicyForExtension]

void ScenarioFileTypePolicy::ListenToWebMessages()
{
CHECK_FAILURE(m_webView2->add_WebMessageReceived(
Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
-> HRESULT
{
LPWSTR message;
args->TryGetWebMessageAsString(&message);
ICoreWebView2Settings* settings;
sender->get_Settings(&settings);
ICoreWebView2Settings8* settings8;
settings->QueryInterface(IID_PPV_ARGS(&settings8));
if (wcscmp(message, L"enable_smartscreen") == 0)
{

settings8->put_IsReputationCheckingRequired(true);
MessageBox(
m_appWindow->GetMainWindow(), (L"Enabled Smartscreen"), L"Info", MB_OK);
}
else if (wcscmp(L"disable_smartscreen", message) == 0)
{
settings8->put_IsReputationCheckingRequired(false);
MessageBox(
m_appWindow->GetMainWindow(), (L"Disabled Smartscreen"), L"Info",
MB_OK);
}
else if (wcscmp(L"block_exe", message) == 0)
{
is_exe_blocked = true;
}
else if (wcscmp(L"allow_exe", message) == 0)
{
is_exe_blocked = false;
}
else if (wcscmp(L"clear_exe_policy", message) == 0)
{
is_exe_blocked = std::nullopt;
}
return S_OK;
})
.Get(),
&m_webMessageReceivedToken));
}

ScenarioFileTypePolicy::~ScenarioFileTypePolicy()
{
if (m_webView2_26)
{
CHECK_FAILURE(m_webView2_26->remove_SaveFileSecurityCheckStarting(
m_saveFileSecurityCheckStartingToken));
}
CHECK_FAILURE(m_webView2_2->remove_WebResourceResponseReceived(m_webMessageReceivedToken));
CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken));
}
3 changes: 3 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.h
Original file line number Diff line number Diff line change
@@ -17,12 +17,15 @@ class ScenarioFileTypePolicy : public ComponentBase

private:
bool SuppressPolicyForExtension();
void ListenToWebMessages();

AppWindow* m_appWindow;
wil::com_ptr<ICoreWebView2> m_webView2;
wil::com_ptr<ICoreWebView2_2> m_webView2_2;
wil::com_ptr<ICoreWebView2_26> m_webView2_26;
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
EventRegistrationToken m_DOMcontentLoadedToken = {};
EventRegistrationToken m_webMessageReceivedToken = {};
std::wstring m_sampleUri;
std::optional<bool> is_exe_blocked = std::nullopt;
};
11 changes: 9 additions & 2 deletions SampleApps/WebView2APISample/ViewComponent.cpp
Original file line number Diff line number Diff line change
@@ -157,6 +157,7 @@ ViewComponent::ViewComponent(
-> HRESULT {
HRESULT hr = S_OK;
HCURSOR cursor;

if (!m_useCursorId)
{
CHECK_FAILURE(sender->get_Cursor(&cursor));
@@ -167,18 +168,24 @@ ViewComponent::ViewComponent(
UINT32 cursorId;
CHECK_FAILURE(m_compositionController->get_SystemCursorId(&cursorId));
cursor = ::LoadCursor(nullptr, MAKEINTRESOURCE(cursorId));
if (cursor == nullptr)

if (cursorId != NULL && cursor == nullptr)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
//! [SystemCursorId]
}

if (SUCCEEDED(hr))
if (cursor != nullptr)
{
SetClassLongPtr(
m_appWindow->GetMainWindow(), GCLP_HCURSOR, (LONG_PTR)cursor);
}
else if (SUCCEEDED(hr))
{
SetCursor(NULL);
}

return hr;
})
.Get(),
4 changes: 2 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
@@ -496,13 +496,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.3116-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.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')" />
</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.3116-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.3116-prerelease\build\native\Microsoft.Web.WebView2.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'))" />
</Target>
</Project>
34 changes: 31 additions & 3 deletions SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<html>
<head>
<title>ScenarioFileTypePolicy</title>
<script>
<script>
document.addEventListener('DOMContentLoaded', () => {
async function saveFile() {
let ext = document.getElementById("extensionText");
@@ -16,15 +16,43 @@
}
document.getElementById('showSaveFilePickerButton').addEventListener('click', saveFile);
});
function enable_smartscreen() {
window.chrome.webview.postMessage("enable_smartscreen");
}
function disable_smartscreen() {
window.chrome.webview.postMessage("disable_smartscreen");
}
function allow_exe() {
window.chrome.webview.postMessage("allow_exe");
}
function block_exe() {
window.chrome.webview.postMessage("block_exe");
}
function clear_exe_policy() {
window.chrome.webview.postMessage("clear_exe_policy");
}

</script>
</head>
<body>
<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>Please enter a file extension: <input type="text" id="extensionText" placeholder="try eml or iso" />
<button id="showSaveFilePickerButton">save</button></p>
<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>
<button onclick="disable_smartscreen()">Disable Smartscreen</button>
<br />
<a href="https://appassets.example/bad.exe" id="download_elem" download>Download Flagged file</a>
</body>
</html>
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.3116-prerelease" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="1.0.3171-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
@@ -25,7 +25,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
</ItemGroup>
<ItemGroup>
<Folder Include="assets\" />
5 changes: 4 additions & 1 deletion SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1991,7 +1991,10 @@ async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)

// <Navigate>
// Setting _iWebView2.Source will not trigger a navigation if the Source is the same
// as the previous Source. CoreWebView.Navigate() will always trigger a navigation.
// as the previous Source.CoreWebView.Navigate() will always trigger a navigation apart
// from few cases:
// 1. When called again after adding fragment to the url, or
// 2. When called again on the same fragmented url.
_iWebView2.CoreWebView2.Navigate(uri.ToString());
// </Navigate>
}
2 changes: 1 addition & 1 deletion SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
Loading
Oops, something went wrong.