Chromium Code Reviews| Index: Source/modules/crypto/CryptoResultImpl.cpp |
| diff --git a/Source/modules/crypto/CryptoResultImpl.cpp b/Source/modules/crypto/CryptoResultImpl.cpp |
| index 557562c2c687bff7ce40fddd8a7e3ef677bb2846..dd598cfcf1eb779ef871b175be3ff59f62e5a9bc 100644 |
| --- a/Source/modules/crypto/CryptoResultImpl.cpp |
| +++ b/Source/modules/crypto/CryptoResultImpl.cpp |
| @@ -32,6 +32,9 @@ |
| #include "modules/crypto/CryptoResultImpl.h" |
| #include "bindings/v8/ScriptPromiseResolver.h" |
| +#include "bindings/v8/ScriptScope.h" |
| +#include "bindings/v8/ScriptState.h" |
| +#include "core/dom/ExecutionContext.h" |
| #include "modules/crypto/Key.h" |
| #include "modules/crypto/KeyPair.h" |
| #include "modules/crypto/NormalizeAlgorithm.h" |
| @@ -49,47 +52,145 @@ CryptoResultImpl::~CryptoResultImpl() |
| PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptPromise promise) |
| { |
| - return adoptRef(new CryptoResultImpl(promise)); |
| + return adoptRef(new CryptoResultImpl(activeExecutionContext(), DOMWrapperWorld::current(), promise)); |
| } |
| void CryptoResultImpl::completeWithError() |
| { |
| + CheckValidThread(); |
| + |
| + if (!canCompletePromise()) |
| + return; |
| + |
| + // Set up V8 |
| + v8::HandleScope handleScope(toIsolate(executionContext())); |
| + v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world.get()); |
| + if (v8Context.IsEmpty()) |
| + return; |
| + v8::Context::Scope scope(v8Context); |
|
abarth-chromium
2014/01/16 04:41:08
Maybe DOMRequestState would be helpful here?
http
eroman
2014/01/16 20:24:16
Perfect, just what I needed!
Updated, PTAL.
|
| + |
| m_promiseResolver->reject(ScriptValue::createNull()); |
| finish(); |
| } |
| void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
| { |
| + CheckValidThread(); |
| + |
| + if (!canCompletePromise()) |
| + return; |
| + |
| + // Set up V8 |
| + v8::HandleScope handleScope(toIsolate(executionContext())); |
| + v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world.get()); |
| + if (v8Context.IsEmpty()) |
| + return; |
| + v8::Context::Scope scope(v8Context); |
| + |
| m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
| finish(); |
| } |
| void CryptoResultImpl::completeWithBoolean(bool b) |
| { |
| + CheckValidThread(); |
| + |
| + if (!canCompletePromise()) |
| + return; |
| + |
| + // Set up V8 |
| + v8::HandleScope handleScope(toIsolate(executionContext())); |
| + v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world.get()); |
| + if (v8Context.IsEmpty()) |
| + return; |
| + v8::Context::Scope scope(v8Context); |
| + |
| m_promiseResolver->resolve(ScriptValue::createBoolean(b)); |
| finish(); |
| } |
| void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
| { |
| + CheckValidThread(); |
| + |
| + if (!canCompletePromise()) |
| + return; |
| + |
| + // Set up V8 |
| + v8::HandleScope handleScope(toIsolate(executionContext())); |
| + v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world.get()); |
| + if (v8Context.IsEmpty()) |
| + return; |
| + v8::Context::Scope scope(v8Context); |
| + |
| m_promiseResolver->resolve(Key::create(key)); |
| finish(); |
| } |
| void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) |
| { |
| + CheckValidThread(); |
| + |
| + if (!canCompletePromise()) |
| + return; |
| + |
| + // Set up V8 |
| + v8::HandleScope handleScope(toIsolate(executionContext())); |
| + v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world.get()); |
| + if (v8Context.IsEmpty()) |
| + return; |
| + v8::Context::Scope scope(v8Context); |
| + |
| m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); |
| + |
| finish(); |
| } |
| -CryptoResultImpl::CryptoResultImpl(ScriptPromise promise) |
| - : m_promiseResolver(ScriptPromiseResolver::create(promise)) |
| - , m_finished(false) { } |
| +CryptoResultImpl::CryptoResultImpl(ExecutionContext* context, DOMWrapperWorld* world, ScriptPromise promise) |
| + : ContextLifecycleObserver(context) |
| + , m_promiseResolver(ScriptPromiseResolver::create(promise)) |
| + , m_world(world) |
| +#if !ASSERT_DISABLED |
| + , m_owningThread(currentThread()) |
| + , m_finished(false) |
| +#endif |
| +{ |
| + ASSERT(toIsolate(context) == promise.isolate()); |
| +} |
| void CryptoResultImpl::finish() |
| { |
| ASSERT(!m_finished); |
| +#if !ASSERT_DISABLED |
| m_finished = true; |
| +#endif |
| + m_promiseResolver.clear(); |
| + m_world.clear(); |
| +} |
| + |
| +void CryptoResultImpl::CheckValidThread() const |
| +{ |
| + ASSERT(m_owningThread == currentThread()); |
| +} |
| + |
| +void CryptoResultImpl::contextDestroyed() |
| +{ |
| + ContextLifecycleObserver::contextDestroyed(); |
| + |
| + // Abandon the promise without completing it when the context goes away. |
| + // The CryptoResultImpl will live on until the operation is completed by |
| + // the embedder (currently not cancellable). Once it does complete |
| + // canCompletePromise() will be false. |
| + m_promiseResolver.clear(); |
| + m_world.clear(); |
| + |
| + ASSERT(!canCompletePromise()); |
| +} |
| + |
| +bool CryptoResultImpl::canCompletePromise() const |
| +{ |
| + ExecutionContext* context = executionContext(); |
| + return context && !context->activeDOMObjectsAreSuspended() && !context->activeDOMObjectsAreStopped(); |
| } |
| } // namespace WebCore |