Index: Source/modules/crypto/SubtleCrypto.cpp |
diff --git a/Source/modules/crypto/SubtleCrypto.cpp b/Source/modules/crypto/SubtleCrypto.cpp |
index 832f916541561c73da9c175f00c8229f9d9976be..b1bf42494a1bd74462d6e49f345730d58e4b040e 100644 |
--- a/Source/modules/crypto/SubtleCrypto.cpp |
+++ b/Source/modules/crypto/SubtleCrypto.cpp |
@@ -32,17 +32,81 @@ |
#include "modules/crypto/SubtleCrypto.h" |
#include "bindings/v8/Dictionary.h" |
+#include "bindings/v8/ScriptPromiseResolverWithContext.h" |
+#include "core/dom/DOMException.h" |
#include "core/dom/ExecutionContext.h" |
-#include "modules/crypto/CryptoResultImpl.h" |
+#include "modules/crypto/CrossThreadCryptoResult.h" |
#include "modules/crypto/Key.h" |
+#include "modules/crypto/KeyPair.h" |
#include "modules/crypto/NormalizeAlgorithm.h" |
#include "public/platform/Platform.h" |
+#include "public/platform/WebArrayBuffer.h" |
#include "public/platform/WebCrypto.h" |
#include "public/platform/WebCryptoAlgorithm.h" |
#include "wtf/ArrayBufferView.h" |
namespace WebCore { |
+class SubtleCrypto::PendingResult : public GarbageCollectedFinalized<PendingResult>, public CryptoResultBase { |
+public: |
+ PendingResult(ScriptState* scriptState, RawPtr<SubtleCrypto> crypto) |
haraken
2014/06/13 14:23:31
RawPtr<SubtleCrypto> => SubtleCrypto*
|
+ : m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) |
+ , m_crypto(crypto) |
+ , m_factory(this) |
+ { |
+ m_resolver->keepObjectWhilePending(crypto); |
+ m_crypto->m_pendingResults.add(this); |
+ } |
+ |
+ virtual ~PendingResult() { } |
+ |
+ virtual void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails) OVERRIDE |
+ { |
+ m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails)); |
+ unregisterThis(); |
+ } |
+ |
+ virtual void completeWithBuffer(const blink::WebArrayBuffer& buffer) OVERRIDE |
+ { |
+ m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
+ unregisterThis(); |
+ } |
+ |
+ virtual void completeWithBoolean(bool b) OVERRIDE |
+ { |
+ m_resolver->resolve(b); |
+ unregisterThis(); |
+ } |
+ |
+ virtual void completeWithKey(const blink::WebCryptoKey& key) OVERRIDE |
+ { |
+ m_resolver->resolve(Key::create(key)); |
+ unregisterThis(); |
+ } |
+ |
+ virtual void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) OVERRIDE |
+ { |
+ m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
+ unregisterThis(); |
+ } |
+ |
+ ScriptPromise promise() { return m_resolver->promise(); } |
+ |
+ WeakPtr<PendingResult> createWeakPtr() { return m_factory.createWeakPtr(); } |
+ |
+ void trace(Visitor* visitor) { visitor->trace(m_crypto); } |
+ |
+private: |
+ void unregisterThis() |
+ { |
+ m_crypto->m_pendingResults.remove(this); |
+ } |
+ |
+ RefPtr<ScriptPromiseResolverWithContext> m_resolver; |
+ Member<SubtleCrypto> m_crypto; |
+ WeakPtrFactory<PendingResult> m_factory; |
+}; |
+ |
// Seems like the generated bindings should take care of these however it |
// currently doesn't. See also http://crbug.com/264520 |
static bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResult* result) |
@@ -85,10 +149,10 @@ static bool canAccessWebCrypto(ScriptState* scriptState, CryptoResult* result) |
return true; |
} |
-static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer) |
+ScriptPromise SubtleCrypto::startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -141,6 +205,10 @@ SubtleCrypto::SubtleCrypto() |
ScriptWrappable::init(this); |
} |
+SubtleCrypto::~SubtleCrypto() |
+{ |
+} |
+ |
ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data) |
{ |
return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCryptoOperationEncrypt, ArrayPiece(), data); |
@@ -168,8 +236,8 @@ ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& r |
ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -188,8 +256,8 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona |
ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& rawFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -215,8 +283,8 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra |
ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& rawFormat, Key* key) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -239,8 +307,8 @@ ScriptPromise SubtleCrypto::exportKey(ScriptState* scriptState, const String& ra |
ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawFormat, Key* key, Key* wrappingKey, const Dictionary& rawWrapAlgorithm) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -273,8 +341,8 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF |
ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& rawFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) |
{ |
- RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); |
- ScriptPromise promise = result->promise(); |
+ ScriptPromise promise; |
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = createCrossThreadCryptoResult(scriptState, &promise); |
if (!canAccessWebCrypto(scriptState, result.get())) |
return promise; |
@@ -307,4 +375,17 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra |
return promise; |
} |
+PassRefPtr<CrossThreadCryptoResult<SubtleCrypto::PendingResult> > SubtleCrypto::createCrossThreadCryptoResult(ScriptState* scriptState, ScriptPromise* promise) |
+{ |
+ RawPtr<PendingResult> pending = new PendingResult(scriptState, this); |
haraken
2014/06/13 14:23:31
RawPtr<PendingResult> => PendingResult*
|
+ RefPtr<CrossThreadCryptoResult<PendingResult> > result = CrossThreadCryptoResult<PendingResult>::create(pending->createWeakPtr()); |
+ *promise = pending->promise(); |
+ return result.release(); |
+} |
+ |
+void SubtleCrypto::trace(Visitor* visitor) |
+{ |
+ visitor->trace(m_pendingResults); |
+} |
+ |
} // namespace WebCore |