-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 124…
….0.2470.0
- Loading branch information
1 parent
5396ff5
commit 28d9d3d
Showing
11 changed files
with
495 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
SampleApps/WebView2APISample/ScenarioFileSystemHandleShare.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
SampleApps/WebView2APISample/assets/ScenarioFileSystemHandleShare.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.