Skip to content

Commit

Permalink
Update projects to use latest WebView2 SDK 1.0.2470-prerelease (#239)
Browse files Browse the repository at this point in the history
* Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 124.0.2470.0
* Updated package version for Win32, WPF and WinForms sample apps to 1.0.2470-prerelease
  • Loading branch information
oggy22 authored Mar 25, 2024
1 parent 5396ff5 commit 928fe12
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 174 deletions.
8 changes: 8 additions & 0 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "ScenarioNonClientRegionSupport.h"
#include "ScenarioNotificationReceived.h"
#include "ScenarioPermissionManagement.h"
#include "ScenarioFileSystemHandleShare.h"
#include "ScenarioSharedBuffer.h"
#include "ScenarioSharedWorkerWRR.h"
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
Expand Down Expand Up @@ -1333,6 +1334,13 @@ void AppWindow::InitializeWebView()
CHECK_FAILURE(options6->put_AreBrowserExtensionsEnabled(TRUE));
}

Microsoft::WRL::ComPtr<ICoreWebView2ExperimentalEnvironmentOptions2> exp_options2;
if (options.As(&exp_options2) == S_OK)
{
COREWEBVIEW2_SCROLLBAR_STYLE style = COREWEBVIEW2_SCROLLBAR_STYLE_FLUENT_OVERLAY;
CHECK_FAILURE(exp_options2->put_ScrollBarStyle(style));
}

HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
subFolder, m_userDataFolder.c_str(), options.Get(),
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
Expand Down
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/ProcessComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ProcessComponent::ProcessComponent(AppWindow* appWindow)
CHECK_FAILURE(args2->get_ExitCode(&exitCode));

auto argFailedModule =
args.try_query<ICoreWebView2ExperimentalProcessFailedEventArgs>();
args.try_query<ICoreWebView2ProcessFailedEventArgs3>();
if (argFailedModule)
{
CHECK_FAILURE(
Expand Down
74 changes: 74 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 "ScenarioFileSystemHandleShare.h"

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

#include <string>

using namespace Microsoft::WRL;

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

extern wil::unique_bstr GetDomainOfUri(PWSTR uri);

ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindow)
: m_appWindow(appWindow)
{
m_webView = m_appWindow->GetWebView();

CHECK_FAILURE(m_webView->Navigate(m_appWindow->GetLocalUri(c_samplePath).c_str()));

CHECK_FAILURE(m_webView->add_NavigationCompleted(
Callback<ICoreWebView2NavigationCompletedEventHandler>(
[this, appWindow](
ICoreWebView2* sender,
ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2Experimental24> webview24 =
m_webView.try_query<ICoreWebView2Experimental24>();
CHECK_FEATURE_RETURN_HRESULT(webview24);
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(
L"C:\\", COREWEBVIEW2_FILE_SYSTEM_HANDLE_PERMISSION_READ_ONLY,
&rootHandle));
wil::com_ptr<ICoreWebView2ExperimentalObjectCollection> webObjectCollection;
IUnknown* webObjects[] = {rootHandle.get()};
CHECK_FAILURE(environment_experimental14->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));

static const wchar_t* expectedDomain = L"appassets.example";
wil::unique_bstr sourceDomain = GetDomainOfUri(source.get());

// Check the source to ensure the message is sent to the correct target content.
if (std::wstring(expectedDomain) == sourceDomain.get())
{
CHECK_FAILURE(webview24->PostWebMessageAsJsonWithAdditionalObjects(
L"{ \"messageType\" : \"RootDirectoryHandle\" }",
webObjectCollectionView.get()));
}

return S_OK;
})
.Get(),
&m_navigationCompletedToken));
}

ScenarioFileSystemHandleShare::~ScenarioFileSystemHandleShare()
{
CHECK_FAILURE(m_webView->remove_WebMessageReceived(m_navigationCompletedToken));
}
22 changes: 22 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

#pragma once
#include "stdafx.h"

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

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

private:
EventRegistrationToken m_navigationCompletedToken = {};

