Move parent HWND management to wxInitializerPeer

While reducing copy & pasted code, this also protects wxTopLevelWindows
list with a lock.

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2019-08-13 12:47:27 +02:00
parent 592d53d8bc
commit 009cd36fe2

View File

@ -33,13 +33,16 @@ public:
/// ///
/// Initialize peer /// Initialize peer
/// ///
wxInitializerPeer(_In_ HINSTANCE instance); wxInitializerPeer(_In_ HINSTANCE instance, _In_opt_ HWND hwndParent);
/// ///
/// Uninitialize peer /// Uninitialize peer
/// ///
virtual ~wxInitializerPeer(); virtual ~wxInitializerPeer();
public:
wxWindow* m_parent; ///< Parent window
protected: protected:
static wxCriticalSection s_lock; ///< Initialization lock static wxCriticalSection s_lock; ///< Initialization lock
static unsigned long s_init_ref_count; ///< Initialization reference counter static unsigned long s_init_ref_count; ///< Initialization reference counter
@ -117,30 +120,15 @@ void eap::peer_ttls_ui::invoke_config_ui(
int result; int result;
{ {
// Initialize application. // Initialize application.
wxInitializerPeer init(m_instance); wxInitializerPeer init(m_instance, hwndParent);
wxWindow *parent;
if (hwndParent) {
// Create wxWidget-approved parent window.
parent = new wxWindow;
parent->SetHWND((WXHWND)hwndParent);
parent->AdoptAttributesFromHWND();
wxTopLevelWindows.Append(parent);
} else
parent = NULL;
// Create and launch configuration dialog. // Create and launch configuration dialog.
wxEAPConfigDialog<wxTTLSConfigWindow> dlg(cfg, parent); wxEAPConfigDialog<wxTTLSConfigWindow> dlg(cfg, init.m_parent);
if (!parent) { if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG }; FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi); ::FlashWindowEx(&fwi);
} }
result = dlg.ShowModal(); result = dlg.ShowModal();
if (parent) {
wxTopLevelWindows.DeleteObject(parent);
parent->SetHWND((WXHWND)NULL);
}
} }
if (result != wxID_OK) if (result != wxID_OK)
@ -188,25 +176,15 @@ void eap::peer_ttls_ui::invoke_identity_ui(
int result; int result;
{ {
// Initialize application. // Initialize application.
wxInitializerPeer init(m_instance); wxInitializerPeer init(m_instance, hwndParent);
wxWindow *parent;
if (hwndParent) {
// Create wxWidget-approved parent window.
parent = new wxWindow;
parent->SetHWND((WXHWND)hwndParent);
parent->AdoptAttributesFromHWND();
wxTopLevelWindows.Append(parent);
} else
parent = NULL;
if (cfg.m_providers.size() > 1) { if (cfg.m_providers.size() > 1) {
// Multiple identity providers: User has to select one first. // Multiple identity providers: User has to select one first.
wxEAPProviderSelectDialog dlg(cfg, parent); wxEAPProviderSelectDialog dlg(cfg, init.m_parent);
// Centre and display dialog. // Centre and display dialog.
dlg.Centre(wxBOTH); dlg.Centre(wxBOTH);
if (!parent) { if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG }; FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi); ::FlashWindowEx(&fwi);
} }
@ -259,7 +237,7 @@ void eap::peer_ttls_ui::invoke_identity_ui(
src_outer != eap::credentials::source_config && eap::config_method::status_cred_begin <= cfg_method->m_last_status && cfg_method->m_last_status < eap::config_method::status_cred_end) src_outer != eap::credentials::source_config && eap::config_method::status_cred_begin <= cfg_method->m_last_status && cfg_method->m_last_status < eap::config_method::status_cred_end)
{ {
// Build dialog to prompt for outer credentials. // Build dialog to prompt for outer credentials.
wxEAPCredentialsDialog dlg(*cfg_prov, parent); wxEAPCredentialsDialog dlg(*cfg_prov, init.m_parent);
if (eap::config_method::status_cred_begin <= cfg_method->m_last_status && cfg_method->m_last_status < eap::config_method::status_cred_end) if (eap::config_method::status_cred_begin <= cfg_method->m_last_status && cfg_method->m_last_status < eap::config_method::status_cred_end)
dlg.AddContent(new wxEAPCredentialWarningPanel(*cfg_prov, cfg_method->m_last_status, &dlg)); dlg.AddContent(new wxEAPCredentialWarningPanel(*cfg_prov, cfg_method->m_last_status, &dlg));
auto panel = new wxTLSCredentialsPanel(*cfg_prov, *cfg_method, *cred, &dlg, false); auto panel = new wxTLSCredentialsPanel(*cfg_prov, *cfg_method, *cred, &dlg, false);
@ -272,13 +250,13 @@ void eap::peer_ttls_ui::invoke_identity_ui(
// Centre and display dialog. // Centre and display dialog.
dlg.Centre(wxBOTH); dlg.Centre(wxBOTH);
if (!parent) { if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG }; FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi); ::FlashWindowEx(&fwi);
} }
if ((result = dlg.ShowModal()) == wxID_OK) { if ((result = dlg.ShowModal()) == wxID_OK) {
// Write credentials to credential manager.
if (panel->GetRemember()) { if (panel->GetRemember()) {
// Write credentials to credential manager.
try { try {
cred->credentials_tls::store(target_name.c_str(), 0); cred->credentials_tls::store(target_name.c_str(), 0);
} catch (winstd::win_runtime_error &err) { } catch (winstd::win_runtime_error &err) {
@ -313,7 +291,7 @@ void eap::peer_ttls_ui::invoke_identity_ui(
#endif #endif
{ {
// Native inner methods. Build dialog to prompt for inner credentials. // Native inner methods. Build dialog to prompt for inner credentials.
wxEAPCredentialsDialog dlg(*cfg_prov, parent); wxEAPCredentialsDialog dlg(*cfg_prov, init.m_parent);
if (eap::config_method::status_cred_begin <= cfg_method->m_inner->m_last_status && cfg_method->m_inner->m_last_status < eap::config_method::status_cred_end) if (eap::config_method::status_cred_begin <= cfg_method->m_inner->m_last_status && cfg_method->m_inner->m_last_status < eap::config_method::status_cred_end)
dlg.AddContent(new wxEAPCredentialWarningPanel(*cfg_prov, cfg_method->m_inner->m_last_status, &dlg)); dlg.AddContent(new wxEAPCredentialWarningPanel(*cfg_prov, cfg_method->m_inner->m_last_status, &dlg));
wxEAPCredentialsPanelBase *panel = NULL; wxEAPCredentialsPanelBase *panel = NULL;
@ -344,7 +322,7 @@ void eap::peer_ttls_ui::invoke_identity_ui(
// Centre and display dialog. // Centre and display dialog.
dlg.Centre(wxBOTH); dlg.Centre(wxBOTH);
if (!parent) { if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG }; FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi); ::FlashWindowEx(&fwi);
} }
@ -401,11 +379,6 @@ void eap::peer_ttls_ui::invoke_identity_ui(
result = wxID_OK; result = wxID_OK;
} }
} }
if (parent) {
wxTopLevelWindows.DeleteObject(parent);
parent->SetHWND((WXHWND)NULL);
}
} }
if (result != wxID_OK) if (result != wxID_OK)
@ -461,25 +434,14 @@ void eap::peer_ttls_ui::invoke_interactive_ui(
int result; int result;
{ {
// Initialize application. // Initialize application.
wxInitializerPeer init(m_instance); wxInitializerPeer init(m_instance, hwndParent);
wxWindow *parent;
if (hwndParent) {
// Create wxWidget-approved parent window.
parent = new wxWindow;
parent->SetHWND((WXHWND)hwndParent);
parent->AdoptAttributesFromHWND();
wxTopLevelWindows.Append(parent);
} else
parent = NULL;
{
sanitizing_wstring sanitizing_wstring
challenge(reinterpret_cast<sanitizing_wstring::const_pointer>(ctx.m_data.data()), ctx.m_data.size()/sizeof(sanitizing_wstring::value_type)), challenge(reinterpret_cast<sanitizing_wstring::const_pointer>(ctx.m_data.data()), ctx.m_data.size()/sizeof(sanitizing_wstring::value_type)),
response; response;
// Build dialog to prompt for response. // Build dialog to prompt for response.
wxGTCResponseDialog dlg(*cfg_prov, parent); wxGTCResponseDialog dlg(*cfg_prov, init.m_parent);
auto panel = new wxGTCResponsePanel(response, challenge.c_str(), &dlg); auto panel = new wxGTCResponsePanel(response, challenge.c_str(), &dlg);
dlg.AddContent(panel); dlg.AddContent(panel);
@ -489,7 +451,7 @@ void eap::peer_ttls_ui::invoke_interactive_ui(
// Centre and display dialog. // Centre and display dialog.
dlg.Centre(wxBOTH); dlg.Centre(wxBOTH);
if (!parent) { if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG }; FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi); ::FlashWindowEx(&fwi);
} }
@ -501,12 +463,6 @@ void eap::peer_ttls_ui::invoke_interactive_ui(
} }
} }
if (parent) {
wxTopLevelWindows.DeleteObject(parent);
parent->SetHWND((WXHWND)NULL);
}
}
if (result != wxID_OK) if (result != wxID_OK)
throw win_runtime_error(ERROR_CANCELLED, __FUNCTION__ " Cancelled."); throw win_runtime_error(ERROR_CANCELLED, __FUNCTION__ " Cancelled.");
} }
@ -548,12 +504,11 @@ void eap::peer_ttls_ui::invoke_interactive_ui(
// wxInitializerPeer // wxInitializerPeer
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
wxInitializerPeer::wxInitializerPeer(_In_ HINSTANCE instance) wxInitializerPeer::wxInitializerPeer(_In_ HINSTANCE instance, _In_opt_ HWND hwndParent)
{ {
wxCriticalSectionLocker locker(s_lock); wxCriticalSectionLocker locker(s_lock);
if (s_init_ref_count++)
return;
if (s_init_ref_count++ == 0) {
// Initialize application. // Initialize application.
new wxApp(); new wxApp();
wxEntryStart(instance); wxEntryStart(instance);
@ -565,21 +520,36 @@ wxInitializerPeer::wxInitializerPeer(_In_ HINSTANCE instance)
s_locale->AddCatalog(wxT("wxExtend") wxT(wxExtendVersion)); s_locale->AddCatalog(wxT("wxExtend") wxT(wxExtendVersion));
s_locale->AddCatalog(wxT("EAPTTLSUI")); s_locale->AddCatalog(wxT("EAPTTLSUI"));
} }
}
if (hwndParent) {
// Create wxWidget-approved parent window.
m_parent = new wxWindow;
m_parent->SetHWND((WXHWND)hwndParent);
m_parent->AdoptAttributesFromHWND();
wxTopLevelWindows.Append(m_parent);
} else
m_parent = NULL;
} }
wxInitializerPeer::~wxInitializerPeer() wxInitializerPeer::~wxInitializerPeer()
{ {
wxCriticalSectionLocker locker(s_lock); wxCriticalSectionLocker locker(s_lock);
if (--s_init_ref_count)
return;
if (m_parent) {
wxTopLevelWindows.DeleteObject(m_parent);
m_parent->SetHWND((WXHWND)NULL);
}
if (--s_init_ref_count == 0) {
wxEntryCleanup(); wxEntryCleanup();
if (s_locale) { if (s_locale) {
delete s_locale; delete s_locale;
s_locale = NULL; s_locale = NULL;
} }
}
} }