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 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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/ScriptPromiseResolverWithContext.h" | |
35 #include "bindings/v8/ScriptState.h" | 34 #include "bindings/v8/ScriptState.h" |
36 #include "core/dom/ContextLifecycleObserver.h" | 35 #include "core/dom/AsyncInitializerResolver.h" |
37 #include "core/dom/DOMError.h" | 36 #include "core/dom/DOMError.h" |
38 #include "core/dom/DOMException.h" | 37 #include "core/dom/DOMException.h" |
39 #include "core/dom/ExecutionContext.h" | |
40 #include "modules/crypto/Key.h" | 38 #include "modules/crypto/Key.h" |
41 #include "modules/crypto/KeyPair.h" | 39 #include "modules/crypto/KeyPair.h" |
42 #include "modules/crypto/NormalizeAlgorithm.h" | 40 #include "modules/crypto/NormalizeAlgorithm.h" |
43 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
44 #include "public/platform/WebArrayBuffer.h" | 42 #include "public/platform/WebArrayBuffer.h" |
45 #include "public/platform/WebCryptoAlgorithm.h" | 43 #include "public/platform/WebCryptoAlgorithm.h" |
46 #include "wtf/ArrayBufferView.h" | 44 #include "wtf/ArrayBufferView.h" |
47 | 45 |
48 namespace WebCore { | 46 namespace WebCore { |
49 | 47 |
(...skipping 22 matching lines...) Expand all Loading... |
72 // revisited. | 70 // revisited. |
73 return DataError; | 71 return DataError; |
74 } | 72 } |
75 | 73 |
76 ASSERT_NOT_REACHED(); | 74 ASSERT_NOT_REACHED(); |
77 return 0; | 75 return 0; |
78 } | 76 } |
79 | 77 |
80 } // namespace | 78 } // namespace |
81 | 79 |
82 // The PromiseState class contains all the state which is tied to an | 80 class CryptoResultImpl::Initializer FINAL { |
83 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, | |
84 // PromiseState is not thread safe and must only be accessed and deleted from | |
85 // the blink thread. | |
86 // | |
87 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat
e. | |
88 // The PromiseState deletes itself after being notified of completion. | |
89 // Additionally the PromiseState is deleted 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 { | |
93 public: | 81 public: |
94 static WeakPtr<PromiseState> create(ScriptState* scriptState) | 82 static PassOwnPtr<Initializer> create(CryptoResultImpl* owner) { return adop
tPtr(new Initializer(owner)); } |
| 83 void start(AsyncInitializerResolver<Initializer>* resolver) |
95 { | 84 { |
96 PromiseState* promiseState = new PromiseState(scriptState); | 85 ASSERT(m_owner); |
97 return promiseState->m_weakFactory.createWeakPtr(); | 86 m_factory = adoptPtr(new WeakPtrFactory<AsyncInitializerResolver<Initial
izer> >(resolver)); |
| 87 m_owner->m_resolver = m_factory->createWeakPtr(); |
| 88 m_owner = nullptr; |
98 } | 89 } |
99 | 90 void contextDestroyed() { } |
100 void contextDestroyed() | |
101 { | |
102 delete this; | |
103 } | |
104 | |
105 ScriptPromise promise() | |
106 { | |
107 return m_promiseResolver->promise(); | |
108 } | |
109 | |
110 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web
String& errorDetails) | |
111 { | |
112 m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType
), errorDetails)); | |
113 delete this; | |
114 } | |
115 | |
116 void completeWithBuffer(const blink::WebArrayBuffer& buffer) | |
117 { | |
118 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); | |
119 delete this; | |
120 } | |
121 | |
122 void completeWithBoolean(bool b) | |
123 { | |
124 m_promiseResolver->resolve(b); | |
125 delete this; | |
126 } | |
127 | |
128 void completeWithKey(const blink::WebCryptoKey& key) | |
129 { | |
130 m_promiseResolver->resolve(Key::create(key)); | |
131 delete this; | |
132 } | |
133 | |
134 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::
WebCryptoKey& privateKey) | |
135 { | |
136 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | |
137 delete this; | |
138 } | |
139 | 91 |
140 private: | 92 private: |
141 // This subclass of ScriptPromiseResolverWithContext is to be notified | 93 explicit Initializer(CryptoResultImpl* owner) : m_owner(owner) { } |
142 // when the context was destroyed. | 94 CryptoResultImpl* m_owner; |
143 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { | 95 OwnPtr<WeakPtrFactory<AsyncInitializerResolver<Initializer> > > m_factory; |
144 public: | |
145 static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, Prom
iseState* promiseState) | |
146 { | |
147 RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scri
ptState, promiseState)); | |
148 resolver->suspendIfNeeded(); | |
149 return resolver.release(); | |
150 } | |
151 | |
152 virtual void contextDestroyed() OVERRIDE | |
153 { | |
154 ScriptPromiseResolverWithContext::contextDestroyed(); | |
155 m_promiseState->contextDestroyed(); | |
156 } | |
157 | |
158 private: | |
159 explicit PromiseResolver(ScriptState* scriptState, PromiseState* promise
State) | |
160 : ScriptPromiseResolverWithContext(scriptState) | |
161 , m_promiseState(promiseState) | |
162 { | |
163 } | |
164 | |
165 PromiseState* m_promiseState; | |
166 }; | |
167 | |
168 explicit PromiseState(ScriptState* scriptState) | |
169 : m_weakFactory(this) | |
170 , m_promiseResolver(PromiseResolver::create(scriptState, this)) | |
171 { | |
172 } | |
173 | |
174 WeakPtrFactory<PromiseState> m_weakFactory; | |
175 RefPtr<PromiseResolver> m_promiseResolver; | |
176 }; | 96 }; |
177 | 97 |
178 CryptoResultImpl::~CryptoResultImpl() | 98 CryptoResultImpl::~CryptoResultImpl() |
179 { | 99 { |
180 } | 100 } |
181 | 101 |
182 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 102 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
183 { | 103 { |
184 return adoptRef(new CryptoResultImpl(scriptState)); | 104 return adoptRef(new CryptoResultImpl(scriptState)); |
185 } | 105 } |
186 | 106 |
187 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) | 107 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) |
188 { | 108 { |
189 if (m_promiseState) | 109 if (m_resolver) |
190 m_promiseState->completeWithError(errorType, errorDetails); | 110 m_resolver->reject(DOMException::create(toExceptionCode(errorType), erro
rDetails)); |
191 } | 111 } |
192 | 112 |
193 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 113 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
194 { | 114 { |
195 if (m_promiseState) | 115 if (m_resolver) |
196 m_promiseState->completeWithBuffer(buffer); | 116 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
197 } | 117 } |
198 | 118 |
199 void CryptoResultImpl::completeWithBoolean(bool b) | 119 void CryptoResultImpl::completeWithBoolean(bool b) |
200 { | 120 { |
201 if (m_promiseState) | 121 if (m_resolver) |
202 m_promiseState->completeWithBoolean(b); | 122 m_resolver->resolve(b); |
203 } | 123 } |
204 | 124 |
205 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | 125 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
206 { | 126 { |
207 if (m_promiseState) | 127 if (m_resolver) |
208 m_promiseState->completeWithKey(key); | 128 m_resolver->resolve(Key::create(key)); |
209 } | 129 } |
210 | 130 |
211 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) | 131 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) |
212 { | 132 { |
213 if (m_promiseState) | 133 if (m_resolver) |
214 m_promiseState->completeWithKeyPair(publicKey, privateKey); | 134 m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
215 } | 135 } |
216 | 136 |
217 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 137 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
218 : m_promiseState(PromiseState::create(scriptState)) | |
219 { | 138 { |
| 139 AsyncInitializerResolver<Initializer>::start(scriptState, Initializer::creat
e(this)); |
220 } | 140 } |
221 | 141 |
222 ScriptPromise CryptoResultImpl::promise() | 142 ScriptPromise CryptoResultImpl::promise() |
223 { | 143 { |
224 return m_promiseState->promise(); | 144 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
225 } | 145 } |
226 | 146 |
227 } // namespace WebCore | 147 } // namespace WebCore |
OLD | NEW |