Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: Source/modules/crypto/CryptoResultImpl.cpp

Issue 312653005: [ABANDONED] Use AsyncInitializerResolver in WebCrypto. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@async-initializer
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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" 34 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
eroman 2014/06/06 18:58:56 Delete
yhirano 2014/06/09 06:42:25 Done.
35 #include "bindings/v8/ScriptState.h" 35 #include "bindings/v8/ScriptState.h"
36 #include "core/dom/AsyncInitializerResolver.h"
36 #include "core/dom/ContextLifecycleObserver.h" 37 #include "core/dom/ContextLifecycleObserver.h"
eroman 2014/06/06 18:58:56 Delete
yhirano 2014/06/09 06:42:26 Done.
37 #include "core/dom/DOMError.h" 38 #include "core/dom/DOMError.h"
38 #include "core/dom/DOMException.h" 39 #include "core/dom/DOMException.h"
39 #include "core/dom/ExecutionContext.h" 40 #include "core/dom/ExecutionContext.h"
eroman 2014/06/06 18:58:56 Delete
yhirano 2014/06/09 06:42:26 Done.
40 #include "modules/crypto/Key.h" 41 #include "modules/crypto/Key.h"
41 #include "modules/crypto/KeyPair.h" 42 #include "modules/crypto/KeyPair.h"
42 #include "modules/crypto/NormalizeAlgorithm.h" 43 #include "modules/crypto/NormalizeAlgorithm.h"
43 #include "public/platform/Platform.h" 44 #include "public/platform/Platform.h"
44 #include "public/platform/WebArrayBuffer.h" 45 #include "public/platform/WebArrayBuffer.h"
45 #include "public/platform/WebCryptoAlgorithm.h" 46 #include "public/platform/WebCryptoAlgorithm.h"
46 #include "wtf/ArrayBufferView.h" 47 #include "wtf/ArrayBufferView.h"
47 48
48 namespace WebCore { 49 namespace WebCore {
49 50
(...skipping 22 matching lines...) Expand all
72 // revisited. 73 // revisited.
73 return DataError; 74 return DataError;
74 } 75 }
75 76
76 ASSERT_NOT_REACHED(); 77 ASSERT_NOT_REACHED();
77 return 0; 78 return 0;
78 } 79 }
79 80
80 } // namespace 81 } // namespace
81 82
82 // The PromiseState class contains all the state which is tied to an 83 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: 84 public:
94 static WeakPtr<PromiseState> create(ScriptState* scriptState) 85 static PassOwnPtr<Initializer> create(CryptoResultImpl* owner) { return adop tPtr(new Initializer(owner)); }
86 void start(AsyncInitializerResolver<Initializer>* resolver)
95 { 87 {
96 PromiseState* promiseState = new PromiseState(scriptState); 88 ASSERT(m_owner);
97 return promiseState->m_weakFactory.createWeakPtr(); 89 m_factory = adoptPtr(new WeakPtrFactory<AsyncInitializerResolver<Initial izer> >(resolver));
90 m_owner->m_resolver = m_factory->createWeakPtr();
91 m_owner = nullptr;
98 } 92 }
99 93 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 94
140 private: 95 private:
141 // This subclass of ScriptPromiseResolverWithContext is to be notified 96 Initializer(CryptoResultImpl* owner) : m_owner(owner) { }
eroman 2014/06/06 18:58:56 explicit
yhirano 2014/06/09 06:42:25 Done.
142 // when the context was destroyed. 97 CryptoResultImpl* m_owner;
143 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { 98 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 }; 99 };
177 100
178 CryptoResultImpl::~CryptoResultImpl() 101 CryptoResultImpl::~CryptoResultImpl()
179 { 102 {
180 } 103 }
181 104
182 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) 105 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
183 { 106 {
184 return adoptRef(new CryptoResultImpl(scriptState)); 107 return adoptRef(new CryptoResultImpl(scriptState));
185 } 108 }
186 109
187 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co nst blink::WebString& errorDetails) 110 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co nst blink::WebString& errorDetails)
188 { 111 {
189 if (m_promiseState) 112 if (m_resolver)
190 m_promiseState->completeWithError(errorType, errorDetails); 113 m_resolver->reject(DOMException::create(toExceptionCode(errorType), erro rDetails));
191 } 114 }
192 115
193 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) 116 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
194 { 117 {
195 if (m_promiseState) 118 if (m_resolver)
196 m_promiseState->completeWithBuffer(buffer); 119 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
197 } 120 }
198 121
199 void CryptoResultImpl::completeWithBoolean(bool b) 122 void CryptoResultImpl::completeWithBoolean(bool b)
200 { 123 {
201 if (m_promiseState) 124 if (m_resolver)
202 m_promiseState->completeWithBoolean(b); 125 m_resolver->resolve(b);
203 } 126 }
204 127
205 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) 128 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
206 { 129 {
207 if (m_promiseState) 130 if (m_resolver)
208 m_promiseState->completeWithKey(key); 131 m_resolver->resolve(Key::create(key));
209 } 132 }
210 133
211 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) 134 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
212 { 135 {
213 if (m_promiseState) 136 if (m_resolver)
214 m_promiseState->completeWithKeyPair(publicKey, privateKey); 137 m_resolver->resolve(KeyPair::create(publicKey, privateKey));
215 } 138 }
216 139
217 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 140 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
218 : m_promiseState(PromiseState::create(scriptState))
219 { 141 {
142 AsyncInitializerResolver<Initializer>::start(scriptState, Initializer::creat e(this));
220 } 143 }
221 144
222 ScriptPromise CryptoResultImpl::promise() 145 ScriptPromise CryptoResultImpl::promise()
223 { 146 {
224 return m_promiseState->promise(); 147 return m_resolver ? m_resolver->promise() : ScriptPromise();
225 } 148 }
226 149
227 } // namespace WebCore 150 } // namespace WebCore
OLDNEW
« Source/modules/crypto/CryptoResultImpl.h ('K') | « Source/modules/crypto/CryptoResultImpl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698