Index: Source/modules/crypto/CryptoOperation.cpp |
diff --git a/Source/modules/crypto/CryptoOperation.cpp b/Source/modules/crypto/CryptoOperation.cpp |
index dff9858af591a68d2da390252f04b337be0114f7..c706dcc42bce2ca6735703a7224d0d84db538aa3 100644 |
--- a/Source/modules/crypto/CryptoOperation.cpp |
+++ b/Source/modules/crypto/CryptoOperation.cpp |
@@ -31,14 +31,106 @@ |
#include "config.h" |
#include "modules/crypto/CryptoOperation.h" |
+#include "bindings/v8/custom/V8ArrayBufferCustom.h" // MUST precede ScriptPromiseResolver 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...
|
+#include "bindings/v8/ScriptPromiseResolver.h" |
#include "modules/crypto/Algorithm.h" |
+#include "public/platform/WebCrypto.h" |
+#include "public/web/WebArrayBuffer.h" |
+#include "wtf/ArrayBuffer.h" |
+#include "wtf/ArrayBufferView.h" |
namespace WebCore { |
-CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm) |
+class CryptoOperation::Result : public WebKit::WebCryptoOperationResult { |
+public: |
+ explicit Result(CryptoOperation* parent) : m_parent(parent) { } |
+ |
+ virtual void setArrayBuffer(const WebKit::WebArrayBuffer& buffer) OVERRIDE |
+ { |
+ ASSERT(m_parent->m_state == Finishing); |
+ m_parent->m_state = Done; |
+ m_parent->m_impl = 0; |
+ |
+ m_parent->m_promiseResolver->fulfill(PassRefPtr<ArrayBuffer>(buffer)); |
+ } |
+ |
+private: |
+ CryptoOperation* m_parent; |
+}; |
+ |
+CryptoOperation::~CryptoOperation() |
+{ |
+ abort(); |
+ ASSERT(!m_impl); |
+} |
+ |
+PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::WebCryptoOperation* impl) |
+{ |
+ return adoptRef(new CryptoOperation(algorithm, impl)); |
+} |
+ |
+CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::WebCryptoOperation* impl) |
: m_algorithm(algorithm) |
+ , m_impl(impl) |
+ , m_state(Processing) |
+ , m_result(adoptPtr(new Result(this))) |
{ |
+ ASSERT(impl); |
ScriptWrappable::init(this); |
+ |
+ m_promiseResolver = ScriptPromiseResolver::create(); |
+ m_promise = m_promiseResolver->promise(); |
+} |
+ |
+CryptoOperation* CryptoOperation::process(ArrayBuffer* data) |
+{ |
+ process(static_cast<unsigned char*>(data->data()), data->byteLength()); |
+ return this; |
+} |
+ |
+CryptoOperation* CryptoOperation::process(ArrayBufferView* data) |
+{ |
+ process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength()); |
+ return this; |
+} |
+ |
+CryptoOperation* CryptoOperation::finish() |
+{ |
+ switch (m_state) { |
+ case Processing: |
+ m_state = Finishing; |
+ // NOTE: The following line can result in re-entrancy to |this| |
+ m_impl->finish(m_result.get()); |
+ break; |
+ case Finishing: |
+ // Calling finish() twice is a no-op. |
+ break; |
+ case Done: |
+ // Calling finish() after already complete is a no-op. |
+ ASSERT(!m_impl); |
+ break; |
+ } |
+ |
+ return this; |
+} |
+ |
+CryptoOperation* CryptoOperation::abort() |
+{ |
+ switch (m_state) { |
+ case Processing: |
+ case Finishing: |
+ // This will cause m_impl to be deleted. |
+ m_impl->abort(); |
+ m_state = Done; |
+ m_impl = 0; |
+ m_promiseResolver->reject(ScriptValue(v8::Null())); |
+ break; |
+ case Done: |
+ ASSERT(!m_impl); |
+ break; |
+ } |
+ |
+ return this; |
} |
Algorithm* CryptoOperation::algorithm() |
@@ -48,4 +140,21 @@ Algorithm* CryptoOperation::algorithm() |
return m_algorithmNode.get(); |
} |
+const ScriptValue& CryptoOperation::promise() |
+{ |
+ return m_promise; |
+} |
+ |
+void CryptoOperation::process(const unsigned char* bytes, size_t size) |
+{ |
+ switch (m_state) { |
+ case Processing: |
+ m_impl->process(bytes, size); |
+ break; |
+ case Finishing: |
+ case Done: |
+ return; |
+ } |
+} |
+ |
} // namespace WebCore |