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

Side by Side Diff: Source/modules/crypto/CryptoOperation.cpp

Issue 19082002: WebCrypto: Add SHA-1 support to crypto.subtle.digest(). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: reorder headers even though it violates style Created 7 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
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "modules/crypto/CryptoOperation.h" 32 #include "modules/crypto/CryptoOperation.h"
33 33
34 #include "bindings/v8/custom/V8ArrayBufferCustom.h" // MUST precede ScriptPromis eResolver for compilation to work.
eroman 2013/07/12 02:58:43 Not sure whether this is working as intended or no
abarth-chromium 2013/07/12 03:53:24 Interesting. Welcome to the bleeding edge...
35 #include "bindings/v8/ScriptPromiseResolver.h"
34 #include "modules/crypto/Algorithm.h" 36 #include "modules/crypto/Algorithm.h"
37 #include "public/platform/WebCrypto.h"
38 #include "public/web/WebArrayBuffer.h"
39 #include "wtf/ArrayBuffer.h"
40 #include "wtf/ArrayBufferView.h"
35 41
36 namespace WebCore { 42 namespace WebCore {
37 43
38 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm) 44 class CryptoOperation::Result : public WebKit::WebCryptoOperationResult {
45 public:
46 explicit Result(CryptoOperation* parent) : m_parent(parent) { }
47
48 virtual void setArrayBuffer(const WebKit::WebArrayBuffer& buffer) OVERRIDE
49 {
50 ASSERT(m_parent->m_state == Finishing);
51 m_parent->m_state = Done;
52 m_parent->m_impl = 0;
53
54 m_parent->m_promiseResolver->fulfill(PassRefPtr<ArrayBuffer>(buffer));
55 }
56
57 private:
58 CryptoOperation* m_parent;
59 };
60
61 CryptoOperation::~CryptoOperation()
62 {
63 abort();
64 ASSERT(!m_impl);
65 }
66
67 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor ithm& algorithm, WebKit::WebCryptoOperation* impl)
68 {
69 return adoptRef(new CryptoOperation(algorithm, impl));
70 }
71
72 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, We bKit::WebCryptoOperation* impl)
39 : m_algorithm(algorithm) 73 : m_algorithm(algorithm)
74 , m_impl(impl)
75 , m_state(Processing)
76 , m_result(adoptPtr(new Result(this)))
40 { 77 {
78 ASSERT(impl);
41 ScriptWrappable::init(this); 79 ScriptWrappable::init(this);
80
81 m_promiseResolver = ScriptPromiseResolver::create();
82 m_promise = m_promiseResolver->promise();
83 }
84
85 CryptoOperation* CryptoOperation::process(ArrayBuffer* data)
86 {
87 process(static_cast<unsigned char*>(data->data()), data->byteLength());
88 return this;
89 }
90
91 CryptoOperation* CryptoOperation::process(ArrayBufferView* data)
92 {
93 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength() );
94 return this;
95 }
96
97 CryptoOperation* CryptoOperation::finish()
98 {
99 switch (m_state) {
100 case Processing:
101 m_state = Finishing;
102 // NOTE: The following line can result in re-entrancy to |this|
103 m_impl->finish(m_result.get());
104 break;
105 case Finishing:
106 // Calling finish() twice is a no-op.
107 break;
108 case Done:
109 // Calling finish() after already complete is a no-op.
110 ASSERT(!m_impl);
111 break;
112 }
113
114 return this;
115 }
116
117 CryptoOperation* CryptoOperation::abort()
118 {
119 switch (m_state) {
120 case Processing:
121 case Finishing:
122 // This will cause m_impl to be deleted.
123 m_impl->abort();
124 m_state = Done;
125 m_impl = 0;
126 m_promiseResolver->reject(ScriptValue(v8::Null()));
127 break;
128 case Done:
129 ASSERT(!m_impl);
130 break;
131 }
132
133 return this;
42 } 134 }
43 135
44 Algorithm* CryptoOperation::algorithm() 136 Algorithm* CryptoOperation::algorithm()
45 { 137 {
46 if (!m_algorithmNode) 138 if (!m_algorithmNode)
47 m_algorithmNode = Algorithm::create(m_algorithm); 139 m_algorithmNode = Algorithm::create(m_algorithm);
48 return m_algorithmNode.get(); 140 return m_algorithmNode.get();
49 } 141 }
50 142
143 const ScriptValue& CryptoOperation::promise()
144 {
145 return m_promise;
146 }
147
148 void CryptoOperation::process(const unsigned char* bytes, size_t size)
149 {
150 switch (m_state) {
151 case Processing:
152 m_impl->process(bytes, size);
153 break;
154 case Finishing:
155 case Done:
156 return;
157 }
158 }
159
51 } // namespace WebCore 160 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698