Skip to content

Commit 22abe8a

Browse files
WebView2Samples update for 1.0.3171-prerelease (#269)
* Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 135.0.3171.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.3171-prerelease --------- Co-authored-by: WebView2 Github Bot <webview2github@microsoft.com>
1 parent 0d3016a commit 22abe8a

9 files changed

+115
-15
lines changed

Diff for: SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp

+63-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
2222
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
2323
CHECK_FAILURE(m_webView2->Navigate(m_sampleUri.c_str()));
2424
SuppressPolicyForExtension();
25-
25+
ListenToWebMessages();
2626
// Turn off this scenario if we navigate away from the demo page.
2727
CHECK_FAILURE(m_webView2_2->add_DOMContentLoaded(
2828
Callback<ICoreWebView2DOMContentLoadedEventHandler>(
@@ -43,7 +43,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
4343
//! [SuppressPolicyForExtension]
4444
// This example will register the event with two custom rules.
4545
// 1. Suppressing file type policy, security dialog, and allows saving ".eml" files
46-
// directly; when the URI is trusted.
46+
// directly; when the URI is trusted.
4747
// 2. Showing customized warning UI when saving ".iso" files. It allows to block
4848
// the saving directly.
4949
bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
@@ -55,8 +55,7 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
5555
Callback<ICoreWebView2SaveFileSecurityCheckStartingEventHandler>(
5656
[this](
5757
ICoreWebView2* sender,
58-
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args)
59-
-> HRESULT
58+
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT
6059
{
6160
// Get the file extension for file to be saved.
6261
// And convert the extension to lower case for a
@@ -92,6 +91,20 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
9291
CHECK_FAILURE(deferral->Complete());
9392
});
9493
}
94+
if (wcscmp(extension_lower.c_str(), L"exe") == 0)
95+
{
96+
if (is_exe_blocked.has_value())
97+
{
98+
if (is_exe_blocked)
99+
{
100+
args->put_CancelSave(true);
101+
}
102+
else
103+
{
104+
args->put_SuppressDefaultPolicy(true);
105+
}
106+
}
107+
}
95108
return S_OK;
96109
})
97110
.Get(),
@@ -105,12 +118,58 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
105118
}
106119
//! [SuppressPolicyForExtension]
107120

121+
void ScenarioFileTypePolicy::ListenToWebMessages()
122+
{
123+
CHECK_FAILURE(m_webView2->add_WebMessageReceived(
124+
Callback<ICoreWebView2WebMessageReceivedEventHandler>(
125+
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
126+
-> HRESULT
127+
{
128+
LPWSTR message;
129+
args->TryGetWebMessageAsString(&message);
130+
ICoreWebView2Settings* settings;
131+
sender->get_Settings(&settings);
132+
ICoreWebView2Settings8* settings8;
133+
settings->QueryInterface(IID_PPV_ARGS(&settings8));
134+
if (wcscmp(message, L"enable_smartscreen") == 0)
135+
{
136+
137+
settings8->put_IsReputationCheckingRequired(true);
138+
MessageBox(
139+
m_appWindow->GetMainWindow(), (L"Enabled Smartscreen"), L"Info", MB_OK);
140+
}
141+
else if (wcscmp(L"disable_smartscreen", message) == 0)
142+
{
143+
settings8->put_IsReputationCheckingRequired(false);
144+
MessageBox(
145+
m_appWindow->GetMainWindow(), (L"Disabled Smartscreen"), L"Info",
146+
MB_OK);
147+
}
148+
else if (wcscmp(L"block_exe", message) == 0)
149+
{
150+
is_exe_blocked = true;
151+
}
152+
else if (wcscmp(L"allow_exe", message) == 0)
153+
{
154+
is_exe_blocked = false;
155+
}
156+
else if (wcscmp(L"clear_exe_policy", message) == 0)
157+
{
158+
is_exe_blocked = std::nullopt;
159+
}
160+
return S_OK;
161+
})
162+
.Get(),
163+
&m_webMessageReceivedToken));
164+
}
165+
108166
ScenarioFileTypePolicy::~ScenarioFileTypePolicy()
109167
{
110168
if (m_webView2_26)
111169
{
112170
CHECK_FAILURE(m_webView2_26->remove_SaveFileSecurityCheckStarting(
113171
m_saveFileSecurityCheckStartingToken));
114172
}
173+
CHECK_FAILURE(m_webView2_2->remove_WebResourceResponseReceived(m_webMessageReceivedToken));
115174
CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken));
116175
}

Diff for: SampleApps/WebView2APISample/ScenarioFileTypePolicy.h

+3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ class ScenarioFileTypePolicy : public ComponentBase
1717

1818
private:
1919
bool SuppressPolicyForExtension();
20+
void ListenToWebMessages();
2021

2122
AppWindow* m_appWindow;
2223
wil::com_ptr<ICoreWebView2> m_webView2;
2324
wil::com_ptr<ICoreWebView2_2> m_webView2_2;
2425
wil::com_ptr<ICoreWebView2_26> m_webView2_26;
2526
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
2627
EventRegistrationToken m_DOMcontentLoadedToken = {};
28+
EventRegistrationToken m_webMessageReceivedToken = {};
2729
std::wstring m_sampleUri;
30+
std::optional<bool> is_exe_blocked = std::nullopt;
2831
};

