2 * Copyright (C) 2006, 2007, 2008, 2009 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 DocumentLoader_h
30 #define DocumentLoader_h
32 #include "NavigationAction.h"
33 #include "ResourceError.h"
34 #include "ResourceRequest.h"
35 #include "ResourceResponse.h"
36 #include "SubstituteData.h"
41 class ApplicationCache
;
42 class ApplicationCacheGroup
;
43 class ApplicationCacheResource
;
45 class ArchiveResource
;
46 class ArchiveResourceCollection
;
50 class MainResourceLoader
;
54 class SubstituteResource
;
56 typedef HashSet
<RefPtr
<ResourceLoader
> > ResourceLoaderSet
;
57 typedef Vector
<ResourceResponse
> ResponseVector
;
59 class DocumentLoader
: public RefCounted
<DocumentLoader
> {
61 static PassRefPtr
<DocumentLoader
> create(const ResourceRequest
& request
, const SubstituteData
& data
)
63 return adoptRef(new DocumentLoader(request
, data
));
65 virtual ~DocumentLoader();
67 void setFrame(Frame
*);
68 Frame
* frame() const { return m_frame
; }
70 virtual void attachToFrame();
71 virtual void detachFromFrame();
73 FrameLoader
* frameLoader() const;
74 MainResourceLoader
* mainResourceLoader() const { return m_mainResourceLoader
.get(); }
75 PassRefPtr
<SharedBuffer
> mainResourceData() const;
77 const ResourceRequest
& originalRequest() const;
78 const ResourceRequest
& originalRequestCopy() const;
80 const ResourceRequest
& request() const;
81 ResourceRequest
& request();
82 void setRequest(const ResourceRequest
&);
84 const SubstituteData
& substituteData() const { return m_substituteData
; }
86 const KURL
& url() const;
87 const KURL
& unreachableURL() const;
89 const KURL
& originalURL() const;
90 const KURL
& requestURL() const;
91 const KURL
& responseURL() const;
92 const String
& responseMIMEType() const;
94 void replaceRequestURLForAnchorScroll(const KURL
&);
95 bool isStopping() const { return m_isStopping
; }
97 void setCommitted(bool committed
) { m_committed
= committed
; }
98 bool isCommitted() const { return m_committed
; }
99 bool isLoading() const { return m_loading
; }
100 void setLoading(bool loading
) { m_loading
= loading
; }
101 void updateLoading();
102 void receivedData(const char*, int);
103 void setupForReplaceByMIMEType(const String
& newMIMEType
);
104 void finishedLoading();
105 const ResourceResponse
& response() const { return m_response
; }
106 const ResourceError
& mainDocumentError() const { return m_mainDocumentError
; }
107 void mainReceivedError(const ResourceError
&, bool isComplete
);
108 void setResponse(const ResourceResponse
& response
) { m_response
= response
; }
109 void prepareForLoadStart();
110 bool isClientRedirect() const { return m_isClientRedirect
; }
111 void setIsClientRedirect(bool isClientRedirect
) { m_isClientRedirect
= isClientRedirect
; }
112 bool isLoadingInAPISense() const;
113 void setPrimaryLoadComplete(bool);
114 void setTitle(const String
&);
115 const String
& overrideEncoding() const { return m_overrideEncoding
; }
118 void schedule(SchedulePair
*);
119 void unschedule(SchedulePair
*);
122 void addAllArchiveResources(Archive
*);
123 void addArchiveResource(PassRefPtr
<ArchiveResource
>);
125 // Return an ArchiveResource for the URL, either creating from live data or
126 // pulling from the ArchiveResourceCollection
127 PassRefPtr
<ArchiveResource
> subresource(const KURL
&) const;
128 // Return the ArchiveResource for the URL only when loading an Archive
129 ArchiveResource
* archiveResourceForURL(const KURL
&) const;
131 PassRefPtr
<Archive
> popArchiveForSubframe(const String
& frameName
);
132 void clearArchiveResources();
133 void setParsedArchiveData(PassRefPtr
<SharedBuffer
>);
134 SharedBuffer
* parsedArchiveData() const;
136 PassRefPtr
<ArchiveResource
> mainResource() const;
137 void getSubresources(Vector
<PassRefPtr
<ArchiveResource
> >&) const;
139 bool scheduleArchiveLoad(ResourceLoader
*, const ResourceRequest
&, const KURL
&);
141 bool isSubstituteLoadPending(ResourceLoader
*) const;
143 void cancelPendingSubstituteLoad(ResourceLoader
*);
145 void addResponse(const ResourceResponse
&);
146 const ResponseVector
& responses() const { return m_responses
; }
148 const NavigationAction
& triggeringAction() const { return m_triggeringAction
; }
149 void setTriggeringAction(const NavigationAction
& action
) { m_triggeringAction
= action
; }
150 void setOverrideEncoding(const String
& encoding
) { m_overrideEncoding
= encoding
; }
151 void setLastCheckedRequest(const ResourceRequest
& request
) { m_lastCheckedRequest
= request
; }
152 const ResourceRequest
& lastCheckedRequest() { return m_lastCheckedRequest
; }
154 void stopRecordingResponses();
155 const String
& title() const { return m_pageTitle
; }
157 KURL
urlForHistory() const;
158 bool urlForHistoryReflectsFailure() const;
159 bool urlForHistoryReflectsServerRedirect() const { return urlForHistory() != url(); }
160 bool urlForHistoryReflectsClientRedirect() const { return m_urlForHistoryReflectsClientRedirect
; }
161 void setURLForHistoryReflectsClientRedirect(bool b
) { m_urlForHistoryReflectsClientRedirect
= b
; }
163 void loadFromCachedPage(PassRefPtr
<CachedPage
>);
164 void setLoadingFromCachedPage(bool loading
) { m_loadingFromCachedPage
= loading
; }
165 bool isLoadingFromCachedPage() const { return m_loadingFromCachedPage
; }
167 void setDefersLoading(bool);
169 bool startLoadingMainResource(unsigned long identifier
);
170 void cancelMainResourceLoad(const ResourceError
&);
172 void iconLoadDecisionAvailable();
174 bool isLoadingMainResource() const;
175 bool isLoadingSubresources() const;
176 bool isLoadingPlugIns() const;
177 bool isLoadingMultipartContent() const;
179 void stopLoadingPlugIns();
180 void stopLoadingSubresources();
182 void addSubresourceLoader(ResourceLoader
*);
183 void removeSubresourceLoader(ResourceLoader
*);
184 void addPlugInStreamLoader(ResourceLoader
*);
185 void removePlugInStreamLoader(ResourceLoader
*);
187 void subresourceLoaderFinishedLoadingOnePart(ResourceLoader
*);
189 void setDeferMainResourceDataLoad(bool defer
) { m_deferMainResourceDataLoad
= defer
; }
190 bool deferMainResourceDataLoad() const { return m_deferMainResourceDataLoad
; }
192 void didTellClientAboutLoad(const String
& url
) { m_resourcesClientKnowsAbout
.add(url
); }
193 bool haveToldClientAboutLoad(const String
& url
) { return m_resourcesClientKnowsAbout
.contains(url
); }
194 void recordMemoryCacheLoadForFutureClientNotification(const String
& url
);
195 void takeMemoryCacheLoadsForClientNotification(Vector
<String
>& loads
);
197 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
198 bool scheduleApplicationCacheLoad(ResourceLoader
*, const ResourceRequest
&, const KURL
& originalURL
);
199 bool scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader
*, const ResourceRequest
&, ApplicationCache
* = 0);
200 bool shouldLoadResourceFromApplicationCache(const ResourceRequest
&, ApplicationCacheResource
*&);
201 bool getApplicationCacheFallbackResource(const ResourceRequest
&, ApplicationCacheResource
*&, ApplicationCache
* = 0);
203 void setCandidateApplicationCacheGroup(ApplicationCacheGroup
* group
);
204 ApplicationCacheGroup
* candidateApplicationCacheGroup() const { return m_candidateApplicationCacheGroup
; }
206 void setApplicationCache(PassRefPtr
<ApplicationCache
> applicationCache
);
207 ApplicationCache
* applicationCache() const { return m_applicationCache
.get(); }
209 ApplicationCache
* mainResourceApplicationCache() const;
213 DocumentLoader(const ResourceRequest
&, const SubstituteData
&);
215 bool m_deferMainResourceDataLoad
;
218 void setupForReplace();
219 void commitIfReady();
221 void setMainDocumentError(const ResourceError
&);
222 void commitLoad(const char*, int);
223 bool doesProgressiveLoad(const String
& MIMEType
) const;
225 void deliverSubstituteResourcesAfterDelay();
226 void substituteResourceDeliveryTimerFired(Timer
<DocumentLoader
>*);
230 RefPtr
<MainResourceLoader
> m_mainResourceLoader
;
231 ResourceLoaderSet m_subresourceLoaders
;
232 ResourceLoaderSet m_multipartSubresourceLoaders
;
233 ResourceLoaderSet m_plugInStreamLoaders
;
235 RefPtr
<SharedBuffer
> m_mainResourceData
;
237 // A reference to actual request used to create the data source.
238 // This should only be used by the resourceLoadDelegate's
239 // identifierForInitialRequest:fromDatasource: method. It is
240 // not guaranteed to remain unchanged, as requests are mutable.
241 ResourceRequest m_originalRequest
;
243 SubstituteData m_substituteData
;
245 // A copy of the original request used to create the data source.
246 // We have to copy the request because requests are mutable.
247 ResourceRequest m_originalRequestCopy
;
249 // The 'working' request. It may be mutated
250 // several times from the original request to include additional
251 // headers, cookie information, canonicalization and redirects.
252 ResourceRequest m_request
;
254 ResourceResponse m_response
;
256 ResourceError m_mainDocumentError
;
262 bool m_primaryLoadComplete
;
263 bool m_isClientRedirect
;
264 bool m_loadingFromCachedPage
;
268 String m_overrideEncoding
;
270 // The action that triggered loading - we keep this around for the
271 // benefit of the various policy handlers.
272 NavigationAction m_triggeringAction
;
274 // The last request that we checked click policy for - kept around
275 // so we can avoid asking again needlessly.
276 ResourceRequest m_lastCheckedRequest
;
278 // We retain all the received responses so we can play back the
279 // WebResourceLoadDelegate messages if the item is loaded from the
281 ResponseVector m_responses
;
282 bool m_stopRecordingResponses
;
284 typedef HashMap
<RefPtr
<ResourceLoader
>, RefPtr
<SubstituteResource
> > SubstituteResourceMap
;
285 SubstituteResourceMap m_pendingSubstituteResources
;
286 Timer
<DocumentLoader
> m_substituteResourceDeliveryTimer
;
288 OwnPtr
<ArchiveResourceCollection
> m_archiveResourceCollection
;
289 RefPtr
<SharedBuffer
> m_parsedArchiveData
;
291 HashSet
<String
> m_resourcesClientKnowsAbout
;
292 Vector
<String
> m_resourcesLoadedFromMemoryCacheForClientNotification
;
294 bool m_urlForHistoryReflectsClientRedirect
;
296 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
297 // The application cache that the document loader is associated with (if any).
298 RefPtr
<ApplicationCache
> m_applicationCache
;
300 // Before an application cache has finished loading, this will be the candidate application
301 // group that the document loader is associated with.
302 ApplicationCacheGroup
* m_candidateApplicationCacheGroup
;
304 // Once the main resource has finished loading, this is the application cache it was loaded from (if any).
305 RefPtr
<ApplicationCache
> m_mainResourceApplicationCache
;
309 inline void DocumentLoader::recordMemoryCacheLoadForFutureClientNotification(const String
& url
)
311 m_resourcesLoadedFromMemoryCacheForClientNotification
.append(url
);
314 inline void DocumentLoader::takeMemoryCacheLoadsForClientNotification(Vector
<String
>& loadsSet
)
316 loadsSet
.swap(m_resourcesLoadedFromMemoryCacheForClientNotification
);
317 m_resourcesLoadedFromMemoryCacheForClientNotification
.clear();
322 #endif // DocumentLoader_h