Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
      3  * Copyright (C) 2013, Intel Corporation
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef DocumentThreadableLoader_h
     33 #define DocumentThreadableLoader_h
     34 
     35 #include "core/fetch/RawResource.h"
     36 #include "core/fetch/ResourceOwner.h"
     37 #include "core/loader/ThreadableLoader.h"
     38 #include "platform/Timer.h"
     39 #include "platform/network/ResourceError.h"
     40 #include "wtf/Forward.h"
     41 #include "wtf/OwnPtr.h"
     42 #include "wtf/PassRefPtr.h"
     43 #include "wtf/RefCounted.h"
     44 #include "wtf/text/WTFString.h"
     45 
     46 namespace WebCore {
     47 
     48 class Document;
     49 class KURL;
     50 class ResourceRequest;
     51 class SecurityOrigin;
     52 class ThreadableLoaderClient;
     53 
     54 class DocumentThreadableLoader : public RefCounted<DocumentThreadableLoader>, public ThreadableLoader, private ResourceOwner<RawResource>  {
     55     WTF_MAKE_FAST_ALLOCATED;
     56     public:
     57         static void loadResourceSynchronously(Document*, const ResourceRequest&, ThreadableLoaderClient&, const ThreadableLoaderOptions&);
     58         static PassRefPtr<DocumentThreadableLoader> create(Document*, ThreadableLoaderClient*, const ResourceRequest&, const ThreadableLoaderOptions&);
     59         virtual ~DocumentThreadableLoader();
     60 
     61         virtual void cancel();
     62         virtual void setDefersLoading(bool);
     63 
     64         using RefCounted<DocumentThreadableLoader>::ref;
     65         using RefCounted<DocumentThreadableLoader>::deref;
     66 
     67     protected:
     68         virtual void refThreadableLoader() { ref(); }
     69         virtual void derefThreadableLoader() { deref(); }
     70 
     71     private:
     72         enum BlockingBehavior {
     73             LoadSynchronously,
     74             LoadAsynchronously
     75         };
     76 
     77         DocumentThreadableLoader(Document*, ThreadableLoaderClient*, BlockingBehavior, const ResourceRequest&, const ThreadableLoaderOptions&);
     78 
     79         // RawResourceClient
     80         virtual void dataSent(Resource*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
     81         virtual void responseReceived(Resource*, const ResourceResponse&);
     82         virtual void dataReceived(Resource*, const char* data, int dataLength);
     83         virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&);
     84         virtual void notifyFinished(Resource*);
     85         virtual void dataDownloaded(Resource*, int);
     86 
     87         void cancelWithError(const ResourceError&);
     88         void didReceiveResponse(unsigned long identifier, const ResourceResponse&);
     89         void didReceiveData(unsigned long identifier, const char* data, int dataLength);
     90         void didFinishLoading(unsigned long identifier, double finishTime);
     91         void didFail(unsigned long identifier, const ResourceError&);
     92         void didTimeout(Timer<DocumentThreadableLoader>*);
     93         void makeCrossOriginAccessRequest(const ResourceRequest&);
     94         void makeSimpleCrossOriginAccessRequest(const ResourceRequest&);
     95         void makeCrossOriginAccessRequestWithPreflight(const ResourceRequest&);
     96         void preflightSuccess();
     97         void preflightFailure(unsigned long identifier, const String& url, const String& errorDescription);
     98 
     99         void loadRequest(const ResourceRequest&, SecurityCheckPolicy);
    100         bool isAllowedRedirect(const KURL&) const;
    101         bool isAllowedByPolicy(const KURL&) const;
    102 
    103         SecurityOrigin* securityOrigin() const;
    104         bool checkCrossOriginAccessRedirectionUrl(const KURL&, String& errorDescription);
    105 
    106         ThreadableLoaderClient* m_client;
    107         Document* m_document;
    108         ThreadableLoaderOptions m_options;
    109         bool m_sameOriginRequest;
    110         bool m_simpleRequest;
    111         bool m_async;
    112         OwnPtr<ResourceRequest> m_actualRequest; // non-null during Access Control preflight checks
    113         Timer<DocumentThreadableLoader> m_timeoutTimer;
    114     };
    115 
    116 } // namespace WebCore
    117 
    118 #endif // DocumentThreadableLoader_h
    119