Index: Source/modules/crypto/CryptoOperation.cpp |
diff --git a/Source/modules/crypto/CryptoOperation.cpp b/Source/modules/crypto/CryptoOperation.cpp |
index dff9858af591a68d2da390252f04b337be0114f7..fd2c1d18ef8bbf1e53eece95249a4e263eb56226 100644 |
--- a/Source/modules/crypto/CryptoOperation.cpp |
+++ b/Source/modules/crypto/CryptoOperation.cpp |
@@ -31,16 +31,93 @@ |
#include "config.h" |
#include "modules/crypto/CryptoOperation.h" |
+#include "bindings/v8/custom/V8ArrayBufferCustom.h" // MUST precede ScriptPromiseResolver for compilation to work. |
+#include "bindings/v8/ScriptPromiseResolver.h" |
#include "modules/crypto/Algorithm.h" |
+#include "public/platform/WebArrayBuffer.h" |
+#include "public/platform/WebCrypto.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->promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer)); |
+ } |
+ |
+private: |
+ CryptoOperation* m_parent; |
+}; |
+ |
+CryptoOperation::~CryptoOperation() |
+{ |
+ abortImpl(); |
+ 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); |
} |
+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; |
+} |
+ |
+ScriptObject 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 promiseResolver()->promise(); |
+} |
+ |
+ScriptObject CryptoOperation::abort() |
+{ |
+ if (abortImpl()) |
+ promiseResolver()->reject(ScriptValue::createNull()); |
+ return promiseResolver()->promise(); |
+} |
+ |
Algorithm* CryptoOperation::algorithm() |
{ |
if (!m_algorithmNode) |
@@ -48,4 +125,41 @@ Algorithm* CryptoOperation::algorithm() |
return m_algorithmNode.get(); |
} |
+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; |
+ } |
+} |
+ |
+bool CryptoOperation::abortImpl() |
+{ |
+ switch (m_state) { |
+ case Processing: |
+ case Finishing: |
+ // This will cause m_impl to be deleted. |
+ m_impl->abort(); |
+ m_state = Done; |
+ m_impl = 0; |
+ return true; |
+ case Done: |
+ ASSERT(!m_impl); |
+ break; |
+ } |
+ |
+ return false; |
+} |
+ |
+ScriptPromiseResolver* CryptoOperation::promiseResolver() |
+{ |
+ if (!m_promiseResolver) |
+ m_promiseResolver = ScriptPromiseResolver::create(); |
+ return m_promiseResolver.get(); |
+} |
+ |
} // namespace WebCore |