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

Side by Side Diff: Source/core/workers/WorkerScriptLoader.cpp

Issue 1213443006: Invoke WorkerScriptLoader's m_finishedCallback callback safely. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: review #7 and notify* Created 5 years, 5 months 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 | Annotate | Revision Log
« no previous file with comments | « Source/core/workers/WorkerScriptLoader.h ('k') | Source/web/WebEmbeddedWorkerImpl.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) 2009 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved. 3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 29 matching lines...) Expand all
40 #include "wtf/RefPtr.h" 40 #include "wtf/RefPtr.h"
41 41
42 namespace blink { 42 namespace blink {
43 43
44 WorkerScriptLoader::WorkerScriptLoader() 44 WorkerScriptLoader::WorkerScriptLoader()
45 : m_responseCallback(nullptr) 45 : m_responseCallback(nullptr)
46 , m_finishedCallback(nullptr) 46 , m_finishedCallback(nullptr)
47 , m_failed(false) 47 , m_failed(false)
48 , m_identifier(0) 48 , m_identifier(0)
49 , m_appCacheID(0) 49 , m_appCacheID(0)
50 , m_finishing(false)
51 , m_requestContext(WebURLRequest::RequestContextWorker) 50 , m_requestContext(WebURLRequest::RequestContextWorker)
52 { 51 {
53 } 52 }
54 53
55 WorkerScriptLoader::~WorkerScriptLoader() 54 WorkerScriptLoader::~WorkerScriptLoader()
56 { 55 {
57 } 56 }
58 57
59 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy) 58 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
60 { 59 {
(...skipping 27 matching lines...) Expand all
88 if (!request) 87 if (!request)
89 return; 88 return;
90 89
91 ThreadableLoaderOptions options; 90 ThreadableLoaderOptions options;
92 options.crossOriginRequestPolicy = crossOriginRequestPolicy; 91 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
93 92
94 ResourceLoaderOptions resourceLoaderOptions; 93 ResourceLoaderOptions resourceLoaderOptions;
95 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; 94 resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
96 95
97 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque st, options, resourceLoaderOptions); 96 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque st, options, resourceLoaderOptions);
97 if (m_failed)
98 notifyFinished();
99 // Do nothing here since notifyFinished() could delete |this|.
98 } 100 }
99 101
100 const KURL& WorkerScriptLoader::responseURL() const 102 const KURL& WorkerScriptLoader::responseURL() const
101 { 103 {
102 ASSERT(!failed()); 104 ASSERT(!failed());
103 return m_responseURL; 105 return m_responseURL;
104 } 106 }
105 107
106 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest() 108 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
107 { 109 {
108 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url)); 110 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
109 request->setHTTPMethod("GET"); 111 request->setHTTPMethod("GET");
110 request->setRequestContext(m_requestContext); 112 request->setRequestContext(m_requestContext);
111 return request.release(); 113 return request.release();
112 } 114 }
113 115
114 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle) 116 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
115 { 117 {
116 ASSERT_UNUSED(handle, !handle); 118 ASSERT_UNUSED(handle, !handle);
117 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { 119 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
118 m_failed = true; 120 notifyError();
119 return; 121 return;
120 } 122 }
121 m_identifier = identifier; 123 m_identifier = identifier;
122 m_responseURL = response.url(); 124 m_responseURL = response.url();
123 m_responseEncoding = response.textEncodingName(); 125 m_responseEncoding = response.textEncodingName();
124 m_appCacheID = response.appCacheID(); 126 m_appCacheID = response.appCacheID();
125 processContentSecurityPolicy(response); 127 processContentSecurityPolicy(response);
126 if (m_responseCallback) 128 if (m_responseCallback)
127 (*m_responseCallback)(); 129 (*m_responseCallback)();
128 } 130 }
(...skipping 17 matching lines...) Expand all
146 } 148 }
147 149
148 void WorkerScriptLoader::didReceiveCachedMetadata(const char* data, int size) 150 void WorkerScriptLoader::didReceiveCachedMetadata(const char* data, int size)
149 { 151 {
150 m_cachedMetadata = adoptPtr(new Vector<char>(size)); 152 m_cachedMetadata = adoptPtr(new Vector<char>(size));
151 memcpy(m_cachedMetadata->data(), data, size); 153 memcpy(m_cachedMetadata->data(), data, size);
152 } 154 }
153 155
154 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) 156 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
155 { 157 {
156 if (m_failed) { 158 if (!m_failed && m_decoder)
157 notifyError();
158 return;
159 }
160
161 if (m_decoder)
162 m_script.append(m_decoder->flush()); 159 m_script.append(m_decoder->flush());
163 160
164 if (m_finishedCallback) 161 notifyFinished();
165 (*m_finishedCallback)();
166 } 162 }
167 163
168 void WorkerScriptLoader::didFail(const ResourceError&) 164 void WorkerScriptLoader::didFail(const ResourceError&)
169 { 165 {
170 notifyError(); 166 notifyError();
171 } 167 }
172 168
173 void WorkerScriptLoader::didFailRedirectCheck() 169 void WorkerScriptLoader::didFailRedirectCheck()
174 { 170 {
175 notifyError(); 171 notifyError();
176 } 172 }
177 173
178 void WorkerScriptLoader::notifyError()
179 {
180 m_failed = true;
181 notifyFinished();
182 }
183
184 void WorkerScriptLoader::cancel() 174 void WorkerScriptLoader::cancel()
185 { 175 {
186 if (m_threadableLoader) 176 if (m_threadableLoader)
187 m_threadableLoader->cancel(); 177 m_threadableLoader->cancel();
188 } 178 }
189 179
190 String WorkerScriptLoader::script() 180 String WorkerScriptLoader::script()
191 { 181 {
192 return m_script.toString(); 182 return m_script.toString();
193 } 183 }
194 184
185 void WorkerScriptLoader::notifyError()
186 {
187 m_failed = true;
188 // notifyError() could be called before ThreadableLoader::create() returns
189 // e.g. from didFail(), and in that case m_threadableLoader is not yet set
190 // (i.e. still null).
191 // Since the callback invocation in notifyFinished() potentially delete
192 // |this| object, the callback invocation should be postponed until the
193 // create() call returns. See loadAsynchronously() for the postponed call.
194 if (m_threadableLoader)
195 notifyFinished();
196 }
197
195 void WorkerScriptLoader::notifyFinished() 198 void WorkerScriptLoader::notifyFinished()
196 { 199 {
197 if (!m_finishedCallback || m_finishing) 200 if (!m_finishedCallback)
198 return; 201 return;
199 202
200 m_finishing = true; 203 OwnPtr<Closure> callback = m_finishedCallback.release();
201 if (m_finishedCallback) 204 (*callback)();
202 (*m_finishedCallback)();
203 } 205 }
204 206
205 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re sponse) 207 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re sponse)
206 { 208 {
207 // Per http://www.w3.org/TR/CSP2/#processing-model-workers, if the Worker's 209 // Per http://www.w3.org/TR/CSP2/#processing-model-workers, if the Worker's
208 // URL is not a GUID, then it grabs its CSP from the response headers 210 // URL is not a GUID, then it grabs its CSP from the response headers
209 // directly. Otherwise, the Worker inherits the policy from the parent 211 // directly. Otherwise, the Worker inherits the policy from the parent
210 // document (which is implemented in WorkerMessagingProxy, and 212 // document (which is implemented in WorkerMessagingProxy, and
211 // m_contentSecurityPolicy should be left as nullptr to inherit the policy). 213 // m_contentSecurityPolicy should be left as nullptr to inherit the policy).
212 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file") && !response.url().protocolIs("filesystem")) { 214 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file") && !response.url().protocolIs("filesystem")) {
213 m_contentSecurityPolicy = ContentSecurityPolicy::create(); 215 m_contentSecurityPolicy = ContentSecurityPolicy::create();
214 m_contentSecurityPolicy->setOverrideURLForSelf(response.url()); 216 m_contentSecurityPolicy->setOverrideURLForSelf(response.url());
215 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse Headers(response)); 217 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse Headers(response));
216 } 218 }
217 } 219 }
218 220
219 } // namespace blink 221 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/workers/WorkerScriptLoader.h ('k') | Source/web/WebEmbeddedWorkerImpl.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698