2 * Copyright (C) 2005, 2006, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef ResourceLoader_h
30 #define ResourceLoader_h
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"
44 class ResourceResponse;
45 class ResourceLoaderHost;
47 class ResourceLoader FINAL : public RefCounted<ResourceLoader>, protected blink::WebURLLoaderClient {
49 static PassRefPtr<ResourceLoader> create(ResourceLoaderHost*, Resource*, const ResourceRequest&, const ResourceLoaderOptions&);
50 virtual ~ResourceLoader();
53 void changeToSynchronous();
56 void cancel(const ResourceError&);
57 void cancelIfNotFinishing();
59 Resource* cachedResource() { return m_resource; }
60 const ResourceRequest& originalRequest() const { return m_originalRequest; }
62 void setDefersLoading(bool);
63 bool defersLoading() const { return m_defersLoading; }
65 void releaseResources();
67 void didChangePriority(ResourceLoadPriority);
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, int64 encodedDataLength) OVERRIDE;
76 virtual void didFail(blink::WebURLLoader*, const blink::WebURLError&) OVERRIDE;
77 virtual void didDownloadData(blink::WebURLLoader*, int, int) OVERRIDE;
79 const KURL& url() const { return m_request.url(); }
80 bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; }
81 bool isLoadedBy(ResourceLoaderHost*) const;
83 bool reachedTerminalState() const { return m_state == Terminated; }
84 const ResourceRequest& request() const { return m_request; }
87 ResourceLoader(ResourceLoaderHost*, Resource*, const ResourceLoaderOptions&);
89 void init(const ResourceRequest&);
90 void requestSynchronously();
92 void didFinishLoadingOnePart(double finishTime, int64_t encodedDataLength);
94 bool responseNeedsAccessControlCheck() const;
96 OwnPtr<blink::WebURLLoader> m_loader;
97 RefPtr<ResourceLoaderHost> m_host;
99 ResourceRequest m_request;
100 ResourceRequest m_originalRequest; // Before redirects.
102 bool m_notifiedLoadComplete;
104 bool m_defersLoading;
105 ResourceRequest m_deferredRequest;
106 ResourceLoaderOptions m_options;
108 enum ResourceLoaderState {
114 enum ConnectionState {
116 ConnectionStateStarted,
117 ConnectionStateReceivedResponse,
118 ConnectionStateReceivingData,
119 ConnectionStateFinishedLoading,
120 ConnectionStateCanceled,
121 ConnectionStateFailed,
124 class RequestCountTracker {
126 RequestCountTracker(ResourceLoaderHost*, Resource*);
127 ~RequestCountTracker();
129 ResourceLoaderHost* m_host;
130 Resource* m_resource;
133 Resource* m_resource;
134 ResourceLoaderState m_state;
136 // Used for sanity checking to make sure we don't experience illegal state
138 ConnectionState m_connectionState;
140 OwnPtr<RequestCountTracker> m_requestCountTracker;