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.
32 #include "WorkerScriptLoader.h"
34 #include "CrossThreadTask.h"
35 #include "ResourceRequest.h"
36 #include "ResourceResponse.h"
37 #include "ScriptExecutionContext.h"
38 #include "SecurityOrigin.h"
39 #include "TextResourceDecoder.h"
40 #include "WorkerContext.h"
41 #include "WorkerScriptLoaderClient.h"
42 #include "WorkerThreadableLoader.h"
44 #include <wtf/OwnPtr.h>
45 #include <wtf/RefPtr.h>
46 #include <wtf/UnusedParam.h>
50 WorkerScriptLoader::WorkerScriptLoader(ResourceRequestBase::TargetType targetType)
54 , m_targetType(targetType)
58 WorkerScriptLoader::~WorkerScriptLoader()
62 void WorkerScriptLoader::loadSynchronously(ScriptExecutionContext* scriptExecutionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
66 OwnPtr<ResourceRequest> request(createResourceRequest());
70 ASSERT(scriptExecutionContext->isWorkerContext());
72 ThreadableLoaderOptions options;
73 options.allowCredentials = true;
74 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
75 options.sendLoadCallbacks = true;
77 WorkerThreadableLoader::loadResourceSynchronously(static_cast<WorkerContext*>(scriptExecutionContext), *request, *this, options);
80 void WorkerScriptLoader::loadAsynchronously(ScriptExecutionContext* scriptExecutionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
86 OwnPtr<ResourceRequest> request(createResourceRequest());
90 ThreadableLoaderOptions options;
91 options.allowCredentials = true;
92 options.crossOriginRequestPolicy = crossOriginRequestPolicy;
93 options.sendLoadCallbacks = true;
95 // During create, callbacks may happen which remove the last reference to this object.
96 RefPtr<WorkerScriptLoader> protect(this);
97 m_threadableLoader = ThreadableLoader::create(scriptExecutionContext, this, *request, options);
100 const KURL& WorkerScriptLoader::responseURL() const
103 return m_responseURL;
106 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
108 OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
109 request->setHTTPMethod("GET");
110 request->setTargetType(m_targetType);
111 return request.release();
114 void WorkerScriptLoader::didReceiveResponse(const ResourceResponse& response)
116 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
120 m_responseURL = response.url();
121 m_responseEncoding = response.textEncodingName();
123 m_client->didReceiveResponse(response);
126 void WorkerScriptLoader::didReceiveData(const char* data, int len)
132 if (!m_responseEncoding.isEmpty())
133 m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
135 m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
144 m_script += m_decoder->decode(data, len);
147 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
153 m_script += m_decoder->flush();
155 m_identifier = identifier;
159 void WorkerScriptLoader::didFail(const ResourceError&)
164 void WorkerScriptLoader::didFailRedirectCheck()
169 void WorkerScriptLoader::didReceiveAuthenticationCancellation(const ResourceResponse&)
174 void WorkerScriptLoader::notifyError()
180 void WorkerScriptLoader::notifyFinished()
183 m_client->notifyFinished();
186 } // namespace WebCore
188 #endif // ENABLE(WORKERS)