Skip to content

Commit

Permalink
Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 131…
Browse files Browse the repository at this point in the history
….0.2895.0
  • Loading branch information
WebView2GithubBot committed Oct 14, 2024
1 parent c05049e commit ce38b81
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 65 deletions.
60 changes: 60 additions & 0 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,66 @@ void AppWindow::RegisterEventHandlers()
nullptr));
//! [NewBrowserVersionAvailable]

//! [RestartRequested]
// After the environment is successfully created,
// register a handler for
auto exp_env15 = m_webViewEnvironment.try_query<ICoreWebView2ExperimentalEnvironment15>();
CHECK_FAILURE(exp_env15->add_RestartRequested(
Callback<ICoreWebView2ExperimentalRestartRequestedEventHandler>(
[this](
ICoreWebView2Environment* sender,
ICoreWebView2ExperimentalRestartRequestedEventArgs* args) -> HRESULT
{
COREWEBVIEW2_RESTART_REQUESTED_PRIORITY priority;
args->get_Priority(&priority);
if (priority == COREWEBVIEW2_RESTART_REQUESTED_PRIORITY_NORMAL)
{
// Remaind user to restart the app when they get a chance.
// Don't force user to restart.
MessageBox(
m_mainWindow, L"Please restart your app when you get a chance",
L"WebView Restart Requested", MB_OK);
}
else if (priority == COREWEBVIEW2_RESTART_REQUESTED_PRIORITY_HIGH)
{
// Don't block the event handler with a message box
RunAsync(
[this]()
{
std::wstring message =
L"We detected there is a critical update for WebView2 runtime.";
if (m_webView)
{
message += L"Do you want to restart the app? \n\n";
message +=
L"Click No if you only want to re-create the webviews. \n";
message += L"Click Cancel for no action. \n";
}
int response = MessageBox(
m_mainWindow, message.c_str(), L"Critical Update Avaliable",
m_webView ? MB_YESNOCANCEL : MB_OK);

if (response == IDYES)
{
RestartApp();
}
else if (response == IDNO)
{
ReinitializeWebViewWithNewBrowser();
}
else
{
// do nothing
}
});
}

return S_OK;
})
.Get(),
nullptr));
//! [RestartRequested]

//! [ProfileDeleted]
auto webView2_13 = m_webView.try_query<ICoreWebView2_13>();
CHECK_FEATURE_RETURN_EMPTY(webView2_13);
Expand Down
7 changes: 5 additions & 2 deletions SampleApps/WebView2APISample/ScenarioCustomScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ using namespace Microsoft::WRL;

