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 ResourceHandle;
48    class SharedBuffer;
49
50    class ResourceLoader : public RefCounted<ResourceLoader>, protected ResourceHandleClient {
51    public:
52        virtual ~ResourceLoader();
53
54        void cancel();
55
56        virtual bool load(const ResourceRequest&);
57
58        FrameLoader* frameLoader() const;
59        DocumentLoader* documentLoader() const { return m_documentLoader.get(); }
60
61        virtual void cancel(const ResourceError&);
62        ResourceError cancelledError();
63        ResourceError blockedError();
64        ResourceError cannotShowURLError();
65
66        virtual void setDefersLoading(bool);
67#if PLATFORM(ANDROID)
68// TODO: This needs upstreaming to WebKit.
69        virtual void pauseLoad(bool);
70#endif
71
72        void setIdentifier(unsigned long identifier) { m_identifier = identifier; }
73        unsigned long identifier() const { return m_identifier; }
74
75        virtual void releaseResources();
76        const ResourceResponse& response() const;
77
78        virtual void addData(const char*, int, bool allAtOnce);
79        virtual PassRefPtr<SharedBuffer> resourceData();
80        void clearResourceData();
81
82        virtual void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
83        virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
84        virtual void didReceiveResponse(const ResourceResponse&);
85        virtual void didReceiveData(const char*, int, long long lengthReceived, bool allAtOnce);
86        void willStopBufferingData(const char*, int);
87        virtual void didFinishLoading();
88        virtual void didFail(const ResourceError&);
89
90        virtual bool shouldUseCredentialStorage();
91        virtual void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
92        void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
93        virtual void receivedCancellation(const AuthenticationChallenge&);
94
95        // ResourceHandleClient
96        virtual void willSendRequest(ResourceHandle*, ResourceRequest&, const ResourceResponse& redirectResponse);
97        virtual void didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
98        virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
99        virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
100        virtual void didFinishLoading(ResourceHandle*);
101        virtual void didFail(ResourceHandle*, const ResourceError&);
102        virtual void wasBlocked(ResourceHandle*);
103        virtual void cannotShowURL(ResourceHandle*);
104        virtual void willStopBufferingData(ResourceHandle*, const char* data, int length) { willStopBufferingData(data, length); }
105        virtual bool shouldUseCredentialStorage(ResourceHandle*) { return shouldUseCredentialStorage(); }
106        virtual void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) { didReceiveAuthenticationChallenge(challenge); }
107        virtual void didCancelAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge) { didCancelAuthenticationChallenge(challenge); }
108        virtual void receivedCancellation(ResourceHandle*, const AuthenticationChallenge& challenge) { receivedCancellation(challenge); }
109        virtual void willCacheResponse(ResourceHandle*, CacheStoragePolicy&);
110#if PLATFORM(MAC)
111        virtual NSCachedURLResponse* willCacheResponse(ResourceHandle*, NSCachedURLResponse*);
112#endif
113#if USE(CFNETWORK)
114        virtual bool shouldCacheResponse(ResourceHandle*, CFCachedURLResponseRef);
115#endif
116
117        ResourceHandle* handle() const { return m_handle.get(); }
118        bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks; }
119
120        void setShouldBufferData(bool shouldBufferData);
121
122    protected:
123        ResourceLoader(Frame*, bool sendResourceLoadCallbacks, bool shouldContentSniff);
124
125#if ENABLE(OFFLINE_WEB_APPLICATIONS)
126        friend class ApplicationCacheHost;  // for access to request()
127#endif
128
129        virtual void didCancel(const ResourceError&);
130        void didFinishLoadingOnePart();
131
132        const ResourceRequest& request() const { return m_request; }
133        bool reachedTerminalState() const { return m_reachedTerminalState; }
134        bool cancelled() const { return m_cancelled; }
135        bool defersLoading() const { return m_defersLoading; }
136
137        RefPtr<ResourceHandle> m_handle;
138        RefPtr<Frame> m_frame;
139        RefPtr<DocumentLoader> m_documentLoader;
140        ResourceResponse m_response;
141
142    private:
143        ResourceRequest m_request;
144        RefPtr<SharedBuffer> m_resourceData;
145
146        unsigned long m_identifier;
147
148        bool m_reachedTerminalState;
149        bool m_cancelled;
150        bool m_calledDidFinishLoad;
151
152        bool m_sendResourceLoadCallbacks;
153        bool m_shouldContentSniff;
154        bool m_shouldBufferData;
155        bool m_defersLoading;
156        ResourceRequest m_deferredRequest;
157    };
158
159}
160
161#endif
162