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

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: Remove an extra newline 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
« no previous file with comments | « Source/modules/crypto/CryptoOperation.h ('k') | Source/modules/crypto/CryptoOperation.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
35 #include "bindings/v8/ScriptPromiseResolver.h"
34 #include "modules/crypto/Algorithm.h" 36 #include "modules/crypto/Algorithm.h"
37 #include "public/platform/WebArrayBuffer.h"
38 #include "public/platform/WebCrypto.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->promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer));
55 }
56
57 private:
58 CryptoOperation* m_parent;
59 };
60
61 CryptoOperation::~CryptoOperation()
62 {
63 abortImpl();
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);
42 } 80 }
43 81
82 CryptoOperation* CryptoOperation::process(ArrayBuffer* data)
83 {
84 process(static_cast<unsigned char*>(data->data()), data->byteLength());
85 return this;
86 }
87
88 CryptoOperation* CryptoOperation::process(ArrayBufferView* data)
89 {
90 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength() );
91 return this;
92 }
93
94 ScriptObject CryptoOperation::finish()
95 {
96 switch (m_state) {
97 case Processing:
98 m_state = Finishing;
99 // NOTE: The following line can result in re-entrancy to |this|
100 m_impl->finish(m_result.get());
101 break;
102 case Finishing:
103 // Calling finish() twice is a no-op.
104 break;
105 case Done:
106 // Calling finish() after already complete is a no-op.
107 ASSERT(!m_impl);
108 break;
109 }
110
111 return promiseResolver()->promise();
112 }
113
114 ScriptObject CryptoOperation::abort()
115 {
116 if (abortImpl())
117 promiseResolver()->reject(ScriptValue::createNull());
118 return promiseResolver()->promise();
119 }
120
44 Algorithm* CryptoOperation::algorithm() 121 Algorithm* CryptoOperation::algorithm()
45 { 122 {
46 if (!m_algorithmNode) 123 if (!m_algorithmNode)
47 m_algorithmNode = Algorithm::create(m_algorithm); 124 m_algorithmNode = Algorithm::create(m_algorithm);
48 return m_algorithmNode.get(); 125 return m_algorithmNode.get();
49 } 126 }
50 127
128 void CryptoOperation::process(const unsigned char* bytes, size_t size)
129 {
130 switch (m_state) {
131 case Processing:
132 m_impl->process(bytes, size);
133 break;
134 case Finishing:
135 case Done:
136 return;
137 }
138 }
139
140 bool CryptoOperation::abortImpl()
141 {
142 switch (m_state) {
143 case Processing:
144 case Finishing:
145 // This will cause m_impl to be deleted.
146 m_impl->abort();
147 m_state = Done;
148 m_impl = 0;
149 return true;
150 case Done:
151 ASSERT(!m_impl);
152 break;
153 }
154
155 return false;
156 }
157
158 ScriptPromiseResolver* CryptoOperation::promiseResolver()
159 {
160 if (!m_promiseResolver)
161 m_promiseResolver = ScriptPromiseResolver::create();
162 return m_promiseResolver.get();
163 }
164
51 } // namespace WebCore 165 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/CryptoOperation.h ('k') | Source/modules/crypto/CryptoOperation.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698