Skip to content

Commit 22f2fd1

Browse files
authored
Merge pull request #298 from MicrosoftEdge/smoketest/1.0.4181-testing
Smoketest/1.0.4181 testing
2 parents eeb4d7c + ea8b00e commit 22f2fd1

11 files changed

Lines changed: 439 additions & 19 deletions

‎SampleApps/WebView2APISample/AppWindow.cpp‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "ScenarioDedicatedWorker.h"
4040
#include "ScenarioDedicatedWorkerPostMessage.h"
4141
#include "ScenarioDefaultBackgroundColor.h"
42+
#include "ScenarioDiagnosticMonitor.h"
4243
#include "ScenarioDragDrop.h"
4344
#include "ScenarioDragDropOverride.h"
4445
#include "ScenarioExtensionsManagement.h"
@@ -48,12 +49,12 @@
4849
#include "ScenarioNavigateWithWebResourceRequest.h"
4950
#include "ScenarioNonClientRegionSupport.h"
5051
#include "ScenarioNotificationReceived.h"
52+
#include "ScenarioOriginConfigurationAPI.h"
5153
#include "ScenarioPermissionManagement.h"
5254
#include "ScenarioServiceWorkerManager.h"
5355
#include "ScenarioServiceWorkerPostMessage.h"
5456
#include "ScenarioServiceWorkerPostMessageSetting.h"
5557
#include "ScenarioSharedWorkerManager.h"
56-
#include "ScenarioOriginConfigurationAPI.h"
5758
#include "ScenarioSaveAs.h"
5859
#include "ScenarioScreenCapture.h"
5960
#include "ScenarioSensitivityLabel.h"
@@ -620,6 +621,28 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
620621
}
621622
return true;
622623
}
624+
case IDM_SCENARIO_DIAGNOSTIC_CREATE_MONITOR:
625+
{
626+
auto* scenario = GetComponent<ScenarioDiagnosticMonitor>();
627+
if (!scenario)
628+
{
629+
NewComponent<ScenarioDiagnosticMonitor>(this);
630+
scenario = GetComponent<ScenarioDiagnosticMonitor>();
631+
}
632+
if (scenario)
633+
{
634+
scenario->CreateMonitorWithDialog();
635+
}
636+
return true;
637+
}
638+
case IDM_SCENARIO_DIAGNOSTIC_CLEAR_ALL:
639+
{
640+
if (auto* scenario = GetComponent<ScenarioDiagnosticMonitor>())
641+
{
642+
scenario->ClearAllMonitors();
643+
}
644+
return true;
645+
}
623646
case IDM_SCENARIO_CUSTOM_SCHEME:
624647
{
625648
NewComponent<ScenarioCustomScheme>(this);
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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 "ScenarioDiagnosticMonitor.h"
8+
9+
#include <sstream>
10+
#include <string>
11+
12+
#include "App.h"
13+
#include "CheckFailure.h"
14+
#include "TextInputDialog.h"
15+
#include "Util.h"
16+
#include "resource.h"
17+
18+
using namespace Microsoft::WRL;
19+
20+
ScenarioDiagnosticMonitor::ScenarioDiagnosticMonitor(AppWindow* appWindow)
21+
: m_appWindow(appWindow)
22+
{
23+
wil::com_ptr<ICoreWebView2ExperimentalEnvironment16> env16;
24+
HRESULT hr = m_appWindow->GetWebViewEnvironment()->QueryInterface(IID_PPV_ARGS(&env16));
25+
if (FAILED(hr) || !env16)
26+
{
27+
MessageBox(
28+
m_appWindow->GetMainWindow(),
29+
L"ICoreWebView2ExperimentalEnvironment16 not available. "
30+
L"The Diagnostic API requires a newer WebView2 Runtime.",
31+
L"Diagnostic Monitor", MB_OK);
32+
return;
33+
}
34+
m_environment16 = env16;
35+
}
36+
37+
ScenarioDiagnosticMonitor::~ScenarioDiagnosticMonitor()
38+
{
39+
ClearAllMonitors();
40+
}
41+
42+
void ScenarioDiagnosticMonitor::CreateMonitorWithDialog()
43+
{
44+
if (!m_environment16)
45+
{
46+
return;
47+
}
48+
49+
// Default monitor name.
50+
std::wstringstream defaultName;
51+
defaultName << L"Diagnostic Monitor " << m_nextMonitorId;
52+
53+
// The filter dialog label is used both as a UI prompt and as the
54+
// lookup key in `dialog.results`, so keep it in a single constant.
55+
// The example matches the JSON schema documented for the
56+
// NETWORK_REQUEST category in environment.idl.
57+
static constexpr wchar_t kFilterLabel[] =
58+
L"Example filter:\r\n"
59+
L"{\r\n"
60+
L" \"errorCode\": [-105, -7],\r\n"
61+
L" \"statusCode\": [404, 500],\r\n"
62+
L" \"uriPattern\": [\"https://*.contoso.com/*\"],\r\n"
63+
L" \"httpMethod\": [\"GET\", \"POST\"]\r\n"
64+
L"}\r\n"
65+
L"\r\n"
66+
L"Match any value within a field (OR).\r\n"
67+
L"All specified fields must match (AND).\r\n"
68+
L"Leave empty to receive all events.";
69+
70+
// Show a single dialog with monitor name, category dropdown, and JSON filter.
71+
TextInputDialog dialog = TextInputDialog::Builder(
72+
m_appWindow->GetMainWindow(), L"Create Diagnostic Monitor",
73+
L"Configure the monitor and its filter")
74+
.AddTextArea(L"Monitor Name", defaultName.str(), false, 20, 24)
75+
.AddDropDown(L"Category", {L"NETWORK_REQUEST"}, 0)
76+
.AddTextArea(kFilterLabel, L"", false, 170, 80)
77+
.Build();
78+
79+
if (!dialog.confirmed)
80+
{
81+
return;
82+
}
83+
84+
// Read user inputs from the dialog results.
85+
std::wstring monitorName = defaultName.str();
86+
std::wstring jsonFilter;
87+
88+
auto nameIt = dialog.results.find(L"Monitor Name");
89+
if (nameIt != dialog.results.end())
90+
{
91+
monitorName = std::get<TextArea>(nameIt->second).input;
92+
}
93+
94+
auto filterIt = dialog.results.find(kFilterLabel);
95+
if (filterIt != dialog.results.end())
96+
{
97+
jsonFilter = std::get<TextArea>(filterIt->second).input;
98+
}
99+
100+
//! [CreateDiagnosticMonitor]
101+
// Create the monitor.
102+
wil::com_ptr<ICoreWebView2ExperimentalDiagnosticMonitor> monitor;
103+
HRESULT hr = m_environment16->CreateDiagnosticMonitor(&monitor);
104+
if (FAILED(hr))
105+
{
106+
std::wstringstream ss;
107+
ss << L"CreateDiagnosticMonitor failed: 0x" << std::hex << hr;
108+
MessageBox(
109+
m_appWindow->GetMainWindow(), ss.str().c_str(), L"Diagnostic Monitor",
110+
MB_OK | MB_ICONERROR);
111+
return;
112+
}
113+
114+
// Set filter for NETWORK_REQUEST category.
115+
hr = monitor->SetDiagnosticFilter(
116+
COREWEBVIEW2_DIAGNOSTIC_CATEGORY_NETWORK_REQUEST,
117+
jsonFilter.empty() ? L"" : jsonFilter.c_str());
118+
if (FAILED(hr))
119+
{
120+
std::wstringstream ss;
121+
ss << L"SetDiagnosticFilter failed: 0x" << std::hex << hr;
122+
MessageBox(
123+
m_appWindow->GetMainWindow(), ss.str().c_str(), L"Diagnostic Monitor",
124+
MB_OK | MB_ICONERROR);
125+
return;
126+
}
127+
128+
// Register the event handler. Capture the monitor name for logging.
129+
std::wstring capturedName = monitorName;
130+
EventRegistrationToken token;
131+
CHECK_FAILURE(monitor->add_DiagnosticReceived(
132+
Callback<ICoreWebView2ExperimentalDiagnosticReceivedEventHandler>(
133+
[this, capturedName](
134+
ICoreWebView2ExperimentalDiagnosticMonitor* sender,
135+
ICoreWebView2ExperimentalDiagnosticReceivedEventArgs* args) -> HRESULT
136+
{
137+
COREWEBVIEW2_DIAGNOSTIC_CATEGORY category;
138+
CHECK_FAILURE(args->get_Category(&category));
139+
140+
COREWEBVIEW2_DIAGNOSTIC_SCOPE scope;
141+
CHECK_FAILURE(args->get_Scope(&scope));
142+
143+
INT64 timestamp = 0;
144+
CHECK_FAILURE(args->get_Timestamp(&timestamp));
145+
146+
wil::unique_cotaskmem_string detailsJson;
147+
CHECK_FAILURE(args->get_DetailsAsJson(&detailsJson));
148+
149+
m_totalEventCount++;
150+
151+
// Pretty-print the JSON details for readability.
152+
std::wstring json = detailsJson.get();
153+
std::wstringstream pretty;
154+
int indent = 0;
155+
bool inString = false;
156+
wchar_t prevCh = 0;
157+
for (wchar_t ch : json)
158+
{
159+
if (ch == L'"' && prevCh != L'\\')
160+
inString = !inString;
161+
if (inString)
162+
{
163+
pretty << ch;
164+
prevCh = ch;
165+
continue;
166+
}
167+
if (ch == L'{' || ch == L'[')
168+
{
169+
pretty << ch << L"\r\n";
170+
indent += 2;
171+
for (int i = 0; i < indent; ++i)
172+
pretty << L' ';
173+
}
174+
else if (ch == L'}' || ch == L']')
175+
{
176+
pretty << L"\r\n";
177+
indent -= 2;
178+
for (int i = 0; i < indent; ++i)
179+
pretty << L' ';
180+
pretty << ch;
181+
}
182+
else if (ch == L',')
183+
{
184+
pretty << ch << L"\r\n";
185+
for (int i = 0; i < indent; ++i)
186+
pretty << L' ';
187+
}
188+
else
189+
{
190+
pretty << ch;
191+
}
192+
prevCh = ch;
193+
}
194+
195+
std::wstringstream ss;
196+
ss << L"Event #" << m_totalEventCount << L"\r\n"
197+
<< L"Category: " << static_cast<int>(category) << L" Scope: "
198+
<< static_cast<int>(scope) << L" Timestamp (ms since UNIX epoch): "
199+
<< timestamp << L"\r\n\r\n"
200+
<< L"Details:\r\n"
201+
<< pretty.str();
202+
203+
MessageBox(
204+
m_appWindow->GetMainWindow(), ss.str().c_str(), capturedName.c_str(),
205+
MB_OK | MB_ICONINFORMATION);
206+
207+
return S_OK;
208+
})
209+
.Get(),
210+
&token));
211+
//! [CreateDiagnosticMonitor]
212+
213+
m_monitors.push_back({monitorName, monitor, token});
214+
m_nextMonitorId++;
215+
216+
std::wstringstream msg;
217+
msg << L"Created \"" << monitorName << L"\" with filter: "
218+
<< (jsonFilter.empty() ? L"(accept all)" : jsonFilter);
219+
MessageBox(
220+
m_appWindow->GetMainWindow(), msg.str().c_str(), L"Diagnostic Monitor",
221+
MB_OK | MB_ICONINFORMATION);
222+
}
223+
224+
void ScenarioDiagnosticMonitor::ClearAllMonitors()
225+
{
226+
for (auto& entry : m_monitors)
227+
{
228+
if (entry.monitor)
229+
{
230+
CHECK_FAILURE(entry.monitor->remove_DiagnosticReceived(entry.token));
231+
CHECK_FAILURE(entry.monitor->Close());
232+
}
233+
}
234+
size_t count = m_monitors.size();
235+
m_monitors.clear();
236+
m_nextMonitorId = 1;
237+
m_totalEventCount = 0;
238+
239+
if (count > 0)
240+
{
241+
std::wstringstream ss;
242+
ss << L"Cleared " << count << L" monitor(s).";
243+
MessageBox(
244+
m_appWindow->GetMainWindow(), ss.str().c_str(), L"Diagnostic Monitor",
245+
MB_OK | MB_ICONINFORMATION);
246+
}
247+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#include "stdafx.h"
7+
8+
#include <string>
9+
#include <vector>
10+
11+
#include "AppWindow.h"
12+
#include "ComponentBase.h"
13+
14+
// Scenario that demonstrates the Diagnostic Monitor API.
15+
// Supports creating multiple named monitors with per-category JSON filters,
16+
// and clearing all monitors. Each monitor logs received events to a message
17+
// box.
18+
class ScenarioDiagnosticMonitor : public ComponentBase
19+
{
20+
public:
21+
ScenarioDiagnosticMonitor(AppWindow* appWindow);
22+
~ScenarioDiagnosticMonitor() override;
23+
24+
// Menu handlers called from AppWindow.
25+
void CreateMonitorWithDialog();
26+
void ClearAllMonitors();
27+
28+
private:
29+
struct MonitorEntry
30+
{
31+
std::wstring name;
32+
wil::com_ptr<ICoreWebView2ExperimentalDiagnosticMonitor> monitor;
33+
EventRegistrationToken token = {};
34+
};
35+
36+
AppWindow* m_appWindow = nullptr;
37+
wil::com_ptr<ICoreWebView2ExperimentalEnvironment16> m_environment16;
38+
39+
std::vector<MonitorEntry> m_monitors;
40+
int m_nextMonitorId = 1;
41+
int m_totalEventCount = 0;
42+
};

0 commit comments

Comments
 (0)