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,49 +434,32 @@ 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; sanitizing_wstring
if (hwndParent) { challenge(reinterpret_cast<sanitizing_wstring::const_pointer>(ctx.m_data.data()), ctx.m_data.size()/sizeof(sanitizing_wstring::value_type)),
// Create wxWidget-approved parent window. response;
parent = new wxWindow;
parent->SetHWND((WXHWND)hwndParent);
parent->AdoptAttributesFromHWND();
wxTopLevelWindows.Append(parent);
} else
parent = NULL;
{ // Build dialog to prompt for response.
sanitizing_wstring wxGTCResponseDialog dlg(*cfg_prov, init.m_parent);
challenge(reinterpret_cast<sanitizing_wstring::const_pointer>(ctx.m_data.data()), ctx.m_data.size()/sizeof(sanitizing_wstring::value_type)), auto panel = new wxGTCResponsePanel(response, challenge.c_str(), &dlg);
response; dlg.AddContent(panel);
// Build dialog to prompt for response. // Update dialog layout.
wxGTCResponseDialog dlg(*cfg_prov, parent); dlg.Layout();
auto panel = new wxGTCResponsePanel(response, challenge.c_str(), &dlg); dlg.GetSizer()->Fit(&dlg);
dlg.AddContent(panel);
// Update dialog layout. // Centre and display dialog.
dlg.Layout(); dlg.Centre(wxBOTH);
dlg.GetSizer()->Fit(&dlg); if (!init.m_parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
// Centre and display dialog. ::FlashWindowEx(&fwi);
dlg.Centre(wxBOTH);
if (!parent) {
FLASHWINFO fwi = { sizeof(FLASHWINFO), dlg.GetHWND(), FLASHW_ALL | FLASHW_TIMERNOFG };
::FlashWindowEx(&fwi);
}
if ((result = dlg.ShowModal()) == wxID_OK) {
// Save response.
ctx.m_data.assign(
reinterpret_cast<sanitizing_blob::const_pointer>(response.data() ),
reinterpret_cast<sanitizing_blob::const_pointer>(response.data() + response.length()));
}
} }
if ((result = dlg.ShowModal()) == wxID_OK) {
if (parent) { // Save response.
wxTopLevelWindows.DeleteObject(parent); ctx.m_data.assign(
parent->SetHWND((WXHWND)NULL); reinterpret_cast<sanitizing_blob::const_pointer>(response.data() ),
reinterpret_cast<sanitizing_blob::const_pointer>(response.data() + response.length()));
} }
} }
@ -548,37 +504,51 @@ 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;
// Initialize application. if (s_init_ref_count++ == 0) {
new wxApp(); // Initialize application.
wxEntryStart(instance); new wxApp();
wxEntryStart(instance);
// Do our wxWidgets configuration and localization initialization. // Do our wxWidgets configuration and localization initialization.
wxInitializeConfig(); wxInitializeConfig();
s_locale = new wxLocale; s_locale = new wxLocale;
if (wxInitializeLocale(*s_locale)) { if (wxInitializeLocale(*s_locale)) {
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;
wxEntryCleanup(); if (m_parent) {
wxTopLevelWindows.DeleteObject(m_parent);
m_parent->SetHWND((WXHWND)NULL);
}
if (s_locale) { if (--s_init_ref_count == 0) {
delete s_locale; wxEntryCleanup();
s_locale = NULL;
if (s_locale) {
delete s_locale;
s_locale = NULL;
}
} }
} }