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 15 matching lines...) Expand all Loading... |
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 "public/platform/WebCrypto.h" | 32 #include "public/platform/WebCrypto.h" |
33 | 33 |
34 #include "platform/CryptoResult.h" | 34 #include "platform/CryptoResult.h" |
35 #include "platform/heap/Heap.h" | 35 #include "platform/heap/Heap.h" |
36 #include <string.h> | 36 #include "wtf/Atomics.h" |
37 | 37 |
38 namespace blink { | 38 namespace blink { |
39 | 39 |
40 void WebCryptoResult::completeWithError(WebCryptoErrorType errorType, const WebS
tring& errorDetails) | 40 void WebCryptoResult::completeWithError(WebCryptoErrorType errorType, const WebS
tring& errorDetails) |
41 { | 41 { |
| 42 if (cancelled()) |
| 43 return; |
| 44 ASSERT(m_impl->isOwnerResult(this)); |
42 m_impl->completeWithError(errorType, errorDetails); | 45 m_impl->completeWithError(errorType, errorDetails); |
43 reset(); | 46 clear(); |
44 } | 47 } |
45 | 48 |
46 void WebCryptoResult::completeWithBuffer(const void* bytes, unsigned bytesSize) | 49 void WebCryptoResult::completeWithBuffer(const void* bytes, unsigned bytesSize) |
47 { | 50 { |
| 51 if (cancelled()) |
| 52 return; |
| 53 ASSERT(m_impl->isOwnerResult(this)); |
48 m_impl->completeWithBuffer(bytes, bytesSize); | 54 m_impl->completeWithBuffer(bytes, bytesSize); |
49 reset(); | 55 clear(); |
50 } | 56 } |
51 | 57 |
52 void WebCryptoResult::completeWithJson(const char* utf8Data, unsigned length) | 58 void WebCryptoResult::completeWithJson(const char* utf8Data, unsigned length) |
53 { | 59 { |
| 60 if (cancelled()) |
| 61 return; |
| 62 ASSERT(m_impl->isOwnerResult(this)); |
54 m_impl->completeWithJson(utf8Data, length); | 63 m_impl->completeWithJson(utf8Data, length); |
55 reset(); | 64 clear(); |
56 } | 65 } |
57 | 66 |
58 void WebCryptoResult::completeWithBoolean(bool b) | 67 void WebCryptoResult::completeWithBoolean(bool b) |
59 { | 68 { |
| 69 if (cancelled()) |
| 70 return; |
| 71 ASSERT(m_impl->isOwnerResult(this)); |
60 m_impl->completeWithBoolean(b); | 72 m_impl->completeWithBoolean(b); |
61 reset(); | 73 clear(); |
62 } | 74 } |
63 | 75 |
64 void WebCryptoResult::completeWithKey(const WebCryptoKey& key) | 76 void WebCryptoResult::completeWithKey(const WebCryptoKey& key) |
65 { | 77 { |
| 78 if (cancelled()) |
| 79 return; |
66 ASSERT(!key.isNull()); | 80 ASSERT(!key.isNull()); |
| 81 ASSERT(m_impl->isOwnerResult(this)); |
67 m_impl->completeWithKey(key); | 82 m_impl->completeWithKey(key); |
68 reset(); | 83 clear(); |
69 } | 84 } |
70 | 85 |
71 void WebCryptoResult::completeWithKeyPair(const WebCryptoKey& publicKey, const W
ebCryptoKey& privateKey) | 86 void WebCryptoResult::completeWithKeyPair(const WebCryptoKey& publicKey, const W
ebCryptoKey& privateKey) |
72 { | 87 { |
| 88 if (cancelled()) |
| 89 return; |
73 ASSERT(!publicKey.isNull()); | 90 ASSERT(!publicKey.isNull()); |
74 ASSERT(!privateKey.isNull()); | 91 ASSERT(!privateKey.isNull()); |
| 92 ASSERT(m_impl->isOwnerResult(this)); |
75 m_impl->completeWithKeyPair(publicKey, privateKey); | 93 m_impl->completeWithKeyPair(publicKey, privateKey); |
76 reset(); | 94 clear(); |
77 } | 95 } |
78 | 96 |
79 bool WebCryptoResult::cancelled() const | 97 bool WebCryptoResult::cancelled() const |
80 { | 98 { |
81 return m_impl->cancelled(); | 99 return acquireLoad(&m_cancelled); |
82 } | 100 } |
83 | 101 |
84 WebCryptoResult::WebCryptoResult(const PassRefPtrWillBeRawPtr<CryptoResult>& imp
l) | 102 WebCryptoResult::WebCryptoResult(const PassRefPtrWillBeRawPtr<CryptoResult>& imp
l) |
85 : m_impl(impl) | 103 : m_cancelled(0) |
| 104 , m_impl(impl) |
86 { | 105 { |
87 ASSERT(m_impl.get()); | 106 ASSERT(m_impl.get()); |
| 107 bool cancel = m_impl->registerResult(this); |
| 108 // The Blink origin thread invokes this constructor; cancellation |
| 109 // cannot happen while doing so. |
| 110 ASSERT_UNUSED(cancel, !cancel && !cancelled()); |
88 } | 111 } |
89 | 112 |
90 void WebCryptoResult::reset() | 113 void WebCryptoResult::reset() |
91 { | 114 { |
92 m_impl.reset(); | 115 CryptoResult* result = m_impl.get(); |
| 116 if (cancelled()) |
| 117 return; |
| 118 |
| 119 if (result) { |
| 120 result->unregisterResult(this); |
| 121 m_impl.reset(); |
| 122 } |
| 123 } |
| 124 |
| 125 void WebCryptoResult::clear() |
| 126 { |
| 127 reset(); |
| 128 releaseStore(&m_cancelled, 1); |
93 } | 129 } |
94 | 130 |
95 void WebCryptoResult::assign(const WebCryptoResult& o) | 131 void WebCryptoResult::assign(const WebCryptoResult& o) |
96 { | 132 { |
97 m_impl = o.m_impl; | 133 m_impl = o.m_impl; |
| 134 m_cancelled = o.cancelled(); |
| 135 if (m_cancelled) { |
| 136 m_impl.reset(); |
| 137 return; |
| 138 } |
| 139 |
| 140 // Notice that m_impl will not be updated by the Blink thread calling cancel
() |
| 141 // until registerResult(this) has been called for it. |
| 142 // |
| 143 // Should the o.m_impl CryptoResultImpl be concurrently cancelled, clear out
the |
| 144 // WebCryptoResult that was attempted registered, effectively forwarding the |
| 145 // cancellation to the assigned result. |
| 146 if (!m_impl.isNull()) { |
| 147 bool cancel = m_impl->registerResult(this); |
| 148 releaseStore(&m_cancelled, cancel); |
| 149 if (cancel) |
| 150 m_impl.reset(); |
| 151 } |
98 } | 152 } |
99 | 153 |
100 } // namespace blink | 154 } // namespace blink |
OLD | NEW |