Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: Source/modules/crypto/CryptoResultImpl.cpp

Issue 133253002: [webcrypto] Fix TODO regarding asynchronous completion of crypto operations. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Remove unused forward declaration of ScriptState Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/modules/crypto/CryptoResultImpl.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 14 matching lines...) Expand all
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/CryptoResultImpl.h" 32 #include "modules/crypto/CryptoResultImpl.h"
33 33
34 #include "bindings/v8/ScriptPromiseResolver.h" 34 #include "bindings/v8/ScriptPromiseResolver.h"
35 #include "core/dom/ExecutionContext.h"
35 #include "modules/crypto/Key.h" 36 #include "modules/crypto/Key.h"
36 #include "modules/crypto/KeyPair.h" 37 #include "modules/crypto/KeyPair.h"
37 #include "modules/crypto/NormalizeAlgorithm.h" 38 #include "modules/crypto/NormalizeAlgorithm.h"
38 #include "public/platform/Platform.h" 39 #include "public/platform/Platform.h"
39 #include "public/platform/WebArrayBuffer.h" 40 #include "public/platform/WebArrayBuffer.h"
40 #include "public/platform/WebCryptoAlgorithm.h" 41 #include "public/platform/WebCryptoAlgorithm.h"
41 #include "wtf/ArrayBufferView.h" 42 #include "wtf/ArrayBufferView.h"
42 43
43 namespace WebCore { 44 namespace WebCore {
44 45
45 CryptoResultImpl::~CryptoResultImpl() 46 CryptoResultImpl::~CryptoResultImpl()
46 { 47 {
47 ASSERT(m_finished); 48 ASSERT(m_finished);
49 ASSERT(!m_promiseResolver.get());
50 ASSERT(!m_requestState.isValid());
48 } 51 }
49 52
50 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptPromise promise) 53 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptPromise promise)
51 { 54 {
52 return adoptRef(new CryptoResultImpl(promise)); 55 return adoptRef(new CryptoResultImpl(activeExecutionContext(), promise));
53 } 56 }
54 57
55 void CryptoResultImpl::completeWithError() 58 void CryptoResultImpl::completeWithError()
56 { 59 {
57 m_promiseResolver->reject(ScriptValue::createNull()); 60 ASSERT(!m_finished);
61
62 if (canCompletePromise()) {
63 DOMRequestState::Scope scope(m_requestState);
64 m_promiseResolver->reject(ScriptValue::createNull());
65 }
66
58 finish(); 67 finish();
59 } 68 }
60 69
61 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) 70 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer)
62 { 71 {
63 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); 72 ASSERT(!m_finished);
73
74 if (canCompletePromise()) {
75 DOMRequestState::Scope scope(m_requestState);
76 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer));
77 }
78
64 finish(); 79 finish();
65 } 80 }
66 81
67 void CryptoResultImpl::completeWithBoolean(bool b) 82 void CryptoResultImpl::completeWithBoolean(bool b)
68 { 83 {
69 m_promiseResolver->resolve(ScriptValue::createBoolean(b)); 84 ASSERT(!m_finished);
85
86 if (canCompletePromise()) {
87 DOMRequestState::Scope scope(m_requestState);
88 m_promiseResolver->resolve(ScriptValue::createBoolean(b));
89 }
90
70 finish(); 91 finish();
71 } 92 }
72 93
73 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) 94 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key)
74 { 95 {
75 m_promiseResolver->resolve(Key::create(key)); 96 ASSERT(!m_finished);
97
98 if (canCompletePromise()) {
99 DOMRequestState::Scope scope(m_requestState);
100 m_promiseResolver->resolve(Key::create(key));
101 }
102
76 finish(); 103 finish();
77 } 104 }
78 105
79 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey) 106 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::WebCryptoKey& privateKey)
80 { 107 {
81 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); 108 ASSERT(!m_finished);
109
110 if (canCompletePromise()) {
111 DOMRequestState::Scope scope(m_requestState);
112 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey));
113 }
114
82 finish(); 115 finish();
83 } 116 }
84 117
85 CryptoResultImpl::CryptoResultImpl(ScriptPromise promise) 118 CryptoResultImpl::CryptoResultImpl(ExecutionContext* context, ScriptPromise prom ise)
86 : m_promiseResolver(ScriptPromiseResolver::create(promise)) 119 : ContextLifecycleObserver(context)
87 , m_finished(false) { } 120 , m_promiseResolver(ScriptPromiseResolver::create(promise))
121 , m_requestState(context)
122 #if !ASSERT_DISABLED
123 , m_owningThread(currentThread())
124 , m_finished(false)
125 #endif
126 {
127 ASSERT(toIsolate(context) == promise.isolate());
128 }
88 129
89 void CryptoResultImpl::finish() 130 void CryptoResultImpl::finish()
90 { 131 {
91 ASSERT(!m_finished); 132 #if !ASSERT_DISABLED
92 m_finished = true; 133 m_finished = true;
134 #endif
135 clearPromiseResolver();
136 }
137
138 void CryptoResultImpl::clearPromiseResolver()
139 {
140 m_promiseResolver.clear();
141 m_requestState.clear();
142 }
143
144 void CryptoResultImpl::CheckValidThread() const
145 {
146 ASSERT(m_owningThread == currentThread());
147 }
148
149 void CryptoResultImpl::contextDestroyed()
150 {
151 ContextLifecycleObserver::contextDestroyed();
152
153 // Abandon the promise without completing it when the context goes away.
154 clearPromiseResolver();
155 ASSERT(!canCompletePromise());
156 }
157
158 bool CryptoResultImpl::canCompletePromise() const
159 {
160 CheckValidThread();
161 ExecutionContext* context = executionContext();
162 return context && !context->activeDOMObjectsAreSuspended() && !context->acti veDOMObjectsAreStopped();
93 } 163 }
94 164
95 } // namespace WebCore 165 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/crypto/CryptoResultImpl.h ('k') | Source/modules/crypto/SubtleCrypto.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698