| OLD | NEW |
| 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 Loading... |
| 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(); |
| 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 |
| 89 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) | 96 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) |
| 90 { | 97 { |
| 91 switch (errorType) { | 98 switch (errorType) { |
| 92 case WebCryptoErrorTypeNotSupported: | 99 case WebCryptoErrorTypeNotSupported: |
| 93 return NotSupportedError; | 100 return NotSupportedError; |
| 94 case WebCryptoErrorTypeSyntax: | 101 case WebCryptoErrorTypeSyntax: |
| 95 return SyntaxError; | 102 return SyntaxError; |
| 96 case WebCryptoErrorTypeInvalidAccess: | 103 case WebCryptoErrorTypeInvalidAccess: |
| 97 return InvalidAccessError; | 104 return InvalidAccessError; |
| 98 case WebCryptoErrorTypeData: | 105 case WebCryptoErrorTypeData: |
| 99 return DataError; | 106 return DataError; |
| 100 case WebCryptoErrorTypeOperation: | 107 case WebCryptoErrorTypeOperation: |
| 101 return OperationError; | 108 return OperationError; |
| 102 case WebCryptoErrorTypeType: | 109 case WebCryptoErrorTypeType: |
| 103 return V8TypeError; | 110 return V8TypeError; |
| 104 } | 111 } |
| 105 | 112 |
| 106 ASSERT_NOT_REACHED(); | 113 ASSERT_NOT_REACHED(); |
| 107 return 0; | 114 return 0; |
| 108 } | 115 } |
| 109 | 116 |
| 117 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
| 118 : m_cancelled(0) |
| 119 { |
| 120 ASSERT(scriptState->contextIsValid()); |
| 121 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) { |
| 122 // If active dom objects have been stopped, avoid creating |
| 123 // CryptoResultImpl::Resolver. |
| 124 m_resolver = nullptr; |
| 125 } else { |
| 126 m_resolver = Resolver::create(scriptState, this).get(); |
| 127 } |
| 128 } |
| 129 |
| 110 CryptoResultImpl::~CryptoResultImpl() | 130 CryptoResultImpl::~CryptoResultImpl() |
| 111 { | 131 { |
| 112 ASSERT(!m_resolver); | 132 ASSERT(!m_resolver); |
| 113 } | 133 } |
| 114 | 134 |
| 135 DEFINE_TRACE(CryptoResultImpl) |
| 136 { |
| 137 visitor->trace(m_resolver); |
| 138 CryptoResult::trace(visitor); |
| 139 } |
| 140 |
| 115 void CryptoResultImpl::clearResolver() | 141 void CryptoResultImpl::clearResolver() |
| 116 { | 142 { |
| 117 m_resolver = nullptr; | 143 m_resolver = nullptr; |
| 118 } | 144 } |
| 119 | 145 |
| 120 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s
criptState) | 146 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s
criptState) |
| 121 { | 147 { |
| 122 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState)); | 148 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState)); |
| 123 } | 149 } |
| 124 | 150 |
| 125 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web
String& errorDetails) | 151 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web
String& errorDetails) |
| 126 { | 152 { |
| 127 if (m_resolver) { | 153 if (!m_resolver) |
| 128 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); | 154 return; |
| 129 | 155 |
| 130 // Handle TypeError separately, as it cannot be created using | 156 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); |
| 131 // DOMException. | 157 |
| 132 if (ec == V8TypeError) | 158 // Handle TypeError separately, as it cannot be created using |
| 133 rejectWithTypeError(errorDetails, m_resolver); | 159 // DOMException. |
| 134 else | 160 if (ec == V8TypeError) |
| 135 m_resolver->reject(DOMException::create(ec, errorDetails)); | 161 rejectWithTypeError(errorDetails, m_resolver); |
| 136 } | 162 else |
| 163 m_resolver->reject(DOMException::create(ec, errorDetails)); |
| 137 clearResolver(); | 164 clearResolver(); |
| 138 } | 165 } |
| 139 | 166 |
| 140 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) | 167 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) |
| 141 { | 168 { |
| 142 if (m_resolver) | 169 if (!m_resolver) |
| 143 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); | 170 return; |
| 171 |
| 172 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); |
| 144 clearResolver(); | 173 clearResolver(); |
| 145 } | 174 } |
| 146 | 175 |
| 147 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) | 176 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) |
| 148 { | 177 { |
| 149 if (m_resolver) { | 178 if (!m_resolver) |
| 150 ScriptPromiseResolver* resolver = m_resolver; | 179 return; |
| 151 ScriptState* scriptState = resolver->scriptState(); | |
| 152 ScriptState::Scope scope(scriptState); | |
| 153 | 180 |
| 154 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate()
, utf8Data, length); | 181 ScriptState* scriptState = m_resolver->scriptState(); |
| 182 ScriptState::Scope scope(scriptState); |
| 155 | 183 |
| 156 v8::TryCatch exceptionCatcher; | 184 v8::Local<v8::String> jsonString = v8AtomicString(scriptState->isolate(), ut
f8Data, length); |
| 157 v8::Local<v8::Value> jsonDictionary; | 185 |
| 158 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDict
ionary, exceptionCatcher)) | 186 v8::TryCatch exceptionCatcher; |
| 159 resolver->resolve(jsonDictionary); | 187 v8::Local<v8::Value> jsonDictionary; |
| 160 else | 188 if (v8Call(v8::JSON::Parse(scriptState->isolate(), jsonString), jsonDictiona
ry, exceptionCatcher)) |
| 161 resolver->reject(exceptionCatcher.Exception()); | 189 m_resolver->resolve(jsonDictionary); |
| 162 } | 190 else |
| 191 m_resolver->reject(exceptionCatcher.Exception()); |
| 163 clearResolver(); | 192 clearResolver(); |
| 164 } | 193 } |
| 165 | 194 |
| 166 void CryptoResultImpl::completeWithBoolean(bool b) | 195 void CryptoResultImpl::completeWithBoolean(bool b) |
| 167 { | 196 { |
| 168 if (m_resolver) | 197 if (!m_resolver) |
| 169 m_resolver->resolve(b); | 198 return; |
| 199 |
| 200 m_resolver->resolve(b); |
| 170 clearResolver(); | 201 clearResolver(); |
| 171 } | 202 } |
| 172 | 203 |
| 173 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) | 204 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) |
| 174 { | 205 { |
| 175 if (m_resolver) | 206 if (!m_resolver) |
| 176 m_resolver->resolve(CryptoKey::create(key)); | 207 return; |
| 208 |
| 209 m_resolver->resolve(CryptoKey::create(key)); |
| 177 clearResolver(); | 210 clearResolver(); |
| 178 } | 211 } |
| 179 | 212 |
| 180 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const
WebCryptoKey& privateKey) | 213 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const
WebCryptoKey& privateKey) |
| 181 { | 214 { |
| 182 if (m_resolver) { | 215 if (!m_resolver) |
| 183 ScriptState* scriptState = m_resolver->scriptState(); | 216 return; |
| 184 ScriptState::Scope scope(scriptState); | |
| 185 | 217 |
| 186 V8ObjectBuilder keyPair(scriptState); | 218 ScriptState* scriptState = m_resolver->scriptState(); |
| 219 ScriptState::Scope scope(scriptState); |
| 187 | 220 |
| 188 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::creat
e(publicKey))); | 221 V8ObjectBuilder keyPair(scriptState); |
| 189 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::crea
te(privateKey))); | |
| 190 | 222 |
| 191 m_resolver->resolve(keyPair.v8Value()); | 223 keyPair.add("publicKey", ScriptValue::from(scriptState, CryptoKey::create(pu
blicKey))); |
| 192 } | 224 keyPair.add("privateKey", ScriptValue::from(scriptState, CryptoKey::create(p
rivateKey))); |
| 225 |
| 226 m_resolver->resolve(keyPair.v8Value()); |
| 193 clearResolver(); | 227 clearResolver(); |
| 194 } | 228 } |
| 195 | 229 |
| 196 bool CryptoResultImpl::cancelled() const | 230 bool CryptoResultImpl::cancelled() const |
| 197 { | 231 { |
| 198 return acquireLoad(&m_cancelled); | 232 return acquireLoad(&m_cancelled); |
| 199 } | 233 } |
| 200 | 234 |
| 201 void CryptoResultImpl::cancel() | 235 void CryptoResultImpl::cancel() |
| 202 { | 236 { |
| 203 releaseStore(&m_cancelled, 1); | 237 releaseStore(&m_cancelled, 1); |
| 204 } | 238 } |
| 205 | 239 |
| 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 } | |
| 218 | |
| 219 ScriptPromise CryptoResultImpl::promise() | 240 ScriptPromise CryptoResultImpl::promise() |
| 220 { | 241 { |
| 221 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 242 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 222 } | 243 } |
| 223 | 244 |
| 224 } // namespace blink | 245 } // namespace blink |
| OLD | NEW |