OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
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. | |
29 */ | |
30 | |
31 #ifndef CryptoOperation_h | |
32 #define CryptoOperation_h | |
33 | |
34 #include "bindings/v8/ScriptObject.h" | |
35 #include "bindings/v8/ScriptWrappable.h" | |
36 #include "modules/crypto/Algorithm.h" | |
37 #include "public/platform/WebCrypto.h" | |
38 #include "public/platform/WebCryptoAlgorithm.h" | |
39 #include "wtf/Forward.h" | |
40 #include "wtf/PassRefPtr.h" | |
41 #include "wtf/ThreadSafeRefCounted.h" | |
42 | |
43 namespace WebCore { | |
44 | |
45 class ScriptPromiseResolver; | |
46 class ExceptionState; | |
47 | |
48 typedef int ExceptionCode; | |
49 | |
50 // CryptoOperation vs CryptoOperationImpl: | |
51 // | |
52 // A CryptoOperation corresponds with the request from JavaScript to start a | |
53 // multi-part cryptographic operation. This is forwarded to the Platform layer, | |
54 // which creates a WebCryptoOperation. When the WebCryptoOperation eventually | |
55 // completes, it resolves a Promise. | |
56 // | |
57 // To avoid a reference cycle between WebCryptoOperation and CryptoOperation, | |
58 // WebCryptoOperation's result handle holds a reference to | |
59 // CryptoOperationImpl rather than CryptoOperation. This prevents extending the | |
60 // lifetime of CryptoOperation beyond JavaScript garbage collection, which is | |
61 // important since: | |
62 // | |
63 // * When JavaScript garbage collects CryptoOperation and finish() has NOT | |
64 // been called on it, the Platform operation can no longer complete and | |
65 // should therefore be aborted. | |
66 // * The WebCryptoOperation may outlive CryptoOperation if finish() was | |
67 // called, as the result is delivered to a separate Promise (this is | |
68 // different than what the current version of the spec says). | |
69 | |
70 class CryptoOperationImpl : public WebKit::WebCryptoOperationResultPrivate, publ
ic ThreadSafeRefCounted<CryptoOperationImpl> { | |
71 public: | |
72 static PassRefPtr<CryptoOperationImpl> create() { return adoptRef(new Crypto
OperationImpl); } | |
73 | |
74 bool throwInitializationError(ExceptionState&); | |
75 | |
76 // The CryptoOperation which started the request is getting destroyed. | |
77 void detach(); | |
78 | |
79 void process(const void* bytes, size_t); | |
80 | |
81 ScriptObject finish(); | |
82 ScriptObject abort(); | |
83 | |
84 // WebCryptoOperationResultPrivate implementation. | |
85 virtual void initializationFailed() OVERRIDE; | |
86 virtual void initializationSucceeded(WebKit::WebCryptoOperation*) OVERRIDE; | |
87 virtual void completeWithError() OVERRIDE; | |
88 virtual void completeWithArrayBuffer(const WebKit::WebArrayBuffer&) OVERRIDE
; | |
89 virtual void completeWithBoolean(bool) OVERRIDE; | |
90 virtual void ref() OVERRIDE; | |
91 virtual void deref() OVERRIDE; | |
92 | |
93 private: | |
94 enum State { | |
95 // Constructing the WebCryptoOperationImpl. | |
96 Initializing, | |
97 | |
98 // Accepting calls to process(). | |
99 Processing, | |
100 | |
101 // finish() has been called, but the Promise has not been resolved yet. | |
102 Finishing, | |
103 | |
104 // The operation either: | |
105 // - completed successfully | |
106 // - failed | |
107 // - was aborted | |
108 Done, | |
109 }; | |
110 | |
111 CryptoOperationImpl(); | |
112 | |
113 ScriptPromiseResolver* promiseResolver(); | |
114 | |
115 State m_state; | |
116 | |
117 WebKit::WebCryptoOperation* m_impl; | |
118 RefPtr<ScriptPromiseResolver> m_promiseResolver; | |
119 ExceptionCode m_initializationError; | |
120 }; | |
121 | |
122 class CryptoOperation : public ScriptWrappable, public RefCounted<CryptoOperatio
n> { | |
123 public: | |
124 ~CryptoOperation(); | |
125 static PassRefPtr<CryptoOperation> create(const WebKit::WebCryptoAlgorithm&,
CryptoOperationImpl*); | |
126 | |
127 CryptoOperation* process(ArrayBuffer* data); | |
128 CryptoOperation* process(ArrayBufferView* data); | |
129 | |
130 ScriptObject finish(); | |
131 ScriptObject abort(); | |
132 | |
133 Algorithm* algorithm(); | |
134 | |
135 CryptoOperationImpl* impl() { return m_impl.get(); } | |
136 | |
137 private: | |
138 explicit CryptoOperation(const WebKit::WebCryptoAlgorithm&, CryptoOperationI
mpl*); | |
139 | |
140 WebKit::WebCryptoAlgorithm m_algorithm; | |
141 RefPtr<Algorithm> m_algorithmNode; | |
142 | |
143 RefPtr<CryptoOperationImpl> m_impl; | |
144 }; | |
145 | |
146 } // namespace WebCore | |
147 | |
148 #endif | |
OLD | NEW |