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

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

Issue 780793002: Make CryptoResultImpl not to use WeakPtr. (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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 if (m_result) {
76 m_result->cancel();
77 m_result->clearResolver();
78 m_result = nullptr;
79 }
80 ScriptPromiseResolver::stop();
81 }
82
83 void clearCryptoResult()
84 {
85 m_result = nullptr;
76 } 86 }
77 87
78 private: 88 private:
79 WeakResolver(ScriptState* scriptState, CryptoResultImpl* result) 89 Resolver(ScriptState* scriptState, CryptoResultImpl* result)
80 : ScriptPromiseResolver(scriptState) 90 : ScriptPromiseResolver(scriptState)
81 , m_weakPtrFactory(this)
82 , m_result(result) { } 91 , m_result(result) { }
83 WeakPtrFactory<ScriptPromiseResolver> m_weakPtrFactory; 92 CryptoResultImpl* m_result;
84 RefPtr<CryptoResultImpl> m_result;
85 }; 93 };
86 94
87 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType) 95 ExceptionCode webCryptoErrorToExceptionCode(WebCryptoErrorType errorType)
88 { 96 {
89 switch (errorType) { 97 switch (errorType) {
90 case WebCryptoErrorTypeNotSupported: 98 case WebCryptoErrorTypeNotSupported:
91 return NotSupportedError; 99 return NotSupportedError;
92 case WebCryptoErrorTypeSyntax: 100 case WebCryptoErrorTypeSyntax:
93 return SyntaxError; 101 return SyntaxError;
94 case WebCryptoErrorTypeInvalidState: 102 case WebCryptoErrorTypeInvalidState:
95 return InvalidStateError; 103 return InvalidStateError;
96 case WebCryptoErrorTypeInvalidAccess: 104 case WebCryptoErrorTypeInvalidAccess:
97 return InvalidAccessError; 105 return InvalidAccessError;
98 case WebCryptoErrorTypeUnknown: 106 case WebCryptoErrorTypeUnknown:
99 return UnknownError; 107 return UnknownError;
100 case WebCryptoErrorTypeData: 108 case WebCryptoErrorTypeData:
101 return DataError; 109 return DataError;
102 case WebCryptoErrorTypeOperation: 110 case WebCryptoErrorTypeOperation:
103 return OperationError; 111 return OperationError;
104 case WebCryptoErrorTypeType: 112 case WebCryptoErrorTypeType:
105 return V8TypeError; 113 return V8TypeError;
106 } 114 }
107 115
108 ASSERT_NOT_REACHED(); 116 ASSERT_NOT_REACHED();
109 return 0; 117 return 0;
110 } 118 }
111 119
112 CryptoResultImpl::~CryptoResultImpl() 120 CryptoResultImpl::~CryptoResultImpl()
113 { 121 {
haraken 2014/12/12 08:49:31 Add ASSERT(!m_resolver) to verify that m_resolver
tasak 2014/12/15 05:26:46 Done.
114 } 122 }
115 123
124 void CryptoResultImpl::clearResolver()
125 {
126 if (m_resolver)
127 static_cast<CryptoResultImpl::Resolver*>(m_resolver)->clearCryptoResult( );
haraken 2014/12/15 01:36:51 I don't think this is needed once you make CryptoR
tasak 2014/12/15 05:26:46 Done.
128 m_resolver = nullptr;
129 }
130
116 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState) 131 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create(ScriptState* scriptState)
117 { 132 {
118 return adoptRef(new CryptoResultImpl(scriptState)); 133 return adoptRef(new CryptoResultImpl(scriptState));
119 } 134 }
120 135
121 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails) 136 void CryptoResultImpl::completeWithError(WebCryptoErrorType errorType, const Web String& errorDetails)
122 { 137 {
123 if (m_resolver) { 138 if (m_resolver) {
124 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType); 139 ExceptionCode ec = webCryptoErrorToExceptionCode(errorType);
125 140
126 // Handle TypeError separately, as it cannot be created using 141 // Handle TypeError separately, as it cannot be created using
127 // DOMException. 142 // DOMException.
128 if (ec == V8TypeError) 143 if (ec == V8TypeError)
129 rejectWithTypeError(errorDetails, m_resolver.get()); 144 rejectWithTypeError(errorDetails, m_resolver);
130 else 145 else
131 m_resolver->reject(DOMException::create(ec, errorDetails)); 146 m_resolver->reject(DOMException::create(ec, errorDetails));
132 } 147 }
148 clearResolver();
133 } 149 }
134 150
135 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize) 151 void CryptoResultImpl::completeWithBuffer(const void* bytes, unsigned bytesSize)
136 { 152 {
137 if (m_resolver) 153 if (m_resolver)
138 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize)); 154 m_resolver->resolve(DOMArrayBuffer::create(bytes, bytesSize));
155 clearResolver();
139 } 156 }
140 157
141 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length) 158 void CryptoResultImpl::completeWithJson(const char* utf8Data, unsigned length)
142 { 159 {
143 if (m_resolver) { 160 if (m_resolver) {
144 ScriptPromiseResolver* resolver = m_resolver.get(); 161 ScriptPromiseResolver* resolver = m_resolver;
145 ScriptState* scriptState = resolver->scriptState(); 162 ScriptState* scriptState = resolver->scriptState();
146 ScriptState::Scope scope(scriptState); 163 ScriptState::Scope scope(scriptState);
147 164
148 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length); 165 v8::Handle<v8::String> jsonString = v8::String::NewFromUtf8(scriptState- >isolate(), utf8Data, v8::String::kInternalizedString, length);
149 166
150 v8::TryCatch exceptionCatcher; 167 v8::TryCatch exceptionCatcher;
151 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString); 168 v8::Handle<v8::Value> jsonDictionary = v8::JSON::Parse(jsonString);
152 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) { 169 if (exceptionCatcher.HasCaught() || jsonDictionary.IsEmpty()) {
153 ASSERT_NOT_REACHED(); 170 ASSERT_NOT_REACHED();
154 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object")); 171 resolver->reject(DOMException::create(OperationError, "Failed inflat ing JWK JSON to object"));
155 } else { 172 } else {
156 resolver->resolve(jsonDictionary); 173 resolver->resolve(jsonDictionary);
157 } 174 }
158 } 175 }
176 clearResolver();
159 } 177 }
160 178
161 void CryptoResultImpl::completeWithBoolean(bool b) 179 void CryptoResultImpl::completeWithBoolean(bool b)
162 { 180 {
163 if (m_resolver) 181 if (m_resolver)
164 m_resolver->resolve(b); 182 m_resolver->resolve(b);
183 clearResolver();
165 } 184 }
166 185
167 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key) 186 void CryptoResultImpl::completeWithKey(const WebCryptoKey& key)
168 { 187 {
169 if (m_resolver) 188 if (m_resolver)
170 m_resolver->resolve(CryptoKey::create(key)); 189 m_resolver->resolve(CryptoKey::create(key));
190 clearResolver();
171 } 191 }
172 192
173 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey) 193 void CryptoResultImpl::completeWithKeyPair(const WebCryptoKey& publicKey, const WebCryptoKey& privateKey)
174 { 194 {
175 if (m_resolver) { 195 if (m_resolver) {
176 ScriptState* scriptState = m_resolver->scriptState(); 196 ScriptState* scriptState = m_resolver->scriptState();
177 ScriptState::Scope scope(scriptState); 197 ScriptState::Scope scope(scriptState);
178 198
179 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate()); 199 Dictionary keyPair = Dictionary::createEmpty(scriptState->isolate());
180 200
181 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate()); 201 v8::Handle<v8::Value> publicKeyValue = toV8(CryptoKey::create(publicKey) , scriptState->context()->Global(), scriptState->isolate());
182 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate()); 202 v8::Handle<v8::Value> privateKeyValue = toV8(CryptoKey::create(privateKe y), scriptState->context()->Global(), scriptState->isolate());
183 203
184 keyPair.set("publicKey", publicKeyValue); 204 keyPair.set("publicKey", publicKeyValue);
185 keyPair.set("privateKey", privateKeyValue); 205 keyPair.set("privateKey", privateKeyValue);
186 206
187 m_resolver->resolve(keyPair.v8Value()); 207 m_resolver->resolve(keyPair.v8Value());
188 } 208 }
209 clearResolver();
189 } 210 }
190 211
191 bool CryptoResultImpl::cancelled() const 212 bool CryptoResultImpl::cancelled() const
192 { 213 {
193 return acquireLoad(&m_cancelled); 214 return acquireLoad(&m_cancelled);
194 } 215 }
195 216
196 void CryptoResultImpl::cancel() 217 void CryptoResultImpl::cancel()
197 { 218 {
198 releaseStore(&m_cancelled, 1); 219 releaseStore(&m_cancelled, 1);
199 } 220 }
200 221
201 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState) 222 CryptoResultImpl::CryptoResultImpl(ScriptState* scriptState)
202 : m_cancelled(0) 223 : m_cancelled(0)
203 { 224 {
204 // Creating the WeakResolver may return nullptr if active dom objects have 225 ASSERT(scriptState);
haraken 2014/12/12 08:49:31 ASSERT(scriptState) => ASSERT(scriptState->context
tasak 2014/12/15 05:26:46 Done.
205 // been stopped. And in the process set m_cancelled to 1. 226 if (scriptState->executionContext()->activeDOMObjectsAreStopped()) {
206 m_resolver = WeakResolver::create(scriptState, this); 227 // If active dom objects have been stopped, avoid creating
228 // CryptoResultResolver.
229 m_resolver = nullptr;
230 } else {
231 m_resolver = Resolver::create(scriptState, this).get();
232 }
207 } 233 }
208 234
209 ScriptPromise CryptoResultImpl::promise() 235 ScriptPromise CryptoResultImpl::promise()
210 { 236 {
211 return m_resolver ? m_resolver->promise() : ScriptPromise(); 237 return m_resolver ? m_resolver->promise() : ScriptPromise();
212 } 238 }
213 239
214 } // namespace blink 240 } // namespace blink
OLDNEW
« Source/modules/crypto/CryptoResultImpl.h ('K') | « Source/modules/crypto/CryptoResultImpl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698