Chromium Code Reviews| 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() = default; |
| 55 { | |
| 56 } | |
| 57 | 56 |
| 58 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy) | 57 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, c onst KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy) |
| 59 { | 58 { |
| 60 m_url = url; | 59 m_url = url; |
| 61 | 60 |
| 62 OwnPtr<ResourceRequest> request(createResourceRequest()); | 61 OwnPtr<ResourceRequest> request(createResourceRequest()); |
| 63 if (!request) | 62 if (!request) |
| 64 return; | 63 return; |
| 65 | 64 |
| 66 ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope()); | 65 ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope()); |
| 67 | 66 |
| 68 ThreadableLoaderOptions options; | 67 ThreadableLoaderOptions options; |
| 69 options.crossOriginRequestPolicy = crossOriginRequestPolicy; | 68 options.crossOriginRequestPolicy = crossOriginRequestPolicy; |
| 70 // FIXME: Should we add EnforceScriptSrcDirective here? | 69 // FIXME: Should we add EnforceScriptSrcDirective here? |
| 71 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy ; | 70 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy ; |
| 72 | 71 |
| 73 ResourceLoaderOptions resourceLoaderOptions; | 72 ResourceLoaderOptions resourceLoaderOptions; |
| 74 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; | 73 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| 75 | 74 |
| 76 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(execut ionContext), *request, *this, options, resourceLoaderOptions); | 75 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(execut ionContext), *request, *this, options, resourceLoaderOptions); |
| 77 } | 76 } |
| 78 | 77 |
| 79 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScript LoaderClient* client) | 78 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, PassOwnPtr<C losure> responseCallback, PassOwnPtr<Closure> finishedCallback) |
| 80 { | 79 { |
| 81 ASSERT(client); | 80 ASSERT(responseCallback || finishedCallback); |
| 82 m_client = client; | 81 m_responseCallback = responseCallback; |
| 82 m_finishedCallback = finishedCallback; | |
| 83 m_url = url; | 83 m_url = url; |
| 84 | 84 |
| 85 OwnPtr<ResourceRequest> request(createResourceRequest()); | 85 OwnPtr<ResourceRequest> request(createResourceRequest()); |
| 86 if (!request) | 86 if (!request) |
| 87 return; | 87 return; |
| 88 | 88 |
| 89 ThreadableLoaderOptions options; | 89 ThreadableLoaderOptions options; |
| 90 options.crossOriginRequestPolicy = crossOriginRequestPolicy; | 90 options.crossOriginRequestPolicy = crossOriginRequestPolicy; |
| 91 | 91 |
| 92 ResourceLoaderOptions resourceLoaderOptions; | 92 ResourceLoaderOptions resourceLoaderOptions; |
| 93 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; | 93 resourceLoaderOptions.allowCredentials = AllowStoredCredentials; |
| 94 | 94 |
| 95 // During create, callbacks may happen which remove the last reference to th is object. | |
|
Takashi Toyoshima
2015/06/19 05:24:46
Note:
This code was introduced by https://bugs.web
kinuko
2015/06/24 07:27:54
This looks to be ok, but can you make sure that Th
| |
| 96 RefPtr<WorkerScriptLoader> protect(this); | |
| 97 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque st, options, resourceLoaderOptions); | 95 m_threadableLoader = ThreadableLoader::create(executionContext, this, *reque st, options, resourceLoaderOptions); |
| 98 } | 96 } |
| 99 | 97 |
| 100 const KURL& WorkerScriptLoader::responseURL() const | 98 const KURL& WorkerScriptLoader::responseURL() const |
| 101 { | 99 { |
| 102 ASSERT(!failed()); | 100 ASSERT(!failed()); |
| 103 return m_responseURL; | 101 return m_responseURL; |
| 104 } | 102 } |
| 105 | 103 |
| 106 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest() | 104 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest() |
| 107 { | 105 { |
| 108 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url)); | 106 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url)); |
| 109 request->setHTTPMethod("GET"); | 107 request->setHTTPMethod("GET"); |
| 110 request->setRequestContext(m_requestContext); | 108 request->setRequestContext(m_requestContext); |
| 111 return request.release(); | 109 return request.release(); |
| 112 } | 110 } |
| 113 | 111 |
| 114 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle) | 112 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const Reso urceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle) |
| 115 { | 113 { |
| 116 ASSERT_UNUSED(handle, !handle); | 114 ASSERT_UNUSED(handle, !handle); |
| 117 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { | 115 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { |
| 118 m_failed = true; | 116 m_failed = true; |
| 119 return; | 117 return; |
| 120 } | 118 } |
| 119 m_identifier = identifier; | |
| 121 m_responseURL = response.url(); | 120 m_responseURL = response.url(); |
| 122 m_responseEncoding = response.textEncodingName(); | 121 m_responseEncoding = response.textEncodingName(); |
| 122 m_appCacheID = response.appCacheID(); | |
| 123 processContentSecurityPolicy(response); | 123 processContentSecurityPolicy(response); |
| 124 if (m_client) | 124 if (m_responseCallback) |
| 125 m_client->didReceiveResponse(identifier, response); | 125 (*m_responseCallback)(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len) | 128 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len) |
| 129 { | 129 { |
| 130 if (m_failed) | 130 if (m_failed) |
| 131 return; | 131 return; |
| 132 | 132 |
| 133 if (!m_decoder) { | 133 if (!m_decoder) { |
| 134 if (!m_responseEncoding.isEmpty()) | 134 if (!m_responseEncoding.isEmpty()) |
| 135 m_decoder = TextResourceDecoder::create("text/javascript", m_respons eEncoding); | 135 m_decoder = TextResourceDecoder::create("text/javascript", m_respons eEncoding); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 152 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) | 152 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) |
| 153 { | 153 { |
| 154 if (m_failed) { | 154 if (m_failed) { |
| 155 notifyError(); | 155 notifyError(); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 | 158 |
| 159 if (m_decoder) | 159 if (m_decoder) |
| 160 m_script.append(m_decoder->flush()); | 160 m_script.append(m_decoder->flush()); |
| 161 | 161 |
| 162 m_identifier = identifier; | 162 if (m_finishedCallback) |
| 163 notifyFinished(); | 163 (*m_finishedCallback)(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void WorkerScriptLoader::didFail(const ResourceError&) | 166 void WorkerScriptLoader::didFail(const ResourceError&) |
| 167 { | 167 { |
| 168 notifyError(); | 168 notifyError(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void WorkerScriptLoader::didFailRedirectCheck() | 171 void WorkerScriptLoader::didFailRedirectCheck() |
| 172 { | 172 { |
| 173 notifyError(); | 173 notifyError(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 185 m_threadableLoader->cancel(); | 185 m_threadableLoader->cancel(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 String WorkerScriptLoader::script() | 188 String WorkerScriptLoader::script() |
| 189 { | 189 { |
| 190 return m_script.toString(); | 190 return m_script.toString(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void WorkerScriptLoader::notifyFinished() | 193 void WorkerScriptLoader::notifyFinished() |
| 194 { | 194 { |
| 195 if (!m_client || m_finishing) | 195 if (!m_finishedCallback || m_finishing) |
| 196 return; | 196 return; |
| 197 | 197 |
| 198 m_finishing = true; | 198 m_finishing = true; |
| 199 m_client->notifyFinished(); | 199 if (m_finishedCallback) |
| 200 (*m_finishedCallback)(); | |
| 200 } | 201 } |
| 201 | 202 |
| 202 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re sponse) | 203 void WorkerScriptLoader::processContentSecurityPolicy(const ResourceResponse& re sponse) |
| 203 { | 204 { |
| 205 m_contentSecurityPolicy = ContentSecurityPolicy::create(); | |
| 204 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file") && !response.url().protocolIs("filesystem")) { | 206 if (!response.url().protocolIs("blob") && !response.url().protocolIs("file") && !response.url().protocolIs("filesystem")) { |
| 205 m_contentSecurityPolicy = ContentSecurityPolicy::create(); | |
| 206 m_contentSecurityPolicy->setOverrideURLForSelf(response.url()); | 207 m_contentSecurityPolicy->setOverrideURLForSelf(response.url()); |
| 207 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse Headers(response)); | 208 m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponse Headers(response)); |
| 208 } | 209 } |
| 209 } | 210 } |
| 210 | 211 |
| 211 } // namespace blink | 212 } // namespace blink |
| OLD | NEW |