Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / workers / WorkerScriptLoader.cpp
1 /*
2  * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  *
26  */
27
28 #include "config.h"
29 #include "core/workers/WorkerScriptLoader.h"
30
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
38 #include "wtf/OwnPtr.h"
39 #include "wtf/RefPtr.h"
40
41 namespace WebCore {
42
43 WorkerScriptLoader::WorkerScriptLoader()
44     : m_client(0)
45     , m_failed(false)
46     , m_identifier(0)
47     , m_finishing(false)
48     , m_targetType(ResourceRequest::TargetIsWorker)
49 {
50 }
51
52 WorkerScriptLoader::~WorkerScriptLoader()
53 {
54 }
55
56 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
57 {
58     m_url = url;
59
60     OwnPtr<ResourceRequest> request(createResourceRequest());
61     if (!request)
62         return;
63
64     ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope());
65
66     ThreadableLoaderOptions options;
67     options.allowCredentials = AllowStoredCredentials;
68     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
69     // FIXME: Should we add EnforceScriptSrcDirective here?
70     options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
71
72     WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(executionContext), *request, *this, options);
73 }
74
75 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
76 {
77     ASSERT(client);
78     m_client = client;
79     m_url = url;
80
81     OwnPtr<ResourceRequest> request(createResourceRequest());
82     if (!request)
83         return;
84
85     ThreadableLoaderOptions options;
86     options.allowCredentials = AllowStoredCredentials;
87     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
88
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);
92 }
93
94 const KURL& WorkerScriptLoader::responseURL() const
95 {
96     ASSERT(!failed());
97     return m_responseURL;
98 }
99
100 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
101 {
102     OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
103     request->setHTTPMethod("GET");
104     request->setTargetType(m_targetType);
105     return request.release();
106 }
107
108 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
109 {
110     if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
111         m_failed = true;
112         return;
113     }
114     m_responseURL = response.url();
115     m_responseEncoding = response.textEncodingName();
116     if (m_client)
117         m_client->didReceiveResponse(identifier, response);
118 }
119
120 void WorkerScriptLoader::didReceiveData(const char* data, int len)
121 {
122     if (m_failed)
123         return;
124
125     if (!m_decoder) {
126         if (!m_responseEncoding.isEmpty())
127             m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
128         else
129             m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
130     }
131
132     if (!len)
133         return;
134
135     if (len == -1)
136         len = strlen(data);
137
138     m_script.append(m_decoder->decode(data, len));
139 }
140
141 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
142 {
143     if (m_failed) {
144         notifyError();
145         return;
146     }
147
148     if (m_decoder)
149         m_script.append(m_decoder->flush());
150
151     m_identifier = identifier;
152     notifyFinished();
153 }
154
155 void WorkerScriptLoader::didFail(const ResourceError&)
156 {
157     notifyError();
158 }
159
160 void WorkerScriptLoader::didFailRedirectCheck()
161 {
162     notifyError();
163 }
164
165 void WorkerScriptLoader::notifyError()
166 {
167     m_failed = true;
168     notifyFinished();
169 }
170
171 void WorkerScriptLoader::cancel()
172 {
173     if (m_threadableLoader)
174         m_threadableLoader->cancel();
175 }
176
177 String WorkerScriptLoader::script()
178 {
179     return m_script.toString();
180 }
181
182 void WorkerScriptLoader::notifyFinished()
183 {
184     if (!m_client || m_finishing)
185         return;
186
187     m_finishing = true;
188     m_client->notifyFinished();
189 }
190
191 } // namespace WebCore