AppWindow* m_appWindow = nullptr;
wil::com_ptr<ICoreWebView2> m_webView = nullptr;
};
10 changes: 8 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
<ClInclude Include="ScenarioDOMContentLoaded.h" />
<ClInclude Include="ScenarioDragDrop.h" />
<ClInclude Include="ScenarioExtensionsManagement.h" />
<ClInclude Include="ScenarioFileSystemHandleShare.h" />
<ClInclude Include="ScenarioIFrameDevicePermission.h" />
<ClInclude Include="ScenarioNavigateWithWebResourceRequest.h" />
<ClInclude Include="ScenarioNonClientRegionSupport.h" />
Expand Down Expand Up @@ -287,6 +288,7 @@
<ClCompile Include="ScenarioDOMContentLoaded.cpp" />
<ClCompile Include="ScenarioDragDrop.cpp" />
<ClCompile Include="ScenarioExtensionsManagement.cpp" />
<ClCompile Include="ScenarioFileSystemHandleShare.cpp" />
<ClCompile Include="ScenarioIFrameDevicePermission.cpp" />
<ClCompile Include="ScenarioNavigateWithWebResourceRequest.cpp" />
<ClCompile Include="ScenarioNonClientRegionSupport.cpp" />
Expand Down Expand Up @@ -320,6 +322,7 @@
</ItemGroup>
<ItemGroup>
<Image Include="AppBackground.bmp" />
<Image Include="small.ico" />
<Image Include="WebView2APISample.ico" />
<Image Include="WebView2APISample_Inprivate.ico" />
</ItemGroup>
Expand Down Expand Up @@ -354,6 +357,9 @@
<CopyFileToFolders Include="assets/ScenarioDragDrop.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioFileSystemHandleShare.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="assets/ScenarioIFrameDevicePermission.html">
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
</CopyFileToFolders>
Expand Down Expand Up @@ -452,13 +458,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.2415-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.2415-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.2470-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.2470-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.2415-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2415-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.2470-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2470-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!DOCTYPE html>
<html>

<head>
<style>
#file-explorer {
width: 400px;
height: 300px;
border: 1px solid black;
overflow: auto;
}

#file-explorer li {
list-style: none;
margin: 5px;
padding: 5px;
cursor: pointer;
}

#file-explorer .directory {
background-color: lightblue;
}

#file-explorer .file {
background-color: lightgreen;
}
</style>
</head>

<body>
<h1>File System Explorer</h1>
<button id="browse-root">Browse root</button>
<div id="file-explorer">
<ul id="file-tree"></ul>
</div>
<script>
// A function that creates a list item from a FileSystemHandle
function createListItem(handle) {
var li = document.createElement("li");
li.textContent = handle.name;
li.addEventListener("click", async function (e) {
e.stopPropagation();
if (handle.kind === "directory") {
li.classList.toggle("expanded");
if (li.classList.contains("expanded")) {
var entries = handle.values();
for await (var entry of entries) {
var child = createListItem(entry);
child.classList.add(entry.kind);
li.appendChild(child);
}
} else {
// Remove all children except the first one
while (li.childNodes.length > 1) {
li.removeChild(li.lastChild);
}
}
} else {
// If it is a file, open it in a new window
// Get a file object from the handle
var file = await handle.getFile();
// Create a URL from the file object
var url = URL.createObjectURL(file);
window.open(url);
}
});
return li;
}
function renderFileExplorer(fileSystemHandle) {
var fileExplorer = document.getElementById("file-explorer");
var fileTree = document.getElementById("file-tree");
fileTree.innerHTML = "";
var root = createListItem(fileSystemHandle);
root.classList.add("directory");
fileTree.appendChild(root);
root.id = "root";
}
chrome.webview.addEventListener("message", function (e) {
if (e.data.messageType === "RootDirectoryHandle") {
renderFileExplorer(e.additionalObjects[0]);
}
})
document.addEventListener("DOMContentLoaded", function () {
var browseRoot = document.getElementById("browse-root");
browseRoot.addEventListener("click", async function () {
var dirHandle = await window.showDirectoryPicker();
renderFileExplorer(dirHandle);
});
});

</script>
</body>

</html>
Loading

0 comments on commit 928fe12

Please sign in to comment.