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

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

Issue 1228373006: Reliably cancel in-progress CryptoResult(Impl) upon shutdown. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Separate out cancellation handling (+rebase) Created 5 years, 4 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 if (!resolver->executionContext() || resolver->executionContext()->activeDOM ObjectsAreStopped()) 56 if (!resolver->executionContext() || resolver->executionContext()->activeDOM ObjectsAreStopped())
57 return; 57 return;
58 58
59 ScriptState::Scope scope(resolver->scriptState()); 59 ScriptState::Scope scope(resolver->scriptState());
60 v8::Isolate* isolate = resolver->scriptState()->isolate(); 60 v8::Isolate* isolate = resolver->scriptState()->isolate();
61 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails))); 61 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails)));
62 } 62 }
63 63
64 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver { 64 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver {
65 public: 65 public:
66 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr iptState, CryptoResultImpl* result) 66 static PassRefPtrWillBeRawPtr<Resolver> create(ScriptState* scriptState, Cry ptoResultImpl* result)
67 { 67 {
68 RefPtrWillBeRawPtr<Resolver> resolver = adoptRefWillBeNoop(new Resolver( scriptState, result)); 68 RefPtrWillBeRawPtr<Resolver> resolver = adoptRefWillBeNoop(new Resolver( scriptState, result));
69 resolver->suspendIfNeeded(); 69 resolver->suspendIfNeeded();
70 resolver->keepAliveWhilePending(); 70 resolver->keepAliveWhilePending();
71 return resolver.release(); 71 return resolver.release();
72 } 72 }
73 73
74 void stop() override 74 void stop() override
75 { 75 {
76 m_result->cancel(); 76 m_result->cancel();
77 m_result->clearResolver(); 77 m_result->clearResolver();
eroman 2015/07/31 23:40:30 optional: It might be clearer to remove this line,
sof 2015/08/01 06:44:31 Moved clearResolver() (it was moved over in some o
78 m_result = nullptr; 78 m_result = nullptr;
79 ScriptPromiseResolver::stop(); 79 ScriptPromiseResolver::stop();
80 } 80 }
81 81
82 DEFINE_INLINE_VIRTUAL_TRACE()
83 {
84 visitor->trace(m_result);
85 ScriptPromiseResolver::trace(visitor);
86 }
87
82 private: 88 private:
83 Resolver(ScriptState* scriptState, CryptoResultImpl* result) 89 Resolver(ScriptState* scriptState, CryptoResultImpl* result)
84 : ScriptPromiseResolver(scriptState) 90 : ScriptPromiseResolver(scriptState)
85 , m_result(result) { } 91 , m_result(result) { }
86 RefPtr<CryptoResultImpl> m_result; 92
93 RefPtrWillBeMember<CryptoResultImpl> m_result;
87 }; 94 };
88 95
96 CryptoResultImpl::ResultCancel::ResultCancel()
97 : m_cancelled(0)
98 {
99 }
100
101 bool CryptoResultImpl::ResultCancel::cancelled() const
102 {
103 return acquireLoad(&m_cancelled);
104 }
105
106 void CryptoResultImpl::ResultCancel::cancel()
107 {
108 releaseStore(&m_cancelled, 1);
109 }
110
89 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) 111 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
90 { 112 {
91 switch (errorType) { 113 switch (errorType) {
92 case WebCryptoErrorTypeNotSupported: 114 case WebCryptoErrorTypeNotSupported:
93 return NotSupportedError; 115 return NotSupportedError;
94 case WebCryptoErrorTypeSyntax: 116 case WebCryptoErrorTypeSyntax:
95 return SyntaxError; 117 return SyntaxError;
96 case WebCryptoErrorTypeInvalidAccess: 118 case WebCryptoErrorTypeInvalidAccess:
97 return InvalidAccessError; 119 return InvalidAccessError;
98 case WebCryptoErrorTypeData: 120 case WebCryptoErrorTypeData:
99 return DataError; 121 return DataError;
100 case WebCryptoErrorTypeOperation: 122 case WebCryptoErrorTypeOperation:
101 return OperationError; 123 return OperationError;
102 case WebCryptoErrorTypeType: 124 case WebCryptoErrorTypeType:
103 return V8TypeError; 125 return V8TypeError;
104 } 126 }
105 127
106 ASSERT_NOT_REACHED(); 128 ASSERT_NOT_REACHED();
107 return 0; 129 return 0;
108 } 130 }
109 131
132 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
133 {
134 ASSERT(scriptState->contextIsValid());
135 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
136 // If active dom objects have been stopped, avoid creating
137 // CryptoResultImpl::Resolver.
138 m_resolver = nullptr;
139 return;
eroman 2015/08/01 00:13:14 IMPORTANT: This return doesn't seem right. In the
sof 2015/08/01 06:44:30 The WebCryptoResult ctor will complain if that hap
eroman 2015/08/03 19:00:29 The WebCryptoResult ctor will merely ASSERT(). It
eroman 2015/08/03 19:45:34 Ah, I didn't notice that you addressed this in the
140 }
141 m_resolver = Resolver::create(scriptState, this).get();
142 m_cancel = ResultCancel::create();
143 }
144
110 CryptoResultImpl::~CryptoResultImpl() 145 CryptoResultImpl::~CryptoResultImpl()
111 { 146 {
112 ASSERT(!m_resolver); 147 ASSERT(!m_resolver);
113 } 148 }
114 149
150 DEFINE_TRACE(CryptoResultImpl)
151 {
152 visitor->trace(m_resolver);
153 CryptoResult::trace(visitor);
154 }
155
115 void CryptoResultImpl::clearResolver() 156 void CryptoResultImpl::clearResolver()
116 { 157 {
117 m_resolver = nullptr; 158 m_resolver = nullptr;
118 } 159 }
119 160
120 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState) 161 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState)
121 { 162 {
122 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState)); 163 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState));
123 } 164 }
124 165
125 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 166 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
126 { 167 {
127 if (m_resolver) { 168 if (!m_resolver)
128 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); 169 return;
129 170
130 // Handle TypeError separately, as it cannot be created using 171 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType);
131 // DOMException. 172
132 if (ec == V8TypeError) 173 // Handle TypeError separately, as it cannot be created using
133 rejectWithTypeError(errorDetails, m_resolver); 174 // DOMException.
134 else 175 if (ec == V8TypeError)
135 m_resolver->reject(DOMException::create(ec, errorDetails)); 176 rejectWithTypeError(errorDetails, m_resolver);
136 } 177 else
178 m_resolver->reject(DOMException::create(ec, errorDetails));
137 clearResolver(); 179 clearResolver();
138 } 180 }
139 181
140 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 182 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
141 { 183 {
142 if (m_resolver) 184 if (!m_resolver)
143 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 185 return;
186
187 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize));
144 clearResolver(); 188 clearResolver();
145 } 189 }
146 190
147 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 191 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
148 { 192 {
149 if (m_resolver) { 193 if (!m_resolver)
150 ScriptPromiseResolver* resolver = m_resolver; 194 return;
151 ScriptState* scriptState = resolver->scriptState();
152 ScriptState::Scope scope(scriptState);
153 195
154 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate() , utf8Data, length); 196 ScriptState* scriptState = m_resolver->scriptState();
197 ScriptState::Scope scope(scriptState);
155 198
156 v8::TryCatch exceptionCatcher; 199 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate(), ut f8Data, length);
157 v8::Local<v8::Value> jsonDictionary; 200
158 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDict ionary, exceptionCatcher)) 201 v8::TryCatch exceptionCatcher;
159 resolver->resolve(jsonDictionary); 202 v8::Local<v8::Value> jsonDictionary;
160 else 203 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDictiona ry, exceptionCatcher))
161 resolver->reject(exceptionCatcher.Exception()); 204 m_resolver->resolve(jsonDictionary);
162 } 205 else
206 m_resolver->reject(exceptionCatcher.Exception());
163 clearResolver(); 207 clearResolver();
164 } 208 }
165 209
166 void CryptoResultImpl::completeWithBoolean(bool b) 210 void CryptoResultImpl::completeWithBoolean(bool b)
167 { 211 {
168 if (m_resolver) 212 if (!m_resolver)
169 m_resolver->resolve(b); 213 return;
214
215 m_resolver->resolve(b);
170 clearResolver(); 216 clearResolver();
171 } 217 }
172 218
173 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 219 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
174 { 220 {
175 if (m_resolver) 221 if (!m_resolver)
176 m_resolver->resolve(CryptoKey::create(key)); 222 return;
223
224 m_resolver->resolve(CryptoKey::create(key));
177 clearResolver(); 225 clearResolver();
178 } 226 }
179 227
180 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 228 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
181 { 229 {
182 if (m_resolver) { 230 if (!m_resolver)
183 ScriptState* scriptState = m_resolver->scriptState(); 231 return;
184 ScriptState::Scope scope(scriptState);
185 232
186 V8ObjectBuilder keyPair(scriptState); 233 ScriptState* scriptState = m_resolver->scriptState();
234 ScriptState::Scope scope(scriptState);
187 235
188 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::creat e(publicKey))); 236 V8ObjectBuilder keyPair(scriptState);
189 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::crea te(privateKey)));
190 237
191 m_resolver->resolve(keyPair.v8Value()); 238 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::create(pu blicKey)));
192 } 239 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::create(p rivateKey)));
240
241 m_resolver->resolve(keyPair.v8Value());
193 clearResolver(); 242 clearResolver();
194 } 243 }
195 244
196 bool CryptoResultImpl::cancelled() const
197 {
198 return acquireLoad(&m_cancelled);
199 }
200
201 void CryptoResultImpl::cancel() 245 void CryptoResultImpl::cancel()
202 { 246 {
203 releaseStore(&m_cancelled, 1); 247 ASSERT(m_cancel);
204 } 248 m_cancel->cancel();
205 249 m_cancel.release();
eroman 2015/07/31 23:40:30 optional: It might be more idiomatic to write this
sof 2015/08/01 06:44:30 Done.
206 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
207 : m_cancelled(0)
208 {
209 ASSERT(scriptState->contextIsValid());
210 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
211 // If active dom objects have been stopped, avoid creating
212 // CryptoResultResolver.
213 m_resolver = nullptr;
214 } else {
215 m_resolver = Resolver::create(scriptState, this).get();
216 }
217 } 250 }
218 251
219 ScriptPromise CryptoResultImpl::promise() 252 ScriptPromise CryptoResultImpl::promise()
220 { 253 {
221 return m_resolver ? m_resolver->promise() : ScriptPromise(); 254 return m_resolver ? m_resolver->promise() : ScriptPromise();
222 } 255 }
223 256
224 } // namespace blink 257 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698