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

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

Issue 311733004: Introduce KeepAliveWhilePending to ScriptPromiseResolverWithContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@refactor-webmidi-initialization
Patch Set: 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
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29 */
30
31 #include "config.h"
32 #include "modules/crypto/CryptoResultImpl.h"
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"
39 #include "core/dom/ExecutionContext.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
48 namespace WebCore {
49
50 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType)
51 {
52 switch (errorType) {
53 case blink::WebCryptoErrorTypeNotSupported:
54 return NotSupportedError;
55 case blink::WebCryptoErrorTypeSyntax:
56 return SyntaxError;
57 case blink::WebCryptoErrorTypeInvalidState:
58 return InvalidStateError;
59 case blink::WebCryptoErrorTypeInvalidAccess:
60 return InvalidAccessError;
61 case blink::WebCryptoErrorTypeUnknown:
62 return UnknownError;
63 case blink::WebCryptoErrorTypeData:
64 return DataError;
65 case blink::WebCryptoErrorTypeOperation:
66 return OperationError;
67 case blink::WebCryptoErrorTypeType:
68 // FIXME: This should construct a TypeError instead. For now do
69 // something to facilitate refactor, but this will need to be
70 // revisited.
71 return DataError;
72 }
73
74 ASSERT_NOT_REACHED();
75 return 0;
76 }
77
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698