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 blink {
     40 class WebThreadedDataReceiver;
     41 }
     42 
     43 namespace blink {
     44 
     45 class Resource;
     46 class KURL;
     47 class ResourceError;
     48 class ResourceResponse;
     49 class ResourceLoaderHost;
     50 
     51 class ResourceLoader FINAL : public RefCountedWillBeGarbageCollectedFinalized<ResourceLoader>, protected WebURLLoaderClient {
     52 public:
     53     static PassRefPtrWillBeRawPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
     54     virtual ~ResourceLoader();
     55     void trace(Visitor*);
     56 
     57     void start();
     58     void changeToSynchronous();
     59 
     60     void cancel();
     61     void cancel(const ResourceError&);
     62     void cancelIfNotFinishing();
     63 
     64     Resource* cachedResource() { return m_resource; }
     65     const ResourceRequest& originalRequest() const { return m_originalRequest; }
     66 
     67     void setDefersLoading(bool);
     68     bool defersLoading() const { return m_defersLoading; }
     69 
     70     void attachThreadedDataReceiver(PassOwnPtr<blink::WebThreadedDataReceiver>);
     71 
     72     void releaseResources();
     73 
     74     void didChangePriority(ResourceLoadPriority, int intraPriorityValue);
     75 
     76     // WebURLLoaderClient
     77     virtual void willSendRequest(blink::WebURLLoader*, blink::WebURLRequest&, const blink::WebURLResponse& redirectResponse) OVERRIDE;
     78     virtual void didSendData(blink::WebURLLoader*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
     79     virtual void didReceiveResponse(blink::WebURLLoader*, const blink::WebURLResponse&) OVERRIDE;
     80     virtual void didReceiveData(blink::WebURLLoader*, const char*, int, int encodedDataLength) OVERRIDE;
     81     virtual void didReceiveCachedMetadata(blink::WebURLLoader*, const char* data, int length) OVERRIDE;
     82     virtual void didFinishLoading(blink::WebURLLoader*, double finishTime, int64 encodedDataLength) OVERRIDE;
     83     virtual void didFail(blink::WebURLLoader*, const blink::WebURLError&) OVERRIDE;
     84     virtual void didDownloadData(blink::WebURLLoader*, int, int) OVERRIDE;
     85 
     86     const KURL& url() const { return m_request.url(); }
     87     bool isLoadedBy(ResourceLoaderHost*) const;
     88 
     89     bool reachedTerminalState() const { return m_state == Terminated; }
     90     const ResourceRequest& request() const { return m_request; }
     91 
     92     class RequestCountTracker {
     93     public:
     94         RequestCountTracker(ResourceLoaderHost*, Resource*);
     95         RequestCountTracker(const RequestCountTracker&);
     96         ~RequestCountTracker();
     97     private:
     98         ResourceLoaderHost* m_host;
     99         Resource* m_resource;
    100     };
    101 
    102 private:
    103     ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&);
    104 
    105     void init(const ResourceRequest&);
    106     void requestSynchronously();
    107 
    108     void didFinishLoadingOnePart(double finishTime, int64_t encodedDataLength);
    109 
    110     bool responseNeedsAccessControlCheck() const;
    111 
    112     ResourceRequest& applyOptions(ResourceRequest&) const;
    113 
    114     OwnPtr<blink::WebURLLoader> m_loader;
    115     RefPtrWillBeMember<ResourceLoaderHost> m_host;
    116 
    117     ResourceRequest m_request;
    118     ResourceRequest m_originalRequest; // Before redirects.
    119 
    120     bool m_notifiedLoadComplete;
    121 
    122     bool m_defersLoading;
    123     ResourceRequest m_deferredRequest;
    124     ResourceLoaderOptions m_options;
    125 
    126     enum ResourceLoaderState {
    127         Initialized,
    128         Finishing,
    129         Terminated
    130     };
    131 
    132     enum ConnectionState {
    133         ConnectionStateNew,
    134         ConnectionStateStarted,
    135         ConnectionStateReceivedResponse,
    136         ConnectionStateReceivingData,
    137         ConnectionStateFinishedLoading,
    138         ConnectionStateCanceled,
    139         ConnectionStateFailed,
    140     };
    141 
    142     RawPtrWillBeMember<Resource> m_resource;
    143     ResourceLoaderState m_state;
    144 
    145     // Used for sanity checking to make sure we don't experience illegal state
    146     // transitions.
    147     ConnectionState m_connectionState;
    148 
    149     OwnPtr<RequestCountTracker> m_requestCountTracker;
    150 };
    151 
    152 }
    153 
    154 #endif
    155