| 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 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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/CryptoOperation.h" | 32 #include "modules/crypto/CryptoOperation.h" |
| 33 | 33 |
| 34 #include "bindings/v8/custom/V8ArrayBufferCustom.h" // MUST precede ScriptPromis
eResolver for compilation to work. | 34 #include "bindings/v8/custom/V8ArrayBufferCustom.h" // MUST precede ScriptPromis
eResolver for compilation to work. |
| 35 #include "bindings/v8/ScriptPromiseResolver.h" | 35 #include "bindings/v8/ScriptPromiseResolver.h" |
| 36 #include "core/dom/ExceptionCode.h" |
| 36 #include "modules/crypto/Algorithm.h" | 37 #include "modules/crypto/Algorithm.h" |
| 37 #include "public/platform/WebArrayBuffer.h" | 38 #include "public/platform/WebArrayBuffer.h" |
| 38 #include "public/platform/WebCrypto.h" | 39 #include "public/platform/WebCrypto.h" |
| 39 #include "wtf/ArrayBuffer.h" | 40 #include "wtf/ArrayBuffer.h" |
| 40 #include "wtf/ArrayBufferView.h" | 41 #include "wtf/ArrayBufferView.h" |
| 41 | 42 |
| 42 namespace WebCore { | 43 namespace WebCore { |
| 43 | 44 |
| 44 class CryptoOperation::Result : public WebKit::WebCryptoOperationResult { | |
| 45 public: | |
| 46 explicit Result(CryptoOperation* parent) : m_parent(parent) { } | |
| 47 | |
| 48 virtual void setArrayBuffer(const WebKit::WebArrayBuffer& buffer) OVERRIDE | |
| 49 { | |
| 50 ASSERT(m_parent->m_state == Finishing); | |
| 51 m_parent->m_state = Done; | |
| 52 m_parent->m_impl = 0; | |
| 53 | |
| 54 m_parent->promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer)); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 CryptoOperation* m_parent; | |
| 59 }; | |
| 60 | |
| 61 CryptoOperation::~CryptoOperation() | 45 CryptoOperation::~CryptoOperation() |
| 62 { | 46 { |
| 63 abortImpl(); | 47 abortImpl(); |
| 64 ASSERT(!m_impl); | 48 ASSERT(!m_impl); |
| 65 } | 49 } |
| 66 | 50 |
| 67 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor
ithm& algorithm, WebKit::WebCryptoOperation* impl) | 51 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor
ithm& algorithm, ExceptionCode* ec) |
| 68 { | 52 { |
| 69 return adoptRef(new CryptoOperation(algorithm, impl)); | 53 return adoptRef(new CryptoOperation(algorithm, ec)); |
| 70 } | 54 } |
| 71 | 55 |
| 72 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, We
bKit::WebCryptoOperation* impl) | 56 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, Ex
ceptionCode* ec) |
| 73 : m_algorithm(algorithm) | 57 : m_algorithm(algorithm) |
| 74 , m_impl(impl) | 58 , m_impl(0) |
| 75 , m_state(Processing) | 59 , m_exceptionCode(ec) |
| 76 , m_result(adoptPtr(new Result(this))) | 60 , m_state(Initializing) |
| 77 { | 61 { |
| 78 ASSERT(impl); | 62 ASSERT(ec); |
| 79 ScriptWrappable::init(this); | 63 ScriptWrappable::init(this); |
| 80 } | 64 } |
| 81 | 65 |
| 82 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) | 66 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) |
| 83 { | 67 { |
| 84 process(static_cast<unsigned char*>(data->data()), data->byteLength()); | 68 process(static_cast<unsigned char*>(data->data()), data->byteLength()); |
| 85 return this; | 69 return this; |
| 86 } | 70 } |
| 87 | 71 |
| 88 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) | 72 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) |
| 89 { | 73 { |
| 90 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength()
); | 74 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength()
); |
| 91 return this; | 75 return this; |
| 92 } | 76 } |
| 93 | 77 |
| 94 ScriptObject CryptoOperation::finish() | 78 ScriptObject CryptoOperation::finish() |
| 95 { | 79 { |
| 96 switch (m_state) { | 80 switch (m_state) { |
| 81 case Initializing: |
| 82 ASSERT_NOT_REACHED(); |
| 83 return ScriptObject(); |
| 97 case Processing: | 84 case Processing: |
| 98 m_state = Finishing; | 85 m_state = Finishing; |
| 99 // NOTE: The following line can result in re-entrancy to |this| | 86 // NOTE: The following line can result in re-entrancy to |this| |
| 100 m_impl->finish(m_result.get()); | 87 m_impl->finish(); |
| 101 break; | 88 break; |
| 102 case Finishing: | 89 case Finishing: |
| 103 // Calling finish() twice is a no-op. | 90 // Calling finish() twice is a no-op. |
| 104 break; | 91 break; |
| 105 case Done: | 92 case Done: |
| 106 // Calling finish() after already complete is a no-op. | 93 // Calling finish() after already complete is a no-op. |
| 107 ASSERT(!m_impl); | 94 ASSERT(!m_impl); |
| 108 break; | 95 break; |
| 109 } | 96 } |
| 110 | 97 |
| 111 return promiseResolver()->promise(); | 98 return promiseResolver()->promise(); |
| 112 } | 99 } |
| 113 | 100 |
| 114 ScriptObject CryptoOperation::abort() | 101 ScriptObject CryptoOperation::abort() |
| 115 { | 102 { |
| 116 if (abortImpl()) | 103 if (abortImpl()) |
| 117 promiseResolver()->reject(ScriptValue::createNull()); | 104 promiseResolver()->reject(ScriptValue::createNull()); |
| 118 return promiseResolver()->promise(); | 105 return promiseResolver()->promise(); |
| 119 } | 106 } |
| 120 | 107 |
| 121 Algorithm* CryptoOperation::algorithm() | 108 Algorithm* CryptoOperation::algorithm() |
| 122 { | 109 { |
| 123 if (!m_algorithmNode) | 110 if (!m_algorithmNode) |
| 124 m_algorithmNode = Algorithm::create(m_algorithm); | 111 m_algorithmNode = Algorithm::create(m_algorithm); |
| 125 return m_algorithmNode.get(); | 112 return m_algorithmNode.get(); |
| 126 } | 113 } |
| 127 | 114 |
| 115 void CryptoOperation::initializationFailed() |
| 116 { |
| 117 ASSERT(m_state == Initializing); |
| 118 |
| 119 *m_exceptionCode = NotSupportedError; |
| 120 |
| 121 m_exceptionCode = 0; |
| 122 m_state = Done; |
| 123 } |
| 124 |
| 125 void CryptoOperation::initializationSucceded(WebKit::WebCryptoOperation* operati
onImpl) |
| 126 { |
| 127 ASSERT(m_state == Initializing); |
| 128 ASSERT(operationImpl); |
| 129 ASSERT(!m_impl); |
| 130 |
| 131 m_exceptionCode = 0; |
| 132 m_impl = operationImpl; |
| 133 m_state = Processing; |
| 134 } |
| 135 |
| 136 void CryptoOperation::completeWithError() |
| 137 { |
| 138 ASSERT(m_state == Processing || m_state == Finishing); |
| 139 |
| 140 m_impl = 0; |
| 141 m_state = Done; |
| 142 |
| 143 promiseResolver()->reject(ScriptValue::createNull()); |
| 144 } |
| 145 |
| 146 void CryptoOperation::completeWithArrayBuffer(const WebKit::WebArrayBuffer& buff
er) |
| 147 { |
| 148 ASSERT(m_state == Processing || m_state == Finishing); |
| 149 |
| 150 m_impl = 0; |
| 151 m_state = Done; |
| 152 |
| 153 promiseResolver()->fulfill(PassRefPtr<ArrayBuffer>(buffer)); |
| 154 } |
| 155 |
| 128 void CryptoOperation::process(const unsigned char* bytes, size_t size) | 156 void CryptoOperation::process(const unsigned char* bytes, size_t size) |
| 129 { | 157 { |
| 130 switch (m_state) { | 158 switch (m_state) { |
| 159 case Initializing: |
| 160 ASSERT_NOT_REACHED(); |
| 131 case Processing: | 161 case Processing: |
| 132 m_impl->process(bytes, size); | 162 m_impl->process(bytes, size); |
| 133 break; | 163 break; |
| 134 case Finishing: | 164 case Finishing: |
| 135 case Done: | 165 case Done: |
| 136 return; | 166 return; |
| 137 } | 167 } |
| 138 } | 168 } |
| 139 | 169 |
| 140 bool CryptoOperation::abortImpl() | 170 bool CryptoOperation::abortImpl() |
| 141 { | 171 { |
| 142 switch (m_state) { | 172 switch (m_state) { |
| 173 case Initializing: |
| 174 ASSERT_NOT_REACHED(); |
| 175 break; |
| 143 case Processing: | 176 case Processing: |
| 144 case Finishing: | 177 case Finishing: |
| 145 // This will cause m_impl to be deleted. | 178 // This will cause m_impl to be deleted. |
| 179 m_state = Done; |
| 146 m_impl->abort(); | 180 m_impl->abort(); |
| 147 m_state = Done; | |
| 148 m_impl = 0; | 181 m_impl = 0; |
| 149 return true; | 182 return true; |
| 150 case Done: | 183 case Done: |
| 151 ASSERT(!m_impl); | 184 ASSERT(!m_impl); |
| 152 break; | 185 break; |
| 153 } | 186 } |
| 154 | 187 |
| 155 return false; | 188 return false; |
| 156 } | 189 } |
| 157 | 190 |
| 158 ScriptPromiseResolver* CryptoOperation::promiseResolver() | 191 ScriptPromiseResolver* CryptoOperation::promiseResolver() |
| 159 { | 192 { |
| 160 if (!m_promiseResolver) | 193 if (!m_promiseResolver) |
| 161 m_promiseResolver = ScriptPromiseResolver::create(); | 194 m_promiseResolver = ScriptPromiseResolver::create(); |
| 162 return m_promiseResolver.get(); | 195 return m_promiseResolver.get(); |
| 163 } | 196 } |
| 164 | 197 |
| 165 } // namespace WebCore | 198 } // namespace WebCore |
| OLD | NEW |