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 29 matching lines...) Expand all Loading... |
40 #include "modules/crypto/Key.h" | 40 #include "modules/crypto/Key.h" |
41 #include "modules/crypto/KeyPair.h" | 41 #include "modules/crypto/KeyPair.h" |
42 #include "modules/crypto/NormalizeAlgorithm.h" | 42 #include "modules/crypto/NormalizeAlgorithm.h" |
43 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
44 #include "public/platform/WebArrayBuffer.h" | 44 #include "public/platform/WebArrayBuffer.h" |
45 #include "public/platform/WebCryptoAlgorithm.h" | 45 #include "public/platform/WebCryptoAlgorithm.h" |
46 #include "wtf/ArrayBufferView.h" | 46 #include "wtf/ArrayBufferView.h" |
47 | 47 |
48 namespace WebCore { | 48 namespace WebCore { |
49 | 49 |
| 50 namespace { |
| 51 |
| 52 class WeakResolver : public ScriptPromiseResolverWithContext { |
| 53 public: |
| 54 static WeakPtr<ScriptPromiseResolverWithContext> create(ScriptState* scriptS
tate) |
| 55 { |
| 56 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState)); |
| 57 p->suspendIfNeeded(); |
| 58 p->keepAliveWhilePending(); |
| 59 return p->m_weakPtrFactory.createWeakPtr(); |
| 60 } |
| 61 |
| 62 private: |
| 63 explicit WeakResolver(ScriptState* scriptState) |
| 64 : ScriptPromiseResolverWithContext(scriptState) |
| 65 , m_weakPtrFactory(this) { } |
| 66 WeakPtrFactory<ScriptPromiseResolverWithContext> m_weakPtrFactory; |
| 67 }; |
| 68 |
| 69 } // namespace |
| 70 |
50 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) | 71 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) |
51 { | 72 { |
52 switch (errorType) { | 73 switch (errorType) { |
53 case blink::WebCryptoErrorTypeNotSupported: | 74 case blink::WebCryptoErrorTypeNotSupported: |
54 return NotSupportedError; | 75 return NotSupportedError; |
55 case blink::WebCryptoErrorTypeSyntax: | 76 case blink::WebCryptoErrorTypeSyntax: |
56 return SyntaxError; | 77 return SyntaxError; |
57 case blink::WebCryptoErrorTypeInvalidState: | 78 case blink::WebCryptoErrorTypeInvalidState: |
58 return InvalidStateError; | 79 return InvalidStateError; |
59 case blink::WebCryptoErrorTypeInvalidAccess: | 80 case blink::WebCryptoErrorTypeInvalidAccess: |
60 return InvalidAccessError; | 81 return InvalidAccessError; |
61 case blink::WebCryptoErrorTypeUnknown: | 82 case blink::WebCryptoErrorTypeUnknown: |
62 return UnknownError; | 83 return UnknownError; |
63 case blink::WebCryptoErrorTypeData: | 84 case blink::WebCryptoErrorTypeData: |
64 return DataError; | 85 return DataError; |
65 case blink::WebCryptoErrorTypeOperation: | 86 case blink::WebCryptoErrorTypeOperation: |
66 return OperationError; | 87 return OperationError; |
67 case blink::WebCryptoErrorTypeType: | 88 case blink::WebCryptoErrorTypeType: |
68 // FIXME: This should construct a TypeError instead. For now do | 89 // FIXME: This should construct a TypeError instead. For now do |
69 // something to facilitate refactor, but this will need to be | 90 // something to facilitate refactor, but this will need to be |
70 // revisited. | 91 // revisited. |
71 return DataError; | 92 return DataError; |
72 } | 93 } |
73 | 94 |
74 ASSERT_NOT_REACHED(); | 95 ASSERT_NOT_REACHED(); |
75 return 0; | 96 return 0; |
76 } | 97 } |
77 | 98 |
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 completeWithJson(const char* utf8Data, unsigned length) | |
119 { | |
120 ScriptState* scriptState = m_promiseResolver->scriptState(); | |
121 ScriptState::Scope scope(scriptState); | |
122 | |
123 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState-
>isolate(), utf8Data, v8::String::kInternalizedString, length); | |
124 | |
125 v8::TryCatch exceptionCatcher; | |
126 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); | |
127 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { | |
128 ASSERT_NOT_REACHED(); | |
129 m_promiseResolver->reject(DOMException::create(OperationError, "Fail
ed inflating JWK JSON to object")); | |
130 } else { | |
131 m_promiseResolver->resolve(jsonDictionary); | |
132 } | |
133 delete this; | |
134 } | |
135 | |
136 void completeWithBoolean(bool b) | |
137 { | |
138 m_promiseResolver->resolve(b); | |
139 delete this; | |
140 } | |
141 | |
142 void completeWithKey(const blink::WebCryptoKey& key) | |
143 { | |
144 m_promiseResolver->resolve(Key::create(key)); | |
145 delete this; | |
146 } | |
147 | |
148 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::
WebCryptoKey& privateKey) | |
149 { | |
150 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | |
151 delete this; | |
152 } | |
153 | |
154 private: | |
155 // This subclass of ScriptPromiseResolverWithContext is to be notified | |
156 // when the context was destroyed. | |
157 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { | |
158 public: | |
159 static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, Prom
iseState* promiseState) | |
160 { | |
161 RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scri
ptState, promiseState)); | |
162 resolver->suspendIfNeeded(); | |
163 return resolver.release(); | |
164 } | |
165 | |
166 virtual void contextDestroyed() OVERRIDE | |
167 { | |
168 ScriptPromiseResolverWithContext::contextDestroyed(); | |
169 m_promiseState->contextDestroyed(); | |
170 } | |
171 | |
172 private: | |
173 explicit PromiseResolver(ScriptState* scriptState, PromiseState* promise
State) | |
174 : ScriptPromiseResolverWithContext(scriptState) | |
175 , m_promiseState(promiseState) | |
176 { | |
177 } | |
178 | |
179 PromiseState* m_promiseState; | |
180 }; | |
181 | |
182 explicit PromiseState(ScriptState* scriptState) | |
183 : m_weakFactory(this) | |
184 , m_promiseResolver(PromiseResolver::create(scriptState, this)) | |
185 { | |
186 } | |
187 | |
188 WeakPtrFactory<PromiseState> m_weakFactory; | |
189 RefPtr<PromiseResolver> m_promiseResolver; | |
190 }; | |
191 | |
192 CryptoResultImpl::~CryptoResultImpl() | 99 CryptoResultImpl::~CryptoResultImpl() |
193 { | 100 { |
194 } | 101 } |
195 | 102 |
196 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 103 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
197 { | 104 { |
198 return adoptRef(new CryptoResultImpl(scriptState)); | 105 return adoptRef(new CryptoResultImpl(scriptState)); |
199 } | 106 } |
200 | 107 |
201 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) | 108 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) |
202 { | 109 { |
203 if (m_promiseState) | 110 if (m_resolver) |
204 m_promiseState->completeWithError(errorType, errorDetails); | 111 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er
rorType), errorDetails)); |
205 } | 112 } |
206 | 113 |
207 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 114 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
208 { | 115 { |
209 if (m_promiseState) | 116 if (m_resolver) |
210 m_promiseState->completeWithBuffer(buffer); | 117 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
211 } | 118 } |
212 | 119 |
213 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) | 120 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) |
214 { | 121 { |
215 if (m_promiseState) | 122 if (m_resolver) { |
216 m_promiseState->completeWithJson(utf8Data, length); | 123 ScriptPromiseResolverWithContext* resolver = m_resolver.get(); |
| 124 ScriptState* scriptState = resolver->scriptState(); |
| 125 ScriptState::Scope scope(scriptState); |
| 126 |
| 127 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState-
>isolate(), utf8Data, v8::String::kInternalizedString, length); |
| 128 |
| 129 v8::TryCatch exceptionCatcher; |
| 130 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); |
| 131 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { |
| 132 ASSERT_NOT_REACHED(); |
| 133 resolver->reject(DOMException::create(OperationError, "Failed inflat
ing JWK JSON to object")); |
| 134 } else { |
| 135 resolver->resolve(jsonDictionary); |
| 136 } |
| 137 } |
217 } | 138 } |
218 | 139 |
219 void CryptoResultImpl::completeWithBoolean(bool b) | 140 void CryptoResultImpl::completeWithBoolean(bool b) |
220 { | 141 { |
221 if (m_promiseState) | 142 if (m_resolver) |
222 m_promiseState->completeWithBoolean(b); | 143 m_resolver->resolve(b); |
223 } | 144 } |
224 | 145 |
225 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | 146 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
226 { | 147 { |
227 if (m_promiseState) | 148 if (m_resolver) |
228 m_promiseState->completeWithKey(key); | 149 m_resolver->resolve(Key::create(key)); |
229 } | 150 } |
230 | 151 |
231 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) | 152 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) |
232 { | 153 { |
233 if (m_promiseState) | 154 if (m_resolver) |
234 m_promiseState->completeWithKeyPair(publicKey, privateKey); | 155 m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
235 } | 156 } |
236 | 157 |
237 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 158 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
238 : m_promiseState(PromiseState::create(scriptState)) | 159 : m_resolver(WeakResolver::create(scriptState)) |
239 { | 160 { |
240 } | 161 } |
241 | 162 |
242 ScriptPromise CryptoResultImpl::promise() | 163 ScriptPromise CryptoResultImpl::promise() |
243 { | 164 { |
244 return m_promiseState->promise(); | 165 return m_resolver->promise(); |
245 } | 166 } |
246 | 167 |
247 } // namespace WebCore | 168 } // namespace WebCore |
OLD | NEW |