Skip to content

Commit 02a1ccb

Browse files
Update projects to use latest WebView2 SDK 1.0.2646-prerelease (#247)
* Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 128.0.2646.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.2646-prerelease --------- Co-authored-by: WebView2 Github Bot <webview2github@microsoft.com>
1 parent 906135e commit 02a1ccb

16 files changed

+697
-25
lines changed

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
#include "ScenarioNotificationReceived.h"
4646
#include "ScenarioPermissionManagement.h"
4747
#include "ScenarioSaveAs.h"
48+
#include "ScenarioScreenCapture.h"
4849
#include "ScenarioSharedBuffer.h"
4950
#include "ScenarioSharedWorkerWRR.h"
51+
#include "ScenarioFileTypePolicy.h"
5052
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
5153
#include "ScenarioVirtualHostMappingForSW.h"
5254
#include "ScenarioWebMessage.h"
@@ -665,6 +667,16 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
665667
NewComponent<ScenarioAcceleratorKeyPressed>(this);
666668
return true;
667669
}
670+
case IDM_SCENARIO_SCREEN_CAPTURE:
671+
{
672+
NewComponent<ScenarioScreenCapture>(this);
673+
return true;
674+
}
675+
case IDM_SCENARIO_FILE_TYPE_POLICY:
676+
{
677+
NewComponent<ScenarioFileTypePolicy>(this);
678+
return true;
679+
}
668680
}
669681
return false;
670682
}

SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static constexpr WCHAR c_samplePath[] = L"ScenarioFileSystemHandleShare.html";
1616

1717
extern wil::unique_bstr GetDomainOfUri(PWSTR uri);
1818

