Skip to content

Commit 928fe12

Browse files
authored
Update projects to use latest WebView2 SDK 1.0.2470-prerelease (#239)
* 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
1 parent 5396ff5 commit 928fe12

File tree

14 files changed

+499
-174
lines changed

14 files changed

+499
-174
lines changed

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "ScenarioNonClientRegionSupport.h"
4444
#include "ScenarioNotificationReceived.h"
4545
#include "ScenarioPermissionManagement.h"
46+
#include "ScenarioFileSystemHandleShare.h"
4647
#include "ScenarioSharedBuffer.h"
4748
#include "ScenarioSharedWorkerWRR.h"
4849
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
@@ -1333,6 +1334,13 @@ void AppWindow::InitializeWebView()
13331334
CHECK_FAILURE(options6->put_AreBrowserExtensionsEnabled(TRUE));
13341335
}
13351336

1337+
Microsoft::WRL::ComPtr<ICoreWebView2ExperimentalEnvironmentOptions2> exp_options2;
1338+
if (options.As(&exp_options2) == S_OK)
1339+
{
1340+
COREWEBVIEW2_SCROLLBAR_STYLE style = COREWEBVIEW2_SCROLLBAR_STYLE_FLUENT_OVERLAY;
1341+
CHECK_FAILURE(exp_options2->put_ScrollBarStyle(style));
1342+
}
1343+
13361344
HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
13371345
subFolder, m_userDataFolder.c_str(), options.Get(),
13381346
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(

SampleApps/WebView2APISample/ProcessComponent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ProcessComponent::ProcessComponent(AppWindow* appWindow)
107107
CHECK_FAILURE(args2->get_ExitCode(&exitCode));
108108

109109
auto argFailedModule =
110-
args.try_query<ICoreWebView2ExperimentalProcessFailedEventArgs>();
110+
args.try_query<ICoreWebView2ProcessFailedEventArgs3>();
111111
if (argFailedModule)
112112
{
113113
CHECK_FAILURE(
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
#include "stdafx.h"
5+
6+
#include "ScenarioFileSystemHandleShare.h"
7+
8+
#include "AppWindow.h"
9+
#include "CheckFailure.h"
10+
11+
#include <string>
12+
13+
using namespace Microsoft::WRL;
14+
15+
static constexpr WCHAR c_samplePath[] = L"ScenarioFileSystemHandleShare.html";
16+
17+
extern wil::unique_bstr GetDomainOfUri(PWSTR uri);
18+
19+
ScenarioFileSystemHandleShare::ScenarioFileSystemHandleShare(AppWindow* appWindow)
20+
: m_appWindow(appWindow)
21+
{
22+
m_webView = m_appWindow->GetWebView();
23+
24+
CHECK_FAILURE(m_webView->Navigate(m_appWindow->GetLocalUri(c_samplePath).c_str()));
25+
26+
CHECK_FAILURE(m_webView->add_NavigationCompleted(
27+
Callback<ICoreWebView2NavigationCompletedEventHandler>(
28+
[this, appWindow](
29+
ICoreWebView2* sender,
30+
ICoreWebView2NavigationCompletedEventArgs* args) -> HRESULT
31+
{
32+
wil::com_ptr<ICoreWebView2Experimental24> webview24 =
33+
m_webView.try_query<ICoreWebView2Experimental24>();
34+
CHECK_FEATURE_RETURN_HRESULT(webview24);
35+
wil::com_ptr<ICoreWebView2Environment> environment =
36+
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(
43+
L"C:\\", COREWEBVIEW2_FILE_SYSTEM_HANDLE_PERMISSION_READ_ONLY,
44+
&rootHandle));
45+
wil::com_ptr<ICoreWebView2ExperimentalObjectCollection> webObjectCollection;
46+
IUnknown* webObjects[] = {rootHandle.get()};
47+
CHECK_FAILURE(environment_experimental14->CreateObjectCollection(
48+
ARRAYSIZE(webObjects), webObjects, &webObjectCollection));
49+
wil::com_ptr<ICoreWebView2ObjectCollectionView> webObjectCollectionView =
50+
webObjectCollection.try_query<ICoreWebView2ObjectCollectionView>();
51+
wil::unique_cotaskmem_string source;
52+
CHECK_FAILURE(m_webView->get_Source(&source));
53+
54+
static const wchar_t* expectedDomain = L"appassets.example";
55+
wil::unique_bstr sourceDomain = GetDomainOfUri(source.get());
56+
57+
// Check the source to ensure the message is sent to the correct target content.
58+
if (std::wstring(expectedDomain) == sourceDomain.get())
59+
{
60+
CHECK_FAILURE(webview24->PostWebMessageAsJsonWithAdditionalObjects(
61+
L"{ \"messageType\" : \"RootDirectoryHandle\" }",
62+
webObjectCollectionView.get()));
63+
}
64+
65+
return S_OK;
66+
})
67+
.Get(),
68+
&m_navigationCompletedToken));
69+
}
70+
71+
ScenarioFileSystemHandleShare::~ScenarioFileSystemHandleShare()
72+
{
73+
CHECK_FAILURE(m_webView->remove_WebMessageReceived(m_navigationCompletedToken));
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#pragma once
6+
#include "stdafx.h"
7+
8+
#include "AppWindow.h"
9+
#include "ComponentBase.h"
10+
11+
class ScenarioFileSystemHandleShare : public ComponentBase
12+
{
13+
public:
14+
ScenarioFileSystemHandleShare(AppWindow* appWindow);
15+
~ScenarioFileSystemHandleShare() override;
16+
17+
private:
18+
EventRegistrationToken m_navigationCompletedToken = {};
19+
20+
AppWindow* m_appWindow = nullptr;
21+
wil::com_ptr<ICoreWebView2> m_webView = nullptr;
22+
};

SampleApps/WebView2APISample/WebView2APISample.vcxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<ClInclude Include="ScenarioDOMContentLoaded.h" />
238238
<ClInclude Include="ScenarioDragDrop.h" />
239239
<ClInclude Include="ScenarioExtensionsManagement.h" />
240+
<ClInclude Include="ScenarioFileSystemHandleShare.h" />
240241
<ClInclude Include="ScenarioIFrameDevicePermission.h" />
241242
<ClInclude Include="ScenarioNavigateWithWebResourceRequest.h" />
242243
<ClInclude Include="ScenarioNonClientRegionSupport.h" />
@@ -287,6 +288,7 @@
287288
<ClCompile Include="ScenarioDOMContentLoaded.cpp" />
288289
<ClCompile Include="ScenarioDragDrop.cpp" />
289290
<ClCompile Include="ScenarioExtensionsManagement.cpp" />
291+
<ClCompile Include="ScenarioFileSystemHandleShare.cpp" />
290292
<ClCompile Include="ScenarioIFrameDevicePermission.cpp" />
291293
<ClCompile Include="ScenarioNavigateWithWebResourceRequest.cpp" />
292294
<ClCompile Include="ScenarioNonClientRegionSupport.cpp" />
@@ -320,6 +322,7 @@
320322
</ItemGroup>
321323
<ItemGroup>
322324
<Image Include="AppBackground.bmp" />
325+
<Image Include="small.ico" />
323326
<Image Include="WebView2APISample.ico" />
324327
<Image Include="WebView2APISample_Inprivate.ico" />
325328
</ItemGroup>
@@ -354,6 +357,9 @@
354357
<CopyFileToFolders Include="assets/ScenarioDragDrop.html">
355358
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
356359
</CopyFileToFolders>
360+
<CopyFileToFolders Include="assets/ScenarioFileSystemHandleShare.html">
361+
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
362+
</CopyFileToFolders>
357363
<CopyFileToFolders Include="assets/ScenarioIFrameDevicePermission.html">
358364
<DestinationFolders>$(OutDir)\assets</DestinationFolders>
359365
</CopyFileToFolders>
@@ -452,13 +458,13 @@
452458
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
453459
<ImportGroup Label="ExtensionTargets">
454460
<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')" />
455-
<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')" />
461+
<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')" />
456462
</ImportGroup>
457463
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
458464
<PropertyGroup>
459465
<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>
460466
</PropertyGroup>
461467
<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'))" />
462-
<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'))" />
468+
<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'))" />
463469
</Target>
464470
</Project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<style>
6+
#file-explorer {
7+
width: 400px;
8+
height: 300px;
9+
border: 1px solid black;
10+
overflow: auto;
11+
}
12+
13+
#file-explorer li {
14+
list-style: none;
15+
margin: 5px;
16+
padding: 5px;
17+
cursor: pointer;
18+
}
19+
20+
#file-explorer .directory {
21+
background-color: lightblue;
22+
}
23+
24+
#file-explorer .file {
25+
background-color: lightgreen;
26+
}
27+
</style>
28+
</head>
29+
30+
<body>
31+
<h1>File System Explorer</h1>
32+
<button id="browse-root">Browse root</button>
33+
<div id="file-explorer">
34+
<ul id="file-tree"></ul>
35+
</div>
36+
<script>
37+
// A function that creates a list item from a FileSystemHandle
38+
function createListItem(handle) {
39+
var li = document.createElement("li");
40+
li.textContent = handle.name;
41+
li.addEventListener("click", async function (e) {
42+
e.stopPropagation();
43+
if (handle.kind === "directory") {
44+
li.classList.toggle("expanded");
45+
if (li.classList.contains("expanded")) {
46+
var entries = handle.values();
47+
for await (var entry of entries) {
48+
var child = createListItem(entry);
49+
child.classList.add(entry.kind);
50+
li.appendChild(child);
51+
}
52+
} else {
53+
// Remove all children except the first one
54+
while (li.childNodes.length > 1) {
55+
li.removeChild(li.lastChild);
56+
}
57+
}
58+
} else {
59+
// If it is a file, open it in a new window
60+
// Get a file object from the handle
61+
var file = await handle.getFile();
62+
// Create a URL from the file object
63+
var url = URL.createObjectURL(file);
64+
window.open(url);
65+
}
66+
});
67+
return li;
68+
}
69+
function renderFileExplorer(fileSystemHandle) {
70+
var fileExplorer = document.getElementById("file-explorer");
71+
var fileTree = document.getElementById("file-tree");
72+
fileTree.innerHTML = "";
73+
var root = createListItem(fileSystemHandle);
74+
root.classList.add("directory");
75+
fileTree.appendChild(root);
76+
root.id = "root";
77+
}
78+
chrome.webview.addEventListener("message", function (e) {
79+
if (e.data.messageType === "RootDirectoryHandle") {
80+
renderFileExplorer(e.additionalObjects[0]);
81+
}
82+
})
83+
document.addEventListener("DOMContentLoaded", function () {
84+
var browseRoot = document.getElementById("browse-root");
85+
browseRoot.addEventListener("click", async function () {
86+
var dirHandle = await window.showDirectoryPicker();
87+
renderFileExplorer(dirHandle);
88+
});
89+
});
90+
91+
</script>
92+
</body>
93+
94+
</html>

0 commit comments

Comments
 (0)