Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Side by Side Diff: content/child/webcrypto/algorithm_registry.cc

Issue 379383002: Refactor WebCrypto code (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/child/webcrypto/algorithm_registry.h"
6
7 #include <map>
8
9 #include "base/lazy_instance.h"
10 #include "base/stl_util.h"
11 #include "content/child/webcrypto/algorithm.h"
12 #include "content/child/webcrypto/status.h"
13 #if defined(USE_OPENSSL)
14 // TODO(eroman):
15 #else
16 #include "content/child/webcrypto/nss/algorithms_nss.h"
17 #include "crypto/nss_util.h"
18 #endif
19
20 namespace content {
21
22 namespace webcrypto {
23
24 namespace {
25
26 class CryptoAlgorithmRegistry {
27 public:
28 CryptoAlgorithmRegistry() {
29 #if defined(USE_OPENSSL)
30 // TODO(eroman):
31 #else
32 crypto::EnsureNSSInit();
33
34 Register(blink::WebCryptoAlgorithmIdSha1, CreateShaImplementation());
35 Register(blink::WebCryptoAlgorithmIdSha256, CreateShaImplementation());
36 Register(blink::WebCryptoAlgorithmIdSha384, CreateShaImplementation());
37 Register(blink::WebCryptoAlgorithmIdSha512, CreateShaImplementation());
38
39 Register(blink::WebCryptoAlgorithmIdAesGcm, CreateAesGcmImplementation());
40 Register(blink::WebCryptoAlgorithmIdAesCbc, CreateAesCbcImplementation());
41 Register(blink::WebCryptoAlgorithmIdAesKw, CreateAesKwImplementation());
42 Register(blink::WebCryptoAlgorithmIdHmac, CreateHmacImplementation());
43 Register(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5,
44 CreateRsaSsaImplementation());
45 Register(blink::WebCryptoAlgorithmIdRsaOaep, CreateRsaOaepImplementation());
46 #endif
47 }
48
49 ~CryptoAlgorithmRegistry() {
50 STLDeleteContainerPairSecondPointers(algorithms_.begin(),
51 algorithms_.end());
52 }
53
54 const AlgorithmImplementation* GetAlgorithm(
55 blink::WebCryptoAlgorithmId id) const {
56 IdToAlgorithmMap::const_iterator it = algorithms_.find(id);
57 if (it == algorithms_.end())
58 return NULL;
59 return it->second;
60 }
61
62 private:
63 void Register(blink::WebCryptoAlgorithmId id,
64 AlgorithmImplementation* algorithm) {
65 DCHECK(algorithms_.find(id) == algorithms_.end())
66 << "Registered algorithm twice";
67 algorithms_[id] = algorithm;
68 }
69
70 typedef std::map<blink::WebCryptoAlgorithmId, AlgorithmImplementation*>
71 IdToAlgorithmMap;
72
73 IdToAlgorithmMap algorithms_;
74 };
75
76 } // namespace
77
78 base::LazyInstance<CryptoAlgorithmRegistry> g_algorithm_registry =
79 LAZY_INSTANCE_INITIALIZER;
80
81 Status GetAlgorithmFromRegistry(blink::WebCryptoAlgorithmId id,
82 const AlgorithmImplementation** impl) {
83 *impl = g_algorithm_registry.Get().GetAlgorithm(id);
84 if (!*impl)
85 return Status::ErrorUnexpected();
86 return Status::Success();
87 }
88
89 } // namespace webcrypto
90
91 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698