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

Method HandleTabMessageReceived

BrowserWindow.cpp:701–809  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

699}
700
701HRESULT BrowserWindow::HandleTabMessageReceived(size_t tabId, ICoreWebView2* webview, ICoreWebView2WebMessageReceivedEventArgs* eventArgs)
702{
703 wil::unique_cotaskmem_string jsonString;
704 RETURN_IF_FAILED(eventArgs->get_WebMessageAsJson(&jsonString));
705 web::json::value jsonObj = web::json::value::parse(jsonString.get());
706
707 wil::unique_cotaskmem_string uri;
708 RETURN_IF_FAILED(webview->get_Source(&uri));
709
710 int message = jsonObj.at(L"message").as_integer();
711 web::json::value args = jsonObj.at(L"args");
712
713 wil::unique_cotaskmem_string source;
714 RETURN_IF_FAILED(webview->get_Source(&source));
715
716 switch (message)
717 {
718 case MG_GET_FAVORITES:
719 case MG_REMOVE_FAVORITE:
720 {
721 std::wstring fileURI = GetFilePathAsURI(GetFullPathFor(L"wvbrowser_ui\\content_ui\\favorites.html"));
722 // Only the favorites UI can request favorites
723 if (fileURI.compare(source.get()) == 0)
724 {
725 jsonObj[L"args"][L"tabId"] = web::json::value::number(tabId);
726 CheckFailure(PostJsonToWebView(jsonObj, m_controlsWebView.Get()), L"Couldn't perform favorites operation.");
727 }
728 }
729 break;
730 case MG_GET_SETTINGS:
731 {
732 std::wstring fileURI = GetFilePathAsURI(GetFullPathFor(L"wvbrowser_ui\\content_ui\\settings.html"));
733 // Only the settings UI can request settings
734 if (fileURI.compare(source.get()) == 0)
735 {
736 jsonObj[L"args"][L"tabId"] = web::json::value::number(tabId);
737 CheckFailure(PostJsonToWebView(jsonObj, m_controlsWebView.Get()), L"Couldn't retrieve settings.");
738 }
739 }
740 break;
741 case MG_CLEAR_CACHE:
742 {
743 std::wstring fileURI = GetFilePathAsURI(GetFullPathFor(L"wvbrowser_ui\\content_ui\\settings.html"));
744 // Only the settings UI can request cache clearing
745 if (fileURI.compare(uri.get()) == 0)
746 {
747 jsonObj[L"args"][L"content"] = web::json::value::boolean(false);
748 jsonObj[L"args"][L"controls"] = web::json::value::boolean(false);
749
750 if (SUCCEEDED(ClearContentCache()))
751 {
752 jsonObj[L"args"][L"content"] = web::json::value::boolean(true);
753 }
754
755 if (SUCCEEDED(ClearControlsCache()))
756 {
757 jsonObj[L"args"][L"controls"] = web::json::value::boolean(true);
758 }

Callers 1

SetMessageBrokerMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected