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.
This commit is contained in:
Vadim Zeitlin 2023-09-08 01:51:47 +02:00
parent c109f7f593
commit 2ef44570fd

View file

@ -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<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new AdvancedWebViewHandler()));
#endif
if (!m_browser->AddScriptMessageHandler("wx"))
wxLogError("Could not add script message handler");
}
else
wxLogMessage("Created new window");