fix: startup crash on macOS when a printer has a print host set (#14747)

This commit is contained in:
Kris Austin
2026-07-22 13:14:49 -05:00
committed by GitHub
parent a3ef75586e
commit 857adad293
3 changed files with 27 additions and 1 deletions

View File

@@ -246,7 +246,10 @@ void PrinterWebView::SendAPIKey()
// RemoveAllUserScripts causes WebView to forget about our script message handler,
// so re-add it here.
m_browser->RemoveScriptMessageHandler("wx");
m_browser->AddScriptMessageHandler("wx");
if (m_browser->AddScriptMessageHandler("wx"))
WebView::MarkScriptMessageHandlerAdded(m_browser);
else
wxLogError("Could not add script message handler");
#ifdef __linux__
// Re-inject the vue-resize/WebKitGTK workaround that RemoveAllUserScripts just cleared.

View File

@@ -242,8 +242,15 @@ public:
g_webviews.erase(iter);
}
wxWebView *m_webView;
// Guards against registering the "wx" handler twice (a duplicate throws on WKWebView).
bool m_script_handler_added = false;
};
static WebViewRef *webview_ref(wxWebView *webView)
{
return webView ? static_cast<WebViewRef *>(webView->GetRefData()) : nullptr;
}
wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
{
#if wxUSE_WEBVIEW_EDGE
@@ -304,10 +311,17 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
Slic3r::GUI::WKWebView_setTransparentBackground(wkWebView);
#endif
auto addScriptMessageHandler = [] (wxWebView *webView) {
// Skip if SendAPIKey() already registered "wx"; a duplicate add throws an
// uncatchable NSException on WKWebView, killing the app at startup.
WebViewRef *ref = webview_ref(webView);
if (ref && ref->m_script_handler_added)
return;
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": begin to add script message handler for wx.";
Slic3r::GUI::wxGetApp().set_adding_script_handler(true);
if (!webView->AddScriptMessageHandler("wx"))
wxLogError("Could not add script message handler");
else if (ref)
ref->m_script_handler_added = true;
Slic3r::GUI::wxGetApp().set_adding_script_handler(false);
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": finished add script message handler for wx.";
};
@@ -336,6 +350,12 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
g_webviews.push_back(webView);
return webView;
}
void WebView::MarkScriptMessageHandlerAdded(wxWebView * webView)
{
if (WebViewRef *ref = webview_ref(webView))
ref->m_script_handler_added = true;
}
#if wxUSE_WEBVIEW_EDGE
bool WebView::CheckWebViewRuntime()
{

View File

@@ -18,6 +18,9 @@ public:
static bool RunScript(wxWebView * webView, wxString const & msg);
// Marks "wx" as registered so CreateWebView's deferred add skips the duplicate.
static void MarkScriptMessageHandlerAdded(wxWebView * webView);
static void RecreateAll();
};