| 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 25 matching lines...) Expand all Loading... |
| 36 #include "bindings/v8/ScriptPromiseResolver.h" | 36 #include "bindings/v8/ScriptPromiseResolver.h" |
| 37 #include "core/dom/ExceptionCode.h" | 37 #include "core/dom/ExceptionCode.h" |
| 38 #include "modules/crypto/Algorithm.h" | 38 #include "modules/crypto/Algorithm.h" |
| 39 #include "public/platform/WebArrayBuffer.h" | 39 #include "public/platform/WebArrayBuffer.h" |
| 40 #include "public/platform/WebCrypto.h" | 40 #include "public/platform/WebCrypto.h" |
| 41 #include "wtf/ArrayBuffer.h" | 41 #include "wtf/ArrayBuffer.h" |
| 42 #include "wtf/ArrayBufferView.h" | 42 #include "wtf/ArrayBufferView.h" |
| 43 | 43 |
| 44 namespace WebCore { | 44 namespace WebCore { |
| 45 | 45 |
| 46 CryptoOperation::~CryptoOperation() | 46 CryptoOperationImpl::CryptoOperationImpl() |
| 47 : m_state(Initializing) |
| 48 , m_impl(0) |
| 49 , m_initializationError(0) |
| 47 { | 50 { |
| 48 abortImpl(); | |
| 49 ASSERT(!m_impl); | |
| 50 } | 51 } |
| 51 | 52 |
| 52 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor
ithm& algorithm, ExceptionState* es) | 53 bool CryptoOperationImpl::throwInitializationError(ExceptionState& es) |
| 53 { | 54 { |
| 54 return adoptRef(new CryptoOperation(algorithm, es)); | 55 ASSERT(m_state != Initializing); |
| 56 |
| 57 if (m_initializationError) { |
| 58 es.throwDOMException(m_initializationError); |
| 59 return true; |
| 60 } |
| 61 return false; |
| 55 } | 62 } |
| 56 | 63 |
| 57 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, Ex
ceptionState* es) | 64 ScriptObject CryptoOperationImpl::finish() |
| 58 : m_algorithm(algorithm) | |
| 59 , m_impl(0) | |
| 60 , m_exceptionState(es) | |
| 61 , m_state(Initializing) | |
| 62 { | |
| 63 ASSERT(es); | |
| 64 ScriptWrappable::init(this); | |
| 65 } | |
| 66 | |
| 67 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) | |
| 68 { | |
| 69 process(static_cast<unsigned char*>(data->data()), data->byteLength()); | |
| 70 return this; | |
| 71 } | |
| 72 | |
| 73 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) | |
| 74 { | |
| 75 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength()
); | |
| 76 return this; | |
| 77 } | |
| 78 | |
| 79 ScriptObject CryptoOperation::finish() | |
| 80 { | 65 { |
| 81 switch (m_state) { | 66 switch (m_state) { |
| 82 case Initializing: | 67 case Initializing: |
| 83 ASSERT_NOT_REACHED(); | 68 ASSERT_NOT_REACHED(); |
| 84 return ScriptObject(); | 69 return ScriptObject(); |
| 85 case Processing: | 70 case Processing: |
| 86 m_state = Finishing; | 71 m_state = Finishing; |
| 87 // NOTE: The following line can result in re-entrancy to |this| | 72 // NOTE: The following line can result in re-entrancy to |this| |
| 88 m_impl->finish(); | 73 m_impl->finish(); |
| 89 break; | 74 break; |
| 90 case Finishing: | 75 case Finishing: |
| 91 // Calling finish() twice is a no-op. | 76 // Calling finish() twice is a no-op. |
| 92 break; | 77 break; |
| 93 case Done: | 78 case Done: |
| 94 // Calling finish() after already complete is a no-op. | 79 // Calling finish() after already complete is a no-op. |
| 95 ASSERT(!m_impl); | 80 ASSERT(!m_impl); |
| 96 break; | 81 break; |
| 97 } | 82 } |
| 98 | 83 |
| 99 return promiseResolver()->promise(); | 84 return promiseResolver()->promise(); |
| 100 } | 85 } |
| 101 | 86 |
| 102 ScriptObject CryptoOperation::abort() | 87 void CryptoOperationImpl::initializationFailed() |
| 103 { | |
| 104 if (abortImpl()) | |
| 105 promiseResolver()->reject(ScriptValue::createNull()); | |
| 106 return promiseResolver()->promise(); | |
| 107 } | |
| 108 | |
| 109 Algorithm* CryptoOperation::algorithm() | |
| 110 { | |
| 111 if (!m_algorithmNode) | |
| 112 m_algorithmNode = Algorithm::create(m_algorithm); | |
| 113 return m_algorithmNode.get(); | |
| 114 } | |
| 115 | |
| 116 void CryptoOperation::initializationFailed() | |
| 117 { | 88 { |
| 118 ASSERT(m_state == Initializing); | 89 ASSERT(m_state == Initializing); |
| 119 | 90 |
| 120 m_exceptionState->throwDOMException(NotSupportedError); | 91 m_initializationError = NotSupportedError; |
| 121 | |
| 122 m_exceptionState = 0; | |
| 123 m_state = Done; | 92 m_state = Done; |
| 124 } | 93 } |
| 125 | 94 |
| 126 void CryptoOperation::initializationSucceded(WebKit::WebCryptoOperation* operati
onImpl) | 95 void CryptoOperationImpl::initializationSucceeded(WebKit::WebCryptoOperation* op
erationImpl) |
| 127 { | 96 { |
| 128 ASSERT(m_state == Initializing); | 97 ASSERT(m_state == Initializing); |
| 129 ASSERT(operationImpl); | 98 ASSERT(operationImpl); |
| 130 ASSERT(!m_impl); | 99 ASSERT(!m_impl); |
| 131 | 100 |
| 132 m_exceptionState = 0; | |
| 133 m_impl = operationImpl; | 101 m_impl = operationImpl; |
| 134 m_state = Processing; | 102 m_state = Processing; |
| 135 } | 103 } |
| 136 | 104 |
| 137 void CryptoOperation::completeWithError() | 105 void CryptoOperationImpl::completeWithError() |
| 138 { | 106 { |
| 139 ASSERT(m_state == Processing || m_state == Finishing); | 107 ASSERT(m_state == Processing || m_state == Finishing); |
| 140 | 108 |
| 141 m_impl = 0; | 109 m_impl = 0; |
| 142 m_state = Done; | 110 m_state = Done; |
| 143 | |
| 144 promiseResolver()->reject(ScriptValue::createNull()); | 111 promiseResolver()->reject(ScriptValue::createNull()); |
| 145 } | 112 } |
| 146 | 113 |
| 147 void CryptoOperation::completeWithArrayBuffer(const WebKit::WebArrayBuffer& buff
er) | 114 void CryptoOperationImpl::completeWithArrayBuffer(const WebKit::WebArrayBuffer&
buffer) |
| 148 { | 115 { |
| 149 ASSERT(m_state == Processing || m_state == Finishing); | 116 ASSERT(m_state == Processing || m_state == Finishing); |
| 150 | 117 |
| 151 m_impl = 0; | 118 m_impl = 0; |
| 152 m_state = Done; | 119 m_state = Done; |
| 153 | |
| 154 promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer)); | 120 promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer)); |
| 155 } | 121 } |
| 156 | 122 |
| 157 void CryptoOperation::process(const unsigned char* bytes, size_t size) | 123 void CryptoOperationImpl::process(const void* bytes, size_t size) |
| 158 { | 124 { |
| 159 switch (m_state) { | 125 switch (m_state) { |
| 160 case Initializing: | 126 case Initializing: |
| 161 ASSERT_NOT_REACHED(); | 127 ASSERT_NOT_REACHED(); |
| 162 case Processing: | 128 case Processing: |
| 163 m_impl->process(bytes, size); | 129 m_impl->process(reinterpret_cast<const unsigned char*>(bytes), size); |
| 164 break; | 130 break; |
| 165 case Finishing: | 131 case Finishing: |
| 166 case Done: | 132 case Done: |
| 167 return; | 133 return; |
| 168 } | 134 } |
| 169 } | 135 } |
| 170 | 136 |
| 171 bool CryptoOperation::abortImpl() | 137 ScriptObject CryptoOperationImpl::abort() |
| 172 { | 138 { |
| 173 switch (m_state) { | 139 switch (m_state) { |
| 174 case Initializing: | 140 case Initializing: |
| 175 ASSERT_NOT_REACHED(); | 141 ASSERT_NOT_REACHED(); |
| 176 break; | 142 break; |
| 177 case Processing: | 143 case Processing: |
| 178 case Finishing: | 144 case Finishing: |
| 179 // This will cause m_impl to be deleted. | 145 // This will cause m_impl to be deleted. |
| 180 m_state = Done; | 146 m_state = Done; |
| 181 m_impl->abort(); | 147 m_impl->abort(); |
| 182 m_impl = 0; | 148 m_impl = 0; |
| 183 return true; | 149 promiseResolver()->reject(ScriptValue::createNull()); |
| 184 case Done: | 150 case Done: |
| 185 ASSERT(!m_impl); | 151 ASSERT(!m_impl); |
| 186 break; | 152 break; |
| 187 } | 153 } |
| 188 | 154 |
| 189 return false; | 155 return promiseResolver()->promise(); |
| 190 } | 156 } |
| 191 | 157 |
| 192 ScriptPromiseResolver* CryptoOperation::promiseResolver() | 158 void CryptoOperationImpl::detach() |
| 159 { |
| 160 switch (m_state) { |
| 161 case Initializing: |
| 162 ASSERT_NOT_REACHED(); |
| 163 break; |
| 164 case Processing: |
| 165 // If the operation has not been finished yet, it has no way of being |
| 166 // finished now that the CryptoOperation is gone. |
| 167 m_state = Done; |
| 168 m_impl->abort(); |
| 169 m_impl = 0; |
| 170 case Finishing: |
| 171 case Done: |
| 172 break; |
| 173 } |
| 174 } |
| 175 |
| 176 void CryptoOperationImpl::ref() |
| 177 { |
| 178 ThreadSafeRefCounted<CryptoOperationImpl>::ref(); |
| 179 } |
| 180 |
| 181 void CryptoOperationImpl::deref() |
| 182 { |
| 183 ThreadSafeRefCounted<CryptoOperationImpl>::deref(); |
| 184 } |
| 185 |
| 186 ScriptPromiseResolver* CryptoOperationImpl::promiseResolver() |
| 193 { | 187 { |
| 194 if (!m_promiseResolver) | 188 if (!m_promiseResolver) |
| 195 m_promiseResolver = ScriptPromiseResolver::create(); | 189 m_promiseResolver = ScriptPromiseResolver::create(); |
| 196 return m_promiseResolver.get(); | 190 return m_promiseResolver.get(); |
| 197 } | 191 } |
| 198 | 192 |
| 193 CryptoOperation::~CryptoOperation() |
| 194 { |
| 195 m_impl->detach(); |
| 196 } |
| 197 |
| 198 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor
ithm& algorithm, CryptoOperationImpl* impl) |
| 199 { |
| 200 return adoptRef(new CryptoOperation(algorithm, impl)); |
| 201 } |
| 202 |
| 203 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, Cr
yptoOperationImpl* impl) |
| 204 : m_algorithm(algorithm) |
| 205 , m_impl(impl) |
| 206 { |
| 207 ASSERT(impl); |
| 208 ScriptWrappable::init(this); |
| 209 } |
| 210 |
| 211 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) |
| 212 { |
| 213 m_impl->process(data->data(), data->byteLength()); |
| 214 return this; |
| 215 } |
| 216 |
| 217 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) |
| 218 { |
| 219 m_impl->process(data->baseAddress(), data->byteLength()); |
| 220 return this; |
| 221 } |
| 222 |
| 223 ScriptObject CryptoOperation::finish() |
| 224 { |
| 225 return m_impl->finish(); |
| 226 } |
| 227 |
| 228 ScriptObject CryptoOperation::abort() |
| 229 { |
| 230 return m_impl->abort(); |
| 231 } |
| 232 |
| 233 Algorithm* CryptoOperation::algorithm() |
| 234 { |
| 235 if (!m_algorithmNode) |
| 236 m_algorithmNode = Algorithm::create(m_algorithm); |
| 237 return m_algorithmNode.get(); |
| 238 } |
| 239 |
| 199 } // namespace WebCore | 240 } // namespace WebCore |
| OLD | NEW |