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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 { | 53 { |
54 // Duplicate some of the checks done by ScriptPromiseResolver. | 54 // Duplicate some of the checks done by ScriptPromiseResolver. |
55 if (!resolver->executionContext() || resolver->executionContext()->activeDOM
ObjectsAreStopped()) | 55 if (!resolver->executionContext() || resolver->executionContext()->activeDOM
ObjectsAreStopped()) |
56 return; | 56 return; |
57 | 57 |
58 ScriptState::Scope scope(resolver->scriptState()); | 58 ScriptState::Scope scope(resolver->scriptState()); |
59 v8::Isolate* isolate = resolver->scriptState()->isolate(); | 59 v8::Isolate* isolate = resolver->scriptState()->isolate(); |
60 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails))); | 60 resolver->reject(v8::Exception::TypeError(v8String(isolate, errorDetails))); |
61 } | 61 } |
62 | 62 |
63 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver { | 63 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver { |
64 public: | 64 public: |
65 static WeakPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Crypt
oResultImpl* result) | 65 static PassRefPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Cr
yptoResultImpl* result) |
66 { | 66 { |
67 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result))
; | 67 RefPtr<Resolver> resolver = adoptRef(new Resolver(scriptState, result)); |
68 p->suspendIfNeeded(); | 68 resolver->suspendIfNeeded(); |
69 p->keepAliveWhilePending(); | 69 resolver->keepAliveWhilePending(); |
70 return p->m_weakPtrFactory.createWeakPtr(); | 70 return resolver.release(); |
71 } | 71 } |
72 | 72 |
73 virtual ~WeakResolver() | 73 virtual void stop() override |
74 { | 74 { |
75 m_result->cancel(); | 75 m_result->cancel(); |
| 76 m_result->clearResolver(); |
| 77 m_result = nullptr; |
| 78 ScriptPromiseResolver::stop(); |
76 } | 79 } |
77 | 80 |
78 private: | 81 private: |
79 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result) | 82 Resolver(ScriptState* scriptState, CryptoResultImpl* result) |
80 : ScriptPromiseResolver(scriptState) | 83 : ScriptPromiseResolver(scriptState) |
81 , m_weakPtrFactory(this) | |
82 , m_result(result) { } | 84 , m_result(result) { } |
83 WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory; | |
84 RefPtr<CryptoResultImpl> m_result; | 85 RefPtr<CryptoResultImpl> m_result; |
85 }; | 86 }; |
86 | 87 |
87 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) | 88 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) |
88 { | 89 { |
89 switch (errorType) { | 90 switch (errorType) { |
90 case WebCryptoErrorTypeNotSupported: | 91 case WebCryptoErrorTypeNotSupported: |
91 return NotSupportedError; | 92 return NotSupportedError; |
92 case WebCryptoErrorTypeSyntax: | 93 case WebCryptoErrorTypeSyntax: |
93 return SyntaxError; | 94 return SyntaxError; |
94 case WebCryptoErrorTypeInvalidAccess: | 95 case WebCryptoErrorTypeInvalidAccess: |
95 return InvalidAccessError; | 96 return InvalidAccessError; |
96 case WebCryptoErrorTypeData: | 97 case WebCryptoErrorTypeData: |
97 return DataError; | 98 return DataError; |
98 case WebCryptoErrorTypeOperation: | 99 case WebCryptoErrorTypeOperation: |
99 return OperationError; | 100 return OperationError; |
100 case WebCryptoErrorTypeType: | 101 case WebCryptoErrorTypeType: |
101 return V8TypeError; | 102 return V8TypeError; |
102 } | 103 } |
103 | 104 |
104 ASSERT_NOT_REACHED(); | 105 ASSERT_NOT_REACHED(); |
105 return 0; | 106 return 0; |
106 } | 107 } |
107 | 108 |
108 CryptoResultImpl::~CryptoResultImpl() | 109 CryptoResultImpl::~CryptoResultImpl() |
109 { | 110 { |
| 111 ASSERT(!m_resolver); |
| 112 } |
| 113 |
| 114 void CryptoResultImpl::clearResolver() |
| 115 { |
| 116 m_resolver = nullptr; |
110 } | 117 } |
111 | 118 |
112 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) | 119 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) |
113 { | 120 { |
114 return adoptRef(new CryptoResultImpl(scriptState)); | 121 return adoptRef(new CryptoResultImpl(scriptState)); |
115 } | 122 } |
116 | 123 |
117 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web
String& errorDetails) | 124 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web
String& errorDetails) |
118 { | 125 { |
119 if (m_resolver) { | 126 if (m_resolver) { |
120 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); | 127 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); |
121 | 128 |
122 // Handle TypeError separately, as it cannot be created using | 129 // Handle TypeError separately, as it cannot be created using |
123 // DOMException. | 130 // DOMException. |
124 if (ec == V8TypeError) | 131 if (ec == V8TypeError) |
125 rejectWithTypeError(errorDetails, m_resolver.get()); | 132 rejectWithTypeError(errorDetails, m_resolver); |
126 else | 133 else |
127 m_resolver->reject(DOMException::create(ec, errorDetails)); | 134 m_resolver->reject(DOMException::create(ec, errorDetails)); |
128 } | 135 } |
| 136 clearResolver(); |
129 } | 137 } |
130 | 138 |
131 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) | 139 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) |
132 { | 140 { |
133 if (m_resolver) | 141 if (m_resolver) |
134 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); | 142 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); |
| 143 clearResolver(); |
135 } | 144 } |
136 | 145 |
137 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) | 146 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) |
138 { | 147 { |
139 if (m_resolver) { | 148 if (m_resolver) { |
140 ScriptPromiseResolver* resolver = m_resolver.get(); | 149 ScriptPromiseResolver* resolver = m_resolver; |
141 ScriptState* scriptState = resolver->scriptState(); | 150 ScriptState* scriptState = resolver->scriptState(); |
142 ScriptState::Scope scope(scriptState); | 151 ScriptState::Scope scope(scriptState); |
143 | 152 |
144 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState-
>isolate(), utf8Data, v8::String::kInternalizedString, length); | 153 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState-
>isolate(), utf8Data, v8::String::kInternalizedString, length); |
145 | 154 |
146 v8::TryCatch exceptionCatcher; | 155 v8::TryCatch exceptionCatcher; |
147 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); | 156 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); |
148 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { | 157 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { |
149 ASSERT_NOT_REACHED(); | 158 ASSERT_NOT_REACHED(); |
150 resolver->reject(DOMException::create(OperationError, "Failed inflat
ing JWK JSON to object")); | 159 resolver->reject(DOMException::create(OperationError, "Failed inflat
ing JWK JSON to object")); |
151 } else { | 160 } else { |
152 resolver->resolve(jsonDictionary); | 161 resolver->resolve(jsonDictionary); |
153 } | 162 } |
154 } | 163 } |
| 164 clearResolver(); |
155 } | 165 } |
156 | 166 |
157 void CryptoResultImpl::completeWithBoolean(bool b) | 167 void CryptoResultImpl::completeWithBoolean(bool b) |
158 { | 168 { |
159 if (m_resolver) | 169 if (m_resolver) |
160 m_resolver->resolve(b); | 170 m_resolver->resolve(b); |
| 171 clearResolver(); |
161 } | 172 } |
162 | 173 |
163 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) | 174 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) |
164 { | 175 { |
165 if (m_resolver) | 176 if (m_resolver) |
166 m_resolver->resolve(CryptoKey::create(key)); | 177 m_resolver->resolve(CryptoKey::create(key)); |
| 178 clearResolver(); |
167 } | 179 } |
168 | 180 |
169 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const
WebCryptoKey& privateKey) | 181 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const
WebCryptoKey& privateKey) |
170 { | 182 { |
171 if (m_resolver) { | 183 if (m_resolver) { |
172 ScriptState* scriptState = m_resolver->scriptState(); | 184 ScriptState* scriptState = m_resolver->scriptState(); |
173 ScriptState::Scope scope(scriptState); | 185 ScriptState::Scope scope(scriptState); |
174 | 186 |
175 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); | 187 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); |
176 | 188 |
177 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey)
, scriptState->context()->Global(), scriptState->isolate()); | 189 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey)
, scriptState->context()->Global(), scriptState->isolate()); |
178 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe
y), scriptState->context()->Global(), scriptState->isolate()); | 190 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe
y), scriptState->context()->Global(), scriptState->isolate()); |
179 | 191 |
180 keyPair.set("publicKey", publicKeyValue); | 192 keyPair.set("publicKey", publicKeyValue); |
181 keyPair.set("privateKey", privateKeyValue); | 193 keyPair.set("privateKey", privateKeyValue); |
182 | 194 |
183 m_resolver->resolve(keyPair.v8Value()); | 195 m_resolver->resolve(keyPair.v8Value()); |
184 } | 196 } |
| 197 clearResolver(); |
185 } | 198 } |
186 | 199 |
187 bool CryptoResultImpl::cancelled() const | 200 bool CryptoResultImpl::cancelled() const |
188 { | 201 { |
189 return acquireLoad(&m_cancelled); | 202 return acquireLoad(&m_cancelled); |
190 } | 203 } |
191 | 204 |
192 void CryptoResultImpl::cancel() | 205 void CryptoResultImpl::cancel() |
193 { | 206 { |
194 releaseStore(&m_cancelled, 1); | 207 releaseStore(&m_cancelled, 1); |
195 } | 208 } |
196 | 209 |
197 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) | 210 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) |
198 : m_cancelled(0) | 211 : m_cancelled(0) |
199 { | 212 { |
200 // Creating the WeakResolver may return nullptr if active dom objects have | 213 ASSERT(scriptState->contextIsValid()); |
201 // been stopped. And in the process set m_cancelled to 1. | 214 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) { |
202 m_resolver = WeakResolver::create(scriptState, this); | 215 // If active dom objects have been stopped, avoid creating |
| 216 // CryptoResultResolver. |
| 217 m_resolver = nullptr; |
| 218 } else { |
| 219 m_resolver = Resolver::create(scriptState, this).get(); |
| 220 } |
203 } | 221 } |
204 | 222 |
205 ScriptPromise CryptoResultImpl::promise() | 223 ScriptPromise CryptoResultImpl::promise() |
206 { | 224 { |
207 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 225 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
208 } | 226 } |
209 | 227 |
210 } // namespace blink | 228 } // namespace blink |
OLD | NEW |