Diff for: SampleApps/WebView2APISample/ViewComponent.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ ViewComponent::ViewComponent(
157157
-> HRESULT {
158158
HRESULT hr = S_OK;
159159
HCURSOR cursor;
160+
160161
if (!m_useCursorId)
161162
{
162163
CHECK_FAILURE(sender->get_Cursor(&cursor));
@@ -167,18 +168,24 @@ ViewComponent::ViewComponent(
167168
UINT32 cursorId;
168169
CHECK_FAILURE(m_compositionController->get_SystemCursorId(&cursorId));
169170
cursor = ::LoadCursor(nullptr, MAKEINTRESOURCE(cursorId));
170-
if (cursor == nullptr)
171+
172+
if (cursorId != NULL && cursor == nullptr)
171173
{
172174
hr = HRESULT_FROM_WIN32(GetLastError());
173175
}
174176
//! [SystemCursorId]
175177
}
176178

177-
if (SUCCEEDED(hr))
179+
if (cursor != nullptr)
178180
{
179181
SetClassLongPtr(
180182
m_appWindow->GetMainWindow(), GCLP_HCURSOR, (LONG_PTR)cursor);
181183
}
184+
else if (SUCCEEDED(hr))
185+
{
186+
SetCursor(NULL);
187+
}
188+
182189
return hr;
183190
})
184191
.Get(),

Diff for: SampleApps/WebView2APISample/WebView2APISample.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -496,13 +496,13 @@
496496
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
497497
<ImportGroup Label="ExtensionTargets">
498498
<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')" />
499-
<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')" />
499+
<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')" />
500500
</ImportGroup>
501501
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
502502
<PropertyGroup>
503503
<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>
504504
</PropertyGroup>
505505
<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'))" />
506-
<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'))" />
506+
<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'))" />
507507
</Target>
508508
</Project>

Diff for: SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html

+31-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<title>ScenarioFileTypePolicy</title>
5-
<script>
5+
<script>
66
document.addEventListener('DOMContentLoaded', () => {
77
async function saveFile() {
88
let ext = document.getElementById("extensionText");
@@ -16,15 +16,43 @@
1616
}
1717
document.getElementById('showSaveFilePickerButton').addEventListener('click', saveFile);
1818
});
19+
function enable_smartscreen() {
20+
window.chrome.webview.postMessage("enable_smartscreen");
21+
}
22+
function disable_smartscreen() {
23+
window.chrome.webview.postMessage("disable_smartscreen");
24+
}
25+
function allow_exe() {
26+
window.chrome.webview.postMessage("allow_exe");
27+
}
28+
function block_exe() {
29+
window.chrome.webview.postMessage("block_exe");
30+
}
31+
function clear_exe_policy() {
32+
window.chrome.webview.postMessage("clear_exe_policy");
33+
}
34+
1935
</script>
2036
</head>
2137
<body>
2238
<h1>File Type Policy API Demo Page</h1>
2339
<p>Two customized example rules in this demo:</p>
2440
<p>1. Smoothly save *.eml file without file extension warning</p>
2541
<p>2. Intentionally block save *.iso file</p>
26-
<p>Please enter a file extension: <input type="text" id="extensionText" placeholder="try eml or iso" />
27-
<button id="showSaveFilePickerButton">save</button></p>
42+
<p>
43+
Please enter a file extension: <input type="text" id="extensionText" placeholder="try eml or iso" />
44+
<button id="showSaveFilePickerButton">save</button>
45+
</p>
2846
<br>
47+
<hr />
48+
<h2>File Type Policy API for download</h2>
49+
<button onclick="allow_exe()">Allow exe</button>
50+
<button onclick="block_exe()">Block exe</button>
51+
<button onclick="clear_exe_policy()">Clear exe policy</button>
52+
<br />
53+
<button onclick="enable_smartscreen()">Enable Smartscreen</button>
54+
<button onclick="disable_smartscreen()">Disable Smartscreen</button>
55+
<br />
56+
<a href="https://appassets.example/bad.exe" id="download_elem" download>Download Flagged file</a>
2957
</body>
3058
</html>

Diff for: SampleApps/WebView2APISample/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.Web.WebView2" version="1.0.3116-prerelease" targetFramework="native" />
3+
<package id="Microsoft.Web.WebView2" version="1.0.3171-prerelease" targetFramework="native" />
44
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220201.1" targetFramework="native" />
55
</packages>

Diff for: SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<PlatformTarget>AnyCPU</PlatformTarget>
2626
</PropertyGroup>
2727
<ItemGroup>
28-
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
28+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
2929
</ItemGroup>
3030
<ItemGroup>
3131
<Folder Include="assets\" />

Diff for: SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1991,7 +1991,10 @@ async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e)
19911991

19921992
// <Navigate>
19931993
// Setting _iWebView2.Source will not trigger a navigation if the Source is the same
1994-
// as the previous Source. CoreWebView.Navigate() will always trigger a navigation.
1994+
// as the previous Source.CoreWebView.Navigate() will always trigger a navigation apart
1995+
// from few cases:
1996+
// 1. When called again after adding fragment to the url, or
1997+
// 2. When called again on the same fragmented url.
19951998
_iWebView2.CoreWebView2.Navigate(uri.ToString());
19961999
// </Navigate>
19972000
}

Diff for: SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
</Content>
6262
</ItemGroup>
6363
<ItemGroup>
64-
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
64+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3171-prerelease" />
6565
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
6666
</ItemGroup>
6767
</Project>

0 commit comments

Comments
 (0)