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

Unified 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 CryptoOperation.promise(), instead make finish() and abort() return Promise 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 side-by-side diff with in-line comments
Download patch
Index: Source/modules/crypto/CryptoOperation.cpp
diff --git a/Source/modules/crypto/CryptoOperation.cpp b/Source/modules/crypto/CryptoOperation.cpp
index dff9858af591a68d2da390252f04b337be0114f7..77f8856bfc803707c6f17f366b512973f8226031 100644
--- a/Source/modules/crypto/CryptoOperation.cpp
+++ b/Source/modules/crypto/CryptoOperation.cpp
@@ -31,14 +31,105 @@
#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->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();
abarth-chromium 2013/07/16 00:29:10 Can we delay creating the m_promiseResolver until
eroman 2013/07/16 07:28:07 Done.
+}
+
+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 m_promiseResolver->promise();
abarth-chromium 2013/07/16 00:29:10 e.g., we could create it right here.
eroman 2013/07/16 07:28:07 Done.
+}
+
+ScriptObject 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::createNull());
+ break;
+ case Done:
+ ASSERT(!m_impl);
+ break;
+ }
+
+ return m_promiseResolver->promise();
}
Algorithm* CryptoOperation::algorithm()
@@ -48,4 +139,16 @@ 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;
+ }
+}
+
} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698