2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "modules/crypto/CryptoResultImpl.h"
34 #include "bindings/core/v8/Dictionary.h"
35 #include "bindings/core/v8/ScriptPromiseResolver.h"
36 #include "bindings/core/v8/ScriptState.h"
37 #include "core/dom/ContextLifecycleObserver.h"
38 #include "core/dom/DOMArrayBuffer.h"
39 #include "core/dom/DOMError.h"
40 #include "core/dom/DOMException.h"
41 #include "core/dom/ExecutionContext.h"
42 #include "modules/crypto/CryptoKey.h"
43 #include "modules/crypto/NormalizeAlgorithm.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebArrayBuffer.h"
46 #include "public/platform/WebCryptoAlgorithm.h"
50 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver {
52 static WeakPtr<ScriptPromiseResolver> create(ScriptState* scriptState, CryptoResultImpl* result)
54 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result));
56 p->keepAliveWhilePending();
57 return p->m_weakPtrFactory.createWeakPtr();
60 virtual ~WeakResolver()
66 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result)
67 : ScriptPromiseResolver(scriptState)
68 , m_weakPtrFactory(this)
69 , m_result(result) { }
70 WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory;
71 RefPtr<CryptoResultImpl> m_result;
74 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
77 case WebCryptoErrorTypeNotSupported:
78 return NotSupportedError;
79 case WebCryptoErrorTypeSyntax:
81 case WebCryptoErrorTypeInvalidState:
82 return InvalidStateError;
83 case WebCryptoErrorTypeInvalidAccess:
84 return InvalidAccessError;
85 case WebCryptoErrorTypeUnknown:
87 case WebCryptoErrorTypeData:
89 case WebCryptoErrorTypeOperation:
90 return OperationError;
91 case WebCryptoErrorTypeType:
92 // FIXME: This should construct a TypeError instead. For now do
93 // something to facilitate refactor, but this will need to be
102 CryptoResultImpl::~CryptoResultImpl()
106 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
108 return adoptRef(new CryptoResultImpl(scriptState));
111 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const WebString& errorDetails)
114 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails));
117 void CryptoResultImpl::completeWithBuffer(const WebArrayBuffer& buffer)
120 m_resolver->resolve(DOMArrayBuffer::create(buffer));
123 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
126 ScriptPromiseResolver* resolver = m_resolver.get();
127 ScriptState* scriptState = resolver->scriptState();
128 ScriptState::Scope scope(scriptState);
130 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState->isolate(), utf8Data, v8::String::kInternalizedString, length);
132 v8::TryCatch exceptionCatcher;
133 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
134 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
135 ASSERT_NOT_REACHED();
136 resolver->reject(DOMException::create(OperationError, "Failed inflating JWK JSON to object"));
138 resolver->resolve(jsonDictionary);
143 void CryptoResultImpl::completeWithBoolean(bool b)
146 m_resolver->resolve(b);
149 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
152 m_resolver->resolve(CryptoKey::create(key));
155 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
158 ScriptState* scriptState = m_resolver->scriptState();
159 ScriptState::Scope scope(scriptState);
161 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate());
163 v8::Handle<v8::Value> publicKeyValue = toV8NoInline(CryptoKey::create(publicKey), scriptState->context()->Global(), scriptState->isolate());
164 v8::Handle<v8::Value> privateKeyValue = toV8NoInline(CryptoKey::create(privateKey), scriptState->context()->Global(), scriptState->isolate());
166 keyPair.set("publicKey", publicKeyValue);
167 keyPair.set("privateKey", privateKeyValue);
169 m_resolver->resolve(keyPair.v8Value());
173 bool CryptoResultImpl::cancelled() const
175 return acquireLoad(&m_cancelled);
178 void CryptoResultImpl::cancel()
180 releaseStore(&m_cancelled, 1);
183 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
186 // Creating the WeakResolver may return nullptr if active dom objects have
187 // been stopped. And in the process set m_cancelled to 1.
188 m_resolver = WeakResolver::create(scriptState, this);
191 ScriptPromise CryptoResultImpl::promise()
193 return m_resolver ? m_resolver->promise() : ScriptPromise();