CryptoAlgorithmRegistry.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRegistry.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithm.h"
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
static Lock registryMutex;
CryptoAlgorithmRegistry& CryptoAlgorithmRegistry::singleton()
{
static NeverDestroyed<CryptoAlgorithmRegistry> registry;
return registry;
}
CryptoAlgorithmRegistry::CryptoAlgorithmRegistry()
{
platformRegisterAlgorithms();
}
std::optional<CryptoAlgorithmIdentifier> CryptoAlgorithmRegistry::identifier(const String& name)
{
if (name.isEmpty())
return std::nullopt;
std::lock_guard<Lock> lock(registryMutex);
auto identifier = m_identifiers.find(name.isolatedCopy());
if (identifier == m_identifiers.end())
return std::nullopt;
return identifier->value;
}
String CryptoAlgorithmRegistry::name(CryptoAlgorithmIdentifier identifier)
{
std::lock_guard<Lock> lock(registryMutex);
auto contructor = m_constructors.find(static_cast<unsigned>(identifier));
if (contructor == m_constructors.end())
return { };
return contructor->value.first.isolatedCopy();
}
RefPtr<CryptoAlgorithm> CryptoAlgorithmRegistry::create(CryptoAlgorithmIdentifier identifier)
{
std::lock_guard<Lock> lock(registryMutex);
auto contructor = m_constructors.find(static_cast<unsigned>(identifier));
if (contructor == m_constructors.end())
return nullptr;
return contructor->value.second();
}
void CryptoAlgorithmRegistry::registerAlgorithm(const String& name, CryptoAlgorithmIdentifier identifier, CryptoAlgorithmConstructor constructor)
{
std::lock_guard<Lock> lock(registryMutex);
ASSERT(!m_identifiers.contains(name));
ASSERT(!m_constructors.contains(static_cast<unsigned>(identifier)));
m_identifiers.add(name, identifier);
m_constructors.add(static_cast<unsigned>(identifier), std::make_pair(name, constructor));
}
}
#endif // ENABLE(SUBTLE_CRYPTO)