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 return p->m_weakPtrFactory.createWeakPtr(); |
| 59 } |
| 60 |
| 61 private: |
| 62 explicit WeakResolver(ScriptState* scriptState) |
| 63 : ScriptPromiseResolverWithContext(scriptState, ScriptPromiseResolverWit
hContext::KeepAliveWhilePending) |
| 64 , m_weakPtrFactory(this) { } |
| 65 WeakPtrFactory<ScriptPromiseResolverWithContext> m_weakPtrFactory; |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
50 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) | 70 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) |
51 { | 71 { |
52 switch (errorType) { | 72 switch (errorType) { |
53 case blink::WebCryptoErrorTypeNotSupported: | 73 case blink::WebCryptoErrorTypeNotSupported: |
54 return NotSupportedError; | 74 return NotSupportedError; |
55 case blink::WebCryptoErrorTypeSyntax: | 75 case blink::WebCryptoErrorTypeSyntax: |
56 return SyntaxError; | 76 return SyntaxError; |
57 case blink::WebCryptoErrorTypeInvalidState: | 77 case blink::WebCryptoErrorTypeInvalidState: |
58 return InvalidStateError; | 78 return InvalidStateError; |
59 case blink::WebCryptoErrorTypeInvalidAccess: | 79 case blink::WebCryptoErrorTypeInvalidAccess: |
60 return InvalidAccessError; | 80 return InvalidAccessError; |
61 case blink::WebCryptoErrorTypeUnknown: | 81 case blink::WebCryptoErrorTypeUnknown: |
62 return UnknownError; | 82 return UnknownError; |
63 case blink::WebCryptoErrorTypeData: | 83 case blink::WebCryptoErrorTypeData: |
64 return DataError; | 84 return DataError; |
65 case blink::WebCryptoErrorTypeOperation: | 85 case blink::WebCryptoErrorTypeOperation: |
66 return OperationError; | 86 return OperationError; |
67 case blink::WebCryptoErrorTypeType: | 87 case blink::WebCryptoErrorTypeType: |
68 // FIXME: This should construct a TypeError instead. For now do | 88 // FIXME: This should construct a TypeError instead. For now do |
69 // something to facilitate refactor, but this will need to be | 89 // something to facilitate refactor, but this will need to be |
70 // revisited. | 90 // revisited. |
71 return DataError; | 91 return DataError; |
72 } | 92 } |
73 | 93 |
74 ASSERT_NOT_REACHED(); | 94 ASSERT_NOT_REACHED(); |
75 return 0; | 95 return 0; |
76 } | 96 } |
77 | 97 |
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() | 98 CryptoResultImpl::~CryptoResultImpl() |
175 { | 99 { |
176 } | 100 } |
177 | 101 |
178 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 102 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
179 { | 103 { |
180 return adoptRef(new CryptoResultImpl(scriptState)); | 104 return adoptRef(new CryptoResultImpl(scriptState)); |
181 } | 105 } |
182 | 106 |
183 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) | 107 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) |
184 { | 108 { |
185 if (m_promiseState) | 109 if (m_resolver) |
186 m_promiseState->completeWithError(errorType, errorDetails); | 110 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er
rorType), errorDetails)); |
187 } | 111 } |
188 | 112 |
189 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 113 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
190 { | 114 { |
191 if (m_promiseState) | 115 if (m_resolver) |
192 m_promiseState->completeWithBuffer(buffer); | 116 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
193 } | 117 } |
194 | 118 |
195 void CryptoResultImpl::completeWithBoolean(bool b) | 119 void CryptoResultImpl::completeWithBoolean(bool b) |
196 { | 120 { |
197 if (m_promiseState) | 121 if (m_resolver) |
198 m_promiseState->completeWithBoolean(b); | 122 m_resolver->resolve(b); |
199 } | 123 } |
200 | 124 |
201 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | 125 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
202 { | 126 { |
203 if (m_promiseState) | 127 if (m_resolver) |
204 m_promiseState->completeWithKey(key); | 128 m_resolver->resolve(Key::create(key)); |
205 } | 129 } |
206 | 130 |
207 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) | 131 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) |
208 { | 132 { |
209 if (m_promiseState) | 133 if (m_resolver) |
210 m_promiseState->completeWithKeyPair(publicKey, privateKey); | 134 m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
211 } | 135 } |
212 | 136 |
213 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 137 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
214 : m_promiseState(PromiseState::create(scriptState)) | 138 : m_resolver(WeakResolver::create(scriptState)) |
215 { | 139 { |
216 } | 140 } |
217 | 141 |
218 ScriptPromise CryptoResultImpl::promise() | 142 ScriptPromise CryptoResultImpl::promise() |
219 { | 143 { |
220 return m_promiseState->promise(); | 144 return m_resolver->promise(); |
221 } | 145 } |
222 | 146 |
223 } // namespace WebCore | 147 } // namespace WebCore |
OLD | NEW |