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 
     30 #include "core/workers/WorkerScriptLoader.h"
     31 
     32 #include "core/dom/ScriptExecutionContext.h"
     33 #include "core/loader/TextResourceDecoder.h"
     34 #include "core/loader/WorkerThreadableLoader.h"
     35 #include "core/platform/network/ResourceResponse.h"
     36 #include "core/workers/WorkerGlobalScope.h"
     37 #include "core/workers/WorkerScriptLoaderClient.h"
     38 
     39 #include <wtf/OwnPtr.h>
     40 #include <wtf/RefPtr.h>
     41 #include <wtf/UnusedParam.h>
     42 
     43 namespace WebCore {
     44 
     45 WorkerScriptLoader::WorkerScriptLoader()
     46     : m_client(0)
     47     , m_failed(false)
     48     , m_identifier(0)
     49     , m_finishing(false)
     50     , m_targetType(ResourceRequest::TargetIsWorker)
     51 {
     52 }
     53 
     54 WorkerScriptLoader::~WorkerScriptLoader()
     55 {
     56 }
     57 
     58 void WorkerScriptLoader::loadSynchronously(ScriptExecutionContext* scriptExecutionContext, const KURL& url, CrossOriginRequestPolicy crossOriginRequestPolicy)
     59 {
     60     m_url = url;
     61 
     62     OwnPtr<ResourceRequest> request(createResourceRequest());
     63     if (!request)
     64         return;
     65 
     66     ASSERT_WITH_SECURITY_IMPLICATION(scriptExecutionContext->isWorkerGlobalScope());
     67 
     68     ThreadableLoaderOptions options;
     69     options.allowCredentials = AllowStoredCredentials;
     70     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
     71     options.sendLoadCallbacks = SendCallbacks;
     72     // FIXME: Should we add EnforceScriptSrcDirective here?
     73     options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy;
     74 
     75     WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(scriptExecutionContext), *request, *this, options);
     76 }
     77 
     78 void WorkerScriptLoader::loadAsynchronously(ScriptExecutionContext* scriptExecutionContext, 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.allowCredentials = AllowStoredCredentials;
     90     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
     91     options.sendLoadCallbacks = SendCallbacks;
     92 
     93     // During create, callbacks may happen which remove the last reference to this object.
     94     RefPtr<WorkerScriptLoader> protect(this);
     95     m_threadableLoader = ThreadableLoader::create(scriptExecutionContext, this, *request, options);
     96 }
     97 
     98 const KURL& WorkerScriptLoader::responseURL() const
     99 {
    100     ASSERT(!failed());
    101     return m_responseURL;
    102 }
    103 
    104 PassOwnPtr<ResourceRequest> WorkerScriptLoader::createResourceRequest()
    105 {
    106     OwnPtr<ResourceRequest> request = adoptPtr(new ResourceRequest(m_url));
    107     request->setHTTPMethod("GET");
    108     request->setTargetType(m_targetType);
    109     return request.release();
    110 }
    111 
    112 void WorkerScriptLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
    113 {
    114     if (response.httpStatusCode() / 100 != 2 && response.httpStatusCode()) {
    115         m_failed = true;
    116         return;
    117     }
    118     m_responseURL = response.url();
    119     m_responseEncoding = response.textEncodingName();
    120     if (m_client)
    121         m_client->didReceiveResponse(identifier, response);
    122 }
    123 
    124 void WorkerScriptLoader::didReceiveData(const char* data, int len)
    125 {
    126     if (m_failed)
    127         return;
    128 
    129     if (!m_decoder) {
    130         if (!m_responseEncoding.isEmpty())
    131             m_decoder = TextResourceDecoder::create("text/javascript", m_responseEncoding);
    132         else
    133             m_decoder = TextResourceDecoder::create("text/javascript", "UTF-8");
    134     }
    135 
    136     if (!len)
    137         return;
    138 
    139     if (len == -1)
    140         len = strlen(data);
    141 
    142     m_script.append(m_decoder->decode(data, len));
    143 }
    144 
    145 void WorkerScriptLoader::didFinishLoading(unsigned long identifier, double)
    146 {
    147     if (m_failed) {
    148         notifyError();
    149         return;
    150     }
    151 
    152     if (m_decoder)
    153         m_script.append(m_decoder->flush());
    154 
    155     m_identifier = identifier;
    156     notifyFinished();
    157 }
    158 
    159 void WorkerScriptLoader::didFail(const ResourceError&)
    160 {
    161     notifyError();
    162 }
    163 
    164 void WorkerScriptLoader::didFailRedirectCheck()
    165 {
    166     notifyError();
    167 }
    168 
    169 void WorkerScriptLoader::notifyError()
    170 {
    171     m_failed = true;
    172     notifyFinished();
    173 }
    174 
    175 String WorkerScriptLoader::script()
    176 {
    177     return m_script.toString();
    178 }
    179 
    180 void WorkerScriptLoader::notifyFinished()
    181 {
    182     if (!m_client || m_finishing)
    183         return;
    184 
    185     m_finishing = true;
    186     m_client->notifyFinished();
    187 }
    188 
    189 } // namespace WebCore
    190