ScenarioCustomScheme::ScenarioCustomScheme(AppWindow* appWindow) : m_appWindow(appWindow)
{
CHECK_FAILURE(m_appWindow->GetWebView()->AddWebResourceRequestedFilter(
L"custom-scheme*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL));
m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webView2_22));
CHECK_FEATURE_RETURN_EMPTY(m_webView2_22);
CHECK_FAILURE(m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds(
L"custom-scheme*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL,
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT));
CHECK_FAILURE(m_appWindow->GetWebView()->add_WebResourceRequested(
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args)
Expand Down
2 changes: 2 additions & 0 deletions SampleApps/WebView2APISample/ScenarioCustomScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ class ScenarioCustomScheme : public ComponentBase
EventRegistrationToken m_webResourceRequestedToken = {};
EventRegistrationToken m_navigationCompletedToken = {};

wil::com_ptr<ICoreWebView2_22> m_webView2_22;

AppWindow* m_appWindow = nullptr;
};
7 changes: 5 additions & 2 deletions SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ using namespace Microsoft::WRL;
ScenarioCustomSchemeNavigate::ScenarioCustomSchemeNavigate(AppWindow* appWindow)
: m_appWindow(appWindow)
{
CHECK_FAILURE(m_appWindow->GetWebView()->AddWebResourceRequestedFilter(
L"wv2rocks*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL));
m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webView2_22));
CHECK_FEATURE_RETURN_EMPTY(m_webView2_22);
CHECK_FAILURE(m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds(
L"wv2rocks*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL,
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT));
CHECK_FAILURE(m_appWindow->GetWebView()->add_WebResourceRequested(
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args)
Expand Down
2 changes: 2 additions & 0 deletions SampleApps/WebView2APISample/ScenarioCustomSchemeNavigate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class ScenarioCustomSchemeNavigate : public ComponentBase
EventRegistrationToken m_navigationCompletedToken = {};

AppWindow* m_appWindow = nullptr;

wil::com_ptr<ICoreWebView2_22> m_webView2_22;
};
20 changes: 10 additions & 10 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
{
if (m_webView2)
{
m_webView2Experimental27 = m_webView2.try_query<ICoreWebView2Experimental27>();
m_webView2_2 = m_webView2.try_query<ICoreWebView2_2>();

m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
Expand All @@ -44,18 +43,19 @@ 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.
// 2. When the URI is trusted.- Showing customized warning UI when saving ".iso"
// files. It allows to block the saving directly.
// 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()
{
if (!m_webView2Experimental27)
m_webView2_26 = m_webView2.try_query<ICoreWebView2_26>();
if (!m_webView2_26)
return false;
m_webView2Experimental27->add_SaveFileSecurityCheckStarting(
Callback<ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventHandler>(
m_webView2_26->add_SaveFileSecurityCheckStarting(
Callback<ICoreWebView2SaveFileSecurityCheckStartingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventArgs* args)
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args)
-> HRESULT
{
// Get the file extension for file to be saved.
Expand Down Expand Up @@ -107,9 +107,9 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()

ScenarioFileTypePolicy::~ScenarioFileTypePolicy()
{
if (m_webView2Experimental27)
if (m_webView2_26)
{
CHECK_FAILURE(m_webView2Experimental27->remove_SaveFileSecurityCheckStarting(
CHECK_FAILURE(m_webView2_26->remove_SaveFileSecurityCheckStarting(
m_saveFileSecurityCheckStartingToken));
}
CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken));
Expand Down
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/ScenarioFileTypePolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ScenarioFileTypePolicy : public ComponentBase
AppWindow* m_appWindow;
wil::com_ptr<ICoreWebView2> m_webView2;
wil::com_ptr<ICoreWebView2_2> m_webView2_2;
wil::com_ptr<ICoreWebView2Experimental27> m_webView2Experimental27;
wil::com_ptr<ICoreWebView2_26> m_webView2_26;
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
EventRegistrationToken m_DOMcontentLoadedToken = {};
std::wstring m_sampleUri;
Expand Down
36 changes: 16 additions & 20 deletions SampleApps/WebView2APISample/ScenarioScreenCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ ScenarioScreenCapture::ScenarioScreenCapture(AppWindow* appWindow)
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);

//! [ScreenCaptureStarting0]
m_webViewExperimental26 = m_webView.try_query<ICoreWebView2Experimental26>();
if (m_webViewExperimental26)
m_webView2_27 = m_webView.try_query<ICoreWebView2_27>();
if (m_webView2_27)
{
m_webViewExperimental26->add_ScreenCaptureStarting(
Callback<ICoreWebView2ExperimentalScreenCaptureStartingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ExperimentalScreenCaptureStartingEventArgs* args) -> HRESULT
m_webView2_27->add_ScreenCaptureStarting(
Callback<ICoreWebView2ScreenCaptureStartingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2ScreenCaptureStartingEventArgs* args)
-> HRESULT
{
// Get Frame Info
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo;
Expand Down Expand Up @@ -121,16 +120,13 @@ ScenarioScreenCapture::ScenarioScreenCapture(AppWindow* appWindow)
.Get(),
nullptr));

m_experimentalFrame6 =
webviewFrame.try_query<ICoreWebView2ExperimentalFrame6>();
m_frame6 = webviewFrame.try_query<ICoreWebView2Frame6>();

m_experimentalFrame6->add_ScreenCaptureStarting(
Callback<
ICoreWebView2ExperimentalFrameScreenCaptureStartingEventHandler>(
m_frame6->add_ScreenCaptureStarting(
Callback<ICoreWebView2FrameScreenCaptureStartingEventHandler>(
[this](
ICoreWebView2Frame* sender,
ICoreWebView2ExperimentalScreenCaptureStartingEventArgs* args)
-> HRESULT
ICoreWebView2ScreenCaptureStartingEventArgs* args) -> HRESULT
{
args->put_Handled(TRUE);

Expand Down Expand Up @@ -243,15 +239,15 @@ ScenarioScreenCapture::~ScenarioScreenCapture()
{
m_webView->remove_ContentLoading(m_contentLoadingToken);
m_webView->remove_WebMessageReceived(m_webMessageReceivedToken);
if (m_webViewExperimental26)
if (m_webView2_27)
{
CHECK_FAILURE(m_webViewExperimental26->remove_ScreenCaptureStarting(
m_screenCaptureStartingToken));
CHECK_FAILURE(
m_webView2_27->remove_ScreenCaptureStarting(m_screenCaptureStartingToken));
}
if (m_experimentalFrame6)
if (m_frame6)
{
CHECK_FAILURE(m_experimentalFrame6->remove_ScreenCaptureStarting(
m_frameScreenCaptureStartingToken));
CHECK_FAILURE(
m_frame6->remove_ScreenCaptureStarting(m_frameScreenCaptureStartingToken));
}
if (m_webView4)
{
Expand Down
4 changes: 2 additions & 2 deletions SampleApps/WebView2APISample/ScenarioScreenCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class ScenarioScreenCapture : public ComponentBase
AppWindow* m_appWindow = nullptr;
wil::com_ptr<ICoreWebView2> m_webView;
wil::com_ptr<ICoreWebView2_4> m_webView4;
wil::com_ptr<ICoreWebView2Experimental26> m_webViewExperimental26;
wil::com_ptr<ICoreWebView2ExperimentalFrame6> m_experimentalFrame6;
wil::com_ptr<ICoreWebView2_27> m_webView2_27;
wil::com_ptr<ICoreWebView2Frame6> m_frame6;
std::wstring m_sampleUri;
std::map<int, BOOL> m_screenCaptureFrameIdPermission;
BOOL m_mainFramePermission = TRUE;
Expand Down
3 changes: 0 additions & 3 deletions SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,6 @@ void ScenarioWebViewEventMonitor::EnableWebResourceRequestedEvent(bool enable)
}
else if (enable && m_webResourceRequestedToken.value == 0)
{
m_webviewEventSource->AddWebResourceRequestedFilter(
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);

auto webView2_22 = m_webviewEventSource.try_query<ICoreWebView2_22>();
if (webView2_22)
{
Expand Down
13 changes: 9 additions & 4 deletions SampleApps/WebView2APISample/SettingsComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SettingsComponent::SettingsComponent(
m_webView2_14 = m_webView.try_query<ICoreWebView2_14>();
m_webView2_15 = m_webView.try_query<ICoreWebView2_15>();
m_webView2_18 = m_webView.try_query<ICoreWebView2_18>();
m_webView2_22 = m_webView.try_query<ICoreWebView2_22>();

// Copy old settings if desired
if (old)
Expand Down Expand Up @@ -1510,8 +1511,10 @@ void SettingsComponent::SetBlockImages(bool blockImages)
//! [WebResourceRequested0]
if (m_blockImages)
{
m_webView->AddWebResourceRequestedFilter(
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE);
CHECK_FEATURE_RETURN_EMPTY(m_webView2_22);
m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds(
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE,
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT);
CHECK_FAILURE(m_webView->add_WebResourceRequested(
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](
Expand Down Expand Up @@ -1561,8 +1564,10 @@ void SettingsComponent::SetReplaceImages(bool replaceImages)
//! [WebResourceRequested1]
if (m_replaceImages)
{
m_webView->AddWebResourceRequestedFilter(
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE);
CHECK_FEATURE_RETURN_EMPTY(m_webView2_22);
m_webView2_22->AddWebResourceRequestedFilterWithRequestSourceKinds(
L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE,
COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_DOCUMENT);
CHECK_FAILURE(m_webView->add_WebResourceRequested(
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](
Expand Down
1 change: 1 addition & 0 deletions SampleApps/WebView2APISample/SettingsComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SettingsComponent : public ComponentBase
wil::com_ptr<ICoreWebView2_14> m_webView2_14;
wil::com_ptr<ICoreWebView2_15> m_webView2_15;
wil::com_ptr<ICoreWebView2_18> m_webView2_18;
wil::com_ptr<ICoreWebView2_22> m_webView2_22;
wil::com_ptr<ICoreWebView2Settings> m_settings;
wil::com_ptr<ICoreWebView2Settings2> m_settings2;
wil::com_ptr<ICoreWebView2Settings3> m_settings3;
Expand Down
10 changes: 8 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
Expand Down Expand Up @@ -391,6 +391,12 @@
<CopyFileToFolders Include="assets/ScenarioScreenCaptureIFrame2.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioServiceWorkerSyncRegistrationManager.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioServiceWorkerSyncRegistrationManagerServiceWorker.js">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioSharedBuffer.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
Expand Down Expand Up @@ -489,4 +495,4 @@
<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.2839-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2839-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
</Project>
Loading

0 comments on commit ce38b81

Please sign in to comment.