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" | |
36 #include "core/dom/ContextLifecycleObserver.h" | |
37 #include "core/dom/DOMError.h" | |
38 #include "core/dom/DOMException.h" | 34 #include "core/dom/DOMException.h" |
39 #include "core/dom/ExecutionContext.h" | 35 #include "public/platform/WebCrypto.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" | |
47 | 36 |
48 namespace WebCore { | 37 namespace WebCore { |
49 | 38 |
50 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) | 39 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) |
51 { | 40 { |
52 switch (errorType) { | 41 switch (errorType) { |
53 case blink::WebCryptoErrorTypeNotSupported: | 42 case blink::WebCryptoErrorTypeNotSupported: |
54 return NotSupportedError; | 43 return NotSupportedError; |
55 case blink::WebCryptoErrorTypeSyntax: | 44 case blink::WebCryptoErrorTypeSyntax: |
56 return SyntaxError; | 45 return SyntaxError; |
(...skipping 11 matching lines...) Expand all Loading... |
68 // FIXME: This should construct a TypeError instead. For now do | 57 // FIXME: This should construct a TypeError instead. For now do |
69 // something to facilitate refactor, but this will need to be | 58 // something to facilitate refactor, but this will need to be |
70 // revisited. | 59 // revisited. |
71 return DataError; | 60 return DataError; |
72 } | 61 } |
73 | 62 |
74 ASSERT_NOT_REACHED(); | 63 ASSERT_NOT_REACHED(); |
75 return 0; | 64 return 0; |
76 } | 65 } |
77 | 66 |
78 // The PromiseState class contains all the state which is tied to an | |
79 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, | |
80 // PromiseState is not thread safe and must only be accessed and deleted from | |
81 // the blink thread. | |
82 // | |
83 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat
e. | |
84 // The PromiseState deletes itself after being notified of completion. | |
85 // Additionally the PromiseState is deleted when the ExecutionContext is | |
86 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, | |
87 // which may die before the operation is completed). | |
88 class CryptoResultImpl::PromiseState FINAL { | |
89 public: | |
90 static WeakPtr<PromiseState> create(ScriptState* scriptState) | |
91 { | |
92 PromiseState* promiseState = new PromiseState(scriptState); | |
93 return promiseState->m_weakFactory.createWeakPtr(); | |
94 } | |
95 | |
96 void contextDestroyed() | |
97 { | |
98 delete this; | |
99 } | |
100 | |
101 ScriptPromise promise() | |
102 { | |
103 return m_promiseResolver->promise(); | |
104 } | |
105 | |
106 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web
String& errorDetails) | |
107 { | |
108 m_promiseResolver->reject(DOMException::create(webCryptoErrorToException
Code(errorType), errorDetails)); | |
109 delete this; | |
110 } | |
111 | |
112 void completeWithBuffer(const blink::WebArrayBuffer& buffer) | |
113 { | |
114 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); | |
115 delete this; | |
116 } | |
117 | |
118 void completeWithBoolean(bool b) | |
119 { | |
120 m_promiseResolver->resolve(b); | |
121 delete this; | |
122 } | |
123 | |
124 void completeWithKey(const blink::WebCryptoKey& key) | |
125 { | |
126 m_promiseResolver->resolve(Key::create(key)); | |
127 delete this; | |
128 } | |
129 | |
130 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::
WebCryptoKey& privateKey) | |
131 { | |
132 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | |
133 delete this; | |
134 } | |
135 | |
136 private: | |
137 // This subclass of ScriptPromiseResolverWithContext is to be notified | |
138 // when the context was destroyed. | |
139 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { | |
140 public: | |
141 static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, Prom
iseState* promiseState) | |
142 { | |
143 RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scri
ptState, promiseState)); | |
144 resolver->suspendIfNeeded(); | |
145 return resolver.release(); | |
146 } | |
147 | |
148 virtual void contextDestroyed() OVERRIDE | |
149 { | |
150 ScriptPromiseResolverWithContext::contextDestroyed(); | |
151 m_promiseState->contextDestroyed(); | |
152 } | |
153 | |
154 private: | |
155 explicit PromiseResolver(ScriptState* scriptState, PromiseState* promise
State) | |
156 : ScriptPromiseResolverWithContext(scriptState) | |
157 , m_promiseState(promiseState) | |
158 { | |
159 } | |
160 | |
161 PromiseState* m_promiseState; | |
162 }; | |
163 | |
164 explicit PromiseState(ScriptState* scriptState) | |
165 : m_weakFactory(this) | |
166 , m_promiseResolver(PromiseResolver::create(scriptState, this)) | |
167 { | |
168 } | |
169 | |
170 WeakPtrFactory<PromiseState> m_weakFactory; | |
171 RefPtr<PromiseResolver> m_promiseResolver; | |
172 }; | |
173 | |
174 CryptoResultImpl::~CryptoResultImpl() | |
175 { | |
176 } | |
177 | |
178 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | |
179 { | |
180 return adoptRef(new CryptoResultImpl(scriptState)); | |
181 } | |
182 | |
183 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) | |
184 { | |
185 if (m_promiseState) | |
186 m_promiseState->completeWithError(errorType, errorDetails); | |
187 } | |
188 | |
189 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | |
190 { | |
191 if (m_promiseState) | |
192 m_promiseState->completeWithBuffer(buffer); | |
193 } | |
194 | |
195 void CryptoResultImpl::completeWithBoolean(bool b) | |
196 { | |
197 if (m_promiseState) | |
198 m_promiseState->completeWithBoolean(b); | |
199 } | |
200 | |
201 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | |
202 { | |
203 if (m_promiseState) | |
204 m_promiseState->completeWithKey(key); | |
205 } | |
206 | |
207 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) | |
208 { | |
209 if (m_promiseState) | |
210 m_promiseState->completeWithKeyPair(publicKey, privateKey); | |
211 } | |
212 | |
213 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | |
214 : m_promiseState(PromiseState::create(scriptState)) | |
215 { | |
216 } | |
217 | |
218 ScriptPromise CryptoResultImpl::promise() | |
219 { | |
220 return m_promiseState->promise(); | |
221 } | |
222 | |
223 } // namespace WebCore | 67 } // namespace WebCore |
OLD | NEW |