Update To 11.40.268.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 #include "public/platform/WebURLRequest.h"
38
39 #include "wtf/OwnPtr.h"
40 #include "wtf/RefPtr.h"
41
42 namespace blink {
43
44 WorkerScriptLoader::WorkerScriptLoader()
45     : m_client(nullptr)
46     , m_failed(false)
47     , m_identifier(0)
48     , m_finishing(false)
49     , m_requestContext(blink::WebURLRequest::RequestContextWorker)
50 {
51 }
52
53 WorkerScriptLoader::~WorkerScriptLoader()
54 {
55 }
56
57 void WorkerScriptLoader::loadSynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
58 {
59     m_url = url;
60
61     OwnPtr<ResourceRequest> request(createResourceRequest());
62     if (!request)
63         return;
64
65     ASSERT_WITH_SECURITY_IMPLICATION(executionContext.isWorkerGlobalScope());
66
67     ThreadableLoaderOptions options;
68     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
69     // FIXME: Should we add EnforceScriptSrcDirective here?
70     options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
71
72     ResourceLoaderOptions resourceLoaderOptions;
73     resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
74
75     WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(executionContext), *request, *this, options, resourceLoaderOptions);
76 }
77
78 void WorkerScriptLoader::loadAsynchronously(ExecutionContext& executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
79 {
80     ASSERT(client);
81     m_client = client;
82     m_url = url;
83
84     OwnPtr<ResourceRequest> request(createResourceRequest());
85     if (!request)
86         return;
87
88     ThreadableLoaderOptions options;
89     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
90
91     ResourceLoaderOptions resourceLoaderOptions;
92     resourceLoaderOptions.allowCredentials = AllowStoredCredentials;
93
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);
97 }
98
99 const KURL& WorkerScriptLoader::responseURL() const
100 {
101     ASSERT(!failed());
102     return m_responseURL;
103 }
104
105 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
106 {
107     OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
108     request->setHTTPMethod("GET");
109     request->setRequestContext(m_requestContext);
110     return request.release();
111 }
112
113 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
114 {
115     ASSERT_UNUSED(handle, !handle);
116     if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
117         m_failed = true;
118         return;
119     }
120     m_responseURL = response.url();
121     m_responseEncoding = response.textEncodingName();
122     if (m_client)
123         m_client->didReceiveResponse(identifier, response);
124 }
125
126 void WorkerScriptLoader::didReceiveData(const char* data, unsigned len)
127 {
128     if (m_failed)
129         return;
130
131     if (!m_decoder) {
132         if (!m_responseEncoding.isEmpty())
133             m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
134         else
135             m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
136     }
137
138     if (!len)
139         return;
140
141     m_script.append(m_decoder->decode(data, len));
142 }
143
144 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
145 {
146     if (m_failed) {
147         notifyError();
148         return;
149     }
150
151     if (m_decoder)
152         m_script.append(m_decoder->flush());
153
154     m_identifier = identifier;
155     notifyFinished();
156 }
157
158 void WorkerScriptLoader::didFail(const ResourceError&)
159 {
160     notifyError();
161 }
162
163 void WorkerScriptLoader::didFailRedirectCheck()
164 {
165     notifyError();
166 }
167
168 void WorkerScriptLoader::notifyError()
169 {
170     m_failed = true;
171     notifyFinished();
172 }
173
174 void WorkerScriptLoader::cancel()
175 {
176     if (m_threadableLoader)
177         m_threadableLoader->cancel();
178 }
179
180 String WorkerScriptLoader::script()
181 {
182     return m_script.toString();
183 }
184
185 void WorkerScriptLoader::notifyFinished()
186 {
187     if (!m_client || m_finishing)
188         return;
189
190     m_finishing = true;
191     m_client->notifyFinished();
192 }
193
194 } // namespace blink