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 Visual Hosting testing instructions #236

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 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
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
73 changes: 73 additions & 0 deletions SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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_staging14 =
environment.try_query<ICoreWebView2ExperimentalEnvironment14>();
CHECK_FEATURE_RETURN_HRESULT(environment_staging14);
wil::com_ptr<ICoreWebView2ExperimentalFileSystemHandle> rootHandle;
CHECK_FAILURE(environment_staging14->CreateWebFileSystemDirectoryHandle(
L"C:\\", COREWEBVIEW2_FILE_SYSTEM_HANDLE_PERMISSION_READ_ONLY,
&rootHandle));
wil::com_ptr<ICoreWebView2ExperimentalObjectCollection> webObjectCollection;
IUnknown* webObjects[] = {rootHandle.get()};
CHECK_FAILURE(environment_staging14->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.2432-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.2432-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.2432-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2432-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>
2 changes: 1 addition & 1 deletion SampleApps/WebView2APISample/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Web.WebView2" version="1.0.2415-prerelease" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="1.0.2432-prerelease" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220201.1" targetFramework="native" />
</packages>
Binary file added SampleApps/WebView2APISample/small.ico
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2415-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2432-prerelease" />
</ItemGroup>
<ItemGroup>
<Folder Include="assets\" />
Expand Down
2 changes: 2 additions & 0 deletions SampleApps/WebView2WpfBrowser/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ found in the LICENSE file.

<CommandBinding Command="{x:Static local:MainWindow.PermissionManagementCommand}" Executed="PermissionManagementExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.NotificationReceivedCommand}" Executed="NotificationReceivedExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
<CommandBinding Command="{x:Static local:MainWindow.FileExplorerCommand}" Executed="FileExplorerExecuted" CanExecute="CoreWebView2RequiringCmdsCanExecute"/>
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
Expand Down Expand Up @@ -225,6 +226,7 @@ found in the LICENSE file.
<MenuItem Header="_Shared Buffer" Command="{x:Static local:MainWindow.SharedBufferRequestedCommand}"/>
<MenuItem Header="Permission Management" Command="{x:Static local:MainWindow.PermissionManagementCommand}"/>
<MenuItem Header="Notification Received" Command="{x:Static local:MainWindow.NotificationReceivedCommand}"/>
<MenuItem Header="File-system explorer" Command="{x:Static local:MainWindow.FileExplorerCommand}"/>
</MenuItem>
<MenuItem Header="_Audio">
<MenuItem Header="Toggle Mute State" IsCheckable="True" IsChecked="False" Command="{x:Static local:MainWindow.ToggleMuteStateCommand}"/>
Expand Down
17 changes: 17 additions & 0 deletions SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3317,7 +3317,24 @@ string GetJSONStringField(string jsonMessage, string fieldName)

void FileExplorerExecuted(object target, ExecutedRoutedEventArgs e)
{
#if USE_WEBVIEW2_EXPERIMENTAL
webView.CoreWebView2.NavigationCompleted += delegate (
object webview2, CoreWebView2NavigationCompletedEventArgs args)
{
if (args.IsSuccess && webView.CoreWebView2.Source.Equals("https://appassets.example/ScenarioFileSystemHandleShare.html"))
{
webView.CoreWebView2.PostWebMessageAsJsonWithAdditionalObjects("{ \"messageType\" : \"RootDirectoryHandle\" }", new List<object>()
{
webView.CoreWebView2.Environment.CreateWebFileSystemDirectoryHandle(
"C:\\",
CoreWebView2FileSystemHandlePermission.ReadOnly)
});
}
};
webView.Source = new Uri("https://appassets.example/ScenarioFileSystemHandleShare.html");
#endif
}

void ThrottlingControlExecuted(object target, ExecutedRoutedEventArgs e)
{
}
Expand Down
2 changes: 1 addition & 1 deletion SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2415-prerelease" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2432-prerelease" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
Loading