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(0) 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) 114 { 115 if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) { 116 m_failed = true; 117 return; 118 } 119 m_responseURL = response.url(); 120 m_responseEncoding = response.textEncodingName(); 121 if (m_client) 122 m_client->didReceiveResponse(identifier, response); 123 } 124 125 void WorkerScriptLoader::didReceiveData(const char* data, int len) 126 { 127 if (m_failed) 128 return; 129 130 if (!m_decoder) { 131 if (!m_responseEncoding.isEmpty()) 132 m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding); 133 else 134 m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8"); 135 } 136 137 if (!len) 138 return; 139 140 if (len == -1) 141 len = strlen(data); 142 143 m_script.append(m_decoder->decode(data, len)); 144 } 145 146 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double) 147 { 148 if (m_failed) { 149 notifyError(); 150 return; 151 } 152 153 if (m_decoder) 154 m_script.append(m_decoder->flush()); 155 156 m_identifier = identifier; 157 notifyFinished(); 158 } 159 160 void WorkerScriptLoader::didFail(const ResourceError&) 161 { 162 notifyError(); 163 } 164 165 void WorkerScriptLoader::didFailRedirectCheck() 166 { 167 notifyError(); 168 } 169 170 void WorkerScriptLoader::notifyError() 171 { 172 m_failed = true; 173 notifyFinished(); 174 } 175 176 void WorkerScriptLoader::cancel() 177 { 178 if (m_threadableLoader) 179 m_threadableLoader->cancel(); 180 } 181 182 String WorkerScriptLoader::script() 183 { 184 return m_script.toString(); 185 } 186 187 void WorkerScriptLoader::notifyFinished() 188 { 189 if (!m_client || m_finishing) 190 return; 191 192 m_finishing = true; 193 m_client->notifyFinished(); 194 } 195 196 } // namespace blink 197