| 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 29 matching lines...) Expand all Loading... |
| 40 #include "modules/crypto/Key.h" | 40 #include "modules/crypto/Key.h" |
| 41 #include "modules/crypto/KeyPair.h" | 41 #include "modules/crypto/KeyPair.h" |
| 42 #include "modules/crypto/NormalizeAlgorithm.h" | 42 #include "modules/crypto/NormalizeAlgorithm.h" |
| 43 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 44 #include "public/platform/WebArrayBuffer.h" | 44 #include "public/platform/WebArrayBuffer.h" |
| 45 #include "public/platform/WebCryptoAlgorithm.h" | 45 #include "public/platform/WebCryptoAlgorithm.h" |
| 46 #include "wtf/ArrayBufferView.h" | 46 #include "wtf/ArrayBufferView.h" |
| 47 | 47 |
| 48 namespace WebCore { | 48 namespace WebCore { |
| 49 | 49 |
| 50 namespace { |
| 51 |
| 52 class WeakResolver : public ScriptPromiseResolverWithContext { |
| 53 public: |
| 54 static WeakPtr<ScriptPromiseResolverWithContext> create(ScriptState* scriptS
tate) |
| 55 { |
| 56 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState)); |
| 57 return p->m_weakPtrFactory.createWeakPtr(); |
| 58 } |
| 59 |
| 60 private: |
| 61 explicit WeakResolver(ScriptState* scriptState) |
| 62 : ScriptPromiseResolverWithContext(scriptState, ScriptPromiseResolverWit
hContext::KeepSelfWhilePending) |
| 63 , m_weakPtrFactory(this) { } |
| 64 WeakPtrFactory<ScriptPromiseResolverWithContext> m_weakPtrFactory; |
| 65 }; |
| 66 |
| 67 } // namespace |
| 68 |
| 50 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) | 69 ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType) |
| 51 { | 70 { |
| 52 switch (errorType) { | 71 switch (errorType) { |
| 53 case blink::WebCryptoErrorTypeNotSupported: | 72 case blink::WebCryptoErrorTypeNotSupported: |
| 54 return NotSupportedError; | 73 return NotSupportedError; |
| 55 case blink::WebCryptoErrorTypeSyntax: | 74 case blink::WebCryptoErrorTypeSyntax: |
| 56 return SyntaxError; | 75 return SyntaxError; |
| 57 case blink::WebCryptoErrorTypeInvalidState: | 76 case blink::WebCryptoErrorTypeInvalidState: |
| 58 return InvalidStateError; | 77 return InvalidStateError; |
| 59 case blink::WebCryptoErrorTypeInvalidAccess: | 78 case blink::WebCryptoErrorTypeInvalidAccess: |
| 60 return InvalidAccessError; | 79 return InvalidAccessError; |
| 61 case blink::WebCryptoErrorTypeUnknown: | 80 case blink::WebCryptoErrorTypeUnknown: |
| 62 return UnknownError; | 81 return UnknownError; |
| 63 case blink::WebCryptoErrorTypeData: | 82 case blink::WebCryptoErrorTypeData: |
| 64 return DataError; | 83 return DataError; |
| 65 case blink::WebCryptoErrorTypeOperation: | 84 case blink::WebCryptoErrorTypeOperation: |
| 66 return OperationError; | 85 return OperationError; |
| 67 case blink::WebCryptoErrorTypeType: | 86 case blink::WebCryptoErrorTypeType: |
| 68 // FIXME: This should construct a TypeError instead. For now do | 87 // FIXME: This should construct a TypeError instead. For now do |
| 69 // something to facilitate refactor, but this will need to be | 88 // something to facilitate refactor, but this will need to be |
| 70 // revisited. | 89 // revisited. |
| 71 return DataError; | 90 return DataError; |
| 72 } | 91 } |
| 73 | 92 |
| 74 ASSERT_NOT_REACHED(); | 93 ASSERT_NOT_REACHED(); |
| 75 return 0; | 94 return 0; |
| 76 } | 95 } |
| 77 | 96 |
| 78 // The PromiseState class contains all the state which is tied to an | |
| 79 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, | |
| 80 // PromiseState is not thread safe and must only be accessed and deleted from | |
| 81 // the blink thread. | |
| 82 // | |
| 83 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat
e. | |
| 84 // The PromiseState deletes itself after being notified of completion. | |
| 85 // Additionally the PromiseState is deleted when the ExecutionContext is | |
| 86 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, | |
| 87 // which may die before the operation is completed). | |
| 88 class CryptoResultImpl::PromiseState FINAL { | |
| 89 public: | |
| 90 static WeakPtr<PromiseState> create(ScriptState* scriptState) | |
| 91 { | |
| 92 PromiseState* promiseState = new PromiseState(scriptState); | |
| 93 return promiseState->m_weakFactory.createWeakPtr(); | |
| 94 } | |
| 95 | |
| 96 void contextDestroyed() | |
| 97 { | |
| 98 delete this; | |
| 99 } | |
| 100 | |
| 101 ScriptPromise promise() | |
| 102 { | |
| 103 return m_promiseResolver->promise(); | |
| 104 } | |
| 105 | |
| 106 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web
String& errorDetails) | |
| 107 { | |
| 108 m_promiseResolver->reject(DOMException::create(webCryptoErrorToException
Code(errorType), errorDetails)); | |
| 109 delete this; | |
| 110 } | |
| 111 | |
| 112 void completeWithBuffer(const blink::WebArrayBuffer& buffer) | |
| 113 { | |
| 114 m_promiseResolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); | |
| 115 delete this; | |
| 116 } | |
| 117 | |
| 118 void completeWithBoolean(bool b) | |
| 119 { | |
| 120 m_promiseResolver->resolve(b); | |
| 121 delete this; | |
| 122 } | |
| 123 | |
| 124 void completeWithKey(const blink::WebCryptoKey& key) | |
| 125 { | |
| 126 m_promiseResolver->resolve(Key::create(key)); | |
| 127 delete this; | |
| 128 } | |
| 129 | |
| 130 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink::
WebCryptoKey& privateKey) | |
| 131 { | |
| 132 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | |
| 133 delete this; | |
| 134 } | |
| 135 | |
| 136 private: | |
| 137 // This subclass of ScriptPromiseResolverWithContext is to be notified | |
| 138 // when the context was destroyed. | |
| 139 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { | |
| 140 public: | |
| 141 static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, Prom
iseState* promiseState) | |
| 142 { | |
| 143 RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scri
ptState, promiseState)); | |
| 144 resolver->suspendIfNeeded(); | |
| 145 return resolver.release(); | |
| 146 } | |
| 147 | |
| 148 virtual void contextDestroyed() OVERRIDE | |
| 149 { | |
| 150 ScriptPromiseResolverWithContext::contextDestroyed(); | |
| 151 m_promiseState->contextDestroyed(); | |
| 152 } | |
| 153 | |
| 154 private: | |
| 155 explicit PromiseResolver(ScriptState* scriptState, PromiseState* promise
State) | |
| 156 : ScriptPromiseResolverWithContext(scriptState) | |
| 157 , m_promiseState(promiseState) | |
| 158 { | |
| 159 } | |
| 160 | |
| 161 PromiseState* m_promiseState; | |
| 162 }; | |
| 163 | |
| 164 explicit PromiseState(ScriptState* scriptState) | |
| 165 : m_weakFactory(this) | |
| 166 , m_promiseResolver(PromiseResolver::create(scriptState, this)) | |
| 167 { | |
| 168 } | |
| 169 | |
| 170 WeakPtrFactory<PromiseState> m_weakFactory; | |
| 171 RefPtr<PromiseResolver> m_promiseResolver; | |
| 172 }; | |
| 173 | |
| 174 CryptoResultImpl::~CryptoResultImpl() | 97 CryptoResultImpl::~CryptoResultImpl() |
| 175 { | 98 { |
| 176 } | 99 } |
| 177 | 100 |
| 178 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 101 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
| 179 { | 102 { |
| 180 return adoptRef(new CryptoResultImpl(scriptState)); | 103 return adoptRef(new CryptoResultImpl(scriptState)); |
| 181 } | 104 } |
| 182 | 105 |
| 183 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) | 106 void CryptoResultImpl::completeWithError(blink::WebCryptoErrorType errorType, co
nst blink::WebString& errorDetails) |
| 184 { | 107 { |
| 185 if (m_promiseState) | 108 if (m_resolver) |
| 186 m_promiseState->completeWithError(errorType, errorDetails); | 109 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er
rorType), errorDetails)); |
| 187 } | 110 } |
| 188 | 111 |
| 189 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) | 112 void CryptoResultImpl::completeWithBuffer(const blink::WebArrayBuffer& buffer) |
| 190 { | 113 { |
| 191 if (m_promiseState) | 114 if (m_resolver) |
| 192 m_promiseState->completeWithBuffer(buffer); | 115 m_resolver->resolve(PassRefPtr<ArrayBuffer>(buffer)); |
| 193 } | 116 } |
| 194 | 117 |
| 195 void CryptoResultImpl::completeWithBoolean(bool b) | 118 void CryptoResultImpl::completeWithBoolean(bool b) |
| 196 { | 119 { |
| 197 if (m_promiseState) | 120 if (m_resolver) |
| 198 m_promiseState->completeWithBoolean(b); | 121 m_resolver->resolve(b); |
| 199 } | 122 } |
| 200 | 123 |
| 201 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) | 124 void CryptoResultImpl::completeWithKey(const blink::WebCryptoKey& key) |
| 202 { | 125 { |
| 203 if (m_promiseState) | 126 if (m_resolver) |
| 204 m_promiseState->completeWithKey(key); | 127 m_resolver->resolve(Key::create(key)); |
| 205 } | 128 } |
| 206 | 129 |
| 207 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) | 130 void CryptoResultImpl::completeWithKeyPair(const blink::WebCryptoKey& publicKey,
const blink::WebCryptoKey& privateKey) |
| 208 { | 131 { |
| 209 if (m_promiseState) | 132 if (m_resolver) |
| 210 m_promiseState->completeWithKeyPair(publicKey, privateKey); | 133 m_resolver->resolve(KeyPair::create(publicKey, privateKey)); |
| 211 } | 134 } |
| 212 | 135 |
| 213 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 136 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
| 214 : m_promiseState(PromiseState::create(scriptState)) | 137 : m_resolver(WeakResolver::create(scriptState)) |
| 215 { | 138 { |
| 216 } | 139 } |
| 217 | 140 |
| 218 ScriptPromise CryptoResultImpl::promise() | 141 ScriptPromise CryptoResultImpl::promise() |
| 219 { | 142 { |
| 220 return m_promiseState->promise(); | 143 return m_resolver->promise(); |
| 221 } | 144 } |
| 222 | 145 |
| 223 } // namespace WebCore | 146 } // namespace WebCore |
| OLD | NEW |