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 "ResourceHandleClient.h"
33#include "ResourceLoaderOptions.h"
34#include "ResourceLoaderTypes.h"
35#include "ResourceRequest.h"
36#include "ResourceResponse.h"
37
38#include <wtf/Forward.h>
39#include <wtf/RefCounted.h>
40
41namespace WebCore {
42
43class AuthenticationChallenge;
44class DocumentLoader;
45class Frame;
46class FrameLoader;
47class KURL;
48class ResourceBuffer;
49class ResourceHandle;
50
51class ResourceLoader : public RefCounted<ResourceLoader>, protected ResourceHandleClient {
52public:
53    virtual ~ResourceLoader();
54
55    void cancel();
56
57    virtual bool init(const ResourceRequest&);
58
59    FrameLoader* frameLoader() const;
60    DocumentLoader* documentLoader() const { return m_documentLoader.get(); }
61    const ResourceRequest& originalRequest() const { return m_originalRequest; }
62
63    virtual void cancel(const ResourceError&);
64    ResourceError cancelledError();
65    ResourceError blockedError();
66    ResourceError cannotShowURLError();
67
68    virtual void setDefersLoading(bool);
69    bool defersLoading() const { return m_defersLoading; }
70
71    unsigned long identifier() const { return m_identifier; }
72
73    virtual void releaseResources();
74    const ResourceResponse& response() const;
75
76    ResourceBuffer* resourceData() const { return m_resourceData.get(); }
77    void clearResourceData();
78
79    virtual bool isSubresourceLoader();
80
81    virtual void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
82    virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
83    virtual void didReceiveResponse(const ResourceResponse&);
84    virtual void didReceiveData(const char*, int, long long encodedDataLength, DataPayloadType);
85    virtual void didReceiveBuffer(PassRefPtr<SharedBuffer>, long long encodedDataLength, DataPayloadType);
86    void willStopBufferingData(const char*, int);
87    virtual void didFinishLoading(double finishTime);
88    virtual void didFail(const ResourceError&);
89#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
90    virtual void didReceiveDataArray(CFArrayRef dataArray);
91#endif
92    void didChangePriority(ResourceLoadPriority);
93
94    virtual bool shouldUseCredentialStorage();
95    virtual void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
96    void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
97#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
98    virtual bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
99#endif
100    virtual void receivedCancellation(const AuthenticationChallenge&);
101
102    // ResourceHandleClient
103    virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse& redirectResponse) OVERRIDE;
104    virtual void didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
105    virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&) OVERRIDE;
106    virtual void didReceiveData(ResourceHandle*, const char*, int, int encodedDataLength) OVERRIDE;
107    virtual void didReceiveBuffer(ResourceHandle*, PassRefPtr<SharedBuffer>, int encodedDataLength) OVERRIDE;
108    virtual void didFinishLoading(ResourceHandle*, double finishTime) OVERRIDE;
109    virtual void didFail(ResourceHandle*, const ResourceError&) OVERRIDE;
110    virtual void wasBlocked(ResourceHandle*) OVERRIDE;
111    virtual void cannotShowURL(ResourceHandle*) OVERRIDE;
112    virtual void willStopBufferingData(ResourceHandle*, const char* data, int length) { willStopBufferingData(data, length); }
113    virtual bool shouldUseCredentialStorage(ResourceHandle*) OVERRIDE { return shouldUseCredentialStorage(); }
114    virtual void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) OVERRIDE { didReceiveAuthenticationChallenge(challenge); }
115    virtual void didCancelAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) OVERRIDE { didCancelAuthenticationChallenge(challenge); }
116#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
117    virtual void didReceiveDataArray(ResourceHandle*, CFArrayRef dataArray) OVERRIDE;
118#endif
119#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
120    virtual bool canAuthenticateAgainstProtectionSpace(ResourceHandle*, const ProtectionSpace& protectionSpace) OVERRIDE { return canAuthenticateAgainstProtectionSpace(protectionSpace); }
121#endif
122    virtual void receivedCancellation(ResourceHandle*, const AuthenticationChallenge& challenge) OVERRIDE { receivedCancellation(challenge); }
123#if PLATFORM(MAC)
124#if USE(CFNETWORK)
125    virtual CFCachedURLResponseRef willCacheResponse(ResourceHandle*, CFCachedURLResponseRef) OVERRIDE;
126#else
127    virtual NSCachedURLResponse* willCacheResponse(ResourceHandle*, NSCachedURLResponse*) OVERRIDE;
128#endif
129#endif // PLATFORM(MAC)
130#if PLATFORM(WIN) && USE(CFNETWORK)
131    // FIXME: Windows should use willCacheResponse - <https://bugs.webkit.org/show_bug.cgi?id=57257>.
132    virtual bool shouldCacheResponse(ResourceHandle*, CFCachedURLResponseRef) OVERRIDE;
133#endif
134
135    const KURL& url() const { return m_request.url(); }
136    ResourceHandle* handle() const { return m_handle.get(); }
137    bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbacks; }
138    void setSendCallbackPolicy(SendCallbackPolicy sendLoadCallbacks) { m_options.sendLoadCallbacks = sendLoadCallbacks; }
139    bool shouldSniffContent() const { return m_options.sniffContent == SniffContent; }
140    ClientCredentialPolicy clientCredentialPolicy() const { return m_options.clientCredentialPolicy; }
141
142    bool reachedTerminalState() const { return m_reachedTerminalState; }
143
144    const ResourceRequest& request() const { return m_request; }
145
146    void setDataBufferingPolicy(DataBufferingPolicy);
147
148protected:
149    ResourceLoader(Frame*, ResourceLoaderOptions);
150
151    friend class ResourceLoadScheduler; // for access to start()
152    // start() actually sends the load to the network (unless the load is being
153    // deferred) and should only be called by ResourceLoadScheduler or setDefersLoading().
154    void start();
155
156    void didFinishLoadingOnePart(double finishTime);
157    void cleanupForError(const ResourceError&);
158
159    bool wasCancelled() const { return m_cancellationStatus >= Cancelled; }
160
161    void didReceiveDataOrBuffer(const char*, int, PassRefPtr<SharedBuffer>, long long encodedDataLength, DataPayloadType);
162
163    RefPtr<ResourceHandle> m_handle;
164    RefPtr<Frame> m_frame;
165    RefPtr<DocumentLoader> m_documentLoader;
166    ResourceResponse m_response;
167
168private:
169    virtual void willCancel(const ResourceError&) = 0;
170    virtual void didCancel(const ResourceError&) = 0;
171
172    void addDataOrBuffer(const char*, int, SharedBuffer*, DataPayloadType);
173
174    ResourceRequest m_request;
175    ResourceRequest m_originalRequest; // Before redirects.
176    RefPtr<ResourceBuffer> m_resourceData;
177
178    unsigned long m_identifier;
179
180    bool m_reachedTerminalState;
181    bool m_notifiedLoadComplete;
182
183    enum CancellationStatus {
184        NotCancelled,
185        CalledWillCancel,
186        Cancelled,
187        FinishedCancel
188    };
189    CancellationStatus m_cancellationStatus;
190
191    bool m_defersLoading;
192    ResourceRequest m_deferredRequest;
193    ResourceLoaderOptions m_options;
194};
195
196inline const ResourceResponse& ResourceLoader::response() const
197{
198    return m_response;
199}
200
201}
202
203#endif
204