PrinterWebViewPanel: tracking (Linux) and removing authentication handlers

This commit is contained in:
Jan Bartipan 2024-08-02 13:25:05 +02:00 committed by Lukas Matena
parent 7fb3638d46
commit b490479d40
3 changed files with 18 additions and 5 deletions

View File

@ -11,6 +11,8 @@ struct Credentials {
std::string password; std::string password;
}; };
std::unordered_map<WebKitWebView*, gulong> g_webview_authorize_handlers;
gboolean webkit_authorize_handler(WebKitWebView *web_view, WebKitAuthenticationRequest *request, gpointer user_data) gboolean webkit_authorize_handler(WebKitWebView *web_view, WebKitAuthenticationRequest *request, gpointer user_data)
{ {
const Credentials& creds = *static_cast<const Credentials*>(user_data); const Credentials& creds = *static_cast<const Credentials*>(user_data);
@ -29,7 +31,8 @@ void setup_webview_with_credentials(wxWebView* web_view, const std::string& user
WebKitWebView* native_backend = static_cast<WebKitWebView *>(web_view->GetNativeBackend()); WebKitWebView* native_backend = static_cast<WebKitWebView *>(web_view->GetNativeBackend());
Credentials* user_data = new Credentials{username, password}; Credentials* user_data = new Credentials{username, password};
g_signal_connect_data( remove_webview_credentials(web_view);
auto handler = g_signal_connect_data(
native_backend, native_backend,
"authenticate", "authenticate",
G_CALLBACK(webkit_authorize_handler), G_CALLBACK(webkit_authorize_handler),
@ -37,12 +40,17 @@ void setup_webview_with_credentials(wxWebView* web_view, const std::string& user
&free_credentials, &free_credentials,
static_cast<GConnectFlags>(0) static_cast<GConnectFlags>(0)
); );
g_webview_authorize_handlers[native_backend] = handler;
} }
void remove_webview_credentials(wxWebView* web_view) void remove_webview_credentials(wxWebView* web_view)
{ {
WebKitWebView* native_backend = static_cast<WebKitWebView *>(web_view->GetNativeBackend()); WebKitWebView* native_backend = static_cast<WebKitWebView *>(web_view->GetNativeBackend());
g_object_disconnect(native_backend, "authenticate"); if (auto it = g_webview_authorize_handlers.find(native_backend);
it != g_webview_authorize_handlers.end()) {
g_signal_handler_disconnect(native_backend, it->second);
g_webview_authorize_handlers.erase(it);
}
} }
} }

View File

@ -118,6 +118,7 @@
namespace Slic3r::GUI { namespace Slic3r::GUI {
void setup_webview_with_credentials(wxWebView* web_view, const std::string& username, const std::string& password) void setup_webview_with_credentials(wxWebView* web_view, const std::string& username, const std::string& password)
{ {
remove_webview_credentials(web_view);
WKWebView* backend = static_cast<WKWebView*>(web_view->GetNativeBackend()); WKWebView* backend = static_cast<WKWebView*>(web_view->GetNativeBackend());
if (![backend.navigationDelegate isKindOfClass:MyNavigationDelegate.class]) { if (![backend.navigationDelegate isKindOfClass:MyNavigationDelegate.class]) {
backend.navigationDelegate = [[MyNavigationDelegate alloc] backend.navigationDelegate = [[MyNavigationDelegate alloc]

View File

@ -26,9 +26,10 @@ void setup_webview_with_credentials(wxWebView* webview, const std::string& usern
return; return;
} }
remove_webview_credentials(webview);
// should it be stored? // should it be stored?
EventRegistrationToken basicAuthenticationRequestedToken = {}; EventRegistrationToken basicAuthenticationRequestedToken = {};
if (FAILED(wv2_10->add_BasicAuthenticationRequested( if (FAILED(wv2_10->add_BasicAuthenticationRequested(
Microsoft::WRL::Callback<ICoreWebView2BasicAuthenticationRequestedEventHandler>( Microsoft::WRL::Callback<ICoreWebView2BasicAuthenticationRequestedEventHandler>(
[username, password](ICoreWebView2 *sender, ICoreWebView2BasicAuthenticationRequestedEventArgs *args) { [username, password](ICoreWebView2 *sender, ICoreWebView2BasicAuthenticationRequestedEventArgs *args) {
@ -64,10 +65,13 @@ void remove_webview_credentials(wxWebView* webview)
return; return;
} }
auto it = g_basic_auth_handler_tokens.find(webView2); if (auto it = g_basic_auth_handler_tokens.find(webView2);
if (it != g_basic_auth_handler_tokens.end()) { it != g_basic_auth_handler_tokens.end()) {
if (FAILED(wv2_10->remove_BasicAuthenticationRequested(it->second))) { if (FAILED(wv2_10->remove_BasicAuthenticationRequested(it->second))) {
BOOST_LOG_TRIVIAL(error) << "WebView: Unregistering authentication request handler failed"; BOOST_LOG_TRIVIAL(error) << "WebView: Unregistering authentication request handler failed";
} else {
g_basic_auth_handler_tokens.erase(it);
} }
} else { } else {
BOOST_LOG_TRIVIAL(error) << "WebView: Cannot unregister authentication request handler"; BOOST_LOG_TRIVIAL(error) << "WebView: Cannot unregister authentication request handler";