From 2ef44570fd15c654fc2ba0b49179cd2d295e6067 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 8 Sep 2023 01:51:47 +0200 Subject: [PATCH] Work around bug with out of order JS execution results Modify the sample to show the correct value of the user agent when using WebKit backend as this got broken by calling it inside an event handler executed later but before the idle event handler could dispatch the result of JS executed from inside WebKit AddScriptMessageHandler() implementation, resulting in misinterpreting this result ("{}") as the user agent string. The real fix is, of course, to deal with the JS execution results coming out of order in some way. --- samples/webview/webview.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 18ba4827ff..7c4e4d69ac 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -490,6 +490,13 @@ WebFrame::WebFrame(const wxString& url, bool isMain, wxWebViewWindowFeatures* wi m_browser->Bind(wxEVT_WEBVIEW_CREATED, [this](wxWebViewEvent& event) { wxLogMessage("Web view created, user agent is \"%s\"", m_browser->GetUserAgent()); + // We need to synchronize this call with GetUserAgent() one, as + // otherwise the results of executing JavaScript inside + // GetUserAgent() and AddScriptMessageHandler() could arrive out of + // order and we'd get the wrong user agent string back. + if (!m_browser->AddScriptMessageHandler("wx")) + wxLogError("Could not add script message handler"); + event.Skip(); }); @@ -500,8 +507,6 @@ WebFrame::WebFrame(const wxString& url, bool isMain, wxWebViewWindowFeatures* wi m_browser->RegisterHandler(wxSharedPtr(new wxWebViewFSHandler("memory"))); m_browser->RegisterHandler(wxSharedPtr(new AdvancedWebViewHandler())); #endif - if (!m_browser->AddScriptMessageHandler("wx")) - wxLogError("Could not add script message handler"); } else wxLogMessage("Created new window");