Skip to content
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

Update projects to use latest WebView2 SDK 1.0.2646-prerelease #247

Merged
merged 2 commits into from
Jun 19, 2024
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
12 changes: 12 additions & 0 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
#include "ScenarioNotificationReceived.h"
#include "ScenarioPermissionManagement.h"
#include "ScenarioSaveAs.h"
#include "ScenarioScreenCapture.h"
#include "ScenarioSharedBuffer.h"
#include "ScenarioSharedWorkerWRR.h"
#include "ScenarioFileTypePolicy.h"
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
#include "ScenarioVirtualHostMappingForSW.h"
#include "ScenarioWebMessage.h"
Expand Down Expand Up @@ -665,6 +667,16 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
NewComponent<ScenarioAcceleratorKeyPressed>(this);
return true;
}
case IDM_SCENARIO_SCREEN_CAPTURE:
{
NewComponent<ScenarioScreenCapture>(this);
return true;
}
case IDM_SCENARIO_FILE_TYPE_POLICY:
{
NewComponent<ScenarioFileTypePolicy>(this);
return true;
}
}
return false;
}
Expand Down
30 changes: 15 additions & 15 deletions SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ static constexpr WCHAR c_samplePath[] = L"ScenarioFileSystemHandleShare.html";

extern wil::unique_bstr GetDomainOfUri(PWSTR uri);

//! [PostWebMessageWithAdditionalObjects]
ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindow)
: m_appWindow(appWindow)
{
Expand All @@ -29,25 +30,23 @@ ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindo
ICoreWebView2* sender,
ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2Experimental24> webview24 =
m_webView.try_query<ICoreWebView2Experimental24>();
CHECK_FEATURE_RETURN_HRESULT(webview24);
wil::com_ptr<ICoreWebView2_23> webview23 =
m_webView.try_query<ICoreWebView2_23>();
CHECK_FEATURE_RETURN_HRESULT(webview23);
wil::com_ptr<ICoreWebView2Environment> environment =
appWindow->GetWebViewEnvironment();
wil::com_ptr<ICoreWebView2ExperimentalEnvironment14>
environment_experimental14 =
environment.try_query<ICoreWebView2ExperimentalEnvironment14>();
CHECK_FEATURE_RETURN_HRESULT(environment_experimental14);
wil::com_ptr<ICoreWebView2ExperimentalFileSystemHandle> rootHandle;
CHECK_FAILURE(environment_experimental14->CreateWebFileSystemDirectoryHandle(
wil::com_ptr<ICoreWebView2Environment14>
environment14 =
environment.try_query<ICoreWebView2Environment14>();
CHECK_FEATURE_RETURN_HRESULT(environment14);
wil::com_ptr<ICoreWebView2FileSystemHandle> rootHandle;
CHECK_FAILURE(environment14->CreateWebFileSystemDirectoryHandle(
L"C:\\", COREWEBVIEW2_FILE_SYSTEM_HANDLE_PERMISSION_READ_ONLY,
&rootHandle));
wil::com_ptr<ICoreWebView2ExperimentalObjectCollection> webObjectCollection;
wil::com_ptr<ICoreWebView2ObjectCollection> webObjectCollection;
IUnknown* webObjects[] = {rootHandle.get()};
CHECK_FAILURE(environment_experimental14->CreateObjectCollection(
CHECK_FAILURE(environment14->CreateObjectCollection(
ARRAYSIZE(webObjects), webObjects, &webObjectCollection));
wil::com_ptr<ICoreWebView2ObjectCollectionView> webObjectCollectionView =
webObjectCollection.try_query<ICoreWebView2ObjectCollectionView>();
wil::unique_cotaskmem_string source;
CHECK_FAILURE(m_webView->get_Source(&source));

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

return S_OK;
})
.Get(),
&m_navigationCompletedToken));
}
//! [PostWebMessageWithAdditionalObjects]

ScenarioFileSystemHandleShare::~ScenarioFileSystemHandleShare()
{
Expand Down
116 changes: 116 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "stdafx.h"

#include "AppWindow.h"
#include "CheckFailure.h"
#include "ScenarioFileTypePolicy.h"

using namespace Microsoft::WRL;

static constexpr WCHAR c_samplePath[] = L"SecnarioFileTypePolicy.html";

ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView2(appWindow->GetWebView())
{
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);
CHECK_FAILURE(m_webView2->Navigate(m_sampleUri.c_str()));
SuppressPolicyForExtension();

// Turn off this scenario if we navigate away from the demo page.
CHECK_FAILURE(m_webView2_2->add_DOMContentLoaded(
Callback<ICoreWebView2DOMContentLoadedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args)
-> HRESULT
{
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
m_appWindow->DeleteComponent(this);
return S_OK;
})
.Get(),
&m_DOMcontentLoadedToken));
}
}

//! [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.
bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
{
if (!m_webView2Experimental27)
return false;
m_webView2Experimental27->add_SaveFileSecurityCheckStarting(
Callback<ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventArgs* args)
-> HRESULT
{
// Get the file extension for file to be saved.
// And convert the extension to lower case for a
// case-insensitive comparasion.
wil::unique_cotaskmem_string extension;
CHECK_FAILURE(args->get_FileExtension(&extension));
std::wstring extension_lower = extension.get();
std::transform(
extension_lower.begin(), extension_lower.end(), extension_lower.begin(),
::towlower);

// Suppress default policy for ".eml" file.
if (wcscmp(extension_lower.c_str(), L".eml") == 0)
{
CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE));
}

// Cancel save/download for ".iso" file.
if (wcscmp(extension_lower.c_str(), L".iso") == 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.
CHECK_FAILURE(args->put_CancelSave(TRUE));
MessageBox(
m_appWindow->GetMainWindow(), L"The saving has been blocked",
L"Info", MB_OK);
CHECK_FAILURE(deferral->Complete());
});
}
return S_OK;
})
.Get(),
&m_saveFileSecurityCheckStartingToken);

MessageBox(
m_appWindow->GetMainWindow(),
(L"Example rules of Dangerous File Security Policy has been applied in this demo page"),
L"Info", MB_OK);
return true;
}
//! [SuppressPolicyForExtension]

ScenarioFileTypePolicy::~ScenarioFileTypePolicy()
{
if (m_webView2Experimental27)
{
CHECK_FAILURE(m_webView2Experimental27->remove_SaveFileSecurityCheckStarting(
m_saveFileSecurityCheckStartingToken));
}
CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken));
}
28 changes: 28 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileTypePolicy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "stdafx.h"

#include <string>

#include "AppWindow.h"
#include "ComponentBase.h"

class ScenarioFileTypePolicy : public ComponentBase
{
public:
ScenarioFileTypePolicy(AppWindow* appWindow);
~ScenarioFileTypePolicy();

private:
bool SuppressPolicyForExtension();

AppWindow* m_appWindow;
wil::com_ptr<ICoreWebView2> m_webView2;
wil::com_ptr<ICoreWebView2_2> m_webView2_2;
wil::com_ptr<ICoreWebView2Experimental27> m_webView2Experimental27;
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
EventRegistrationToken m_DOMcontentLoadedToken = {};
std::wstring m_sampleUri;
};
Loading
Loading