a54d18328c2c5b1628b4d81aa1cb7c0b6072a92f
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / DocumentLoader.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2011 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef DocumentLoader_h
31 #define DocumentLoader_h
32
33 #include "core/fetch/RawResource.h"
34 #include "core/fetch/ResourceLoaderOptions.h"
35 #include "core/fetch/ResourcePtr.h"
36 #include "core/loader/DocumentLoadTiming.h"
37 #include "core/loader/DocumentWriter.h"
38 #include "core/loader/NavigationAction.h"
39 #include "core/loader/SubstituteData.h"
40 #include "platform/network/ResourceError.h"
41 #include "platform/network/ResourceRequest.h"
42 #include "platform/network/ResourceResponse.h"
43 #include "wtf/HashSet.h"
44 #include "wtf/RefPtr.h"
45
46 namespace WTF {
47 class SchedulePair;
48 }
49
50 namespace WebCore {
51     class ApplicationCacheHost;
52     class ArchiveResource;
53     class ArchiveResourceCollection;
54     class ResourceFetcher;
55     class ContentFilter;
56     class FormState;
57     class Frame;
58     class FrameLoader;
59     class MHTMLArchive;
60     class Page;
61     class ResourceLoader;
62     class SharedBuffer;
63
64     class DocumentLoader : public RefCounted<DocumentLoader>, private RawResourceClient {
65         WTF_MAKE_FAST_ALLOCATED;
66     public:
67         static PassRefPtr<DocumentLoader> create(Frame* frame, const ResourceRequest& request, const SubstituteData& data)
68         {
69             return adoptRef(new DocumentLoader(frame, request, data));
70         }
71         virtual ~DocumentLoader();
72
73         Frame* frame() const { return m_frame; }
74
75         void detachFromFrame();
76
77         unsigned long mainResourceIdentifier() const;
78
79         void replaceDocument(const String& source, Document*);
80
81         const AtomicString& mimeType() const;
82
83         void setUserChosenEncoding(const String& charset);
84
85         const ResourceRequest& originalRequest() const;
86
87         const ResourceRequest& request() const;
88
89         ResourceFetcher* fetcher() const { return m_fetcher.get(); }
90
91         const SubstituteData& substituteData() const { return m_substituteData; }
92
93         const KURL& url() const;
94         const KURL& unreachableURL() const;
95         bool isURLValidForNewHistoryEntry() const;
96
97         const KURL& originalURL() const;
98         const AtomicString& responseMIMEType() const;
99
100         void updateForSameDocumentNavigation(const KURL&);
101         void stopLoading();
102         bool isCommitted() const { return m_committed; }
103         bool isLoading() const;
104         const ResourceResponse& response() const { return m_response; }
105         const ResourceError& mainDocumentError() const { return m_mainDocumentError; }
106         bool isClientRedirect() const { return m_isClientRedirect; }
107         void setIsClientRedirect(bool isClientRedirect) { m_isClientRedirect = isClientRedirect; }
108         bool replacesCurrentHistoryItem() const { return m_replacesCurrentHistoryItem; }
109         void setReplacesCurrentHistoryItem(bool replacesCurrentHistoryItem) { m_replacesCurrentHistoryItem = replacesCurrentHistoryItem; }
110         bool isLoadingInAPISense() const;
111         const AtomicString& overrideEncoding() const { return m_overrideEncoding; }
112
113         bool scheduleArchiveLoad(Resource*, const ResourceRequest&);
114
115         bool shouldContinueForNavigationPolicy(const ResourceRequest&);
116         const NavigationAction& triggeringAction() const { return m_triggeringAction; }
117         void setTriggeringAction(const NavigationAction& action) { m_triggeringAction = action; }
118
119         void setOverrideEncoding(const AtomicString& encoding) { m_overrideEncoding = encoding; }
120
121         void setDefersLoading(bool);
122
123         void startLoadingMainResource();
124         void cancelMainResourceLoad(const ResourceError&);
125
126         DocumentLoadTiming* timing() { return &m_documentLoadTiming; }
127
128         ApplicationCacheHost* applicationCacheHost() const { return m_applicationCacheHost.get(); }
129
130         void checkLoadComplete();
131
132         bool isRedirect() const { return m_redirectChain.size() > 1; }
133         void clearRedirectChain();
134         void appendRedirect(const KURL&);
135
136     protected:
137         DocumentLoader(Frame*, const ResourceRequest&, const SubstituteData&);
138
139         Vector<KURL> m_redirectChain;
140
141     private:
142         static PassRefPtr<DocumentWriter> createWriterFor(Frame*, const Document* ownerDocument, const KURL&, const AtomicString& mimeType, const AtomicString& encoding, bool userChosen, bool dispatch);
143
144         void ensureWriter(const AtomicString& mimeType, const KURL& overridingURL = KURL());
145         void endWriting(DocumentWriter*);
146
147         Document* document() const;
148         FrameLoader* frameLoader() const;
149
150         void commitIfReady();
151         void commitData(const char* bytes, size_t length);
152         void setMainDocumentError(const ResourceError&);
153         void clearMainResourceLoader();
154         ResourceLoader* mainResourceLoader() const;
155         void clearMainResourceHandle();
156
157         bool maybeCreateArchive();
158         void clearArchiveResources();
159
160         void prepareSubframeArchiveLoadIfNeeded();
161         void addAllArchiveResources(MHTMLArchive*);
162
163         void willSendRequest(ResourceRequest&, const ResourceResponse&);
164         void finishedLoading(double finishTime);
165         void mainReceivedError(const ResourceError&);
166         virtual void redirectReceived(Resource*, ResourceRequest&, const ResourceResponse&) OVERRIDE FINAL;
167         virtual void updateRequest(Resource*, const ResourceRequest&) OVERRIDE FINAL;
168         virtual void responseReceived(Resource*, const ResourceResponse&) OVERRIDE FINAL;
169         virtual void dataReceived(Resource*, const char* data, int length) OVERRIDE FINAL;
170         virtual void notifyFinished(Resource*) OVERRIDE FINAL;
171
172         bool maybeLoadEmpty();
173
174         bool isRedirectAfterPost(const ResourceRequest&, const ResourceResponse&);
175
176         bool shouldContinueForResponse() const;
177
178         Frame* m_frame;
179         RefPtr<ResourceFetcher> m_fetcher;
180
181         ResourcePtr<RawResource> m_mainResource;
182
183         RefPtr<DocumentWriter> m_writer;
184
185         // A reference to actual request used to create the data source.
186         // The only part of this request that should change is the url, and
187         // that only in the case of a same-document navigation.
188         ResourceRequest m_originalRequest;
189
190         SubstituteData m_substituteData;
191
192         // The 'working' request. It may be mutated
193         // several times from the original request to include additional
194         // headers, cookie information, canonicalization and redirects.
195         ResourceRequest m_request;
196
197         ResourceResponse m_response;
198
199         ResourceError m_mainDocumentError;
200
201         bool m_committed;
202         bool m_isClientRedirect;
203         bool m_replacesCurrentHistoryItem;
204
205         AtomicString m_overrideEncoding;
206
207         // The action that triggered loading - we keep this around for the
208         // benefit of the various policy handlers.
209         NavigationAction m_triggeringAction;
210
211         OwnPtr<ArchiveResourceCollection> m_archiveResourceCollection;
212         RefPtr<MHTMLArchive> m_archive;
213
214         bool m_loadingMainResource;
215         DocumentLoadTiming m_documentLoadTiming;
216
217         double m_timeOfLastDataReceived;
218
219         friend class ApplicationCacheHost;  // for substitute resource delivery
220         OwnPtr<ApplicationCacheHost> m_applicationCacheHost;
221     };
222 }
223
224 #endif // DocumentLoader_h