2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "core/workers/WorkerScriptLoader.h"
31 #include "core/dom/ExecutionContext.h"
32 #include "core/html/parser/TextResourceDecoder.h"
33 #include "core/loader/WorkerThreadableLoader.h"
34 #include "core/workers/WorkerGlobalScope.h"
35 #include "core/workers/WorkerScriptLoaderClient.h"
36 #include "platform/network/ResourceResponse.h"
37 #include "public/platform/WebURLRequest.h"
39 #include "wtf/OwnPtr.h"
40 #include "wtf/RefPtr.h"
44 WorkerScriptLoader::WorkerScriptLoader()
49 , m_requestContext(blink::WebURLRequest::RequestContextWorker)
53 WorkerScriptLoader::~WorkerScriptLoader()
57 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
61 OwnPtr<ResourceRequest> request(createResourceRequest());
65 ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope());
67 ThreadableLoaderOptions options;
68 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
69 // FIXME: Should we add EnforceScriptSrcDirective here?
70 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
72 ResourceLoaderOptions resourceLoaderOptions;
73 resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
75 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(executionContext), *request, *this, options, resourceLoaderOptions);
78 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
84 OwnPtr<ResourceRequest> request(createResourceRequest());
88 ThreadableLoaderOptions options;
89 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
91 ResourceLoaderOptions resourceLoaderOptions;
92 resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
94 // During create, callbacks may happen which remove the last reference to this object.
95 RefPtr<WorkerScriptLoader> protect(this);
96 m_threadableLoader = ThreadableLoader::create(executionContext, this, *request, options, resourceLoaderOptions);
99 const KURL& WorkerScriptLoader::responseURL() const
102 return m_responseURL;
105 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
107 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
108 request->setHTTPMethod("GET");
109 request->setRequestContext(m_requestContext);
110 return request.release();
113 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
115 ASSERT_UNUSED(handle, !handle);
116 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
120 m_responseURL = response.url();
121 m_responseEncoding = response.textEncodingName();
123 m_client->didReceiveResponse(identifier, response);
126 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len)
132 if (!m_responseEncoding.isEmpty())
133 m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
135 m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
141 m_script.append(m_decoder->decode(data, len));
144 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
152 m_script.append(m_decoder->flush());
154 m_identifier = identifier;
158 void WorkerScriptLoader::didFail(const ResourceError&)
163 void WorkerScriptLoader::didFailRedirectCheck()
168 void WorkerScriptLoader::notifyError()
174 void WorkerScriptLoader::cancel()
176 if (m_threadableLoader)
177 m_threadableLoader->cancel();
180 String WorkerScriptLoader::script()
182 return m_script.toString();
185 void WorkerScriptLoader::notifyFinished()
187 if (!m_client || m_finishing)
191 m_client->notifyFinished();