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 20 matching lines...) Expand all Loading... |
31 #ifndef CryptoOperation_h | 31 #ifndef CryptoOperation_h |
32 #define CryptoOperation_h | 32 #define CryptoOperation_h |
33 | 33 |
34 #include "bindings/v8/ScriptObject.h" | 34 #include "bindings/v8/ScriptObject.h" |
35 #include "bindings/v8/ScriptWrappable.h" | 35 #include "bindings/v8/ScriptWrappable.h" |
36 #include "modules/crypto/Algorithm.h" | 36 #include "modules/crypto/Algorithm.h" |
37 #include "public/platform/WebCrypto.h" | 37 #include "public/platform/WebCrypto.h" |
38 #include "public/platform/WebCryptoAlgorithm.h" | 38 #include "public/platform/WebCryptoAlgorithm.h" |
39 #include "wtf/Forward.h" | 39 #include "wtf/Forward.h" |
40 #include "wtf/PassRefPtr.h" | 40 #include "wtf/PassRefPtr.h" |
41 #include "wtf/RefCounted.h" | 41 #include "wtf/ThreadSafeRefCounted.h" |
42 | 42 |
43 namespace WebCore { | 43 namespace WebCore { |
44 | 44 |
45 class ScriptPromiseResolver; | 45 class ScriptPromiseResolver; |
46 class ExceptionState; | 46 class ExceptionState; |
47 | 47 |
48 class CryptoOperation : public ScriptWrappable, public WebKit::WebCryptoOperatio
nResult, public RefCounted<CryptoOperation> { | 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> { |
49 public: | 71 public: |
50 ~CryptoOperation(); | 72 static PassRefPtr<CryptoOperationImpl> create() { return adoptRef(new Crypto
OperationImpl); } |
51 static PassRefPtr<CryptoOperation> create(const WebKit::WebCryptoAlgorithm&,
ExceptionState*); | |
52 | 73 |
53 CryptoOperation* process(ArrayBuffer* data); | 74 bool throwInitializationError(ExceptionState&); |
54 CryptoOperation* process(ArrayBufferView* data); | 75 |
| 76 // The CryptoOperation which started the request is getting destroyed. |
| 77 void detach(); |
| 78 |
| 79 void process(const void* bytes, size_t); |
55 | 80 |
56 ScriptObject finish(); | 81 ScriptObject finish(); |
57 ScriptObject abort(); | 82 ScriptObject abort(); |
58 | 83 |
59 Algorithm* algorithm(); | 84 // WebCryptoOperationResultPrivate implementation. |
60 | |
61 // Implementation of WebKit::WebCryptoOperationResult. | |
62 virtual void initializationFailed() OVERRIDE; | 85 virtual void initializationFailed() OVERRIDE; |
63 virtual void initializationSucceded(WebKit::WebCryptoOperation*) OVERRIDE; | 86 virtual void initializationSucceeded(WebKit::WebCryptoOperation*) OVERRIDE; |
64 virtual void completeWithError() OVERRIDE; | 87 virtual void completeWithError() OVERRIDE; |
65 virtual void completeWithArrayBuffer(const WebKit::WebArrayBuffer&) OVERRIDE
; | 88 virtual void completeWithArrayBuffer(const WebKit::WebArrayBuffer&) OVERRIDE
; |
| 89 virtual void ref() OVERRIDE; |
| 90 virtual void deref() OVERRIDE; |
66 | 91 |
67 private: | 92 private: |
68 enum State { | 93 enum State { |
69 // Constructing the WebCryptoOperation. | 94 // Constructing the WebCryptoOperationImpl. |
70 Initializing, | 95 Initializing, |
71 | 96 |
72 // Accepting calls to process(). | 97 // Accepting calls to process(). |
73 Processing, | 98 Processing, |
74 | 99 |
75 // finish() has been called, but the Promise has not been resolved yet. | 100 // finish() has been called, but the Promise has not been resolved yet. |
76 Finishing, | 101 Finishing, |
77 | 102 |
78 // The operation either: | 103 // The operation either: |
79 // - completed successfully | 104 // - completed successfully |
80 // - failed | 105 // - failed |
81 // - was aborted | 106 // - was aborted |
82 Done, | 107 Done, |
83 }; | 108 }; |
84 | 109 |
85 CryptoOperation(const WebKit::WebCryptoAlgorithm&, ExceptionState*); | 110 CryptoOperationImpl(); |
86 | |
87 void process(const unsigned char*, size_t); | |
88 | |
89 // Aborts and clears m_impl. If the operation has already comleted then | |
90 // returns false. | |
91 bool abortImpl(); | |
92 | 111 |
93 ScriptPromiseResolver* promiseResolver(); | 112 ScriptPromiseResolver* promiseResolver(); |
94 | 113 |
95 WebKit::WebCryptoAlgorithm m_algorithm; | |
96 WebKit::WebCryptoOperation* m_impl; | |
97 RefPtr<Algorithm> m_algorithmNode; | |
98 State m_state; | 114 State m_state; |
99 | 115 |
| 116 WebKit::WebCryptoOperation* m_impl; |
100 RefPtr<ScriptPromiseResolver> m_promiseResolver; | 117 RefPtr<ScriptPromiseResolver> m_promiseResolver; |
| 118 ExceptionCode m_initializationError; |
| 119 }; |
101 | 120 |
102 ExceptionState* m_exceptionState; | 121 class CryptoOperation : public ScriptWrappable, public RefCounted<CryptoOperatio
n> { |
| 122 public: |
| 123 ~CryptoOperation(); |
| 124 static PassRefPtr<CryptoOperation> create(const WebKit::WebCryptoAlgorithm&,
CryptoOperationImpl*); |
| 125 |
| 126 CryptoOperation* process(ArrayBuffer* data); |
| 127 CryptoOperation* process(ArrayBufferView* data); |
| 128 |
| 129 ScriptObject finish(); |
| 130 ScriptObject abort(); |
| 131 |
| 132 Algorithm* algorithm(); |
| 133 |
| 134 CryptoOperationImpl* impl() { return m_impl.get(); } |
| 135 |
| 136 private: |
| 137 explicit CryptoOperation(const WebKit::WebCryptoAlgorithm&, CryptoOperationI
mpl*); |
| 138 |
| 139 WebKit::WebCryptoAlgorithm m_algorithm; |
| 140 RefPtr<Algorithm> m_algorithmNode; |
| 141 |
| 142 RefPtr<CryptoOperationImpl> m_impl; |
103 }; | 143 }; |
104 | 144 |
105 } // namespace WebCore | 145 } // namespace WebCore |
106 | 146 |
107 #endif | 147 #endif |
OLD | NEW |