Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Merges vavadevs/WebWindow:master #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
displayName: 'Use .NET Core sdk'
inputs:
packageType: sdk
version: 3.0.100
version: 3.1.101
installationPath: $(Agent.ToolsDirectory)/dotnet
- task: CmdLine@2
displayName: 'Install linux dependencies'
Expand Down
2 changes: 1 addition & 1 deletion src/WebWindow.Blazor.JS/WebWindow.Blazor.JS.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<DefaultItemExcludes>${DefaultItemExcludes};node_modules\**</DefaultItemExcludes>
Expand Down
10 changes: 5 additions & 5 deletions src/WebWindow.Blazor/WebWindow.Blazor.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Title>WebWindow.Blazor</Title>
<PackageDescription>Host a Blazor application inside a native OS window on Windows, Mac, and Linux</PackageDescription>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="$(BlazorPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.1.0-preview4.19579.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.1" />
<EmbeddedResource Include="../WebWindow.Blazor.JS/dist/blazor.desktop.js" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions src/WebWindow.Native/WebWindow.Native.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\..\packages\Microsoft.Web.WebView2.0.8.270\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\..\packages\Microsoft.Web.WebView2.0.8.270\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.190716.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.190716.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="..\..\packages\Microsoft.Web.WebView2.0.8.355\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\..\packages\Microsoft.Web.WebView2.0.8.355\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.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.Web.WebView2.0.8.270\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Web.WebView2.0.8.270\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.190716.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.190716.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Web.WebView2.0.8.355\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Web.WebView2.0.8.355\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.191107.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/WebWindow.Native/WebWindow.Native.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="$(MSBuildThisFileDirectory)$(EffectivePlatform)\WebView2Loader.dll" />
<None Include="packages.config" />
</ItemGroup>
</Project>
163 changes: 91 additions & 72 deletions src/WebWindow.Native/WebWindow.Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,81 +208,99 @@ void WebWindow::AttachWebView()

