Skip to content
This repository was archived by the owner on Jun 5, 2023. It is now read-only.
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
4 changes: 2 additions & 2 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.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')" />
<Import Project="..\..\packages\Microsoft.Web.WebView2.0.9.579-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\..\packages\Microsoft.Web.WebView2.0.9.579-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.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'))" />
<Error Condition="!Exists('..\..\packages\Microsoft.Web.WebView2.0.9.579-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Web.WebView2.0.9.579-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
45 changes: 26 additions & 19 deletions src/WebWindow.Native/WebWindow.Windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

void WebWindow::RefitContent()
{
if (_webviewWindow)
if (_webviewController)
{
RECT bounds;
GetClientRect(_hWnd, &bounds);
_webviewWindow->put_Bounds(bounds);
_webviewController->put_Bounds(bounds);
}
}

Expand All @@ -162,7 +162,7 @@ void WebWindow::Show()
// Strangely, it only works to create the webview2 *after* the window has been shown,
// so defer it until here. This unfortunately means you can't call the Navigate methods
// until the window is shown.
if (!_webviewWindow)
if (!_webviewController)
{
AttachWebView();
}
Expand Down Expand Up @@ -206,25 +206,32 @@ void WebWindow::AttachWebView()
std::atomic_flag flag = ATOMIC_FLAG_INIT;
flag.test_and_set();

HRESULT envResult = CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
[&, this](HRESULT result, IWebView2Environment* env) -> HRESULT {
HRESULT envResult = CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[&, this](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
if (result != S_OK) { return result; }
HRESULT envResult = env->QueryInterface(&_webviewEnvironment);
if (envResult != S_OK)
{
return envResult;
}

// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(_hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
[&, this](HRESULT result, IWebView2WebView* webview) -> HRESULT {
if (result != S_OK) { return result; }
result = webview->QueryInterface(&_webviewWindow);
env->CreateCoreWebView2Controller(_hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[&, this](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {

if (result != S_OK) { return result; }

HRESULT envResult = controller->QueryInterface(&_webviewController);
if (envResult != S_OK)
{
return envResult;
}
_webviewController->get_CoreWebView2(&_webviewWindow);

// Add a few settings for the webview
// this is a redundant demo step as they are the default settings values
IWebView2Settings* Settings;
ICoreWebView2Settings* Settings;
_webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Expand All @@ -233,20 +240,20 @@ void WebWindow::AttachWebView()
// 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 {
_webviewWindow->add_WebMessageReceived(Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string message;
args->get_WebMessageAsString(&message);
args->TryGetWebMessageAsString(&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)
_webviewWindow->AddWebResourceRequestedFilter(L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
_webviewWindow->add_WebResourceRequested(Callback<ICoreWebView2WebResourceRequestedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args)
{
IWebView2WebResourceRequest* req;
ICoreWebView2WebResourceRequest* req;
args->get_Request(&req);

wil::unique_cotaskmem_string uri;
Expand All @@ -268,7 +275,7 @@ void WebWindow::AttachWebView()
std::wstring contentTypeWS = contentType;

IStream* dataStream = SHCreateMemStream((BYTE*)dotNetResponse.get(), numBytes);
wil::com_ptr<IWebView2WebResourceResponse> response;
wil::com_ptr<ICoreWebView2WebResourceResponse> response;
_webviewEnvironment->CreateWebResourceResponse(
dataStream, 200, L"OK", (L"Content-Type: " + contentTypeWS).c_str(),
&response);
Expand Down
8 changes: 5 additions & 3 deletions src/WebWindow.Native/WebWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

#ifdef _WIN32
#include <Windows.h>
#include <wrl/event.h>
#include <stdlib.h>
#include <wrl.h>
#include <map>
#include <string>
#include <wil/com.h>
Expand Down Expand Up @@ -42,8 +43,9 @@ class WebWindow
static HINSTANCE _hInstance;
HWND _hWnd;
WebWindow* _parent;
wil::com_ptr<IWebView2Environment3> _webviewEnvironment;
wil::com_ptr<IWebView2WebView5> _webviewWindow;
wil::com_ptr<ICoreWebView2Environment> _webviewEnvironment;
wil::com_ptr<ICoreWebView2> _webviewWindow;
wil::com_ptr<ICoreWebView2Controller> _webviewController;
std::map<std::wstring, WebResourceRequestedCallback> _schemeToRequestHandler;
void AttachWebView();
#elif OS_LINUX
Expand Down
2 changes: 1 addition & 1 deletion 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.355" targetFramework="native" />
<package id="Microsoft.Web.WebView2" version="0.9.579-prerelease" targetFramework="native" />
<package id="Microsoft.Windows.ImplementationLibrary" version="1.0.191107.2" targetFramework="native" />
</packages>