Home | History | Annotate | Download | only in workers
      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/fetch/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     options.sendLoadCallbacks = SendCallbacks;
     70     // FIXME: Should we add EnforceScriptSrcDirective here?
     71     options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
     72 
     73     WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(executionContext), *request, *this, options);
     74 }
     75 
     76 void WorkerScriptLoader::loadAsynchronously(ExecutionContext* executionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
     77 {
     78     ASSERT(client);
     79     m_client = client;
     80     m_url = url;
     81 
     82     OwnPtr<ResourceRequest> request(createResourceRequest());
     83     if (!request)
     84         return;
     85 
     86     ThreadableLoaderOptions options;
     87     options.allowCredentials = AllowStoredCredentials;
     88     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
     89     options.sendLoadCallbacks = SendCallbacks;
     90 
     91     // During create, callbacks may happen which remove the last reference to this object.
     92     RefPtr<WorkerScriptLoader> protect(this);
     93     m_threadableLoader = ThreadableLoader::create(executionContext, this, *request, options);
     94 }
     95 
     96 const KURL& WorkerScriptLoader::responseURL() const
     97 {
     98     ASSERT(!failed());
     99     return m_responseURL;
    100 }
    101 
    102 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
    103 {
    104     OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
    105     request->setHTTPMethod("GET");
    106     request->setTargetType(m_targetType);
    107     return request.release();
    108 }
    109 
    110 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
    111 {
    112     if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
    113         m_failed = true;
    114         return;
    115     }
    116     m_responseURL = response.url();
    117     m_responseEncoding = response.textEncodingName();
    118     if (m_client)
    119         m_client->didReceiveResponse(identifier, response);
    120 }
    121 
    122 void WorkerScriptLoader::didReceiveData(const char* data, int len)
    123 {
    124     if (m_failed)
    125         return;
    126 
    127     if (!m_decoder) {
    128         if (!m_responseEncoding.isEmpty())
    129             m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
    130         else
    131             m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
    132     }
    133 
    134     if (!len)
    135         return;
    136 
    137     if (len == -1)
    138         len = strlen(data);
    139 
    140     m_script.append(m_decoder->decode(data, len));
    141 }
    142 
    143 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
    144 {
    145     if (m_failed) {
    146         notifyError();
    147         return;
    148     }
    149 
    150     if (m_decoder)
    151         m_script.append(m_decoder->flush());
    152 
    153     m_identifier = identifier;
    154     notifyFinished();
    155 }
    156 
    157 void WorkerScriptLoader::didFail(const ResourceError&)
    158 {
    159     notifyError();
    160 }
    161 
    162 void WorkerScriptLoader::didFailRedirectCheck()
    163 {
    164     notifyError();
    165 }
    166 
    167 void WorkerScriptLoader::notifyError()
    168 {
    169     m_failed = true;
    170     notifyFinished();
    171 }
    172 
    173 void WorkerScriptLoader::cancel()
    174 {
    175     if (m_threadableLoader)
    176         m_threadableLoader->cancel();
    177 }
    178 
    179 String WorkerScriptLoader::script()
    180 {
    181     return m_script.toString();
    182 }
    183 
    184 void WorkerScriptLoader::notifyFinished()
    185 {
    186     if (!m_client || m_finishing)
    187         return;
    188 
    189     m_finishing = true;
    190     m_client->notifyFinished();
    191 }
    192 
    193 } // namespace WebCore
    194