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/v8/ScriptPromiseResolverWithContext.h"
35 #include "bindings/v8/ScriptState.h"
36 #include "core/dom/ContextLifecycleObserver.h"
37 #include "core/dom/DOMError.h"
38 #include "core/dom/DOMException.h"
39 #include "core/dom/ExecutionContext.h"
40 #include "modules/crypto/Key.h"
41 #include "modules/crypto/KeyPair.h"
42 #include "modules/crypto/NormalizeAlgorithm.h"
43 #include "public/platform/Platform.h"
44 #include "public/platform/WebArrayBuffer.h"
45 #include "public/platform/WebCryptoAlgorithm.h"
46 #include "wtf/ArrayBufferView.h"
52 ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
55 case blink::WebCryptoErrorTypeNotSupported:
56 return NotSupportedError;
57 case blink::WebCryptoErrorTypeSyntax:
59 case blink::WebCryptoErrorTypeInvalidState:
60 return InvalidStateError;
61 case blink::WebCryptoErrorTypeInvalidAccess:
62 return InvalidAccessError;
63 case blink::WebCryptoErrorTypeUnknown:
65 case blink::WebCryptoErrorTypeData:
67 case blink::WebCryptoErrorTypeOperation:
68 return OperationError;
69 case blink::WebCryptoErrorTypeType:
70 // FIXME: This should construct a TypeError instead. For now do
71 // something to facilitate refactor, but this will need to be
82 // The PromiseState class contains all the state which is tied to an
83 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread,
84 // PromiseState is not thread safe and must only be accessed and deleted from
87 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseState.
88 // The PromiseState deletes itself after being notified of completion.
89 // Additionally the PromiseState deletes itself when the ExecutionContext is
90 // destroyed (necessary to avoid leaks when dealing with WebWorker threads,
91 // which may die before the operation is completed).
92 class CryptoResultImpl::PromiseState FINAL : public ContextLifecycleObserver {
94 static WeakPtr<PromiseState> create(ExecutionContext* context)
96 PromiseState* promiseState = new PromiseState(context);
97 return promiseState->m_weakFactory.createWeakPtr();
100 // Override from ContextLifecycleObserver
101 virtual void contextDestroyed() OVERRIDE
103 ContextLifecycleObserver::contextDestroyed();
107 ScriptPromise promise()
109 return m_promiseResolver->promise();
112 void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
114 m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails));
118 void completeWithBuffer(const blink::WebArrayBuffer& buffer)
120 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
124 void completeWithBoolean(bool b)
126 m_promiseResolver->resolve(b);
130 void completeWithKey(const blink::WebCryptoKey& key)
132 m_promiseResolver->resolve(Key::create(key));
136 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
138 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey));
143 explicit PromiseState(ExecutionContext* context)
144 : ContextLifecycleObserver(context)
145 , m_weakFactory(this)
146 , m_promiseResolver(ScriptPromiseResolverWithContext::create(ScriptState::current(toIsolate(context))))
150 WeakPtrFactory<PromiseState> m_weakFactory;
151 RefPtr<ScriptPromiseResolverWithContext> m_promiseResolver;
154 CryptoResultImpl::~CryptoResultImpl()
158 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create()
160 return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::GetCurrent())));
163 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
166 m_promiseState->completeWithError(errorType, errorDetails);
169 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
172 m_promiseState->completeWithBuffer(buffer);
175 void CryptoResultImpl::completeWithBoolean(bool b)
178 m_promiseState->completeWithBoolean(b);
181 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
184 m_promiseState->completeWithKey(key);
187 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
190 m_promiseState->completeWithKeyPair(publicKey, privateKey);
193 CryptoResultImpl::CryptoResultImpl(ExecutionContext* context)
194 : m_promiseState(PromiseState::create(context))
198 ScriptPromise CryptoResultImpl::promise()
200 return m_promiseState->promise();
203 } // namespace WebCore