OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/crypto/CryptoResultImpl.h" | 32 #include "modules/crypto/CryptoResultImpl.h" |
33 | 33 |
34 #include "bindings/v8/ScriptPromiseResolver.h" | 34 #include "bindings/v8/ScriptPromiseResolver.h" |
35 #include "bindings/v8/ScriptScope.h" | |
36 #include "bindings/v8/ScriptState.h" | |
37 #include "core/dom/ExecutionContext.h" | |
35 #include "modules/crypto/Key.h" | 38 #include "modules/crypto/Key.h" |
36 #include "modules/crypto/KeyPair.h" | 39 #include "modules/crypto/KeyPair.h" |
37 #include "modules/crypto/NormalizeAlgorithm.h" | 40 #include "modules/crypto/NormalizeAlgorithm.h" |
38 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
39 #include "public/platform/WebArrayBuffer.h" | 42 #include "public/platform/WebArrayBuffer.h" |
40 #include "public/platform/WebCryptoAlgorithm.h" | 43 #include "public/platform/WebCryptoAlgorithm.h" |
41 #include "wtf/ArrayBufferView.h" | 44 #include "wtf/ArrayBufferView.h" |
42 | 45 |
43 namespace WebCore { | 46 namespace WebCore { |
44 | 47 |
45 CryptoResultImpl::~CryptoResultImpl() | 48 CryptoResultImpl::~CryptoResultImpl() |
46 { | 49 { |
47 ASSERT(m_finished); | 50 ASSERT(m_finished); |
48 } | 51 } |
49 | 52 |
50 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptPromise promise) | 53 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptPromise promise) |
51 { | 54 { |
52 return adoptRef(new CryptoResultImpl(promise)); | 55 return adoptRef(new CryptoResultImpl(activeExecutionContext(), DOMWrapperWor ld::current(), promise)); |
53 } | 56 } |
54 | 57 |
55 void CryptoResultImpl::completeWithError() | 58 void CryptoResultImpl::completeWithError() |
56 { | 59 { |
60 CheckValidThread(); | |
61 | |
62 if (!canCompletePromise()) | |
63 return; | |
64 | |
65 // Set up V8 | |
66 v8::HandleScope handleScope(toIsolate(executionContext())); | |
67 v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world. get()); | |
68 if (v8Context.IsEmpty()) | |
69 return; | |
70 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.
| |
71 | |
57 m_promiseResolver->reject(ScriptValue::createNull()); | 72 m_promiseResolver->reject(ScriptValue::createNull()); |
58 finish(); | 73 finish(); |
59 } | 74 } |
60 | 75 |
61 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 76 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
62 { | 77 { |
78 CheckValidThread(); | |
79 | |
80 if (!canCompletePromise()) | |
81 return; | |
82 | |
83 // Set up V8 | |
84 v8::HandleScope handleScope(toIsolate(executionContext())); | |
85 v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world. get()); | |
86 if (v8Context.IsEmpty()) | |
87 return; | |
88 v8::Context::Scope scope(v8Context); | |
89 | |
63 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); | 90 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
64 finish(); | 91 finish(); |
65 } | 92 } |
66 | 93 |
67 void CryptoResultImpl::completeWithBoolean(bool b) | 94 void CryptoResultImpl::completeWithBoolean(bool b) |
68 { | 95 { |
96 CheckValidThread(); | |
97 | |
98 if (!canCompletePromise()) | |
99 return; | |
100 | |
101 // Set up V8 | |
102 v8::HandleScope handleScope(toIsolate(executionContext())); | |
103 v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world. get()); | |
104 if (v8Context.IsEmpty()) | |
105 return; | |
106 v8::Context::Scope scope(v8Context); | |
107 | |
69 m_promiseResolver->resolve(ScriptValue::createBoolean(b)); | 108 m_promiseResolver->resolve(ScriptValue::createBoolean(b)); |
70 finish(); | 109 finish(); |
71 } | 110 } |
72 | 111 |
73 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | 112 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
74 { | 113 { |
114 CheckValidThread(); | |
115 | |
116 if (!canCompletePromise()) | |
117 return; | |
118 | |
119 // Set up V8 | |
120 v8::HandleScope handleScope(toIsolate(executionContext())); | |
121 v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world. get()); | |
122 if (v8Context.IsEmpty()) | |
123 return; | |
124 v8::Context::Scope scope(v8Context); | |
125 | |
75 m_promiseResolver->resolve(Key::create(key)); | 126 m_promiseResolver->resolve(Key::create(key)); |
76 finish(); | 127 finish(); |
77 } | 128 } |
78 | 129 |
79 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) | 130 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) |
80 { | 131 { |
132 CheckValidThread(); | |
133 | |
134 if (!canCompletePromise()) | |
135 return; | |
136 | |
137 // Set up V8 | |
138 v8::HandleScope handleScope(toIsolate(executionContext())); | |
139 v8::Handle<v8::Context> v8Context = toV8Context(executionContext(), m_world. get()); | |
140 if (v8Context.IsEmpty()) | |
141 return; | |
142 v8::Context::Scope scope(v8Context); | |
143 | |
81 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | 144 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); |
145 | |
82 finish(); | 146 finish(); |
83 } | 147 } |
84 | 148 |
85 CryptoResultImpl::CryptoResultImpl(ScriptPromise promise) | 149 CryptoResultImpl::CryptoResultImpl(ExecutionContext* context, DOMWrapperWorld* w orld, ScriptPromise promise) |
86 : m_promiseResolver(ScriptPromiseResolver::create(promise)) | 150 : ContextLifecycleObserver(context) |
87 , m_finished(false) { } | 151 , m_promiseResolver(ScriptPromiseResolver::create(promise)) |
152 , m_world(world) | |
153 #if !ASSERT_DISABLED | |
154 , m_owningThread(currentThread()) | |
155 , m_finished(false) | |
156 #endif | |
157 { | |
158 ASSERT(toIsolate(context) == promise.isolate()); | |
159 } | |
88 | 160 |
89 void CryptoResultImpl::finish() | 161 void CryptoResultImpl::finish() |
90 { | 162 { |
91 ASSERT(!m_finished); | 163 ASSERT(!m_finished); |
164 #if !ASSERT_DISABLED | |
92 m_finished = true; | 165 m_finished = true; |
166 #endif | |
167 m_promiseResolver.clear(); | |
168 m_world.clear(); | |
169 } | |
170 | |
171 void CryptoResultImpl::CheckValidThread() const | |
172 { | |
173 ASSERT(m_owningThread == currentThread()); | |
174 } | |
175 | |
176 void CryptoResultImpl::contextDestroyed() | |
177 { | |
178 ContextLifecycleObserver::contextDestroyed(); | |
179 | |
180 // Abandon the promise without completing it when the context goes away. | |
181 // The CryptoResultImpl will live on until the operation is completed by | |
182 // the embedder (currently not cancellable). Once it does complete | |
183 // canCompletePromise() will be false. | |
184 m_promiseResolver.clear(); | |
185 m_world.clear(); | |
186 | |
187 ASSERT(!canCompletePromise()); | |
188 } | |
189 | |
190 bool CryptoResultImpl::canCompletePromise() const | |
191 { | |
192 ExecutionContext* context = executionContext(); | |
193 return context && !context->activeDOMObjectsAreSuspended() && !context->acti veDOMObjectsAreStopped(); | |
93 } | 194 } |
94 | 195 |
95 } // namespace WebCore | 196 } // namespace WebCore |
OLD | NEW |