Chromium Code Reviews| 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 30 matching lines...) Expand all Loading... | |
| 41 #include "core/dom/DOMError.h" | 41 #include "core/dom/DOMError.h" |
| 42 #include "core/dom/DOMException.h" | 42 #include "core/dom/DOMException.h" |
| 43 #include "core/dom/ExecutionContext.h" | 43 #include "core/dom/ExecutionContext.h" |
| 44 #include "modules/crypto/CryptoKey.h" | 44 #include "modules/crypto/CryptoKey.h" |
| 45 #include "modules/crypto/NormalizeAlgorithm.h" | 45 #include "modules/crypto/NormalizeAlgorithm.h" |
| 46 #include "public/platform/Platform.h" | 46 #include "public/platform/Platform.h" |
| 47 #include "public/platform/WebCryptoAlgorithm.h" | 47 #include "public/platform/WebCryptoAlgorithm.h" |
| 48 | 48 |
| 49 namespace blink { | 49 namespace blink { |
| 50 | 50 |
| 51 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver { | 51 class CryptoResultResolver : public ScriptPromiseResolver { |
|
haraken
2014/12/12 00:04:10
Add final.
tasak
2014/12/12 07:56:50
Done.
| |
| 52 public: | 52 public: |
| 53 static WeakPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Crypt oResultImpl* result) | 53 static PassRefPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Cr yptoResultImpl* result) |
| 54 { | 54 { |
| 55 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result)) ; | 55 RefPtr<CryptoResultResolver> p = adoptRef(new CryptoResultResolver(scrip tState, result)); |
|
haraken
2014/12/12 00:04:10
p => resolver
tasak
2014/12/12 07:56:51
Done.
| |
| 56 p->suspendIfNeeded(); | 56 p->suspendIfNeeded(); |
| 57 p->keepAliveWhilePending(); | 57 p->keepAliveWhilePending(); |
| 58 return p->m_weakPtrFactory.createWeakPtr(); | 58 return p.release(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 virtual ~WeakResolver() | 61 virtual void stop() override |
| 62 { | 62 { |
| 63 m_result->cancel(); | 63 if (m_result) { |
| 64 m_result->cancel(); | |
| 65 m_result->clearResolver(); | |
| 66 m_result = nullptr; | |
| 67 } | |
| 68 ScriptPromiseResolver::stop(); | |
| 69 } | |
| 70 | |
| 71 void clearCryptoResult() | |
| 72 { | |
| 73 m_result = nullptr; | |
| 64 } | 74 } |
| 65 | 75 |
| 66 private: | 76 private: |
| 67 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result) | 77 CryptoResultResolver(ScriptState* scriptState, CryptoResultImpl* result) |
| 68 : ScriptPromiseResolver(scriptState) | 78 : ScriptPromiseResolver(scriptState) |
| 69 , m_weakPtrFactory(this) | |
| 70 , m_result(result) { } | 79 , m_result(result) { } |
| 71 WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory; | 80 CryptoResultImpl* m_result; |
|
haraken
2014/12/12 00:04:10
Help me understand: Why can't this a RefPtr?
tasak
2014/12/12 07:56:50
As far as I investigated, CryptoResultImpl should
| |
| 72 RefPtr<CryptoResultImpl> m_result; | |
| 73 }; | 81 }; |
| 74 | 82 |
| 83 DEFINE_TYPE_CASTS(CryptoResultResolver, ScriptPromiseResolver, resolver, true, t rue); | |
| 84 | |
| 75 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) | 85 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) |
| 76 { | 86 { |
| 77 switch (errorType) { | 87 switch (errorType) { |
| 78 case WebCryptoErrorTypeNotSupported: | 88 case WebCryptoErrorTypeNotSupported: |
| 79 return NotSupportedError; | 89 return NotSupportedError; |
| 80 case WebCryptoErrorTypeSyntax: | 90 case WebCryptoErrorTypeSyntax: |
| 81 return SyntaxError; | 91 return SyntaxError; |
| 82 case WebCryptoErrorTypeInvalidState: | 92 case WebCryptoErrorTypeInvalidState: |
| 83 return InvalidStateError; | 93 return InvalidStateError; |
| 84 case WebCryptoErrorTypeInvalidAccess: | 94 case WebCryptoErrorTypeInvalidAccess: |
| 85 return InvalidAccessError; | 95 return InvalidAccessError; |
| 86 case WebCryptoErrorTypeUnknown: | 96 case WebCryptoErrorTypeUnknown: |
| 87 return UnknownError; | 97 return UnknownError; |
| 88 case WebCryptoErrorTypeData: | 98 case WebCryptoErrorTypeData: |
| 89 return DataError; | 99 return DataError; |
| 90 case WebCryptoErrorTypeOperation: | 100 case WebCryptoErrorTypeOperation: |
| 91 return OperationError; | 101 return OperationError; |
| 92 case WebCryptoErrorTypeType: | 102 case WebCryptoErrorTypeType: |
| 93 // FIXME: This should construct a TypeError instead. For now do | 103 // FIXME: This should construct a TypeError instead. For now do |
| 94 // something to facilitate refactor, but this will need to be | 104 // something to facilitate refactor, but this will need to be |
| 95 // revisited. | 105 // revisited. |
| 96 return DataError; | 106 return DataError; |
| 97 } | 107 } |
| 98 | 108 |
| 99 ASSERT_NOT_REACHED(); | 109 ASSERT_NOT_REACHED(); |
| 100 return 0; | 110 return 0; |
| 101 } | 111 } |
| 102 | 112 |
| 103 CryptoResultImpl::~CryptoResultImpl() | 113 CryptoResultImpl::~CryptoResultImpl() |
| 104 { | 114 { |
|
haraken
2014/12/12 00:04:10
Add ASSERT(!m_resolver) to verify that the resolve
| |
| 105 } | 115 } |
| 106 | 116 |
| 117 void CryptoResultImpl::clearResolver() | |
| 118 { | |
| 119 if (m_resolver) | |
| 120 toCryptoResultResolver(m_resolver)->clearCryptoResult(); | |
| 121 m_resolver = nullptr; | |
| 122 } | |
| 123 | |
| 107 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 124 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
| 108 { | 125 { |
| 109 return adoptRef(new CryptoResultImpl(scriptState)); | 126 return adoptRef(new CryptoResultImpl(scriptState)); |
| 110 } | 127 } |
| 111 | 128 |
| 112 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) | 129 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) |
| 113 { | 130 { |
| 114 if (m_resolver) | 131 if (m_resolver) |
| 115 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er rorType), errorDetails)); | 132 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er rorType), errorDetails)); |
| 133 clearResolver(); | |
| 116 } | 134 } |
| 117 | 135 |
| 118 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) | 136 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) |
| 119 { | 137 { |
| 120 if (m_resolver) | 138 if (m_resolver) |
| 121 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); | 139 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); |
| 140 clearResolver(); | |
| 122 } | 141 } |
| 123 | 142 |
| 124 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) | 143 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) |
| 125 { | 144 { |
| 126 if (m_resolver) { | 145 if (m_resolver) { |
| 127 ScriptPromiseResolver* resolver = m_resolver.get(); | 146 ScriptPromiseResolver* resolver = m_resolver.get(); |
| 128 ScriptState* scriptState = resolver->scriptState(); | 147 ScriptState* scriptState = resolver->scriptState(); |
| 129 ScriptState::Scope scope(scriptState); | 148 ScriptState::Scope scope(scriptState); |
| 130 | 149 |
| 131 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length); | 150 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length); |
| 132 | 151 |
| 133 v8::TryCatch exceptionCatcher; | 152 v8::TryCatch exceptionCatcher; |
| 134 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); | 153 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); |
| 135 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { | 154 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { |
| 136 ASSERT_NOT_REACHED(); | 155 ASSERT_NOT_REACHED(); |
| 137 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object")); | 156 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object")); |
| 138 } else { | 157 } else { |
| 139 resolver->resolve(jsonDictionary); | 158 resolver->resolve(jsonDictionary); |
| 140 } | 159 } |
| 141 } | 160 } |
| 161 clearResolver(); | |
| 142 } | 162 } |
| 143 | 163 |
| 144 void CryptoResultImpl::completeWithBoolean(bool b) | 164 void CryptoResultImpl::completeWithBoolean(bool b) |
| 145 { | 165 { |
| 146 if (m_resolver) | 166 if (m_resolver) |
| 147 m_resolver->resolve(b); | 167 m_resolver->resolve(b); |
| 168 clearResolver(); | |
| 148 } | 169 } |
| 149 | 170 |
| 150 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) | 171 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) |
| 151 { | 172 { |
| 152 if (m_resolver) | 173 if (m_resolver) |
| 153 m_resolver->resolve(CryptoKey::create(key)); | 174 m_resolver->resolve(CryptoKey::create(key)); |
| 175 clearResolver(); | |
| 154 } | 176 } |
| 155 | 177 |
| 156 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) | 178 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) |
| 157 { | 179 { |
| 158 if (m_resolver) { | 180 if (m_resolver) { |
| 159 ScriptState* scriptState = m_resolver->scriptState(); | 181 ScriptState* scriptState = m_resolver->scriptState(); |
| 160 ScriptState::Scope scope(scriptState); | 182 ScriptState::Scope scope(scriptState); |
| 161 | 183 |
| 162 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); | 184 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); |
| 163 | 185 |
| 164 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate()); | 186 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate()); |
| 165 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate()); | 187 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate()); |
| 166 | 188 |
| 167 keyPair.set("publicKey", publicKeyValue); | 189 keyPair.set("publicKey", publicKeyValue); |
| 168 keyPair.set("privateKey", privateKeyValue); | 190 keyPair.set("privateKey", privateKeyValue); |
| 169 | 191 |
| 170 m_resolver->resolve(keyPair.v8Value()); | 192 m_resolver->resolve(keyPair.v8Value()); |
| 171 } | 193 } |
| 194 clearResolver(); | |
| 172 } | 195 } |
| 173 | 196 |
| 174 bool CryptoResultImpl::cancelled() const | 197 bool CryptoResultImpl::cancelled() const |
| 175 { | 198 { |
| 176 return acquireLoad(&m_cancelled); | 199 return acquireLoad(&m_cancelled); |
| 177 } | 200 } |
| 178 | 201 |
| 179 void CryptoResultImpl::cancel() | 202 void CryptoResultImpl::cancel() |
| 180 { | 203 { |
| 181 releaseStore(&m_cancelled, 1); | 204 releaseStore(&m_cancelled, 1); |
| 182 } | 205 } |
| 183 | 206 |
| 184 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 207 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
| 185 : m_cancelled(0) | 208 : m_cancelled(0) |
| 186 { | 209 { |
| 187 // Creating the WeakResolver may return nullptr if active dom objects have | 210 ASSERT(scriptState); |
|
haraken
2014/12/12 00:04:10
scriptState should never be 0. Instead you should
| |
| 188 // been stopped. And in the process set m_cancelled to 1. | 211 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) { |
| 189 m_resolver = WeakResolver::create(scriptState, this); | 212 // If active dom objects have been stopped, avoid creating |
| 213 // CryptoResultResolver. | |
| 214 m_resolver = nullptr; | |
| 215 } else { | |
| 216 m_resolver = CryptoResultResolver::create(scriptState, this).get(); | |
| 217 } | |
| 190 } | 218 } |
| 191 | 219 |
| 192 ScriptPromise CryptoResultImpl::promise() | 220 ScriptPromise CryptoResultImpl::promise() |
| 193 { | 221 { |
| 194 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 222 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 195 } | 223 } |
| 196 | 224 |
| 197 } // namespace blink | 225 } // namespace blink |
| OLD | NEW |