19+
//! [PostWebMessageWithAdditionalObjects]
1920
ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindow)
2021
: m_appWindow(appWindow)
2122
{
@@ -29,25 +30,23 @@ ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindo
2930
ICoreWebView2* sender,
3031
ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT
3132
{
32-
wil::com_ptr<ICoreWebView2Experimental24> webview24 =
33-
m_webView.try_query<ICoreWebView2Experimental24>();
34-
CHECK_FEATURE_RETURN_HRESULT(webview24);
33+
wil::com_ptr<ICoreWebView2_23> webview23 =
34+
m_webView.try_query<ICoreWebView2_23>();
35+
CHECK_FEATURE_RETURN_HRESULT(webview23);
3536
wil::com_ptr<ICoreWebView2Environment> environment =
3637
appWindow->GetWebViewEnvironment();
37-
wil::com_ptr<ICoreWebView2ExperimentalEnvironment14>
38-
environment_experimental14 =
39-
environment.try_query<ICoreWebView2ExperimentalEnvironment14>();
40-
CHECK_FEATURE_RETURN_HRESULT(environment_experimental14);
41-
wil::com_ptr<ICoreWebView2ExperimentalFileSystemHandle> rootHandle;
42-
CHECK_FAILURE(environment_experimental14->CreateWebFileSystemDirectoryHandle(
38+
wil::com_ptr<ICoreWebView2Environment14>
39+
environment14 =
40+
environment.try_query<ICoreWebView2Environment14>();
41+
CHECK_FEATURE_RETURN_HRESULT(environment14);
42+
wil::com_ptr<ICoreWebView2FileSystemHandle> rootHandle;
43+
CHECK_FAILURE(environment14->CreateWebFileSystemDirectoryHandle(
4344
L"C:\\", COREWEBVIEW2_FILE_SYSTEM_HANDLE_PERMISSION_READ_ONLY,
4445
&rootHandle));
45-
wil::com_ptr<ICoreWebView2ExperimentalObjectCollection> webObjectCollection;
46+
wil::com_ptr<ICoreWebView2ObjectCollection> webObjectCollection;
4647
IUnknown* webObjects[] = {rootHandle.get()};
47-
CHECK_FAILURE(environment_experimental14->CreateObjectCollection(
48+
CHECK_FAILURE(environment14->CreateObjectCollection(
4849
ARRAYSIZE(webObjects), webObjects, &webObjectCollection));
49-
wil::com_ptr<ICoreWebView2ObjectCollectionView> webObjectCollectionView =
50-
webObjectCollection.try_query<ICoreWebView2ObjectCollectionView>();
5150
wil::unique_cotaskmem_string source;
5251
CHECK_FAILURE(m_webView->get_Source(&source));
5352

@@ -57,16 +56,17 @@ ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindo
5756
// Check the source to ensure the message is sent to the correct target content.
5857
if (std::wstring(expectedDomain) == sourceDomain.get())
5958
{
60-
CHECK_FAILURE(webview24->PostWebMessageAsJsonWithAdditionalObjects(
59+
CHECK_FAILURE(webview23->PostWebMessageAsJsonWithAdditionalObjects(
6160
L"{ \"messageType\" : \"RootDirectoryHandle\" }",
62-
webObjectCollectionView.get()));
61+
webObjectCollection.get()));
6362
}
6463

6564
return S_OK;
6665
})
6766
.Get(),
6867
&m_navigationCompletedToken));
6968
}
69+
//! [PostWebMessageWithAdditionalObjects]
7070

7171
ScenarioFileSystemHandleShare::~ScenarioFileSystemHandleShare()
7272
{
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "stdafx.h"
6+
7+
#include "AppWindow.h"
8+
#include "CheckFailure.h"
9+
#include "ScenarioFileTypePolicy.h"
10+
11+
using namespace Microsoft::WRL;
12+
13+
static constexpr WCHAR c_samplePath[] = L"SecnarioFileTypePolicy.html";
14+
15+
ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
16+
: m_appWindow(appWindow), m_webView2(appWindow->GetWebView())
17+
{
18+
if (m_webView2)
19+
{
20+
m_webView2Experimental27 = m_webView2.try_query<ICoreWebView2Experimental27>();
21+
m_webView2_2 = m_webView2.try_query<ICoreWebView2_2>();
22+
23+
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
24+
CHECK_FAILURE(m_webView2->Navigate(m_sampleUri.c_str()));
25+
SuppressPolicyForExtension();
26+
27+
// Turn off this scenario if we navigate away from the demo page.
28+
CHECK_FAILURE(m_webView2_2->add_DOMContentLoaded(
29+
Callback<ICoreWebView2DOMContentLoadedEventHandler>(
30+
[this](ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args)
31+
-> HRESULT
32+
{
33+
wil::unique_cotaskmem_string uri;
34+
sender->get_Source(&uri);
35+
if (uri.get() != m_sampleUri)
36+
m_appWindow->DeleteComponent(this);
37+
return S_OK;
38+
})
39+
.Get(),
40+
&m_DOMcontentLoadedToken));
41+
}
42+
}
43+
44+
//! [SuppressPolicyForExtension]
45+
// This example will register the event with two custom rules.
46+
// 1. Suppressing file type policy, security dialog, and allows saving ".eml" files
47+
// directly.
48+
// 2. When the URI is trusted.- Showing customized warning UI when saving ".iso"
49+
// files. It allows to block the saving directly.
50+
bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
51+
{
52+
if (!m_webView2Experimental27)
53+
return false;
54+
m_webView2Experimental27->add_SaveFileSecurityCheckStarting(
55+
Callback<ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventHandler>(
56+
[this](
57+
ICoreWebView2* sender,
58+
ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventArgs* args)
59+
-> HRESULT
60+
{
61+
// Get the file extension for file to be saved.
62+
// And convert the extension to lower case for a
63+
// case-insensitive comparasion.
64+
wil::unique_cotaskmem_string extension;
65+
CHECK_FAILURE(args->get_FileExtension(&extension));
66+
std::wstring extension_lower = extension.get();
67+
std::transform(
68+
extension_lower.begin(), extension_lower.end(), extension_lower.begin(),
69+
::towlower);
70+
71+
// Suppress default policy for ".eml" file.
72+
if (wcscmp(extension_lower.c_str(), L".eml") == 0)
73+
{
74+
CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE));
75+
}
76+
77+
// Cancel save/download for ".iso" file.
78+
if (wcscmp(extension_lower.c_str(), L".iso") == 0)
79+
{
80+
wil::com_ptr<ICoreWebView2Deferral> deferral;
81+
CHECK_FAILURE(args->GetDeferral(&deferral));
82+
83+
m_appWindow->RunAsync(
84+
[this, args = wil::make_com_ptr(args), deferral]()
85+
{
86+
// With the deferral, the cancel decision and
87+
// message box can be replaced with a customized UI.
88+
CHECK_FAILURE(args->put_CancelSave(TRUE));
89+
MessageBox(
90+
m_appWindow->GetMainWindow(), L"The saving has been blocked",
91+
L"Info", MB_OK);
92+
CHECK_FAILURE(deferral->Complete());
93+
});
94+
}
95+
return S_OK;
96+
})
97+
.Get(),
98+
&m_saveFileSecurityCheckStartingToken);
99+
100+
MessageBox(
101+
m_appWindow->GetMainWindow(),
102+
(L"Example rules of Dangerous File Security Policy has been applied in this demo page"),
103+
L"Info", MB_OK);
104+
return true;
105+
}
106+
//! [SuppressPolicyForExtension]
107+
108+
ScenarioFileTypePolicy::~ScenarioFileTypePolicy()
109+
{
110+
if (m_webView2Experimental27)
111+
{
112+
CHECK_FAILURE(m_webView2Experimental27->remove_SaveFileSecurityCheckStarting(
113+
m_saveFileSecurityCheckStartingToken));
114+
}
115+
CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken));
116+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) Microsoft Corporation. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "stdafx.h"
6+
7+
#include <string>
8+
9+
#include "AppWindow.h"
10+
#include "ComponentBase.h"
11+
12+
class ScenarioFileTypePolicy : public ComponentBase
13+
{
14+
public:
15+
ScenarioFileTypePolicy(AppWindow* appWindow);
16+
~ScenarioFileTypePolicy();
17+
18+
private:
19+
bool SuppressPolicyForExtension();
20+
21+
AppWindow* m_appWindow;
22+
wil::com_ptr<ICoreWebView2> m_webView2;
23+
wil::com_ptr<ICoreWebView2_2> m_webView2_2;
24+
wil::com_ptr<ICoreWebView2Experimental27> m_webView2Experimental27;
25+
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
26+
EventRegistrationToken m_DOMcontentLoadedToken = {};
27+
std::wstring m_sampleUri;
28+
};

0 commit comments

Comments
 (0)