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" | |
34 #include "modules/crypto/Algorithm.h" | 36 #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" | |
35 | 41 |
36 namespace WebCore { | 42 namespace WebCore { |
37 | 43 |
38 CryptoOperation::CryptoOperation(const WebKit::WebCryptoAlgorithm& algorithm) | 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->m_promiseResolver->fulfill(PassRefPtr<ArrayBuffer>(buffer)); | |
55 } | |
56 | |
57 private: | |
58 CryptoOperation* m_parent; | |
59 }; | |
60 | |
61 CryptoOperation::~CryptoOperation() | |
62 { | |
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) | |
39 : m_algorithm(algorithm) | 73 : m_algorithm(algorithm) |
74 , m_impl(impl) | |
75 , m_state(Processing) | |
76 , m_result(adoptPtr(new Result(this))) | |
40 { | 77 { |
78 ASSERT(impl); | |
41 ScriptWrappable::init(this); | 79 ScriptWrappable::init(this); |
80 | |
81 m_promiseResolver = ScriptPromiseResolver::create(); | |
abarth-chromium
2013/07/16 00:29:10
Can we delay creating the m_promiseResolver until
eroman
2013/07/16 07:28:07
Done.
| |
82 } | |
83 | |
84 CryptoOperation* CryptoOperation::process(ArrayBuffer* data) | |
85 { | |
86 process(static_cast<unsigned char*>(data->data()), data->byteLength()); | |
87 return this; | |
88 } | |
89 | |
90 CryptoOperation* CryptoOperation::process(ArrayBufferView* data) | |
91 { | |
92 process(static_cast<unsigned char*>(data->baseAddress()), data->byteLength() ); | |
93 return this; | |
94 } | |
95 | |
96 ScriptObject CryptoOperation::finish() | |
97 { | |
98 switch (m_state) { | |
99 case Processing: | |
100 m_state = Finishing; | |
101 // NOTE: The following line can result in re-entrancy to |this| | |
102 m_impl->finish(m_result.get()); | |
103 break; | |
104 case Finishing: | |
105 // Calling finish() twice is a no-op. | |
106 break; | |
107 case Done: | |
108 // Calling finish() after already complete is a no-op. | |
109 ASSERT(!m_impl); | |
110 break; | |
111 } | |
112 | |
113 return m_promiseResolver->promise(); | |
abarth-chromium
2013/07/16 00:29:10
e.g., we could create it right here.
eroman
2013/07/16 07:28:07
Done.
| |
114 } | |
115 | |
116 ScriptObject CryptoOperation::abort() | |
117 { | |
118 switch (m_state) { | |
119 case Processing: | |
120 case Finishing: | |
121 // This will cause m_impl to be deleted. | |
122 m_impl->abort(); | |
123 m_state = Done; | |
124 m_impl = 0; | |
125 m_promiseResolver->reject(ScriptValue::createNull()); | |
126 break; | |
127 case Done: | |
128 ASSERT(!m_impl); | |
129 break; | |
130 } | |
131 | |
132 return m_promiseResolver->promise(); | |
42 } | 133 } |
43 | 134 |
44 Algorithm* CryptoOperation::algorithm() | 135 Algorithm* CryptoOperation::algorithm() |
45 { | 136 { |
46 if (!m_algorithmNode) | 137 if (!m_algorithmNode) |
47 m_algorithmNode = Algorithm::create(m_algorithm); | 138 m_algorithmNode = Algorithm::create(m_algorithm); |
48 return m_algorithmNode.get(); | 139 return m_algorithmNode.get(); |
49 } | 140 } |
50 | 141 |
142 void CryptoOperation::process(const unsigned char* bytes, size_t size) | |
143 { | |
144 switch (m_state) { | |
145 case Processing: | |
146 m_impl->process(bytes, size); | |
147 break; | |
148 case Finishing: | |
149 case Done: | |
150 return; | |
151 } | |
152 } | |
153 | |
51 } // namespace WebCore | 154 } // namespace WebCore |
OLD | NEW |