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

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: initialize stopped crypto result as cancelled 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
« no previous file with comments | « Source/modules/crypto/CryptoResultImpl.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 29 matching lines...) Expand all
40 #include "bindings/modules/v8/V8CryptoKey.h" 40 #include "bindings/modules/v8/V8CryptoKey.h"
41 #include "core/dom/ContextLifecycleObserver.h" 41 #include "core/dom/ContextLifecycleObserver.h"
42 #include "core/dom/DOMArrayBuffer.h" 42 #include "core/dom/DOMArrayBuffer.h"
43 #include "core/dom/DOMError.h" 43 #include "core/dom/DOMError.h"
44 #include "core/dom/DOMException.h" 44 #include "core/dom/DOMException.h"
45 #include "core/dom/ExecutionContext.h" 45 #include "core/dom/ExecutionContext.h"
46 #include "modules/crypto/CryptoKey.h" 46 #include "modules/crypto/CryptoKey.h"
47 #include "modules/crypto/NormalizeAlgorithm.h" 47 #include "modules/crypto/NormalizeAlgorithm.h"
48 #include "public/platform/Platform.h" 48 #include "public/platform/Platform.h"
49 #include "public/platform/WebCryptoAlgorithm.h" 49 #include "public/platform/WebCryptoAlgorithm.h"
50 #include "wtf/Atomics.h"
50 51
51 namespace blink { 52 namespace blink {
52 53
53 static void rejectWithTypeError(const String& errorDetails, ScriptPromiseResolve r* resolver) 54 static void rejectWithTypeError(const String& errorDetails, ScriptPromiseResolve r* resolver)
54 { 55 {
55 // Duplicate some of the checks done by ScriptPromiseResolver. 56 // Duplicate some of the checks done by ScriptPromiseResolver.
56 if (!resolver->executionContext() || resolver->executionContext()->activeDOM ObjectsAreStopped()) 57 if (!resolver->executionContext() || resolver->executionContext()->activeDOM ObjectsAreStopped())
57 return; 58 return;
58 59
59 ScriptState::Scope scope(resolver->scriptState()); 60 ScriptState::Scope scope(resolver->scriptState());
60 v8::Isolate* isolate = resolver->scriptState()->isolate(); 61 v8::Isolate* isolate = resolver->scriptState()->isolate();
61 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails))); 62 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails)));
62 } 63 }
63 64
64 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver { 65 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver {
65 public: 66 public:
66 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr iptState, CryptoResultImpl* result) 67 static PassRefPtrWillBeRawPtr<Resolver> create(ScriptState* scriptState, Cry ptoResultImpl* result)
67 { 68 {
68 RefPtrWillBeRawPtr<Resolver> resolver = adoptRefWillBeNoop(new Resolver( scriptState, result)); 69 RefPtrWillBeRawPtr<Resolver> resolver = adoptRefWillBeNoop(new Resolver( scriptState, result));
69 resolver->suspendIfNeeded(); 70 resolver->suspendIfNeeded();
70 resolver->keepAliveWhilePending(); 71 resolver->keepAliveWhilePending();
71 return resolver.release(); 72 return resolver.release();
72 } 73 }
73 74
74 void stop() override 75 void stop() override
75 { 76 {
76 m_result->cancel(); 77 m_result->cancel();
77 m_result->clearResolver();
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 : m_cancel(ResultCancel::create())
134 {
135 ASSERT(scriptState->contextIsValid());
136 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
137 // If active dom objects have been stopped, avoid creating
138 // CryptoResultImpl::Resolver.
139 m_resolver = nullptr;
140 m_cancel->cancel();
141 return;
142 }
143 m_resolver = Resolver::create(scriptState, this).get();
144 }
145
110 CryptoResultImpl::~CryptoResultImpl() 146 CryptoResultImpl::~CryptoResultImpl()
111 { 147 {
112 ASSERT(!m_resolver); 148 ASSERT(!m_resolver);
113 } 149 }
114 150
151 DEFINE_TRACE(CryptoResultImpl)
152 {
153 visitor->trace(m_resolver);
154 CryptoResult::trace(visitor);
155 }
156
115 void CryptoResultImpl::clearResolver() 157 void CryptoResultImpl::clearResolver()
116 { 158 {
117 m_resolver = nullptr; 159 m_resolver = nullptr;
118 } 160 }
119 161
120 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState) 162 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState)
121 { 163 {
122 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState)); 164 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState));
123 } 165 }
124 166
125 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 167 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
126 { 168 {
127 if (m_resolver) { 169 if (!m_resolver)
128 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); 170 return;
129 171
130 // Handle TypeError separately, as it cannot be created using 172 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType);
131 // DOMException. 173
132 if (ec == V8TypeError) 174 // Handle TypeError separately, as it cannot be created using
133 rejectWithTypeError(errorDetails, m_resolver); 175 // DOMException.
134 else 176 if (ec == V8TypeError)
135 m_resolver->reject(DOMException::create(ec, errorDetails)); 177 rejectWithTypeError(errorDetails, m_resolver);
136 } 178 else
179 m_resolver->reject(DOMException::create(ec, errorDetails));
137 clearResolver(); 180 clearResolver();
138 } 181 }
139 182
140 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 183 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
141 { 184 {
142 if (m_resolver) 185 if (!m_resolver)
143 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 186 return;
187
188 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize));
144 clearResolver(); 189 clearResolver();
145 } 190 }
146 191
147 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 192 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
148 { 193 {
149 if (m_resolver) { 194 if (!m_resolver)
150 ScriptPromiseResolver* resolver = m_resolver; 195 return;
151 ScriptState* scriptState = resolver->scriptState();
152 ScriptState::Scope scope(scriptState);
153 196
154 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate() , utf8Data, length); 197 ScriptState* scriptState = m_resolver->scriptState();
198 ScriptState::Scope scope(scriptState);
155 199
156 v8::TryCatch exceptionCatcher; 200 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate(), ut f8Data, length);
157 v8::Local<v8::Value> jsonDictionary; 201
158 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDict ionary, exceptionCatcher)) 202 v8::TryCatch exceptionCatcher;
159 resolver->resolve(jsonDictionary); 203 v8::Local<v8::Value> jsonDictionary;
160 else 204 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDictiona ry, exceptionCatcher))
161 resolver->reject(exceptionCatcher.Exception()); 205 m_resolver->resolve(jsonDictionary);
162 } 206 else
207 m_resolver->reject(exceptionCatcher.Exception());
163 clearResolver(); 208 clearResolver();
164 } 209 }
165 210
166 void CryptoResultImpl::completeWithBoolean(bool b) 211 void CryptoResultImpl::completeWithBoolean(bool b)
167 { 212 {
168 if (m_resolver) 213 if (!m_resolver)
169 m_resolver->resolve(b); 214 return;
215
216 m_resolver->resolve(b);
170 clearResolver(); 217 clearResolver();
171 } 218 }
172 219
173 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 220 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
174 { 221 {
175 if (m_resolver) 222 if (!m_resolver)
176 m_resolver->resolve(CryptoKey::create(key)); 223 return;
224
225 m_resolver->resolve(CryptoKey::create(key));
177 clearResolver(); 226 clearResolver();
178 } 227 }
179 228
180 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 229 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
181 { 230 {
182 if (m_resolver) { 231 if (!m_resolver)
183 ScriptState* scriptState = m_resolver->scriptState(); 232 return;
184 ScriptState::Scope scope(scriptState);
185 233
186 V8ObjectBuilder keyPair(scriptState); 234 ScriptState* scriptState = m_resolver->scriptState();
235 ScriptState::Scope scope(scriptState);
187 236
188 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::creat e(publicKey))); 237 V8ObjectBuilder keyPair(scriptState);
189 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::crea te(privateKey)));
190 238
191 m_resolver->resolve(keyPair.v8Value()); 239 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::create(pu blicKey)));
192 } 240 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::create(p rivateKey)));
241
242 m_resolver->resolve(keyPair.v8Value());
193 clearResolver(); 243 clearResolver();
194 } 244 }
195 245
196 bool CryptoResultImpl::cancelled() const
197 {
198 return acquireLoad(&m_cancelled);
199 }
200
201 void CryptoResultImpl::cancel() 246 void CryptoResultImpl::cancel()
202 { 247 {
203 releaseStore(&m_cancelled, 1); 248 ASSERT(m_cancel);
204 } 249 m_cancel->cancel();
205 250 m_cancel.clear();
206 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 251 clearResolver();
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 } 252 }
218 253
219 ScriptPromise CryptoResultImpl::promise() 254 ScriptPromise CryptoResultImpl::promise()
220 { 255 {
221 return m_resolver ? m_resolver->promise() : ScriptPromise(); 256 return m_resolver ? m_resolver->promise() : ScriptPromise();
222 } 257 }
223 258
224 } // namespace blink 259 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/crypto/CryptoResultImpl.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698