|
| 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(×tamp)); |
| 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 | +} |
0 commit comments