Make wxSecretValue always available, even when !wxUSE_SECRETSTORE

This allows to write the code using this class without peppering it with
wxUSE_SECRETSTORE checks that would otherwise be necessary to support
Unix builds on system without libsecret.

No real changes.
This commit is contained in:
Vadim Zeitlin
2021-01-10 01:20:47 +01:00
parent 8820bb9609
commit f8aa5785ce

View File

@@ -12,10 +12,10 @@
#include "wx/defs.h" #include "wx/defs.h"
#if wxUSE_SECRETSTORE
#include "wx/string.h" #include "wx/string.h"
#if wxUSE_SECRETSTORE
// Initial version of wxSecretStore required passing user name to Load(), which // Initial version of wxSecretStore required passing user name to Load(), which
// didn't make much sense without support for multiple usernames per service, // didn't make much sense without support for multiple usernames per service,
// so the API was changed to load the username too. Test for this symbol to // so the API was changed to load the username too. Test for this symbol to
@@ -168,6 +168,66 @@ private:
wxSecretStoreImpl* const m_impl; wxSecretStoreImpl* const m_impl;
}; };
#endif // wxUSE_SECRETSTORE #else // !wxUSE_SECRETSTORE
// Provide stand in for wxSecretValue allowing to use it without having #if
// wxUSE_SECRETSTORE checks everywhere. Unlike the real version, this class
// doesn't provide any added security.
class wxSecretValue
{
public:
wxSecretValue() { m_valid = false; }
wxSecretValue(size_t size, const void *data)
{
Init(size, data);
}
explicit wxSecretValue(const wxString& secret)
{
const wxScopedCharBuffer buf(secret.utf8_str());
Init(buf.length(), buf.data());
}
bool IsOk() const { return m_valid; }
bool operator==(const wxSecretValue& other) const
{
return m_valid == other.m_valid && m_data == other.m_data;
}
bool operator!=(const wxSecretValue& other) const
{
return !(*this == other);
}
size_t GetSize() const { return m_data.utf8_str().length(); }
const void *GetData() const { return m_data.utf8_str().data(); }
wxString GetAsString(const wxMBConv& conv = wxConvWhateverWorks) const
{
wxUnusedVar(conv);
return m_data;
}
static void Wipe(size_t size, void *data) { memset(data, 0, size); }
static void WipeString(wxString& str)
{
str.assign(str.length(), '*');
str.clear();
}
private:
void Init(size_t size, const void *data)
{
m_data = wxString::From8BitData(static_cast<const char*>(data), size);
}
wxString m_data;
bool m_valid;
};
#endif // wxUSE_SECRETSTORE/!wxUSE_SECRETSTORE
#endif // _WX_SECRETSTORE_H_ #endif // _WX_SECRETSTORE_H_