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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 | 81 |
82 } // namespace | 82 } // namespace |
83 | 83 |
84 // The PromiseState class contains all the state which is tied to an | 84 // The PromiseState class contains all the state which is tied to an |
85 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, | 85 // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, |
86 // PromiseState is not thread safe and must only be accessed and deleted from | 86 // PromiseState is not thread safe and must only be accessed and deleted from |
87 // the blink thread. | 87 // the blink thread. |
88 // | 88 // |
89 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat e. | 89 // This is achieved by making CryptoResultImpl hold a WeakPtr to the PromiseStat e. |
90 // The PromiseState deletes itself after being notified of completion. | 90 // The PromiseState deletes itself after being notified of completion. |
91 // Additionally the PromiseState deletes itself when the ExecutionContext is | 91 // Additionally the PromiseState is deleted when the ExecutionContext is |
92 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, | 92 // destroyed (necessary to avoid leaks when dealing with WebWorker threads, |
93 // which may die before the operation is completed). | 93 // which may die before the operation is completed). |
94 class CryptoResultImpl::PromiseState FINAL : public ContextLifecycleObserver { | 94 class CryptoResultImpl::PromiseState FINAL { |
95 public: | 95 public: |
96 static WeakPtr<PromiseState> create(ExecutionContext* context) | 96 static WeakPtr<PromiseState> create(ExecutionContext* context) |
97 { | 97 { |
98 PromiseState* promiseState = new PromiseState(context); | 98 PromiseState* promiseState = new PromiseState(context); |
99 return promiseState->m_weakFactory.createWeakPtr(); | 99 return promiseState->m_weakFactory.createWeakPtr(); |
100 } | 100 } |
101 | 101 |
102 // Override from ContextLifecycleObserver | 102 void contextDestroyed() |
103 virtual void contextDestroyed() OVERRIDE | |
104 { | 103 { |
105 ContextLifecycleObserver::contextDestroyed(); | |
106 delete this; | 104 delete this; |
107 } | 105 } |
108 | 106 |
109 ScriptPromise promise() | 107 ScriptPromise promise() |
110 { | 108 { |
111 return m_promiseResolver->promise(); | 109 return m_promiseResolver->promise(); |
112 } | 110 } |
113 | 111 |
114 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web String& errorDetails) | 112 void completeWithError(blink::WebCryptoErrorType errorType, const blink::Web String& errorDetails) |
115 { | 113 { |
(...skipping 19 matching lines...) Expand all Loading... | |
135 delete this; | 133 delete this; |
136 } | 134 } |
137 | 135 |
138 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink:: WebCryptoKey& privateKey) | 136 void completeWithKeyPair(const blink::WebCryptoKey& publicKey, const blink:: WebCryptoKey& privateKey) |
139 { | 137 { |
140 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); | 138 m_promiseResolver->resolve(KeyPair::create(publicKey, privateKey)); |
141 delete this; | 139 delete this; |
142 } | 140 } |
143 | 141 |
144 private: | 142 private: |
143 // This subclass of ScriptPromiseResolverWithContext is to be notified | |
144 // when the context was destroyed. | |
145 class PromiseResolver FINAL : public ScriptPromiseResolverWithContext { | |
146 public: | |
147 static PassRefPtr<PromiseResolver> create(ScriptState* scriptState, Prom iseState* promiseState) | |
148 { | |
149 RefPtr<PromiseResolver> resolver = adoptRef(new PromiseResolver(scri ptState, promiseState)); | |
150 resolver->suspendIfNeeded(); | |
151 return resolver.release(); | |
152 } | |
153 | |
154 virtual void contextDestroyed() OVERRIDE | |
haraken
2014/05/06 01:44:02
How about adding the functionality to ScriptPromis
eroman
2014/05/06 22:59:22
Sure what do you have in mind? Perhaps pass in a d
| |
155 { | |
156 ScriptPromiseResolverWithContext::contextDestroyed(); | |
157 m_promiseState->contextDestroyed(); | |
158 } | |
159 | |
160 private: | |
161 explicit PromiseResolver(ScriptState* scriptState, PromiseState* promise State) | |
162 : ScriptPromiseResolverWithContext(scriptState) | |
163 , m_promiseState(promiseState) | |
164 { | |
165 } | |
166 | |
167 PromiseState* m_promiseState; | |
168 }; | |
169 | |
145 explicit PromiseState(ExecutionContext* context) | 170 explicit PromiseState(ExecutionContext* context) |
146 : ContextLifecycleObserver(context) | 171 : m_weakFactory(this) |
147 , m_weakFactory(this) | 172 , m_promiseResolver(PromiseResolver::create(ScriptState::current(toIsola te(context)), this)) |
148 , m_promiseResolver(ScriptPromiseResolverWithContext::create(ScriptState ::current(toIsolate(context)))) | |
149 { | 173 { |
150 } | 174 } |
151 | 175 |
152 WeakPtrFactory<PromiseState> m_weakFactory; | 176 WeakPtrFactory<PromiseState> m_weakFactory; |
153 RefPtr<ScriptPromiseResolverWithContext> m_promiseResolver; | 177 RefPtr<PromiseResolver> m_promiseResolver; |
154 }; | 178 }; |
155 | 179 |
156 CryptoResultImpl::~CryptoResultImpl() | 180 CryptoResultImpl::~CryptoResultImpl() |
157 { | 181 { |
158 } | 182 } |
159 | 183 |
160 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create() | 184 PassRefPtr<CryptoResultImpl> CryptoResultImpl::create() |
161 { | 185 { |
162 return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::Ge tCurrent()))); | 186 return adoptRef(new CryptoResultImpl(callingExecutionContext(v8::Isolate::Ge tCurrent()))); |
163 } | 187 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 : m_promiseState(PromiseState::create(context)) | 220 : m_promiseState(PromiseState::create(context)) |
197 { | 221 { |
198 } | 222 } |
199 | 223 |
200 ScriptPromise CryptoResultImpl::promise() | 224 ScriptPromise CryptoResultImpl::promise() |
201 { | 225 { |
202 return m_promiseState->promise(); | 226 return m_promiseState->promise(); |
203 } | 227 } |
204 | 228 |
205 } // namespace WebCore | 229 } // namespace WebCore |
OLD | NEW |