MCPcopy Create free account
hub / github.com/MicrosoftEdge/WebView2Browser / SetUIMessageBroker

Method SetUIMessageBroker

BrowserWindow.cpp:327–476  ·  view source on GitHub ↗

Set the message broker for the UI webview. This will capture messages from ui web content. Lambda is used to capture the instance while satisfying Microsoft::WRL::Callback ()

Source from the content-addressed store, hash-verified

325// Set the message broker for the UI webview. This will capture messages from ui web content.
326// Lambda is used to capture the instance while satisfying Microsoft::WRL::Callback<T>()
327void BrowserWindow::SetUIMessageBroker()
328{
329 m_uiMessageBroker = Callback<ICoreWebView2WebMessageReceivedEventHandler>(
330 [this](ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* eventArgs) -> HRESULT
331 {
332 wil::unique_cotaskmem_string jsonString;
333 CheckFailure(eventArgs->get_WebMessageAsJson(&jsonString), L""); // Get the message from the UI WebView as JSON formatted string
334 web::json::value jsonObj = web::json::value::parse(jsonString.get());
335
336 if (!jsonObj.has_field(L"message"))
337 {
338 OutputDebugString(L"No message code provided\n");
339 return S_OK;
340 }
341
342 if (!jsonObj.has_field(L"args"))
343 {
344 OutputDebugString(L"The message has no args field\n");
345 return S_OK;
346 }
347
348 int message = jsonObj.at(L"message").as_integer();
349 web::json::value args = jsonObj.at(L"args");
350
351 switch (message)
352 {
353 case MG_CREATE_TAB:
354 {
355 size_t id = args.at(L"tabId").as_number().to_uint32();
356 bool shouldBeActive = args.at(L"active").as_bool();
357 std::unique_ptr<Tab> newTab = Tab::CreateNewTab(m_hWnd, m_contentEnv.Get(), id, shouldBeActive);
358
359 std::map<size_t, std::unique_ptr<Tab>>::iterator it = m_tabs.find(id);
360 if (it == m_tabs.end())
361 {
362 m_tabs.insert(std::pair<size_t,std::unique_ptr<Tab>>(id, std::move(newTab)));
363 }
364 else
365 {
366 m_tabs.at(id)->m_contentController->Close();
367 it->second = std::move(newTab);
368 }
369 }
370 break;
371 case MG_NAVIGATE:
372 {
373 std::wstring uri(args.at(L"uri").as_string());
374 std::wstring browserScheme(L"browser://");
375
376 if (uri.substr(0, browserScheme.size()).compare(browserScheme) == 0)
377 {
378 // No encoded search URI
379 std::wstring path = uri.substr(browserScheme.size());
380 if (path.compare(L"favorites") == 0 ||
381 path.compare(L"settings") == 0 ||
382 path.compare(L"history") == 0)
383 {
384 std::wstring filePath(L"wvbrowser_ui\\content_ui\\");

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected