|
| 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 | +} |
0 commit comments