Chromium Code Reviews| Index: Source/modules/crypto/SubtleCrypto.cpp |
| diff --git a/Source/modules/crypto/SubtleCrypto.cpp b/Source/modules/crypto/SubtleCrypto.cpp |
| index e0e75ccb25244f1a819295c82288f3ccc6ddaa04..5382b02b2cf80a27484c00b5573d44e483401a4f 100644 |
| --- a/Source/modules/crypto/SubtleCrypto.cpp |
| +++ b/Source/modules/crypto/SubtleCrypto.cpp |
| @@ -33,105 +33,35 @@ |
| #include "core/dom/ExceptionCode.h" |
| #include "modules/crypto/CryptoOperation.h" |
| +#include "modules/crypto/Key.h" |
| +#include "modules/crypto/KeyOperation.h" |
| #include "modules/crypto/NormalizeAlgorithm.h" |
| #include "public/platform/Platform.h" |
| -#include "public/platform/WebArrayBuffer.h" // FIXME: temporary |
| #include "public/platform/WebCrypto.h" |
| -#include "wtf/ArrayBuffer.h" |
| #include "wtf/ArrayBufferView.h" |
| -#include "wtf/SHA1.h" // FIXME: temporary |
| - |
| namespace WebCore { |
| namespace { |
| -// FIXME: The following are temporary implementations of what *should* go on the |
| -// embedder's side. Since SHA1 is easily implemented, this serves as |
| -// a useful proof of concept to get layout tests up and running and |
| -// returning correct results, until the embedder's side is implemented. |
| -//------------------------------------------------------------------------------ |
| -class DummyOperation : public WebKit::WebCryptoOperation { |
| -public: |
| - explicit DummyOperation(WebKit::WebCryptoOperationResult* result) : m_result(result) { } |
| - |
| - virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
| - { |
| - m_result->completeWithError(); |
| - delete this; |
| - } |
| - |
| - virtual void abort() OVERRIDE |
| - { |
| - delete this; |
| - } |
| - |
| - virtual void finish() OVERRIDE |
| - { |
| - m_result->completeWithError(); |
| - delete this; |
| - } |
| - |
| -protected: |
| - WebKit::WebCryptoOperationResult* m_result; |
| -}; |
| - |
| -class MockSha1Operation : public DummyOperation { |
| -public: |
| - explicit MockSha1Operation(WebKit::WebCryptoOperationResult* result) : DummyOperation(result) { } |
| - |
| - virtual void process(const unsigned char* bytes, size_t size) OVERRIDE |
| - { |
| - m_sha1.addBytes(bytes, size); |
| - } |
| - |
| - virtual void finish() OVERRIDE |
| - { |
| - Vector<uint8_t, 20> hash; |
| - m_sha1.computeHash(hash); |
| - |
| - WebKit::WebArrayBuffer buffer = WebKit::WebArrayBuffer::create(hash.size(), 1); |
| - memcpy(buffer.data(), hash.data(), hash.size()); |
| - |
| - m_result->completeWithArrayBuffer(buffer); |
| - delete this; |
| - } |
| - |
| -private: |
| - SHA1 m_sha1; |
| -}; |
| - |
| -class MockPlatformCrypto : public WebKit::WebCrypto { |
| -public: |
| - virtual void digest(const WebKit::WebCryptoAlgorithm& algorithm, WebKit::WebCryptoOperationResult* result) OVERRIDE |
| - { |
| - if (algorithm.id() == WebKit::WebCryptoAlgorithmIdSha1) { |
| - result->initializationSucceded(new MockSha1Operation(result)); |
| - } else { |
| - // Don't fail synchronously, since existing layout tests rely on |
| - // digest for testing algorithm normalization. |
| - result->initializationSucceded(new DummyOperation(result)); |
| - } |
| - } |
| -}; |
| - |
| -WebKit::WebCrypto* mockPlatformCrypto() |
| +// FIXME: Temporary |
| +PassRefPtr<CryptoOperation> dummyOperation(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, ExceptionCode& ec) |
| { |
| - DEFINE_STATIC_LOCAL(MockPlatformCrypto, crypto, ()); |
| - return &crypto; |
| -} |
| + WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
| + if (!platformCrypto) { |
| + ec = NotSupportedError; |
| + return 0; |
| + } |
| -PassRefPtr<CryptoOperation> doDummyOperation(const Dictionary& rawAlgorithm, AlgorithmOperation operationType, ExceptionCode& ec) |
| -{ |
| WebKit::WebCryptoAlgorithm algorithm; |
| if (!normalizeAlgorithm(rawAlgorithm, operationType, algorithm, ec)) |
| return 0; |
| - RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec); |
| - op->initializationSucceded(new DummyOperation(op.get())); |
| - return op.release(); |
| + RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm); |
| + WebKit::WebCryptoOperationResult result(op.get()); |
| + platformCrypto->digest(algorithm, result); |
| + return op->returnValue(ec); |
| } |
| -//------------------------------------------------------------------------------ |
| } // namespace |
| @@ -142,27 +72,27 @@ SubtleCrypto::SubtleCrypto() |
| PassRefPtr<CryptoOperation> SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, ExceptionCode& ec) |
| { |
| - return doDummyOperation(rawAlgorithm, Encrypt, ec); |
| + return dummyOperation(rawAlgorithm, Encrypt, ec); |
| } |
| PassRefPtr<CryptoOperation> SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, ExceptionCode& ec) |
| { |
| - return doDummyOperation(rawAlgorithm, Decrypt, ec); |
| + return dummyOperation(rawAlgorithm, Decrypt, ec); |
| } |
| PassRefPtr<CryptoOperation> SubtleCrypto::sign(const Dictionary& rawAlgorithm, ExceptionCode& ec) |
| { |
| - return doDummyOperation(rawAlgorithm, Sign, ec); |
| + return dummyOperation(rawAlgorithm, Sign, ec); |
| } |
| PassRefPtr<CryptoOperation> SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, ExceptionCode& ec) |
| { |
| - return doDummyOperation(rawAlgorithm, Verify, ec); |
| + return dummyOperation(rawAlgorithm, Verify, ec); |
| } |
| PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, ExceptionCode& ec) |
| { |
| - WebKit::WebCrypto* platformCrypto = mockPlatformCrypto(); |
| + WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
| if (!platformCrypto) { |
| ec = NotSupportedError; |
| return 0; |
| @@ -172,9 +102,47 @@ PassRefPtr<CryptoOperation> SubtleCrypto::digest(const Dictionary& rawAlgorithm, |
| if (!normalizeAlgorithm(rawAlgorithm, Digest, algorithm, ec)) |
| return 0; |
| - RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm, &ec); |
| - platformCrypto->digest(algorithm, op.get()); |
| - return op.release(); |
| + RefPtr<CryptoOperation> op = CryptoOperation::create(algorithm); |
| + WebKit::WebCryptoOperationResult result(op.get()); |
| + platformCrypto->digest(algorithm, result); |
| + return op->returnValue(ec); |
|
abarth-chromium
2013/07/23 21:45:49
Thanks. This looks much better.
|
| +} |
| + |
| +ScriptObject SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages, ExceptionCode& ec) |
| +{ |
| + WebKit::WebCrypto* platformCrypto = WebKit::Platform::current()->crypto(); |
| + if (!platformCrypto) { |
| + ec = NotSupportedError; |
| + return ScriptObject(); |
| + } |
| + |
| + WebKit::WebCryptoKeyUsageMask keyUsages; |
| + if (!Key::parseUsageMask(rawKeyUsages, keyUsages)) { |
| + ec = TypeError; |
| + return ScriptObject(); |
| + } |
| + |
| + WebKit::WebCryptoKeyFormat format; |
| + if (!Key::parseFormat(rawFormat, format)) { |
| + ec = TypeError; |
| + return ScriptObject(); |
| + } |
| + |
| + WebKit::WebCryptoAlgorithm algorithm; |
| + if (!normalizeAlgorithmForImportKey(rawAlgorithm, algorithm, ec)) |
| + return ScriptObject(); |
| + |
| + const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress()); |
| + |
| + // FIXME: KeyOperation is never aborted. It should probably be aborted when |
| + // the SubtleCrypto object that started it gets deleted. The concern being |
| + // if the operation eventually does complete, the ScriptPromiseResolver |
| + // might no longer be valid because the context it belonged to got torn |
| + // down. |
| + RefPtr<KeyOperation> keyOp = adoptRef(new KeyOperation); |
|
abarth-chromium
2013/07/23 21:45:49
Usually classes have a static "create" function th
eroman
2013/07/23 23:29:02
Done.
|
| + WebKit::WebCryptoKeyOperationResult result(keyOp.get()); |
| + platformCrypto->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result); |
| + return keyOp->returnValue(ec); |
| } |
| } // namespace WebCore |