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

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: WIP: 1st trial 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
« 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 #include "core/dom/ExecutionContext.h" 43 #include "core/dom/ExecutionContext.h"
44 #include "modules/crypto/CryptoKey.h" 44 #include "modules/crypto/CryptoKey.h"
45 #include "modules/crypto/NormalizeAlgorithm.h" 45 #include "modules/crypto/NormalizeAlgorithm.h"
46 #include "public/platform/Platform.h" 46 #include "public/platform/Platform.h"
47 #include "public/platform/WebCryptoAlgorithm.h" 47 #include "public/platform/WebCryptoAlgorithm.h"
48 48
49 namespace blink { 49 namespace blink {
50 50
51 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver { 51 class CryptoResultImpl::WeakResolver : public ScriptPromiseResolver {
52 public: 52 public:
53 static WeakPtr<ScriptPromiseResolver> create(ScriptState* scriptState, Crypt oResultImpl* result) 53 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr iptState, CryptoResultImpl* result)
54 { 54 {
55 RefPtr<WeakResolver> p = adoptRef(new WeakResolver(scriptState, result)) ; 55 RefPtrWillBeRawPtr<WeakResolver> p = adoptRefWillBeNoop(new WeakResolver (scriptState, result));
56 p->suspendIfNeeded(); 56 p->suspendIfNeeded();
57 p->keepAliveWhilePending(); 57 p->keepAliveWhilePending();
58 return p->m_weakPtrFactory.createWeakPtr(); 58 return p.release();
59 } 59 }
60 60
61 virtual ~WeakResolver() 61 virtual void stop() override
62 { 62 {
63 m_result->cancel(); 63 m_result->cancel();
64 m_result->clearResolver();
65 ScriptPromiseResolver::stop();
66 }
67
68
69 virtual void trace(Visitor* visitor)
70 {
71 ScriptPromiseResolver::trace(visitor);
64 } 72 }
65 73
66 private: 74 private:
67 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result) 75 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result)
68 : ScriptPromiseResolver(scriptState) 76 : ScriptPromiseResolver(scriptState)
69 , m_weakPtrFactory(this)
70 , m_result(result) { } 77 , m_result(result) { }
71 WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory; 78 RawPtr<CryptoResultImpl> m_result;
tasak 2014/12/09 10:38:28 Since CryptResultImpl has a persistent member: m_r
72 RefPtr<CryptoResultImpl> m_result;
73 }; 79 };
74 80
75 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) 81 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
76 { 82 {
77 switch (errorType) { 83 switch (errorType) {
78 case WebCryptoErrorTypeNotSupported: 84 case WebCryptoErrorTypeNotSupported:
79 return NotSupportedError; 85 return NotSupportedError;
80 case WebCryptoErrorTypeSyntax: 86 case WebCryptoErrorTypeSyntax:
81 return SyntaxError; 87 return SyntaxError;
82 case WebCryptoErrorTypeInvalidState: 88 case WebCryptoErrorTypeInvalidState:
(...skipping 12 matching lines...) Expand all
95 // revisited. 101 // revisited.
96 return DataError; 102 return DataError;
97 } 103 }
98 104
99 ASSERT_NOT_REACHED(); 105 ASSERT_NOT_REACHED();
100 return 0; 106 return 0;
101 } 107 }
102 108
103 CryptoResultImpl::~CryptoResultImpl() 109 CryptoResultImpl::~CryptoResultImpl()
104 { 110 {
111 #if ENABLE(OILPAN)
112 ASSERT(!m_resolver);
113 #endif
105 } 114 }
106 115
107 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) 116 void CryptoResultImpl::clearResolver()
108 { 117 {
109 return adoptRef(new CryptoResultImpl(scriptState)); 118 #if ENABLE(OILPAN)
119 m_resolver.clear();
120 #else
121 m_resolver = nullptr;
122 #endif
123 }
124
125 ScriptPromiseResolver* CryptoResultImpl::resolver()
126 {
127 #if ENABLE(OILPAN)
128 return m_resolver ? m_resolver.get()->get() : nullptr;
129 #else
130 return m_resolver.get();
131 #endif
132 }
133
134 PassRefPtrWillBeRawPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* s criptState)
135 {
136 return adoptRefWillBeNoop(new CryptoResultImpl(scriptState));
110 } 137 }
111 138
112 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 139 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
113 { 140 {
114 if (m_resolver) 141 if (resolver())
115 m_resolver->reject(DOMException::create(webCryptoErrorToExceptionCode(er rorType), errorDetails)); 142 resolver()->reject(DOMException::create(webCryptoErrorToExceptionCode(er rorType), errorDetails));
143 clearResolver();
116 } 144 }
117 145
118 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 146 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
119 { 147 {
120 if (m_resolver) 148 if (resolver())
121 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 149 resolver()->resolve(DOMArrayBuffer::create(bytes, bytesSize));
150 clearResolver();
122 } 151 }
123 152
124 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 153 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
125 { 154 {
126 if (m_resolver) { 155 if (resolver()) {
127 ScriptPromiseResolver* resolver = m_resolver.get(); 156 ScriptPromiseResolver* scriptPromiseResolver = resolver();
128 ScriptState* scriptState = resolver->scriptState(); 157 ScriptState* scriptState = scriptPromiseResolver->scriptState();
129 ScriptState::Scope scope(scriptState); 158 ScriptState::Scope scope(scriptState);
130 159
131 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length); 160 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length);
132 161
133 v8::TryCatch exceptionCatcher; 162 v8::TryCatch exceptionCatcher;
134 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); 163 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
135 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { 164 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
136 ASSERT_NOT_REACHED(); 165 ASSERT_NOT_REACHED();
137 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object")); 166 scriptPromiseResolver->reject(DOMException::create(OperationError, " Failed inflating JWK JSON to object"));
138 } else { 167 } else {
139 resolver->resolve(jsonDictionary); 168 scriptPromiseResolver->resolve(jsonDictionary);
140 } 169 }
141 } 170 }
171 clearResolver();
142 } 172 }
143 173
144 void CryptoResultImpl::completeWithBoolean(bool b) 174 void CryptoResultImpl::completeWithBoolean(bool b)
145 { 175 {
146 if (m_resolver) 176 if (resolver())
147 m_resolver->resolve(b); 177 resolver()->resolve(b);
178 clearResolver();
148 } 179 }
149 180
150 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 181 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
151 { 182 {
152 if (m_resolver) 183 if (resolver())
153 m_resolver->resolve(CryptoKey::create(key)); 184 resolver()->resolve(CryptoKey::create(key));
185 clearResolver();
154 } 186 }
155 187
156 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 188 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
157 { 189 {
158 if (m_resolver) { 190 if (resolver()) {
159 ScriptState* scriptState = m_resolver->scriptState(); 191 ScriptState* scriptState = resolver()->scriptState();
160 ScriptState::Scope scope(scriptState); 192 ScriptState::Scope scope(scriptState);
161 193
162 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); 194 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate());
163 195
164 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate()); 196 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate());
165 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate()); 197 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate());
166 198
167 keyPair.set("publicKey", publicKeyValue); 199 keyPair.set("publicKey", publicKeyValue);
168 keyPair.set("privateKey", privateKeyValue); 200 keyPair.set("privateKey", privateKeyValue);
169 201
170 m_resolver->resolve(keyPair.v8Value()); 202 resolver()->resolve(keyPair.v8Value());
171 } 203 }
204 clearResolver();
172 } 205 }
173 206
174 bool CryptoResultImpl::cancelled() const 207 bool CryptoResultImpl::cancelled() const
175 { 208 {
176 return acquireLoad(&m_cancelled); 209 return acquireLoad(&m_cancelled);
177 } 210 }
178 211
179 void CryptoResultImpl::cancel() 212 void CryptoResultImpl::cancel()
180 { 213 {
181 releaseStore(&m_cancelled, 1); 214 releaseStore(&m_cancelled, 1);
182 } 215 }
183 216
184 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 217 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
185 : m_cancelled(0) 218 : m_cancelled(0)
186 { 219 {
187 // Creating the WeakResolver may return nullptr if active dom objects have 220 // Creating the WeakResolver may return nullptr if active dom objects have
188 // been stopped. And in the process set m_cancelled to 1. 221 // been stopped. And in the process set m_cancelled to 1.
189 m_resolver = WeakResolver::create(scriptState, this); 222 #if ENABLE(OILPAN)
223 if (!m_resolver)
224 m_resolver = adoptPtr(new Persistent<ScriptPromiseResolver>());
225 *m_resolver = Persistent<ScriptPromiseResolver>(WeakResolver::create(scriptS tate, this));
226 #else
227 m_resolver = WeakResolver::create(scriptState, this).get();
228 #endif
190 } 229 }
191 230
192 ScriptPromise CryptoResultImpl::promise() 231 ScriptPromise CryptoResultImpl::promise()
193 { 232 {
194 return m_resolver ? m_resolver->promise() : ScriptPromise(); 233 if (ScriptPromiseResolver* scriptPromiseResolver = resolver())
234 return scriptPromiseResolver->promise();
235 return ScriptPromise();
195 } 236 }
196 237
197 } // namespace blink 238 } // namespace blink
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