Skip to content

Commit fc1269e

Browse files
Update projects to use latest WebView2 SDK 1.0.1466-prerelease (#155)
* Updates for Win32, WPF and WinForms sample apps from 109.0.1466.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.1466-prerelease * HTML files for WebView2WpfBrowser * Re-add the fix for Win7 * one more win7 fix readd Co-authored-by: WebView2 Github Bot <webview2github@microsoft.com>
1 parent 20e62df commit fc1269e

20 files changed

+1656
-151
lines changed

Diff for: SampleApps/WebView2APISample/AppWindow.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "ScenarioExtensionsManagement.h"
3939
#include "ScenarioIFrameDevicePermission.h"
4040
#include "ScenarioNavigateWithWebResourceRequest.h"
41+
#include "ScenarioSharedBuffer.h"
4142
#include "ScenarioSharedWorkerWRR.h"
4243
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
4344
#include "ScenarioVirtualHostMappingForSW.h"
@@ -47,6 +48,7 @@
4748
#include "SettingsComponent.h"
4849
#include "TextInputDialog.h"
4950
#include "ViewComponent.h"
51+
5052
using namespace Microsoft::WRL;
5153
static constexpr size_t s_maxLoadString = 100;
5254
static constexpr UINT s_runAsyncWindowMessage = WM_APP;
@@ -562,6 +564,11 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
562564
NewComponent<ScenarioSharedWorkerWRR>(this);
563565
return true;
564566
}
567+
case IDM_SCENARIO_SHARED_BUFFER:
568+
{
569+
NewComponent<ScenarioSharedBuffer>(this);
570+
return true;
571+
}
565572
case IDM_SCENARIO_DOM_CONTENT_LOADED:
566573
{
567574
NewComponent<ScenarioDOMContentLoaded>(this);
+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
#include "stdafx.h"
6+
7+
#include <iomanip>
8+
#include <sstream>
9+
10+
#include "ScenarioSharedBuffer.h"
11+
12+
#include "AppWindow.h"
13+
#include "CheckFailure.h"
14+
15+
using namespace Microsoft::WRL;
16+
17+
static constexpr WCHAR c_samplePath[] = L"ScenarioSharedBuffer.html";
18+
19+
ScenarioSharedBuffer::ScenarioSharedBuffer(AppWindow* appWindow)
20+
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
21+
{
22+
m_webView18 = m_webView.try_query<ICoreWebView2Experimental18>();
23+
if (!m_webView18)
24+
{
25+
// Feature not supported.
26+
return;
27+
}
28+
29+
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
30+
31+
// Turn off this scenario if we navigate away from the sample page
32+
CHECK_FAILURE(m_webView->add_ContentLoading(
33+
Callback<ICoreWebView2ContentLoadingEventHandler>(
34+
[this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT
35+
{
36+
wil::unique_cotaskmem_string uri;
37+
sender->get_Source(&uri);
38+
if (uri.get() != m_sampleUri)
39+
{
40+
m_appWindow->DeleteComponent(this);
41+
}
42+
return S_OK;
43+
})
44+
.Get(),
45+
&m_contentLoadingToken));
46+
47+
CHECK_FAILURE(m_webView->add_WebMessageReceived(
48+
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
49+
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
50+
{
51+
WebViewMessageReceived(args, false);
52+
return S_OK;
53+
})
54+
.Get(),
55+
&m_webMessageReceivedToken));
56+
57+
wil::com_ptr<ICoreWebView2_4> webview2_4 = m_webView.try_query<ICoreWebView2_4>();
58+
if (webview2_4)
59+
{
60+
CHECK_FAILURE(webview2_4->add_FrameCreated(
61+
Callback<ICoreWebView2FrameCreatedEventHandler>(
62+
[this](
63+
ICoreWebView2* sender, ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT
64+
{
65+
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
66+
CHECK_FAILURE(args->get_Frame(&webviewFrame));
67+
wil::com_ptr<ICoreWebView2Frame2> webviewFrame2 =
68+
webviewFrame.try_query<ICoreWebView2Frame2>();
69+
if (!webviewFrame2)
70+
{
71+
return S_OK;
72+
}
73+
CHECK_FAILURE(webviewFrame2->add_WebMessageReceived(
74+
Microsoft::WRL::Callback<
75+
ICoreWebView2FrameWebMessageReceivedEventHandler>(
76+
[this](
77+
ICoreWebView2Frame* sender,
78+
ICoreWebView2WebMessageReceivedEventArgs* args)
79+
{
80+
WebViewMessageReceived(args, true);
81+
return S_OK;
82+
})
83+
.Get(),
84+
nullptr));
85+
m_webviewFrame4 = webviewFrame.try_query<ICoreWebView2ExperimentalFrame4>();
86+
return S_OK;
87+
})
88+
.Get(),
89+
&m_frameCreatedToken));
90+
}
91+
92+
// Changes to CoreWebView2 settings apply to the next document to which we navigate.
93+
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
94+
}
95+
96+
void ScenarioSharedBuffer::DisplaySharedBufferData()
97+
{
98+
if (!m_sharedBuffer)
99+
{
100+
m_appWindow->AsyncMessageBox(L"Share Buffer not setup", L"Shared Buffer Data");
101+
return;
102+
}
103+
BYTE* buffer = nullptr;
104+
UINT64 size = 0;
105+
CHECK_FAILURE(m_sharedBuffer->get_Buffer(&buffer));
106+
CHECK_FAILURE(m_sharedBuffer->get_Size(&size));
107+
// Display first 256 bytes of the data
108+
std::wstringstream message;
109+
message << L"Share Buffer Data:" << std::endl;
110+
for (int i = 0; i < size && i < 256; ++i)
111+
{
112+
if (isprint(buffer[i]))
113+
message << (char)buffer[i];
114+
else
115+
message << "0x" << std::hex << std::setfill(L'0') << std::setw(2) << buffer[i];
116+
}
117+
message << std::endl;
118+
m_appWindow->AsyncMessageBox(std::move(message.str()), L"Shared Buffer Data");
119+
}
120+
121+
void ScenarioSharedBuffer::WebViewMessageReceived(
122+
ICoreWebView2WebMessageReceivedEventArgs* args, bool fromFrame)
123+
{
124+
wil::unique_cotaskmem_string uri;
125+
CHECK_FAILURE(args->get_Source(&uri));
126+
127+
// Always validate that the origin of the message is what you expect.
128+
if (uri.get() != m_sampleUri)
129+
{
130+
// Ignore messages from untrusted sources.
131+
return;
132+
}
133+
wil::unique_cotaskmem_string messageRaw;
134+
HRESULT hr = args->TryGetWebMessageAsString(&messageRaw);
135+
if (hr == E_INVALIDARG)
136+
{
137+
// Was not a string message. Ignore.
138+
return;
139+
}
140+
// Any other problems are fatal.
141+
CHECK_FAILURE(hr);
142+
std::wstring message = messageRaw.get();
143+
144+
if (message == L"SharedBufferDataUpdated")
145+
{
146+
// Shared buffer updated, display it.
147+
DisplaySharedBufferData();
148+
}
149+
else if (message == L"RequestShareBuffer")
150+
{
151+
EnsureSharedBuffer();
152+
if (fromFrame)
153+
{
154+
m_webviewFrame4->PostSharedBufferToScript(
155+
m_sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_WRITE, nullptr);
156+
}
157+
else
158+
{
159+
m_webView18->PostSharedBufferToScript(
160+
m_sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_WRITE, nullptr);
161+
}
162+
}
163+
else if (message == L"RequestOneTimeShareBuffer")
164+
{
165+
const UINT64 bufferSize = 128;
166+
BYTE data[] = "some read only data";
167+
//! [OneTimeShareBuffer]
168+
wil::com_ptr<ICoreWebView2ExperimentalEnvironment10> environment;
169+
CHECK_FAILURE(
170+
m_appWindow->GetWebViewEnvironment()->QueryInterface(IID_PPV_ARGS(&environment)));
171+
172+
wil::com_ptr<ICoreWebView2ExperimentalSharedBuffer> sharedBuffer;
173+
CHECK_FAILURE(environment->CreateSharedBuffer(bufferSize, &sharedBuffer));
174+
// Set data into the shared memory via IStream.
175+
wil::com_ptr<IStream> stream;
176+
CHECK_FAILURE(sharedBuffer->OpenStream(&stream));
177+
CHECK_FAILURE(stream->Write(data, sizeof(data), nullptr));
178+
PCWSTR additionalDataAsJson = L"{\"myBufferType\":\"bufferType1\"}";
179+
if (fromFrame)
180+
{
181+
m_webviewFrame4->PostSharedBufferToScript(
182+
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
183+
additionalDataAsJson);
184+
}
185+
else
186+
{
187+
m_webView18->PostSharedBufferToScript(
188+
sharedBuffer.get(), COREWEBVIEW2_SHARED_BUFFER_ACCESS_READ_ONLY,
189+
additionalDataAsJson);
190+
}
191+
// Explicitly close the one time shared buffer to ensure that the resource is released.
192+
sharedBuffer->Close();
193+
//! [OneTimeShareBuffer]
194+
}
195+
else
196+
{
197+
// Ignore unrecognized messages, but log for further investigation
198+
// since it suggests a mismatch between the web content and the host.
199+
OutputDebugString(
200+
std::wstring(L"Unexpected message from main page:" + message).c_str());
201+
}
202+
}
203+
204+
void ScenarioSharedBuffer::EnsureSharedBuffer()
205+
{
206+
if (m_sharedBuffer)
207+
{
208+
// already created
209+
return;
210+
}
211+
wil::com_ptr<ICoreWebView2ExperimentalEnvironment10> environment;
212+
CHECK_FAILURE(
213+
m_appWindow->GetWebViewEnvironment()->QueryInterface(IID_PPV_ARGS(&environment)));
214+
215+
const UINT64 size = 128;
216+
CHECK_FAILURE(environment->CreateSharedBuffer(size, &m_sharedBuffer));
217+
// Set some data into the shared memory
218+
BYTE* buffer = nullptr;
219+
CHECK_FAILURE(m_sharedBuffer->get_Buffer(&buffer));
220+
BYTE data[] = "some app data";
221+
memcpy(buffer, data, sizeof(data));
222+
}
223+
224+
ScenarioSharedBuffer::~ScenarioSharedBuffer()
225+
{
226+
m_webView->remove_ContentLoading(m_contentLoadingToken);
227+
m_webView->remove_WebMessageReceived(m_webMessageReceivedToken);
228+
wil::com_ptr<ICoreWebView2_4> webview2_4 = m_webView.try_query<ICoreWebView2_4>();
229+
if (webview2_4)
230+
{
231+
webview2_4->remove_FrameCreated(m_frameCreatedToken);
232+
}
233+
}

Diff for: SampleApps/WebView2APISample/ScenarioSharedBuffer.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
7+
#include "stdafx.h"
8+
9+
#include <string>
10+
11+
#include "AppWindow.h"
12+
#include "ComponentBase.h"
13+
14+
class ScenarioSharedBuffer : public ComponentBase
15+
{
16+
public:
17+
ScenarioSharedBuffer(AppWindow* appWindow);
18+
19+
~ScenarioSharedBuffer() override;
20+
21+
private:
22+
void WebViewMessageReceived(ICoreWebView2WebMessageReceivedEventArgs* args, bool fromFrame);
23+
void EnsureSharedBuffer();
24+
void DisplaySharedBufferData();
25+
26+
AppWindow* m_appWindow;
27+
wil::com_ptr<ICoreWebView2> m_webView;
28+
wil::com_ptr<ICoreWebView2Experimental18> m_webView18;
29+
wil::com_ptr<ICoreWebView2ExperimentalFrame4> m_webviewFrame4;
30+
wil::com_ptr<ICoreWebView2ExperimentalSharedBuffer> m_sharedBuffer;
31+
std::wstring m_sampleUri;
32+
EventRegistrationToken m_webMessageReceivedToken = {};
33+
EventRegistrationToken m_contentLoadingToken = {};
34+
EventRegistrationToken m_frameCreatedToken = {};
35+
};

0 commit comments

Comments
 (0)