Home | History | Annotate | Download | only in fetch
      1 /*
      2  * Copyright (C) 2005, 2006, 2011 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      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  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef ResourceLoader_h
     30 #define ResourceLoader_h
     31 
     32 #include "core/fetch/ResourceLoaderOptions.h"
     33 #include "platform/network/ResourceRequest.h"
     34 #include "public/platform/WebURLLoader.h"
     35 #include "public/platform/WebURLLoaderClient.h"
     36 #include "wtf/Forward.h"
     37 #include "wtf/RefCounted.h"
     38 
     39 namespace WebCore {
     40 
     41 class Resource;
     42 class KURL;
     43 class ResourceError;
     44 class ResourceResponse;
     45 class ResourceLoaderHost;
     46 
     47 class ResourceLoader : public RefCounted<ResourceLoader>, protected blink::WebURLLoaderClient {
     48 public:
     49     static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
     50     virtual ~ResourceLoader();
     51 
     52     void start();
     53     void changeToSynchronous();
     54 
     55     void cancel();
     56     void cancel(const ResourceError&);
     57     void cancelIfNotFinishing();
     58 
     59     Resource* cachedResource() { return m_resource; }
     60     const ResourceRequest& originalRequest() const { return m_originalRequest; }
     61 
     62     void setDefersLoading(bool);
     63     bool defersLoading() const { return m_defersLoading; }
     64 
     65     void releaseResources();
     66 
     67     void didChangePriority(ResourceLoadPriority);
     68 
     69     // WebURLLoaderClient
     70     virtual void willSendRequest(blink::WebURLLoader*, blink::WebURLRequest&, const blink::WebURLResponse& redirectResponse) OVERRIDE;
     71     virtual void didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
     72     virtual void didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse&) OVERRIDE;
     73     virtual void didReceiveData(blink::WebURLLoader*, const char*, int, int encodedDataLength) OVERRIDE;
     74     virtual void didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length) OVERRIDE;
     75     virtual void didFinishLoading(blink::WebURLLoader*, double finishTime) OVERRIDE;
     76     virtual void didFail(blink::WebURLLoader*, const blink::WebURLError&) OVERRIDE;
     77     virtual void didDownloadData(blink::WebURLLoader*, int, int) OVERRIDE;
     78 
     79     const KURL& url() const { return m_request.url(); }
     80     bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbacks; }
     81     bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; }
     82     bool isLoadedBy(ResourceLoaderHost*) const;
     83 
     84     bool reachedTerminalState() const { return m_state == Terminated; }
     85     const ResourceRequest& request() const { return m_request; }
     86 
     87 private:
     88     ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&);
     89 
     90     void init(const ResourceRequest&);
     91     void requestSynchronously();
     92 
     93     void didFinishLoadingOnePart(double finishTime);
     94 
     95     OwnPtr<blink::WebURLLoader> m_loader;
     96     RefPtr<ResourceLoaderHost> m_host;
     97 
     98     ResourceRequest m_request;
     99     ResourceRequest m_originalRequest; // Before redirects.
    100 
    101     bool m_notifiedLoadComplete;
    102 
    103     bool m_defersLoading;
    104     ResourceRequest m_deferredRequest;
    105     ResourceLoaderOptions m_options;
    106 
    107     enum ResourceLoaderState {
    108         Initialized,
    109         Finishing,
    110         Terminated
    111     };
    112 
    113     enum ConnectionState {
    114         ConnectionStateNew,
    115         ConnectionStateStarted,
    116         ConnectionStateReceivedResponse,
    117         ConnectionStateReceivingData,
    118         ConnectionStateFinishedLoading,
    119         ConnectionStateCanceled,
    120         ConnectionStateFailed,
    121     };
    122 
    123     class RequestCountTracker {
    124     public:
    125         RequestCountTracker(ResourceLoaderHost*, Resource*);
    126         ~RequestCountTracker();
    127     private:
    128         ResourceLoaderHost* m_host;
    129         Resource* m_resource;
    130     };
    131 
    132     Resource* m_resource;
    133     ResourceLoaderState m_state;
    134 
    135     // Used for sanity checking to make sure we don't experience illegal state
    136     // transitions.
    137     ConnectionState m_connectionState;
    138 
    139     OwnPtr<RequestCountTracker> m_requestCountTracker;
    140 };
    141 
    142 }
    143 
    144 #endif
    145