1/*
2 * Copyright (C) 2005, 2006 Apple Computer, 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 "ResourceRequest.h"
34#include "ResourceResponse.h"
35#include <wtf/RefCounted.h>
36#include "AuthenticationChallenge.h"
37#include "KURL.h"
38
39#include <wtf/Forward.h>
40
41namespace WebCore {
42
43    class ApplicationCacheHost;
44    class DocumentLoader;
45    class Frame;
46    class FrameLoader;
47    class ProtectionSpace;
48    class ResourceHandle;
49    class SharedBuffer;
50
51    class ResourceLoader : public RefCounted<ResourceLoader>, protected ResourceHandleClient {
52    public:
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
62        virtual void cancel(const ResourceError&);
63        ResourceError cancelledError();
64        ResourceError blockedError();
65        ResourceError cannotShowURLError();
66
67        virtual void setDefersLoading(bool);
68#if PLATFORM(ANDROID)
69// TODO: This needs upstreaming to WebKit.
70        virtual void pauseLoad(bool);
71#endif
72
73        void setIdentifier(unsigned long identifier) { m_identifier = identifier; }
74        unsigned long identifier() const { return m_identifier; }
75
76        virtual void releaseResources();
77        const ResourceResponse& response() const;
78
79        virtual void addData(const char*, int, bool allAtOnce);
80        virtual PassRefPtr<SharedBuffer> resourceData();
81        void clearResourceData();
82
83        virtual void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
84        virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
85        virtual void didReceiveResponse(const ResourceResponse&);
86        virtual void didReceiveData(const char*, int, long long encodedDataLength, bool allAtOnce);
87        virtual void didReceiveCachedMetadata(const char*, int) { }
88        void willStopBufferingData(const char*, int);
89        virtual void didFinishLoading(double finishTime);
90        virtual void didFail(const ResourceError&);
91#if HAVE(CFNETWORK_DATA_ARRAY_CALLBACK)
92        virtual void didReceiveDataArray(CFArrayRef dataArray);
93#endif
94
95        virtual bool shouldUseCredentialStorage();
96        virtual void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
97        void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
98#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
99        virtual bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
100#endif
101        virtual void receivedCancellation(const AuthenticationChallenge&);
102
103        // ResourceHandleClient
104        virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse& redirectResponse);
105        virtual void didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
106        virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
107        virtual void didReceiveData(ResourceHandle*, const char*, int, int encodedDataLength);
108        virtual void didReceiveCachedMetadata(ResourceHandle*, const char* data, int length) { didReceiveCachedMetadata(data, length); }
109        virtual void didFinishLoading(ResourceHandle*, double finishTime);
110        virtual void didFail(ResourceHandle*, const ResourceError&);
111        virtual void wasBlocked(ResourceHandle*);
112        virtual void cannotShowURL(ResourceHandle*);
113        virtual void willStopBufferingData(ResourceHandle*, const char* data, int length) { willStopBufferingData(data, length); }
114        virtual bool shouldUseCredentialStorage(ResourceHandle*) { return shouldUseCredentialStorage(); }
115        virtual void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) { didReceiveAuthenticationChallenge(challenge); }
116        virtual void didCancelAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) { didCancelAuthenticationChallenge(challenge); }
117#if HAVE(CFNETWORK_DATA_ARRAY_CALLBACK)
118        virtual void didReceiveDataArray(ResourceHandle*, CFArrayRef dataArray);
119#endif
120#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
121        virtual bool canAuthenticateAgainstProtectionSpace(ResourceHandle*, const ProtectionSpace& protectionSpace) { return canAuthenticateAgainstProtectionSpace(protectionSpace); }
122#endif
123        virtual void receivedCancellation(ResourceHandle*, const AuthenticationChallenge& challenge) { receivedCancellation(challenge); }
124        virtual void willCacheResponse(ResourceHandle*, CacheStoragePolicy&);
125#if PLATFORM(MAC)
126        virtual NSCachedURLResponse* willCacheResponse(ResourceHandle*, NSCachedURLResponse*);
127#endif
128#if USE(CFNETWORK)
129        virtual bool shouldCacheResponse(ResourceHandle*, CFCachedURLResponseRef);
130#endif
131#if ENABLE(BLOB)
132        virtual AsyncFileStream* createAsyncFileStream(FileStreamClient*);
133#endif
134
135        const KURL& url() const { return m_request.url(); }
136        ResourceHandle* handle() const { return m_handle.get(); }
137        bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
138
139        bool reachedTerminalState() const { return m_reachedTerminalState; }
140
141        void setShouldBufferData(bool shouldBufferData);
142
143    protected:
144        ResourceLoader(Frame*, bool sendResourceLoadCallbacks, bool shouldContentSniff);
145
146#if ENABLE(OFFLINE_WEB_APPLICATIONS)
147        friend class ApplicationCacheHost;  // for access to request()
148#endif
149        friend class ResourceLoadScheduler; // for access to start()
150        // start() actually sends the load to the network (unless the load is being
151        // deferred) and should only be called by ResourceLoadScheduler or setDefersLoading().
152        void start();
153
154        virtual void didCancel(const ResourceError&);
155        void didFinishLoadingOnePart(double finishTime);
156
157        const ResourceRequest& request() const { return m_request; }
158        bool cancelled() const { return m_cancelled; }
159        bool defersLoading() const { return m_defersLoading; }
160
161        RefPtr<ResourceHandle> m_handle;
162        RefPtr<Frame> m_frame;
163        RefPtr<DocumentLoader> m_documentLoader;
164        ResourceResponse m_response;
165
166    private:
167        ResourceRequest m_request;
168        RefPtr<SharedBuffer> m_resourceData;
169
170        unsigned long m_identifier;
171
172        bool m_reachedTerminalState;
173        bool m_cancelled;
174        bool m_calledDidFinishLoad;
175
176        bool m_sendResourceLoadCallbacks;
177        bool m_shouldContentSniff;
178        bool m_shouldBufferData;
179        bool m_defersLoading;
180        ResourceRequest m_deferredRequest;
181    };
182
183}
184
185#endif
186