HRESULT envResult = CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
[&, this](HRESULT result, IWebView2Environment* env) -> HRESULT {
_webviewEnvironment = env;

// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(_hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
[&, this](HRESULT result, IWebView2WebView* webview) -> HRESULT {
if (webview != nullptr) {
_webviewWindow = webview;
[&, this](HRESULT result, IWebView2Environment* env) -> HRESULT
{
_webviewEnvironment = env;

// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(_hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
[&, this](HRESULT result, IWebView2WebView* webview) -> HRESULT
{
if (webview != nullptr)
{
_webviewWindow = static_cast<IWebView2WebView5*>(webview);
}

// Add a few settings for the webview
// this is a redundant demo step as they are the default settings values
IWebView2Settings* Settings;
_webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);

// Register interop APIs
EventRegistrationToken webMessageToken;
_webviewWindow->AddScriptToExecuteOnDocumentCreated(L"window.external = { sendMessage: function(message) { window.chrome.webview.postMessage(message); }, receiveMessage: function(callback) { window.chrome.webview.addEventListener(\'message\', function(e) { callback(e.data); }); } };", nullptr);

_webviewWindow->add_WebMessageReceived(Callback<IWebView2WebMessageReceivedEventHandler>(
[this](IWebView2WebView* webview, IWebView2WebMessageReceivedEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string message;
args->get_WebMessageAsString(&message);
_webMessageReceivedCallback(message.get());
return S_OK;
}).Get(), &webMessageToken);

EventRegistrationToken webResourceRequestedToken;
_webviewWindow->AddWebResourceRequestedFilter(L"*", WEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);

_webviewWindow->add_WebResourceRequested(
Callback<IWebView2WebResourceRequestedEventHandler>(
[this](IWebView2WebView* sender, IWebView2WebResourceRequestedEventArgs* args)
{
IWebView2WebResourceRequest* req;
args->get_Request(&req);

wil::unique_cotaskmem_string uri;
req->get_Uri(&uri);
std::wstring uriString = uri.get();
size_t colonPos = uriString.find(L':', 0);

if (colonPos > 0)
{
std::wstring scheme = uriString.substr(0, colonPos);
WebResourceRequestedCallback handler = _schemeToRequestHandler[scheme];

if (handler != NULL)
{
int numBytes;

AutoString contentType;

wil::unique_cotaskmem dotNetResponse(handler(uriString.c_str(), &numBytes, &contentType));

if (dotNetResponse != nullptr && contentType != nullptr)
{
std::wstring contentTypeWS = contentType;

IStream* dataStream = SHCreateMemStream((BYTE*)dotNetResponse.get(), numBytes);

wil::com_ptr<IWebView2WebResourceResponse> response;

_webviewEnvironment->CreateWebResourceResponse(
dataStream, 200, L"OK", (L"Content-Type: " + contentTypeWS).c_str(), &response);

args->put_Response(response.get());
}
}
}

// Add a few settings for the webview
// this is a redundant demo step as they are the default settings values
IWebView2Settings* Settings;
_webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);

// Register interop APIs
EventRegistrationToken webMessageToken;
_webviewWindow->AddScriptToExecuteOnDocumentCreated(L"window.external = { sendMessage: function(message) { window.chrome.webview.postMessage(message); }, receiveMessage: function(callback) { window.chrome.webview.addEventListener(\'message\', function(e) { callback(e.data); }); } };", nullptr);
_webviewWindow->add_WebMessageReceived(Callback<IWebView2WebMessageReceivedEventHandler>(
[this](IWebView2WebView* webview, IWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string message;
args->get_WebMessageAsString(&message);
_webMessageReceivedCallback(message.get());
return S_OK;
}).Get(), &webMessageToken);

EventRegistrationToken webResourceRequestedToken;
_webviewWindow->add_WebResourceRequested(nullptr, nullptr, 0, Callback<IWebView2WebResourceRequestedEventHandler>(
[this](IWebView2WebView* sender, IWebView2WebResourceRequestedEventArgs* args)
{
IWebView2WebResourceRequest* req;
args->get_Request(&req);

wil::unique_cotaskmem_string uri;
req->get_Uri(&uri);
std::wstring uriString = uri.get();
size_t colonPos = uriString.find(L':', 0);
if (colonPos > 0)
{
std::wstring scheme = uriString.substr(0, colonPos);
WebResourceRequestedCallback handler = _schemeToRequestHandler[scheme];
if (handler != NULL)
{
int numBytes;
AutoString contentType;
wil::unique_cotaskmem dotNetResponse(handler(uriString.c_str(), &numBytes, &contentType));

if (dotNetResponse != nullptr && contentType != nullptr)
{
std::wstring contentTypeWS = contentType;

IStream* dataStream = SHCreateMemStream((BYTE*)dotNetResponse.get(), numBytes);
wil::com_ptr<IWebView2WebResourceResponse> response;
_webviewEnvironment->CreateWebResourceResponse(
dataStream, 200, L"OK", (L"Content-Type: " + contentTypeWS).c_str(),
&response);
args->put_Response(response.get());
}
}
}

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

RefitContent();

flag.clear();
return S_OK;
}).Get());
return S_OK;
}).Get());

}).Get(), &webResourceRequestedToken);

RefitContent();

flag.clear();

return S_OK;

}).Get());

return S_OK;

}).Get());

if (envResult != S_OK)
{
Expand All @@ -295,6 +313,7 @@ void WebWindow::AttachWebView()
// Block until it's ready. This simplifies things for the caller, so they
// don't need to regard this process as async.
MSG msg = { };

while (flag.test_and_set() && GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
Expand Down
2 changes: 1 addition & 1 deletion src/WebWindow.Native/WebWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WebWindow
HWND _hWnd;
WebWindow* _parent;
wil::com_ptr<IWebView2Environment> _webviewEnvironment;
wil::com_ptr<IWebView2WebView> _webviewWindow;
wil::com_ptr<IWebView2WebView5> _webviewWindow;
std::map<std::wstring, WebResourceRequestedCallback> _schemeToRequestHandler;
void AttachWebView();
#elif OS_LINUX
Expand Down
4 changes: 2 additions & 2 deletions src/WebWindow.Native/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="0.8.270" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.190716.2" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="0.8.355" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.191107.2" targetFramework="native" />
</packages>
2 changes: 1 addition & 1 deletion testassets/HelloWorldApp/HelloWorldApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion testassets/MyBlazorApp/MyBlazorApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>WinExe</OutputType>
</PropertyGroup>

Expand Down