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"
38 #include "wtf/OwnPtr.h"
39 #include "wtf/RefPtr.h"
43 WorkerScriptLoader::WorkerScriptLoader()
48 , m_targetType(ResourceRequest::TargetIsWorker)
52 WorkerScriptLoader::~WorkerScriptLoader()
56 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
60 OwnPtr<ResourceRequest> request(createResourceRequest());
64 ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope());
66 ThreadableLoaderOptions options;
67 options.allowCredentials = AllowStoredCredentials;
68 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
69 // FIXME: Should we add EnforceScriptSrcDirective here?
70 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
72 WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(executionContext), *request, *this, options);
75 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
81 OwnPtr<ResourceRequest> request(createResourceRequest());
85 ThreadableLoaderOptions options;
86 options.allowCredentials = AllowStoredCredentials;
87 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
89 // During create, callbacks may happen which remove the last reference to this object.
90 RefPtr<WorkerScriptLoader> protect(this);
91 m_threadableLoader = ThreadableLoader::create(executionContext, this, *request, options);
94 const KURL& WorkerScriptLoader::responseURL() const
100 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
102 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
103 request->setHTTPMethod("GET");
104 request->setTargetType(m_targetType);
105 return request.release();
108 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
110 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
114 m_responseURL = response.url();
115 m_responseEncoding = response.textEncodingName();
117 m_client->didReceiveResponse(identifier, response);
120 void WorkerScriptLoader::didReceiveData(const char* data, int len)
126 if (!m_responseEncoding.isEmpty())
127 m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
129 m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
138 m_script.append(m_decoder->decode(data, len));
141 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
149 m_script.append(m_decoder->flush());
151 m_identifier = identifier;
155 void WorkerScriptLoader::didFail(const ResourceError&)
160 void WorkerScriptLoader::didFailRedirectCheck()
165 void WorkerScriptLoader::notifyError()
171 void WorkerScriptLoader::cancel()
173 if (m_threadableLoader)
174 m_threadableLoader->cancel();
177 String WorkerScriptLoader::script()
179 return m_script.toString();
182 void WorkerScriptLoader::notifyFinished()
184 if (!m_client || m_finishing)
188 m_client->notifyFinished();
191 } // namespace WebCore