-
Notifications
You must be signed in to change notification settings - Fork 458
/
ScenarioWebMessage.cpp
92 lines (78 loc) · 3.26 KB
/
ScenarioWebMessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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 "ScenarioWebMessage.h"
#include "AppWindow.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioWebMessage.html";
ScenarioWebMessage::ScenarioWebMessage(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
//! [IsWebMessageEnabled]
ComPtr<ICoreWebView2Settings> settings;
CHECK_FAILURE(m_webView->get_Settings(&settings));
CHECK_FAILURE(settings->put_IsWebMessageEnabled(TRUE));
//! [IsWebMessageEnabled]
//! [WebMessageReceived]
// Setup the web message received event handler before navigating to
// ensure we don't miss any messages.
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
if (message.compare(0, 13, L"SetTitleText ") == 0)
{
m_appWindow->SetTitleText(message.substr(13).c_str());
}
else if (message.compare(L"GetWindowBounds") == 0)
{
RECT bounds = m_appWindow->GetWindowBounds();
std::wstring reply =
L"{\"WindowBounds\":\"Left:" + std::to_wstring(bounds.left)
+ L"\\nTop:" + std::to_wstring(bounds.top)
+ L"\\nRight:" + std::to_wstring(bounds.right)
+ L"\\nBottom:" + std::to_wstring(bounds.bottom)
+ L"\"}";
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
}
return S_OK;
}).Get(), &m_webMessageReceivedToken));
//! [WebMessageReceived]
// Turn off this scenario if we navigate away from the sample page
CHECK_FAILURE(m_webView->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
// Changes to ICoreWebView2Settings::IsWebMessageEnabled apply to the next document
// to which we navigate.
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioWebMessage::~ScenarioWebMessage()
{
m_webView->remove_WebMessageReceived(m_webMessageReceivedToken);
m_webView->remove_ContentLoading(m_contentLoadingToken);
}