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

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

Issue 783423003: Make ScriptPromiseResolver RefCountedWillBeRefCountedGarbageCollected. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years 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
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::Resolver final : public ScriptPromiseResolver { 63 class CryptoResultImpl::Resolver final : public ScriptPromiseResolver {
64 public: 64 public:
65 static PassRefPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Cr yptoResultImpl* result) 65 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr iptState, CryptoResultImpl* result)
66 { 66 {
67 RefPtr<Resolver> resolver = adoptRef(new Resolver(scriptState, result)); 67 RefPtrWillBeRawPtr<Resolver> resolver = adoptRefWillBeNoop(new Resolver( scriptState, result));
68 resolver->suspendIfNeeded(); 68 resolver->suspendIfNeeded();
69 resolver->keepAliveWhilePending(); 69 resolver->keepAliveWhilePending();
70 return resolver.release(); 70 return resolver.release();
71 } 71 }
72 72
73 virtual void stop() override 73 virtual void stop() override
74 { 74 {
75 m_result->cancel(); 75 m_result->cancel();
76 m_result->clearResolver(); 76 m_result->clearResolver();
77 m_result = nullptr; 77 m_result = nullptr;
78 ScriptPromiseResolver::stop(); 78 ScriptPromiseResolver::stop();
79 } 79 }
80 80
81 virtual void trace(Visitor* visitor)
haraken 2014/12/16 09:43:29 Add override. The same comment for other overridd
tasak 2014/12/16 11:54:28 Done.
82 {
83 ScriptPromiseResolver::trace(visitor);
84 }
85
81 private: 86 private:
82 Resolver(ScriptState* scriptState, CryptoResultImpl* result) 87 Resolver(ScriptState* scriptState, CryptoResultImpl* result)
83 : ScriptPromiseResolver(scriptState) 88 : ScriptPromiseResolver(scriptState)
84 , m_result(result) { } 89 , m_result(result) { }
85 RefPtr<CryptoResultImpl> m_result; 90 RefPtr<CryptoResultImpl> m_result;
86 }; 91 };
87 92
88 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) 93 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
89 { 94 {
90 switch (errorType) { 95 switch (errorType) {
(...skipping 15 matching lines...) Expand all
106 return 0; 111 return 0;
107 } 112 }
108 113
109 CryptoResultImpl::~CryptoResultImpl() 114 CryptoResultImpl::~CryptoResultImpl()
110 { 115 {
111 ASSERT(!m_resolver); 116 ASSERT(!m_resolver);
112 } 117 }
113 118
114 void CryptoResultImpl::clearResolver() 119 void CryptoResultImpl::clearResolver()
115 { 120 {
121 #if ENABLE(OILPAN)
122 m_resolver.clear();
123 #else
116 m_resolver = nullptr; 124 m_resolver = nullptr;
125 #endif
117 } 126 }
118 127
119 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) 128 ScriptPromiseResolver* CryptoResultImpl::resolver()
120 { 129 {
121 return adoptRef(new CryptoResultImpl(scriptState)); 130 #if ENABLE(OILPAN)
131 return m_resolver ? m_resolver.get()->get() : nullptr;
132 #else
133 return m_resolver;
134 #endif
135 }
136
137 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState)
138 {
139 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState));
122 } 140 }
123 141
124 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 142 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
125 { 143 {
126 if (m_resolver) { 144 if (resolver()) {
145 ScriptPromiseResolver* scriptPromiseResolver = resolver();
127 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); 146 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType);
128 147
129 // Handle TypeError separately, as it cannot be created using 148 // Handle TypeError separately, as it cannot be created using
130 // DOMException. 149 // DOMException.
131 if (ec == V8TypeError) 150 if (ec == V8TypeError)
132 rejectWithTypeError(errorDetails, m_resolver); 151 rejectWithTypeError(errorDetails, scriptPromiseResolver);
133 else 152 else
134 m_resolver->reject(DOMException::create(ec, errorDetails)); 153 scriptPromiseResolver->reject(DOMException::create(ec, errorDetails) );
135 } 154 }
136 clearResolver(); 155 clearResolver();
137 } 156 }
138 157
139 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 158 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
140 { 159 {
141 if (m_resolver) 160 if (resolver())
142 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 161 resolver()->resolve(DOMArrayBuffer::create(bytes, bytesSize));
143 clearResolver(); 162 clearResolver();
144 } 163 }
145 164
146 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 165 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
147 { 166 {
148 if (m_resolver) { 167 if (resolver()) {
149 ScriptPromiseResolver* resolver = m_resolver; 168 ScriptPromiseResolver* scriptPromiseResolver = resolver();
150 ScriptState* scriptState = resolver->scriptState(); 169 ScriptState* scriptState = scriptPromiseResolver->scriptState();
151 ScriptState::Scope scope(scriptState); 170 ScriptState::Scope scope(scriptState);
152 171
153 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length); 172 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length);
154 173
155 v8::TryCatch exceptionCatcher; 174 v8::TryCatch exceptionCatcher;
156 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); 175 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
157 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { 176 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
158 ASSERT_NOT_REACHED(); 177 ASSERT_NOT_REACHED();
159 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object")); 178 scriptPromiseResolver->reject(DOMException::create(OperationError, " Failed inflating JWK JSON to object"));
160 } else { 179 } else {
161 resolver->resolve(jsonDictionary); 180 scriptPromiseResolver->resolve(jsonDictionary);
162 } 181 }
163 } 182 }
164 clearResolver(); 183 clearResolver();
165 } 184 }
166 185
167 void CryptoResultImpl::completeWithBoolean(bool b) 186 void CryptoResultImpl::completeWithBoolean(bool b)
168 { 187 {
169 if (m_resolver) 188 if (resolver())
170 m_resolver->resolve(b); 189 resolver()->resolve(b);
171 clearResolver(); 190 clearResolver();
172 } 191 }
173 192
174 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 193 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
175 { 194 {
176 if (m_resolver) 195 if (resolver())
177 m_resolver->resolve(CryptoKey::create(key)); 196 resolver()->resolve(CryptoKey::create(key));
178 clearResolver(); 197 clearResolver();
179 } 198 }
180 199
181 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 200 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
182 { 201 {
183 if (m_resolver) { 202 if (resolver()) {
184 ScriptState* scriptState = m_resolver->scriptState(); 203 ScriptState* scriptState = resolver()->scriptState();
185 ScriptState::Scope scope(scriptState); 204 ScriptState::Scope scope(scriptState);
186 205
187 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); 206 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate());
188 207
189 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate()); 208 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate());
190 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate()); 209 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate());
191 210
192 keyPair.set("publicKey", publicKeyValue); 211 keyPair.set("publicKey", publicKeyValue);
193 keyPair.set("privateKey", privateKeyValue); 212 keyPair.set("privateKey", privateKeyValue);
194 213
195 m_resolver->resolve(keyPair.v8Value()); 214 resolver()->resolve(keyPair.v8Value());
196 } 215 }
197 clearResolver(); 216 clearResolver();
198 } 217 }
199 218
200 bool CryptoResultImpl::cancelled() const 219 bool CryptoResultImpl::cancelled() const
201 { 220 {
202 return acquireLoad(&m_cancelled); 221 return acquireLoad(&m_cancelled);
203 } 222 }
204 223
205 void CryptoResultImpl::cancel() 224 void CryptoResultImpl::cancel()
206 { 225 {
207 releaseStore(&m_cancelled, 1); 226 releaseStore(&m_cancelled, 1);
208 } 227 }
209 228
210 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 229 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
211 : m_cancelled(0) 230 : m_cancelled(0)
212 { 231 {
213 ASSERT(scriptState->contextIsValid()); 232 ASSERT(scriptState->contextIsValid());
214 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) { 233 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
215 // If active dom objects have been stopped, avoid creating 234 // If active dom objects have been stopped, avoid creating
216 // CryptoResultResolver. 235 // CryptoResultResolver.
217 m_resolver = nullptr; 236 m_resolver = nullptr;
218 } else { 237 } else {
238 #if ENABLE(OILPAN)
239 if (!m_resolver)
240 m_resolver = adoptPtr(new Persistent<ScriptPromiseResolver>());
241 *m_resolver = Persistent<ScriptPromiseResolver>(Resolver::create(scriptS tate, this));
242 #else
219 m_resolver = Resolver::create(scriptState, this).get(); 243 m_resolver = Resolver::create(scriptState, this).get();
244 #endif
220 } 245 }
221 } 246 }
222 247
223 ScriptPromise CryptoResultImpl::promise() 248 ScriptPromise CryptoResultImpl::promise()
224 { 249 {
225 return m_resolver ? m_resolver->promise() : ScriptPromise(); 250 if (ScriptPromiseResolver* scriptPromiseResolver = resolver())
251 return scriptPromiseResolver->promise();
252 return ScriptPromise();
226 } 253 }
227 254
228 } // namespace blink 255 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698