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 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
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. | |
35 #include "bindings/v8/ScriptPromiseResolver.h" | |
36 #include "modules/crypto/Algorithm.h" | 34 #include "modules/crypto/Algorithm.h" |
37 #include "public/platform/WebArrayBuffer.h" | |
38 #include "public/platform/WebCrypto.h" | |
39 #include "wtf/ArrayBuffer.h" | |
40 #include "wtf/ArrayBufferView.h" | |
41 | 35 |
42 namespace WebCore { | 36 namespace WebCore { |
43 | 37 |
44 class CryptoOperation::Result : public WebKit::WebCryptoOperationResult { | 38 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm) |
45 public: | 39 : m_algorithm(algorithm) |
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() | |
62 { | 40 { |
63 abort(); | |
64 ASSERT(!m_impl); | |
65 } | |
66 | |
67 PassRefPtr<CryptoOperation> CryptoOperation::create(const WebKit::WebCryptoAlgor
ithm& algorithm, WebKit::WebCryptoOperation* impl) | |
68 { | |
69 return adoptRef(new CryptoOperation(algorithm, impl)); | |
70 } | |
71 | |
72 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm, We
bKit::WebCryptoOperation* impl) | |
73 : m_algorithm(algorithm) | |
74 , m_impl(impl) | |
75 , m_state(Processing) | |
76 , m_result(adoptPtr(new Result(this))) | |
77 { | |
78 ASSERT(impl); | |
79 ScriptWrappable::init(this); | 41 ScriptWrappable::init(this); |
80 } | 42 } |
81 | 43 |
82 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) | |
83 { | |
84 process(static_cast<unsigned char*>(data->data()), data->byteLength()); | |
85 return this; | |
86 } | |
87 | |
88 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) | |
89 { | |
90 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength()
); | |
91 return this; | |
92 } | |
93 | |
94 ScriptObject CryptoOperation::finish() | |
95 { | |
96 switch (m_state) { | |
97 case Processing: | |
98 m_state = Finishing; | |
99 // NOTE: The following line can result in re-entrancy to |this| | |
100 m_impl->finish(m_result.get()); | |
101 break; | |
102 case Finishing: | |
103 // Calling finish() twice is a no-op. | |
104 break; | |
105 case Done: | |
106 // Calling finish() after already complete is a no-op. | |
107 ASSERT(!m_impl); | |
108 break; | |
109 } | |
110 | |
111 return promiseResolver()->promise(); | |
112 } | |
113 | |
114 ScriptObject CryptoOperation::abort() | |
115 { | |
116 switch (m_state) { | |
117 case Processing: | |
118 case Finishing: | |
119 // This will cause m_impl to be deleted. | |
120 m_impl->abort(); | |
121 m_state = Done; | |
122 m_impl = 0; | |
123 promiseResolver()->reject(ScriptValue::createNull()); | |
124 break; | |
125 case Done: | |
126 ASSERT(!m_impl); | |
127 break; | |
128 } | |
129 | |
130 return promiseResolver()->promise(); | |
131 } | |
132 | |
133 Algorithm* CryptoOperation::algorithm() | 44 Algorithm* CryptoOperation::algorithm() |
134 { | 45 { |
135 if (!m_algorithmNode) | 46 if (!m_algorithmNode) |
136 m_algorithmNode = Algorithm::create(m_algorithm); | 47 m_algorithmNode = Algorithm::create(m_algorithm); |
137 return m_algorithmNode.get(); | 48 return m_algorithmNode.get(); |
138 } | 49 } |
139 | 50 |
140 void CryptoOperation::process(const unsigned char* bytes, size_t size) | |
141 { | |
142 switch (m_state) { | |
143 case Processing: | |
144 m_impl->process(bytes, size); | |
145 break; | |
146 case Finishing: | |
147 case Done: | |
148 return; | |
149 } | |
150 } | |
151 | |
152 ScriptPromiseResolver* CryptoOperation::promiseResolver() | |
153 { | |
154 if (!m_promiseResolver) | |
155 m_promiseResolver = ScriptPromiseResolver::create(); | |
156 return m_promiseResolver.get(); | |
157 } | |
158 | |
159 } // namespace WebCore | 51 } // namespace WebCore |
OLD | NEW |