| 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 16 matching lines...) Expand all Loading... |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "modules/crypto/CryptoResultImpl.h" | 32 #include "modules/crypto/CryptoResultImpl.h" |
| 33 | 33 |
| 34 #include "bindings/v8/NewScriptState.h" | 34 #include "bindings/v8/NewScriptState.h" |
| 35 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 35 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| 36 #include "core/dom/ContextLifecycleObserver.h" | 36 #include "core/dom/ContextLifecycleObserver.h" |
| 37 #include "core/dom/DOMError.h" |
| 38 #include "core/dom/DOMException.h" |
| 37 #include "core/dom/ExecutionContext.h" | 39 #include "core/dom/ExecutionContext.h" |
| 38 #include "modules/crypto/Key.h" | 40 #include "modules/crypto/Key.h" |
| 39 #include "modules/crypto/KeyPair.h" | 41 #include "modules/crypto/KeyPair.h" |
| 40 #include "modules/crypto/NormalizeAlgorithm.h" | 42 #include "modules/crypto/NormalizeAlgorithm.h" |
| 41 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 42 #include "public/platform/WebArrayBuffer.h" | 44 #include "public/platform/WebArrayBuffer.h" |
| 43 #include "public/platform/WebCryptoAlgorithm.h" | 45 #include "public/platform/WebCryptoAlgorithm.h" |
| 44 #include "wtf/ArrayBufferView.h" | 46 #include "wtf/ArrayBufferView.h" |
| 45 | 47 |
| 46 namespace WebCore { | 48 namespace WebCore { |
| 47 | 49 |
| 50 namespace { |
| 51 |
| 52 ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType) |
| 53 { |
| 54 switch (errorType) { |
| 55 case blink::WebCryptoErrorTypeNotSupported: |
| 56 return NotSupportedError; |
| 57 case blink::WebCryptoErrorTypeSyntax: |
| 58 return SyntaxError; |
| 59 case blink::WebCryptoErrorTypeInvalidState: |
| 60 return InvalidStateError; |
| 61 case blink::WebCryptoErrorTypeInvalidAccess: |
| 62 return InvalidAccessError; |
| 63 case blink::WebCryptoErrorTypeUnknown: |
| 64 return UnknownError; |
| 65 case blink::WebCryptoErrorTypeData: |
| 66 return DataError; |
| 67 case blink::WebCryptoErrorTypeOperation: |
| 68 // FIXME: This exception type is new to WebCrypto and not yet defined. |
| 69 // Use a placeholder for now. |
| 70 return InvalidStateError; |
| 71 case blink::WebCryptoErrorTypeType: |
| 72 // FIXME: This should construct a TypeError instead. For now do |
| 73 // something to facilitate refactor, but this will need to be |
| 74 // revisited. |
| 75 return DataError; |
| 76 } |
| 77 |
| 78 ASSERT_NOT_REACHED(); |
| 79 return 0; |
| 80 } |
| 81 |
| 82 } // namespace |
| 83 |
| 48 // The PromiseState class contains all the state which is tied to an | 84 // The PromiseState class contains all the state which is tied to an |
| 49 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, | 85 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, |
| 50 // PromiseState is not thread safe and must only be accessed and deleted from | 86 // PromiseState is not thread safe and must only be accessed and deleted from |
| 51 // the blink thread. | 87 // the blink thread. |
| 52 // | 88 // |
| 53 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat
e. | 89 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat
e. |
| 54 // The PromiseState deletes itself after being notified of completion. | 90 // The PromiseState deletes itself after being notified of completion. |
| 55 // Additionally the PromiseState deletes itself when the ExecutionContext is | 91 // Additionally the PromiseState deletes itself when the ExecutionContext is |
| 56 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, | 92 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, |
| 57 // which may die before the operation is completed). | 93 // which may die before the operation is completed). |
| (...skipping 10 matching lines...) Expand all Loading... |
| 68 { | 104 { |
| 69 ContextLifecycleObserver::contextDestroyed(); | 105 ContextLifecycleObserver::contextDestroyed(); |
| 70 delete this; | 106 delete this; |
| 71 } | 107 } |
| 72 | 108 |
| 73 ScriptPromise promise() | 109 ScriptPromise promise() |
| 74 { | 110 { |
| 75 return m_promiseResolver->promise(); | 111 return m_promiseResolver->promise(); |
| 76 } | 112 } |
| 77 | 113 |
| 78 void completeWithError(const blink::WebString& errorDetails) | 114 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web
String& errorDetails) |
| 79 { | 115 { |
| 80 if (!errorDetails.isEmpty()) { | 116 m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType
), errorDetails)); |
| 81 // FIXME: Include the line number which started the crypto operation
. | |
| 82 executionContext()->addConsoleMessage(JSMessageSource, ErrorMessageL
evel, errorDetails); | |
| 83 } | |
| 84 m_promiseResolver->reject(V8NullType()); | |
| 85 delete this; | 117 delete this; |
| 86 } | 118 } |
| 87 | 119 |
| 88 void completeWithError() | |
| 89 { | |
| 90 completeWithError(blink::WebString()); | |
| 91 } | |
| 92 | |
| 93 void completeWithBuffer(const blink::WebArrayBuffer& buffer) | 120 void completeWithBuffer(const blink::WebArrayBuffer& buffer) |
| 94 { | 121 { |
| 95 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); | 122 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
| 96 delete this; | 123 delete this; |
| 97 } | 124 } |
| 98 | 125 |
| 99 void completeWithBoolean(bool b) | 126 void completeWithBoolean(bool b) |
| 100 { | 127 { |
| 101 m_promiseResolver->resolve(b); | 128 m_promiseResolver->resolve(b); |
| 102 delete this; | 129 delete this; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 128 | 155 |
| 129 CryptoResultImpl::~CryptoResultImpl() | 156 CryptoResultImpl::~CryptoResultImpl() |
| 130 { | 157 { |
| 131 } | 158 } |
| 132 | 159 |
| 133 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create() | 160 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create() |
| 134 { | 161 { |
| 135 return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::Ge
tCurrent()))); | 162 return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::Ge
tCurrent()))); |
| 136 } | 163 } |
| 137 | 164 |
| 138 void CryptoResultImpl::completeWithError(const blink::WebString& errorDetails) | 165 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) |
| 139 { | 166 { |
| 140 if (m_promiseState) | 167 if (m_promiseState) |
| 141 m_promiseState->completeWithError(errorDetails); | 168 m_promiseState->completeWithError(errorType, errorDetails); |
| 142 } | |
| 143 | |
| 144 void CryptoResultImpl::completeWithError() | |
| 145 { | |
| 146 completeWithError(blink::WebString()); | |
| 147 } | 169 } |
| 148 | 170 |
| 149 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 171 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
| 150 { | 172 { |
| 151 if (m_promiseState) | 173 if (m_promiseState) |
| 152 m_promiseState->completeWithBuffer(buffer); | 174 m_promiseState->completeWithBuffer(buffer); |
| 153 } | 175 } |
| 154 | 176 |
| 155 void CryptoResultImpl::completeWithBoolean(bool b) | 177 void CryptoResultImpl::completeWithBoolean(bool b) |
| 156 { | 178 { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 174 : m_promiseState(PromiseState::create(context)) | 196 : m_promiseState(PromiseState::create(context)) |
| 175 { | 197 { |
| 176 } | 198 } |
| 177 | 199 |
| 178 ScriptPromise CryptoResultImpl::promise() | 200 ScriptPromise CryptoResultImpl::promise() |
| 179 { | 201 { |
| 180 return m_promiseState->promise(); | 202 return m_promiseState->promise(); |
| 181 } | 203 } |
| 182 | 204 |
| 183 } // namespace WebCore | 205 } // namespace WebCore |
| OLD | NEW |