1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "CryptoAlgorithmRegistry.h"
28
29#if ENABLE(SUBTLE_CRYPTO)
30
31#include "CryptoAlgorithm.h"
32#include <mutex>
33#include <wtf/NeverDestroyed.h>
34
35namespace WebCore {
36
37CryptoAlgorithmRegistry& CryptoAlgorithmRegistry::shared()
38{
39    static NeverDestroyed<CryptoAlgorithmRegistry> registry;
40
41    return registry;
42}
43
44static std::mutex& registryMutex()
45{
46    static std::once_flag onceFlag;
47    static LazyNeverDestroyed<std::mutex> mutex;
48
49    std::call_once(onceFlag, []{
50        mutex.construct();
51    });
52
53    return mutex;
54}
55
56CryptoAlgorithmRegistry::CryptoAlgorithmRegistry()
57{
58    platformRegisterAlgorithms();
59}
60
61bool CryptoAlgorithmRegistry::getIdentifierForName(const String& name, CryptoAlgorithmIdentifier& result)
62{
63    if (name.isEmpty())
64        return false;
65
66    std::lock_guard<std::mutex> lock(registryMutex());
67
68    auto iter = m_nameToIdentifierMap.find(name.isolatedCopy());
69    if (iter == m_nameToIdentifierMap.end())
70        return false;
71
72    result = iter->value;
73    return true;
74}
75
76String CryptoAlgorithmRegistry::nameForIdentifier(CryptoAlgorithmIdentifier identifier)
77{
78    std::lock_guard<std::mutex> lock(registryMutex());
79
80    return m_identifierToNameMap.get(static_cast<unsigned>(identifier)).isolatedCopy();
81}
82
83std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmRegistry::create(CryptoAlgorithmIdentifier identifier)
84{
85    std::lock_guard<std::mutex> lock(registryMutex());
86
87    auto iter = m_identifierToConstructorMap.find(static_cast<unsigned>(identifier));
88    if (iter == m_identifierToConstructorMap.end())
89        return nullptr;
90
91    return iter->value();
92}
93
94void CryptoAlgorithmRegistry::registerAlgorithm(const String& name, CryptoAlgorithmIdentifier identifier, CryptoAlgorithmConstructor constructor)
95{
96    std::lock_guard<std::mutex> lock(registryMutex());
97
98    bool added = m_nameToIdentifierMap.add(name, identifier).isNewEntry;
99    ASSERT_UNUSED(added, added);
100
101    added = m_identifierToNameMap.add(static_cast<unsigned>(identifier), name).isNewEntry;
102    ASSERT_UNUSED(added, added);
103
104    added = m_identifierToConstructorMap.add(static_cast<unsigned>(identifier), constructor).isNewEntry;
105    ASSERT_UNUSED(added, added);
106}
107
108
109} // namespace WebCore
110
111#endif // ENABLE(SUBTLE_CRYPTO)
112