| OLD | NEW |
| 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 14 matching lines...) Expand all Loading... |
| 25 * | 25 * |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 #include "config.h" | 28 #include "config.h" |
| 29 #include "core/workers/WorkerScriptLoader.h" | 29 #include "core/workers/WorkerScriptLoader.h" |
| 30 | 30 |
| 31 #include "core/dom/ExecutionContext.h" | 31 #include "core/dom/ExecutionContext.h" |
| 32 #include "core/html/parser/TextResourceDecoder.h" | 32 #include "core/html/parser/TextResourceDecoder.h" |
| 33 #include "core/loader/WorkerThreadableLoader.h" | 33 #include "core/loader/WorkerThreadableLoader.h" |
| 34 #include "core/workers/WorkerGlobalScope.h" | 34 #include "core/workers/WorkerGlobalScope.h" |
| 35 #include "core/workers/WorkerScriptLoaderClient.h" | |
| 36 #include "platform/network/ContentSecurityPolicyResponseHeaders.h" | 35 #include "platform/network/ContentSecurityPolicyResponseHeaders.h" |
| 37 #include "platform/network/ResourceResponse.h" | 36 #include "platform/network/ResourceResponse.h" |
| 38 #include "public/platform/WebURLRequest.h" | 37 #include "public/platform/WebURLRequest.h" |
| 39 | 38 |
| 40 #include "wtf/OwnPtr.h" | 39 #include "wtf/OwnPtr.h" |
| 41 #include "wtf/RefPtr.h" | 40 #include "wtf/RefPtr.h" |
| 42 | 41 |
| 43 namespace blink { | 42 namespace blink { |
| 44 | 43 |
| 45 WorkerScriptLoader::WorkerScriptLoader() | 44 WorkerScriptLoader::WorkerScriptLoader() |
| 46 : m_client(nullptr) | 45 : m_responseCallback(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_finishing(false) | 50 , m_finishing(false) |
| 50 , m_requestContext(WebURLRequest::RequestContextWorker) | 51 , m_requestContext(WebURLRequest::RequestContextWorker) |
| 51 { | 52 { |
| 52 } | 53 } |
| 53 | 54 |
| 54 WorkerScriptLoader::~WorkerScriptLoader() | 55 WorkerScriptLoader::~WorkerScriptLoader() |
| 55 { | 56 { |
| 56 } | 57 } |
| 57 | 58 |
| 58 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c
onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy) | 59 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c
onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 options.crossOriginRequestPolicy = crossOriginRequestPolicy; | 70 options.crossOriginRequestPolicy = crossOriginRequestPolicy; |
| 70 // FIXME: Should we add EnforceScriptSrcDirective here? | 71 // FIXME: Should we add EnforceScriptSrcDirective here? |
| 71 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy
; | 72 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy
; |
| 72 | 73 |
| 73 ResourceLoaderOptions resourceLoaderOptions; | 74 ResourceLoaderOptions resourceLoaderOptions; |
| 74 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; | 75 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| 75 | 76 |
| 76 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(execut
ionContext), *request, *this, options, resourceLoaderOptions); | 77 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(execut
ionContext), *request, *this, options, resourceLoaderOptions); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext,
const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScript
LoaderClient* client) | 80 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext,
const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, PassOwnPtr<C
losure> responseCallback, PassOwnPtr<Closure> finishedCallback) |
| 80 { | 81 { |
| 81 ASSERT(client); | 82 ASSERT(responseCallback || finishedCallback); |
| 82 m_client = client; | 83 m_responseCallback = responseCallback; |
| 84 m_finishedCallback = finishedCallback; |
| 83 m_url = url; | 85 m_url = url; |
| 84 | 86 |
| 85 OwnPtr<ResourceRequest> request(createResourceRequest()); | 87 OwnPtr<ResourceRequest> request(createResourceRequest()); |
| 86 if (!request) | 88 if (!request) |
| 87 return; | 89 return; |
| 88 | 90 |
| 89 ThreadableLoaderOptions options; | 91 ThreadableLoaderOptions options; |
| 90 options.crossOriginRequestPolicy = crossOriginRequestPolicy; | 92 options.crossOriginRequestPolicy = crossOriginRequestPolicy; |
| 91 | 93 |
| 92 ResourceLoaderOptions resourceLoaderOptions; | 94 ResourceLoaderOptions resourceLoaderOptions; |
| 93 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; | 95 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| 94 | 96 |
| 95 // During create, callbacks may happen which remove the last reference to th
is object. | |
| 96 RefPtr<WorkerScriptLoader> protect(this); | |
| 97 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque
st, options, resourceLoaderOptions); | 97 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque
st, options, resourceLoaderOptions); |
| 98 } | 98 } |
| 99 | 99 |
| 100 const KURL& WorkerScriptLoader::responseURL() const | 100 const KURL& WorkerScriptLoader::responseURL() const |
| 101 { | 101 { |
| 102 ASSERT(!failed()); | 102 ASSERT(!failed()); |
| 103 return m_responseURL; | 103 return m_responseURL; |
| 104 } | 104 } |
| 105 | 105 |
| 106 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest() | 106 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest() |
| 107 { | 107 { |
| 108 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url)); | 108 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url)); |
| 109 request->setHTTPMethod("GET"); | 109 request->setHTTPMethod("GET"); |
| 110 request->setRequestContext(m_requestContext); | 110 request->setRequestContext(m_requestContext); |
| 111 return request.release(); | 111 return request.release(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso
urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle) | 114 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso
urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle) |
| 115 { | 115 { |
| 116 ASSERT_UNUSED(handle, !handle); | 116 ASSERT_UNUSED(handle, !handle); |
| 117 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { | 117 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { |
| 118 m_failed = true; | 118 m_failed = true; |
| 119 return; | 119 return; |
| 120 } | 120 } |
| 121 m_identifier = identifier; |
| 121 m_responseURL = response.url(); | 122 m_responseURL = response.url(); |
| 122 m_responseEncoding = response.textEncodingName(); | 123 m_responseEncoding = response.textEncodingName(); |
| 124 m_appCacheID = response.appCacheID(); |
| 123 processContentSecurityPolicy(response); | 125 processContentSecurityPolicy(response); |
| 124 if (m_client) | 126 if (m_responseCallback) |
| 125 m_client->didReceiveResponse(identifier, response); | 127 (*m_responseCallback)(); |
| 126 } | 128 } |
| 127 | 129 |
| 128 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len) | 130 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len) |
| 129 { | 131 { |
| 130 if (m_failed) | 132 if (m_failed) |
| 131 return; | 133 return; |
| 132 | 134 |
| 133 if (!m_decoder) { | 135 if (!m_decoder) { |
| 134 if (!m_responseEncoding.isEmpty()) | 136 if (!m_responseEncoding.isEmpty()) |
| 135 m_decoder = TextResourceDecoder::create("text/javascript", m_respons
eEncoding); | 137 m_decoder = TextResourceDecoder::create("text/javascript", m_respons
eEncoding); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 152 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) | 154 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) |
| 153 { | 155 { |
| 154 if (m_failed) { | 156 if (m_failed) { |
| 155 notifyError(); | 157 notifyError(); |
| 156 return; | 158 return; |
| 157 } | 159 } |
| 158 | 160 |
| 159 if (m_decoder) | 161 if (m_decoder) |
| 160 m_script.append(m_decoder->flush()); | 162 m_script.append(m_decoder->flush()); |
| 161 | 163 |
| 162 m_identifier = identifier; | 164 if (m_finishedCallback) |
| 163 notifyFinished(); | 165 (*m_finishedCallback)(); |
| 164 } | 166 } |
| 165 | 167 |
| 166 void WorkerScriptLoader::didFail(const ResourceError&) | 168 void WorkerScriptLoader::didFail(const ResourceError&) |
| 167 { | 169 { |
| 168 notifyError(); | 170 notifyError(); |
| 169 } | 171 } |
| 170 | 172 |
| 171 void WorkerScriptLoader::didFailRedirectCheck() | 173 void WorkerScriptLoader::didFailRedirectCheck() |
| 172 { | 174 { |
| 173 notifyError(); | 175 notifyError(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 185 m_threadableLoader->cancel(); | 187 m_threadableLoader->cancel(); |
| 186 } | 188 } |
| 187 | 189 |
| 188 String WorkerScriptLoader::script() | 190 String WorkerScriptLoader::script() |
| 189 { | 191 { |
| 190 return m_script.toString(); | 192 return m_script.toString(); |
| 191 } | 193 } |
| 192 | 194 |
| 193 void WorkerScriptLoader::notifyFinished() | 195 void WorkerScriptLoader::notifyFinished() |
| 194 { | 196 { |
| 195 if (!m_client || m_finishing) | 197 if (!m_finishedCallback || m_finishing) |
| 196 return; | 198 return; |
| 197 | 199 |
| 198 m_finishing = true; | 200 m_finishing = true; |
| 199 m_client->notifyFinished(); | 201 if (m_finishedCallback) |
| 202 (*m_finishedCallback)(); |
| 200 } | 203 } |
| 201 | 204 |
| 202 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re
sponse) | 205 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re
sponse) |
| 203 { | 206 { |
| 207 // 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 |
| 209 // directly. Otherwise, the Worker inherits the policy from the parent |
| 210 // document (which is implemented in WorkerMessagingProxy, and |
| 211 // m_contentSecurityPolicy should be left as nullptr to inherit the policy). |
| 204 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file")
&& !response.url().protocolIs("filesystem")) { | 212 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file")
&& !response.url().protocolIs("filesystem")) { |
| 205 m_contentSecurityPolicy = ContentSecurityPolicy::create(); | 213 m_contentSecurityPolicy = ContentSecurityPolicy::create(); |
| 206 m_contentSecurityPolicy->setOverrideURLForSelf(response.url()); | 214 m_contentSecurityPolicy->setOverrideURLForSelf(response.url()); |
| 207 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse
Headers(response)); | 215 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse
Headers(response)); |
| 208 } | 216 } |
| 209 } | 217 } |
| 210 | 218 |
| 211 } // namespace blink | 219 } // namespace blink |
| OLD | NEW |