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

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: rebased upto r199421 and use it for (again) heap-based CryptoResults Created 5 years, 5 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();
78 m_result = nullptr; 77 m_result = nullptr;
79 ScriptPromiseResolver::stop(); 78 ScriptPromiseResolver::stop();
80 } 79 }
81 80
81 DEFINE_INLINE_VIRTUAL_TRACE()
82 {
83 visitor->trace(m_result);
84 ScriptPromiseResolver::trace(visitor);
85 }
86
82 private: 87 private:
83 Resolver(ScriptState* scriptState, CryptoResultImpl* result) 88 Resolver(ScriptState* scriptState, CryptoResultImpl* result)
84 : ScriptPromiseResolver(scriptState) 89 : ScriptPromiseResolver(scriptState)
85 , m_result(result) { } 90 , m_result(result) { }
86 RefPtr<CryptoResultImpl> m_result; 91
92 RefPtrWillBeMember<CryptoResultImpl> m_result;
87 }; 93 };
88 94
89 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) 95 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
90 { 96 {
91 switch (errorType) { 97 switch (errorType) {
92 case WebCryptoErrorTypeNotSupported: 98 case WebCryptoErrorTypeNotSupported:
93 return NotSupportedError; 99 return NotSupportedError;
94 case WebCryptoErrorTypeSyntax: 100 case WebCryptoErrorTypeSyntax:
95 return SyntaxError; 101 return SyntaxError;
96 case WebCryptoErrorTypeInvalidAccess: 102 case WebCryptoErrorTypeInvalidAccess:
97 return InvalidAccessError; 103 return InvalidAccessError;
98 case WebCryptoErrorTypeData: 104 case WebCryptoErrorTypeData:
99 return DataError; 105 return DataError;
100 case WebCryptoErrorTypeOperation: 106 case WebCryptoErrorTypeOperation:
101 return OperationError; 107 return OperationError;
102 case WebCryptoErrorTypeType: 108 case WebCryptoErrorTypeType:
103 return V8TypeError; 109 return V8TypeError;
104 } 110 }
105 111
106 ASSERT_NOT_REACHED(); 112 ASSERT_NOT_REACHED();
107 return 0; 113 return 0;
108 } 114 }
109 115
116 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
117 : m_resultOwner(nullptr)
118 {
119 ASSERT(scriptState->contextIsValid());
120 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
121 // If active dom objects have been stopped, avoid creating
122 // CryptoResultImpl::Resolver.
123 m_resolver = nullptr;
124 } else {
125 m_resolver = Resolver::create(scriptState, this).get();
126 }
127 }
128
110 CryptoResultImpl::~CryptoResultImpl() 129 CryptoResultImpl::~CryptoResultImpl()
111 { 130 {
112 ASSERT(!m_resolver); 131 ASSERT(!m_resolver);
113 } 132 }
114 133
134 DEFINE_TRACE(CryptoResultImpl)
135 {
136 visitor->trace(m_resolver);
137 CryptoResult::trace(visitor);
138 }
139
115 void CryptoResultImpl::clearResolver() 140 void CryptoResultImpl::clearResolver()
116 { 141 {
117 m_resolver = nullptr; 142 m_resolver = nullptr;
118 } 143 }
119 144
120 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState) 145 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState)
121 { 146 {
122 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState)); 147 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState));
123 } 148 }
124 149
125 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 150 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
126 { 151 {
127 if (m_resolver) { 152 if (!m_resolver)
128 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); 153 return;
129 154
130 // Handle TypeError separately, as it cannot be created using 155 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType);
131 // DOMException. 156
132 if (ec == V8TypeError) 157 // Handle TypeError separately, as it cannot be created using
133 rejectWithTypeError(errorDetails, m_resolver); 158 // DOMException.
134 else 159 if (ec == V8TypeError)
135 m_resolver->reject(DOMException::create(ec, errorDetails)); 160 rejectWithTypeError(errorDetails, m_resolver);
136 } 161 else
162 m_resolver->reject(DOMException::create(ec, errorDetails));
137 clearResolver(); 163 clearResolver();
138 } 164 }
139 165
140 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 166 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
141 { 167 {
142 if (m_resolver) 168 if (!m_resolver)
143 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 169 return;
170
171 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize));
144 clearResolver(); 172 clearResolver();
145 } 173 }
146 174
147 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 175 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
148 { 176 {
149 if (m_resolver) { 177 if (!m_resolver)
150 ScriptPromiseResolver* resolver = m_resolver; 178 return;
151 ScriptState* scriptState = resolver->scriptState();
152 ScriptState::Scope scope(scriptState);
153 179
154 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate() , utf8Data, length); 180 ScriptState* scriptState = m_resolver->scriptState();
181 ScriptState::Scope scope(scriptState);
155 182
156 v8::TryCatch exceptionCatcher; 183 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate(), ut f8Data, length);
157 v8::Local<v8::Value> jsonDictionary; 184
158 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDict ionary, exceptionCatcher)) 185 v8::TryCatch exceptionCatcher;
159 resolver->resolve(jsonDictionary); 186 v8::Local<v8::Value> jsonDictionary;
160 else 187 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDictiona ry, exceptionCatcher))
161 resolver->reject(exceptionCatcher.Exception()); 188 m_resolver->resolve(jsonDictionary);
162 } 189 else
190 m_resolver->reject(exceptionCatcher.Exception());
163 clearResolver(); 191 clearResolver();
164 } 192 }
165 193
166 void CryptoResultImpl::completeWithBoolean(bool b) 194 void CryptoResultImpl::completeWithBoolean(bool b)
167 { 195 {
168 if (m_resolver) 196 if (!m_resolver)
169 m_resolver->resolve(b); 197 return;
198
199 m_resolver->resolve(b);
170 clearResolver(); 200 clearResolver();
171 } 201 }
172 202
173 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 203 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
174 { 204 {
175 if (m_resolver) 205 if (!m_resolver)
176 m_resolver->resolve(CryptoKey::create(key)); 206 return;
207
208 m_resolver->resolve(CryptoKey::create(key));
177 clearResolver(); 209 clearResolver();
178 } 210 }
179 211
180 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 212 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
181 { 213 {
182 if (m_resolver) { 214 if (!m_resolver)
183 ScriptState* scriptState = m_resolver->scriptState(); 215 return;
184 ScriptState::Scope scope(scriptState);
185 216
186 V8ObjectBuilder keyPair(scriptState); 217 ScriptState* scriptState = m_resolver->scriptState();
218 ScriptState::Scope scope(scriptState);
187 219
188 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::creat e(publicKey))); 220 V8ObjectBuilder keyPair(scriptState);
189 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::crea te(privateKey)));
190 221
191 m_resolver->resolve(keyPair.v8Value()); 222 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::create(pu blicKey)));
192 } 223 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::create(p rivateKey)));
224
225 m_resolver->resolve(keyPair.v8Value());
193 clearResolver(); 226 clearResolver();
194 } 227 }
195 228
196 bool CryptoResultImpl::cancelled() const
197 {
198 return acquireLoad(&m_cancelled);
199 }
200
201 void CryptoResultImpl::cancel() 229 void CryptoResultImpl::cancel()
202 { 230 {
203 releaseStore(&m_cancelled, 1); 231 // Hold a lock to prevent m_resultOwner being updated by a webcrypto thread
204 } 232 // while cancelling.
233 MutexLocker lock(m_mutex);
205 234
206 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 235 if (!m_resultOwner)
207 : m_cancelled(0) 236 return;
208 { 237
209 ASSERT(scriptState->contextIsValid()); 238 // Eagerly detach the WebCryptoResult, so as to allow clean shutdowns.
210 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) { 239 m_resultOwner->clear();
211 // If active dom objects have been stopped, avoid creating 240 // Verify that it unregistered.
212 // CryptoResultResolver. 241 ASSERT(!m_resultOwner);
213 m_resolver = nullptr; 242
214 } else { 243 clearResolver();
215 m_resolver = Resolver::create(scriptState, this).get();
216 }
217 } 244 }
218 245
219 ScriptPromise CryptoResultImpl::promise() 246 ScriptPromise CryptoResultImpl::promise()
220 { 247 {
221 return m_resolver ? m_resolver->promise() : ScriptPromise(); 248 return m_resolver ? m_resolver->promise() : ScriptPromise();
222 } 249 }
223 250
251 bool CryptoResultImpl::registerResult(WebCryptoResult* result)
252 {
253 MutexLocker lock(m_mutex);
254
255 // Cancelled?
256 if (!m_resolver)
257 return true;
258
259 m_resultOwner = result;
260 return false;
261 }
262
263 void CryptoResultImpl::unregisterResult(WebCryptoResult* result)
264 {
265 MutexLocker lock(m_mutex);
266
267 if (result == m_resultOwner)
268 m_resultOwner = nullptr;
269 }
270
224 } // namespace blink 271 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698