0
|
1 |
/*
|
|
2 |
* Copyright (C) 2006, 2007, 2008 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 |
#include "config.h"
|
|
30 |
#include "DocumentLoader.h"
|
|
31 |
|
|
32 |
#include "ApplicationCacheHost.h"
|
|
33 |
#include "ArchiveFactory.h"
|
|
34 |
#include "ArchiveResourceCollection.h"
|
|
35 |
#include "CachedPage.h"
|
|
36 |
#include "DocLoader.h"
|
|
37 |
#include "Document.h"
|
|
38 |
#include "Event.h"
|
|
39 |
#include "Frame.h"
|
|
40 |
#include "FrameLoader.h"
|
|
41 |
#include "FrameTree.h"
|
|
42 |
#include "HistoryItem.h"
|
|
43 |
#include "Logging.h"
|
|
44 |
#include "MainResourceLoader.h"
|
|
45 |
#include "Page.h"
|
|
46 |
#include "PlatformString.h"
|
|
47 |
#include "Settings.h"
|
|
48 |
#include "SharedBuffer.h"
|
|
49 |
#include "StringBuffer.h"
|
|
50 |
#include "XMLTokenizer.h"
|
|
51 |
|
|
52 |
#include <wtf/Assertions.h>
|
|
53 |
#include <wtf/unicode/Unicode.h>
|
|
54 |
|
|
55 |
namespace WebCore {
|
|
56 |
|
|
57 |
/*
|
|
58 |
* Performs four operations:
|
|
59 |
* 1. Convert backslashes to currency symbols
|
|
60 |
* 2. Convert control characters to spaces
|
|
61 |
* 3. Trim leading and trailing spaces
|
|
62 |
* 4. Collapse internal whitespace.
|
|
63 |
*/
|
|
64 |
static inline String canonicalizedTitle(const String& title, Frame* frame)
|
|
65 |
{
|
|
66 |
ASSERT(!title.isEmpty());
|
|
67 |
|
|
68 |
const UChar* characters = title.characters();
|
|
69 |
unsigned length = title.length();
|
|
70 |
unsigned i;
|
|
71 |
|
|
72 |
StringBuffer buffer(length);
|
|
73 |
unsigned builderIndex = 0;
|
|
74 |
|
|
75 |
// Skip leading spaces and leading characters that would convert to spaces
|
|
76 |
for (i = 0; i < length; ++i) {
|
|
77 |
UChar c = characters[i];
|
|
78 |
if (!(c <= 0x20 || c == 0x7F))
|
|
79 |
break;
|
|
80 |
}
|
|
81 |
|
|
82 |
if (i == length)
|
|
83 |
return "";
|
|
84 |
|
|
85 |
// Replace control characters with spaces, and backslashes with currency symbols, and collapse whitespace.
|
|
86 |
bool previousCharWasWS = false;
|
|
87 |
for (; i < length; ++i) {
|
|
88 |
UChar c = characters[i];
|
|
89 |
if (c <= 0x20 || c == 0x7F || (WTF::Unicode::category(c) & (WTF::Unicode::Separator_Line | WTF::Unicode::Separator_Paragraph))) {
|
|
90 |
if (previousCharWasWS)
|
|
91 |
continue;
|
|
92 |
buffer[builderIndex++] = ' ';
|
|
93 |
previousCharWasWS = true;
|
|
94 |
} else {
|
|
95 |
buffer[builderIndex++] = c;
|
|
96 |
previousCharWasWS = false;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
// Strip trailing spaces
|
|
101 |
while (builderIndex > 0) {
|
|
102 |
--builderIndex;
|
|
103 |
if (buffer[builderIndex] != ' ')
|
|
104 |
break;
|
|
105 |
}
|
|
106 |
|
|
107 |
if (!builderIndex && buffer[builderIndex] == ' ')
|
|
108 |
return "";
|
|
109 |
|
|
110 |
buffer.shrink(builderIndex + 1);
|
|
111 |
|
|
112 |
// Replace the backslashes with currency symbols if the encoding requires it.
|
|
113 |
frame->document()->displayBufferModifiedByEncoding(buffer.characters(), buffer.length());
|
|
114 |
|
|
115 |
return String::adopt(buffer);
|
|
116 |
}
|
|
117 |
|
|
118 |
static void cancelAll(const ResourceLoaderSet& loaders)
|
|
119 |
{
|
|
120 |
const ResourceLoaderSet copy = loaders;
|
|
121 |
ResourceLoaderSet::const_iterator end = copy.end();
|
|
122 |
for (ResourceLoaderSet::const_iterator it = copy.begin(); it != end; ++it)
|
|
123 |
(*it)->cancel();
|
|
124 |
}
|
|
125 |
|
|
126 |
static void setAllDefersLoading(const ResourceLoaderSet& loaders, bool defers)
|
|
127 |
{
|
|
128 |
const ResourceLoaderSet copy = loaders;
|
|
129 |
ResourceLoaderSet::const_iterator end = copy.end();
|
|
130 |
for (ResourceLoaderSet::const_iterator it = copy.begin(); it != end; ++it)
|
|
131 |
(*it)->setDefersLoading(defers);
|
|
132 |
}
|
|
133 |
|
|
134 |
DocumentLoader::DocumentLoader(const ResourceRequest& req, const SubstituteData& substituteData)
|
|
135 |
: m_deferMainResourceDataLoad(true)
|
|
136 |
, m_frame(0)
|
|
137 |
, m_originalRequest(req)
|
|
138 |
, m_substituteData(substituteData)
|
|
139 |
, m_originalRequestCopy(req)
|
|
140 |
, m_request(req)
|
|
141 |
, m_committed(false)
|
|
142 |
, m_isStopping(false)
|
|
143 |
, m_loading(false)
|
|
144 |
, m_gotFirstByte(false)
|
|
145 |
, m_primaryLoadComplete(false)
|
|
146 |
, m_isClientRedirect(false)
|
|
147 |
, m_stopRecordingResponses(false)
|
|
148 |
, m_substituteResourceDeliveryTimer(this, &DocumentLoader::substituteResourceDeliveryTimerFired)
|
|
149 |
, m_didCreateGlobalHistoryEntry(false)
|
|
150 |
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
|
|
151 |
, m_applicationCacheHost(new ApplicationCacheHost(this))
|
|
152 |
#endif
|
|
153 |
{
|
|
154 |
}
|
|
155 |
|
|
156 |
FrameLoader* DocumentLoader::frameLoader() const
|
|
157 |
{
|
|
158 |
if (!m_frame)
|
|
159 |
return 0;
|
|
160 |
return m_frame->loader();
|
|
161 |
}
|
|
162 |
|
|
163 |
DocumentLoader::~DocumentLoader()
|
|
164 |
{
|
|
165 |
ASSERT(!m_frame || frameLoader()->activeDocumentLoader() != this || !frameLoader()->isLoading());
|
|
166 |
}
|
|
167 |
|
|
168 |
PassRefPtr<SharedBuffer> DocumentLoader::mainResourceData() const
|
|
169 |
{
|
|
170 |
if (m_mainResourceData)
|
|
171 |
return m_mainResourceData;
|
|
172 |
if (m_mainResourceLoader)
|
|
173 |
return m_mainResourceLoader->resourceData();
|
|
174 |
return 0;
|
|
175 |
}
|
|
176 |
|
|
177 |
const ResourceRequest& DocumentLoader::originalRequest() const
|
|
178 |
{
|
|
179 |
return m_originalRequest;
|
|
180 |
}
|
|
181 |
|
|
182 |
const ResourceRequest& DocumentLoader::originalRequestCopy() const
|
|
183 |
{
|
|
184 |
return m_originalRequestCopy;
|
|
185 |
}
|
|
186 |
|
|
187 |
const ResourceRequest& DocumentLoader::request() const
|
|
188 |
{
|
|
189 |
return m_request;
|
|
190 |
}
|
|
191 |
|
|
192 |
ResourceRequest& DocumentLoader::request()
|
|
193 |
{
|
|
194 |
return m_request;
|
|
195 |
}
|
|
196 |
|
|
197 |
const KURL& DocumentLoader::url() const
|
|
198 |
{
|
|
199 |
return request().url();
|
|
200 |
}
|
|
201 |
|
|
202 |
void DocumentLoader::replaceRequestURLForAnchorScroll(const KURL& url)
|
|
203 |
{
|
|
204 |
m_originalRequestCopy.setURL(url);
|
|
205 |
m_request.setURL(url);
|
|
206 |
}
|
|
207 |
|
|
208 |
void DocumentLoader::setRequest(const ResourceRequest& req)
|
|
209 |
{
|
|
210 |
// Replacing an unreachable URL with alternate content looks like a server-side
|
|
211 |
// redirect at this point, but we can replace a committed dataSource.
|
|
212 |
bool handlingUnreachableURL = false;
|
|
213 |
|
|
214 |
handlingUnreachableURL = m_substituteData.isValid() && !m_substituteData.failingURL().isEmpty();
|
|
215 |
|
|
216 |
if (handlingUnreachableURL)
|
|
217 |
m_committed = false;
|
|
218 |
|
|
219 |
// We should never be getting a redirect callback after the data
|
|
220 |
// source is committed, except in the unreachable URL case. It
|
|
221 |
// would be a WebFoundation bug if it sent a redirect callback after commit.
|
|
222 |
ASSERT(!m_committed);
|
|
223 |
|
|
224 |
KURL oldURL = m_request.url();
|
|
225 |
m_request = req;
|
|
226 |
|
|
227 |
// Only send webView:didReceiveServerRedirectForProvisionalLoadForFrame: if URL changed.
|
|
228 |
// Also, don't send it when replacing unreachable URLs with alternate content.
|
|
229 |
if (!handlingUnreachableURL && oldURL != req.url())
|
|
230 |
frameLoader()->didReceiveServerRedirectForProvisionalLoadForFrame();
|
|
231 |
}
|
|
232 |
|
|
233 |
void DocumentLoader::setMainDocumentError(const ResourceError& error)
|
|
234 |
{
|
|
235 |
m_mainDocumentError = error;
|
|
236 |
frameLoader()->setMainDocumentError(this, error);
|
|
237 |
}
|
|
238 |
|
|
239 |
void DocumentLoader::clearErrors()
|
|
240 |
{
|
|
241 |
m_mainDocumentError = ResourceError();
|
|
242 |
}
|
|
243 |
|
|
244 |
void DocumentLoader::mainReceivedError(const ResourceError& error, bool isComplete)
|
|
245 |
{
|
|
246 |
ASSERT(!error.isNull());
|
|
247 |
|
|
248 |
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
|
|
249 |
m_applicationCacheHost->failedLoadingMainResource();
|
|
250 |
#endif
|
|
251 |
|
|
252 |
if (!frameLoader())
|
|
253 |
return;
|
|
254 |
setMainDocumentError(error);
|
|
255 |
if (isComplete)
|
|
256 |
frameLoader()->mainReceivedCompleteError(this, error);
|
|
257 |
}
|
|
258 |
|
|
259 |
// Cancels the data source's pending loads. Conceptually, a data source only loads
|
|
260 |
// one document at a time, but one document may have many related resources.
|
|
261 |
// stopLoading will stop all loads initiated by the data source,
|
|
262 |
// but not loads initiated by child frames' data sources -- that's the WebFrame's job.
|
|
263 |
void DocumentLoader::stopLoading(DatabasePolicy databasePolicy)
|
|
264 |
{
|
|
265 |
// In some rare cases, calling FrameLoader::stopLoading could set m_loading to false.
|
|
266 |
// (This can happen when there's a single XMLHttpRequest currently loading and stopLoading causes it
|
|
267 |
// to stop loading. Because of this, we need to save it so we don't return early.
|
|
268 |
bool loading = m_loading;
|
|
269 |
|
|
270 |
if (m_committed) {
|
|
271 |
// Attempt to stop the frame if the document loader is loading, or if it is done loading but
|
|
272 |
// still parsing. Failure to do so can cause a world leak.
|
|
273 |
Document* doc = m_frame->document();
|
|
274 |
|
|
275 |
if (loading || doc->parsing())
|
|
276 |
m_frame->loader()->stopLoading(UnloadEventPolicyNone, databasePolicy);
|
|
277 |
}
|
|
278 |
|
|
279 |
// Always cancel multipart loaders
|
|
280 |
cancelAll(m_multipartSubresourceLoaders);
|
|
281 |
|
|
282 |
if (!loading)
|
|
283 |
return;
|
|
284 |
|
|
285 |
RefPtr<Frame> protectFrame(m_frame);
|
|
286 |
RefPtr<DocumentLoader> protectLoader(this);
|
|
287 |
|
|
288 |
m_isStopping = true;
|
|
289 |
|
|
290 |
FrameLoader* frameLoader = DocumentLoader::frameLoader();
|
|
291 |
|
|
292 |
if (m_mainResourceLoader)
|
|
293 |
// Stop the main resource loader and let it send the cancelled message.
|
|
294 |
m_mainResourceLoader->cancel();
|
|
295 |
else if (!m_subresourceLoaders.isEmpty())
|
|
296 |
// The main resource loader already finished loading. Set the cancelled error on the
|
|
297 |
// document and let the subresourceLoaders send individual cancelled messages below.
|
|
298 |
setMainDocumentError(frameLoader->cancelledError(m_request));
|
|
299 |
else
|
|
300 |
// If there are no resource loaders, we need to manufacture a cancelled message.
|
|
301 |
// (A back/forward navigation has no resource loaders because its resources are cached.)
|
|
302 |
mainReceivedError(frameLoader->cancelledError(m_request), true);
|
|
303 |
|
|
304 |
stopLoadingSubresources();
|
|
305 |
stopLoadingPlugIns();
|
|
306 |
|
|
307 |
m_isStopping = false;
|
|
308 |
}
|
|
309 |
|
|
310 |
void DocumentLoader::setupForReplace()
|
|
311 |
{
|
|
312 |
frameLoader()->setupForReplace();
|
|
313 |
m_committed = false;
|
|
314 |
}
|
|
315 |
|
|
316 |
void DocumentLoader::commitIfReady()
|
|
317 |
{
|
|
318 |
if (m_gotFirstByte && !m_committed) {
|
|
319 |
m_committed = true;
|
|
320 |
frameLoader()->commitProvisionalLoad(0);
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
void DocumentLoader::finishedLoading()
|
|
325 |
{
|
|
326 |
m_gotFirstByte = true;
|
|
327 |
commitIfReady();
|
|
328 |
if (FrameLoader* loader = frameLoader()) {
|
|
329 |
loader->finishedLoadingDocument(this);
|
|
330 |
loader->end();
|
|
331 |
}
|
|
332 |
}
|
|
333 |
|
|
334 |
void DocumentLoader::commitLoad(const char* data, int length)
|
|
335 |
{
|
|
336 |
// Both unloading the old page and parsing the new page may execute JavaScript which destroys the datasource
|
|
337 |
// by starting a new load, so retain temporarily.
|
|
338 |
RefPtr<DocumentLoader> protect(this);
|
|
339 |
|
|
340 |
commitIfReady();
|
|
341 |
if (FrameLoader* frameLoader = DocumentLoader::frameLoader())
|
|
342 |
frameLoader->committedLoad(this, data, length);
|
|
343 |
}
|
|
344 |
|
|
345 |
bool DocumentLoader::doesProgressiveLoad(const String& MIMEType) const
|
|
346 |
{
|
|
347 |
return !frameLoader()->isReplacing() || MIMEType == "text/html";
|
|
348 |
}
|
|
349 |
|
|
350 |
void DocumentLoader::receivedData(const char* data, int length)
|
|
351 |
{
|
|
352 |
m_gotFirstByte = true;
|
|
353 |
if (doesProgressiveLoad(m_response.mimeType()))
|
|
354 |
commitLoad(data, length);
|
|
355 |
}
|
|
356 |
|
|
357 |
void DocumentLoader::setupForReplaceByMIMEType(const String& newMIMEType)
|
|
358 |
{
|
|
359 |
if (!m_gotFirstByte)
|
|
360 |
return;
|
|
361 |
|
|
362 |
String oldMIMEType = m_response.mimeType();
|
|
363 |
|
|
364 |
if (!doesProgressiveLoad(oldMIMEType)) {
|
|
365 |
frameLoader()->revertToProvisional(this);
|
|
366 |
setupForReplace();
|
|
367 |
RefPtr<SharedBuffer> resourceData = mainResourceData();
|
|
368 |
commitLoad(resourceData->data(), resourceData->size());
|
|
369 |
}
|
|
370 |
|
|
371 |
frameLoader()->finishedLoadingDocument(this);
|
|
372 |
m_frame->loader()->end();
|
|
373 |
|
|
374 |
frameLoader()->setReplacing();
|
|
375 |
m_gotFirstByte = false;
|
|
376 |
|
|
377 |
if (doesProgressiveLoad(newMIMEType)) {
|
|
378 |
frameLoader()->revertToProvisional(this);
|
|
379 |
setupForReplace();
|
|
380 |
}
|
|
381 |
|
|
382 |
stopLoadingSubresources();
|
|
383 |
stopLoadingPlugIns();
|
|
384 |
clearArchiveResources();
|
|
385 |
}
|
|
386 |
|
|
387 |
void DocumentLoader::updateLoading()
|
|
388 |
{
|
|
389 |
if (!m_frame) {
|
|
390 |
setLoading(false);
|
|
391 |
return;
|
|
392 |
}
|
|
393 |
ASSERT(this == frameLoader()->activeDocumentLoader());
|
|
394 |
setLoading(frameLoader()->isLoading());
|
|
395 |
}
|
|
396 |
|
|
397 |
void DocumentLoader::setFrame(Frame* frame)
|
|
398 |
{
|
|
399 |
if (m_frame == frame)
|
|
400 |
return;
|
|
401 |
ASSERT(frame && !m_frame);
|
|
402 |
m_frame = frame;
|
|
403 |
attachToFrame();
|
|
404 |
}
|
|
405 |
|
|
406 |
void DocumentLoader::attachToFrame()
|
|
407 |
{
|
|
408 |
ASSERT(m_frame);
|
|
409 |
}
|
|
410 |
|
|
411 |
void DocumentLoader::detachFromFrame()
|
|
412 |
{
|
|
413 |
ASSERT(m_frame);
|
|
414 |
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
|
|
415 |
m_applicationCacheHost->setDOMApplicationCache(0);
|
|
416 |
#endif
|
|
417 |
m_frame = 0;
|
|
418 |
}
|
|
419 |
|
|
420 |
void DocumentLoader::prepareForLoadStart()
|
|
421 |
{
|
|
422 |
ASSERT(!m_isStopping);
|
|
423 |
setPrimaryLoadComplete(false);
|
|
424 |
ASSERT(frameLoader());
|
|
425 |
clearErrors();
|
|
426 |
|
|
427 |
setLoading(true);
|
|
428 |
|
|
429 |
frameLoader()->prepareForLoadStart();
|
|
430 |
}
|
|
431 |
|
|
432 |
void DocumentLoader::setPrimaryLoadComplete(bool flag)
|
|
433 |
{
|
|
434 |
m_primaryLoadComplete = flag;
|
|
435 |
if (flag) {
|
|
436 |
if (m_mainResourceLoader) {
|
|
437 |
m_mainResourceData = m_mainResourceLoader->resourceData();
|
|
438 |
m_mainResourceLoader = 0;
|
|
439 |
}
|
|
440 |
|
|
441 |
if (this == frameLoader()->activeDocumentLoader())
|
|
442 |
updateLoading();
|
|
443 |
}
|
|
444 |
}
|
|
445 |
|
|
446 |
bool DocumentLoader::isLoadingInAPISense() const
|
|
447 |
{
|
|
448 |
// Once a frame has loaded, we no longer need to consider subresources,
|
|
449 |
// but we still need to consider subframes.
|
|
450 |
if (frameLoader()->state() != FrameStateComplete) {
|
|
451 |
if (!m_primaryLoadComplete && isLoading())
|
|
452 |
return true;
|
|
453 |
if (!m_subresourceLoaders.isEmpty())
|
|
454 |
return true;
|
|
455 |
Document* doc = m_frame->document();
|
|
456 |
if (doc->docLoader()->requestCount())
|
|
457 |
return true;
|
|
458 |
if (Tokenizer* tok = doc->tokenizer())
|
|
459 |
if (tok->processingData())
|
|
460 |
return true;
|
|
461 |
}
|
|
462 |
return frameLoader()->subframeIsLoading();
|
|
463 |
}
|
|
464 |
|
|
465 |
void DocumentLoader::addAllArchiveResources(Archive* archive)
|
|
466 |
{
|
|
467 |
if (!m_archiveResourceCollection)
|
|
468 |
m_archiveResourceCollection.set(new ArchiveResourceCollection);
|
|
469 |
|
|
470 |
ASSERT(archive);
|
|
471 |
if (!archive)
|
|
472 |
return;
|
|
473 |
|
|
474 |
m_archiveResourceCollection->addAllResources(archive);
|
|
475 |
}
|
|
476 |
|
|
477 |
// FIXME: Adding a resource directly to a DocumentLoader/ArchiveResourceCollection seems like bad design, but is API some apps rely on.
|
|
478 |
// Can we change the design in a manner that will let us deprecate that API without reducing functionality of those apps?
|
|
479 |
void DocumentLoader::addArchiveResource(PassRefPtr<ArchiveResource> resource)
|
|
480 |
{
|
|
481 |
if (!m_archiveResourceCollection)
|
|
482 |
m_archiveResourceCollection.set(new ArchiveResourceCollection);
|
|
483 |
|
|
484 |
ASSERT(resource);
|
|
485 |
if (!resource)
|
|
486 |
return;
|
|
487 |
|
|
488 |
m_archiveResourceCollection->addResource(resource);
|
|
489 |
}
|
|
490 |
|
|
491 |
ArchiveResource* DocumentLoader::archiveResourceForURL(const KURL& url) const
|
|
492 |
{
|
|
493 |
if (!m_archiveResourceCollection)
|
|
494 |
return 0;
|
|
495 |
|
|
496 |
ArchiveResource* resource = m_archiveResourceCollection->archiveResourceForURL(url);
|
|
497 |
|
|
498 |
return resource && !resource->shouldIgnoreWhenUnarchiving() ? resource : 0;
|
|
499 |
}
|
|
500 |
|
|
501 |
PassRefPtr<Archive> DocumentLoader::popArchiveForSubframe(const String& frameName)
|
|
502 |
{
|
|
503 |
return m_archiveResourceCollection ? m_archiveResourceCollection->popSubframeArchive(frameName) : 0;
|
|
504 |
}
|
|
505 |
|
|
506 |
void DocumentLoader::clearArchiveResources()
|
|
507 |
{
|
|
508 |
m_archiveResourceCollection.clear();
|
|
509 |
m_substituteResourceDeliveryTimer.stop();
|
|
510 |
}
|
|
511 |
|
|
512 |
void DocumentLoader::setParsedArchiveData(PassRefPtr<SharedBuffer> data)
|
|
513 |
{
|
|
514 |
m_parsedArchiveData = data;
|
|
515 |
}
|
|
516 |
|
|
517 |
SharedBuffer* DocumentLoader::parsedArchiveData() const
|
|
518 |
{
|
|
519 |
return m_parsedArchiveData.get();
|
|
520 |
}
|
|
521 |
|
|
522 |
PassRefPtr<ArchiveResource> DocumentLoader::mainResource() const
|
|
523 |
{
|
|
524 |
const ResourceResponse& r = response();
|
|
525 |
RefPtr<SharedBuffer> mainResourceBuffer = mainResourceData();
|
|
526 |
if (!mainResourceBuffer)
|
|
527 |
mainResourceBuffer = SharedBuffer::create();
|
|
528 |
|
|
529 |
return ArchiveResource::create(mainResourceBuffer, r.url(), r.mimeType(), r.textEncodingName(), frame()->tree()->name());
|
|
530 |
}
|
|
531 |
|
|
532 |
PassRefPtr<ArchiveResource> DocumentLoader::subresource(const KURL& url) const
|
|
533 |
{
|
|
534 |
if (!isCommitted())
|
|
535 |
return 0;
|
|
536 |
|
|
537 |
CachedResource* resource = m_frame->document()->docLoader()->cachedResource(url);
|
|
538 |
if (!resource || !resource->isLoaded())
|
|
539 |
return archiveResourceForURL(url);
|
|
540 |
|
|
541 |
// FIXME: This has the side effect of making the resource non-purgeable.
|
|
542 |
// It would be better if it didn't have this permanent effect.
|
|
543 |
if (!resource->makePurgeable(false))
|
|
544 |
return 0;
|
|
545 |
|
|
546 |
RefPtr<SharedBuffer> data = resource->data();
|
|
547 |
if (!data)
|
|
548 |
return 0;
|
|
549 |
|
|
550 |
return ArchiveResource::create(data.release(), url, resource->response());
|
|
551 |
}
|
|
552 |
|
|
553 |
void DocumentLoader::getSubresources(Vector<PassRefPtr<ArchiveResource> >& subresources) const
|
|
554 |
{
|
|
555 |
if (!isCommitted())
|
|
556 |
return;
|
|
557 |
|
|
558 |
Document* document = m_frame->document();
|
|
559 |
|
|
560 |
const DocLoader::DocumentResourceMap& allResources = document->docLoader()->allCachedResources();
|
|
561 |
DocLoader::DocumentResourceMap::const_iterator end = allResources.end();
|
|
562 |
for (DocLoader::DocumentResourceMap::const_iterator it = allResources.begin(); it != end; ++it) {
|
|
563 |
RefPtr<ArchiveResource> subresource = this->subresource(KURL(ParsedURLString, it->second->url()));
|
|
564 |
if (subresource)
|
|
565 |
subresources.append(subresource.release());
|
|
566 |
}
|
|
567 |
|
|
568 |
return;
|
|
569 |
}
|
|
570 |
|
|
571 |
void DocumentLoader::deliverSubstituteResourcesAfterDelay()
|
|
572 |
{
|
|
573 |
if (m_pendingSubstituteResources.isEmpty())
|
|
574 |
return;
|
|
575 |
ASSERT(m_frame && m_frame->page());
|
|
576 |
if (m_frame->page()->defersLoading())
|
|
577 |
return;
|
|
578 |
if (!m_substituteResourceDeliveryTimer.isActive())
|
|
579 |
m_substituteResourceDeliveryTimer.startOneShot(0);
|
|
580 |
}
|
|
581 |
|
|
582 |
void DocumentLoader::substituteResourceDeliveryTimerFired(Timer<DocumentLoader>*)
|
|
583 |
{
|
|
584 |
if (m_pendingSubstituteResources.isEmpty())
|
|
585 |
return;
|
|
586 |
ASSERT(m_frame && m_frame->page());
|
|
587 |
if (m_frame->page()->defersLoading())
|
|
588 |
return;
|
|
589 |
|
|
590 |
SubstituteResourceMap copy;
|
|
591 |
copy.swap(m_pendingSubstituteResources);
|
|
592 |
|
|
593 |
SubstituteResourceMap::const_iterator end = copy.end();
|
|
594 |
for (SubstituteResourceMap::const_iterator it = copy.begin(); it != end; ++it) {
|
|
595 |
RefPtr<ResourceLoader> loader = it->first;
|
|
596 |
SubstituteResource* resource = it->second.get();
|
|
597 |
|
|
598 |
if (resource) {
|
|
599 |
SharedBuffer* data = resource->data();
|
|
600 |
|
|
601 |
loader->didReceiveResponse(resource->response());
|
|
602 |
loader->didReceiveData(data->data(), data->size(), data->size(), true);
|
|
603 |
loader->didFinishLoading();
|
|
604 |
} else {
|
|
605 |
// A null resource means that we should fail the load.
|
|
606 |
// FIXME: Maybe we should use another error here - something like "not in cache".
|
|
607 |
loader->didFail(loader->cannotShowURLError());
|
|
608 |
}
|
|
609 |
}
|
|
610 |
}
|
|
611 |
|
|
612 |
#ifndef NDEBUG
|
|
613 |
bool DocumentLoader::isSubstituteLoadPending(ResourceLoader* loader) const
|
|
614 |
{
|
|
615 |
return m_pendingSubstituteResources.contains(loader);
|
|
616 |
}
|
|
617 |
#endif
|
|
618 |
|
|
619 |
void DocumentLoader::cancelPendingSubstituteLoad(ResourceLoader* loader)
|
|
620 |
{
|
|
621 |
if (m_pendingSubstituteResources.isEmpty())
|
|
622 |
return;
|
|
623 |
m_pendingSubstituteResources.remove(loader);
|
|
624 |
if (m_pendingSubstituteResources.isEmpty())
|
|
625 |
m_substituteResourceDeliveryTimer.stop();
|
|
626 |
}
|
|
627 |
|
|
628 |
bool DocumentLoader::scheduleArchiveLoad(ResourceLoader* loader, const ResourceRequest& request, const KURL& originalURL)
|
|
629 |
{
|
|
630 |
ArchiveResource* resource = 0;
|
|
631 |
|
|
632 |
if (request.url() == originalURL)
|
|
633 |
resource = archiveResourceForURL(originalURL);
|
|
634 |
|
|
635 |
if (!resource) {
|
|
636 |
// WebArchiveDebugMode means we fail loads instead of trying to fetch them from the network if they're not in the archive.
|
|
637 |
bool shouldFailLoad = m_frame->settings()->webArchiveDebugModeEnabled() && ArchiveFactory::isArchiveMimeType(responseMIMEType());
|
|
638 |
|
|
639 |
if (!shouldFailLoad)
|
|
640 |
return false;
|
|
641 |
}
|
|
642 |
|
|
643 |
m_pendingSubstituteResources.set(loader, resource);
|
|
644 |
deliverSubstituteResourcesAfterDelay();
|
|
645 |
|
|
646 |
return true;
|
|
647 |
}
|
|
648 |
|
|
649 |
void DocumentLoader::addResponse(const ResourceResponse& r)
|
|
650 |
{
|
|
651 |
if (!m_stopRecordingResponses)
|
|
652 |
m_responses.append(r);
|
|
653 |
}
|
|
654 |
|
|
655 |
void DocumentLoader::stopRecordingResponses()
|
|
656 |
{
|
|
657 |
m_stopRecordingResponses = true;
|
|
658 |
}
|
|
659 |
|
|
660 |
void DocumentLoader::setTitle(const String& title)
|
|
661 |
{
|
|
662 |
if (title.isEmpty())
|
|
663 |
return;
|
|
664 |
|
|
665 |
String trimmed = canonicalizedTitle(title, m_frame);
|
|
666 |
if (!trimmed.isEmpty() && m_pageTitle != trimmed) {
|
|
667 |
frameLoader()->willChangeTitle(this);
|
|
668 |
m_pageTitle = trimmed;
|
|
669 |
frameLoader()->didChangeTitle(this);
|
|
670 |
}
|
|
671 |
}
|
|
672 |
|
|
673 |
KURL DocumentLoader::urlForHistory() const
|
|
674 |
{
|
|
675 |
// Return the URL to be used for history and B/F list.
|
|
676 |
// Returns nil for WebDataProtocol URLs that aren't alternates
|
|
677 |
// for unreachable URLs, because these can't be stored in history.
|
|
678 |
if (m_substituteData.isValid())
|
|
679 |
return unreachableURL();
|
|
680 |
|
|
681 |
return m_originalRequestCopy.url();
|
|
682 |
}
|
|
683 |
|
|
684 |
bool DocumentLoader::urlForHistoryReflectsFailure() const
|
|
685 |
{
|
|
686 |
return m_substituteData.isValid() || m_response.httpStatusCode() >= 400;
|
|
687 |
}
|
|
688 |
|
|
689 |
const KURL& DocumentLoader::originalURL() const
|
|
690 |
{
|
|
691 |
return m_originalRequestCopy.url();
|
|
692 |
}
|
|
693 |
|
|
694 |
const KURL& DocumentLoader::requestURL() const
|
|
695 |
{
|
|
696 |
return request().url();
|
|
697 |
}
|
|
698 |
|
|
699 |
const KURL& DocumentLoader::responseURL() const
|
|
700 |
{
|
|
701 |
return m_response.url();
|
|
702 |
}
|
|
703 |
|
|
704 |
const String& DocumentLoader::responseMIMEType() const
|
|
705 |
{
|
|
706 |
return m_response.mimeType();
|
|
707 |
}
|
|
708 |
|
|
709 |
const KURL& DocumentLoader::unreachableURL() const
|
|
710 |
{
|
|
711 |
return m_substituteData.failingURL();
|
|
712 |
}
|
|
713 |
|
|
714 |
void DocumentLoader::setDefersLoading(bool defers)
|
|
715 |
{
|
|
716 |
if (m_mainResourceLoader)
|
|
717 |
m_mainResourceLoader->setDefersLoading(defers);
|
|
718 |
setAllDefersLoading(m_subresourceLoaders, defers);
|
|
719 |
setAllDefersLoading(m_plugInStreamLoaders, defers);
|
|
720 |
if (!defers)
|
|
721 |
deliverSubstituteResourcesAfterDelay();
|
|
722 |
}
|
|
723 |
|
|
724 |
void DocumentLoader::stopLoadingPlugIns()
|
|
725 |
{
|
|
726 |
cancelAll(m_plugInStreamLoaders);
|
|
727 |
}
|
|
728 |
|
|
729 |
void DocumentLoader::stopLoadingSubresources()
|
|
730 |
{
|
|
731 |
cancelAll(m_subresourceLoaders);
|
|
732 |
}
|
|
733 |
|
|
734 |
void DocumentLoader::addSubresourceLoader(ResourceLoader* loader)
|
|
735 |
{
|
|
736 |
m_subresourceLoaders.add(loader);
|
|
737 |
setLoading(true);
|
|
738 |
}
|
|
739 |
|
|
740 |
void DocumentLoader::removeSubresourceLoader(ResourceLoader* loader)
|
|
741 |
{
|
|
742 |
m_subresourceLoaders.remove(loader);
|
|
743 |
updateLoading();
|
|
744 |
if (Frame* frame = m_frame)
|
|
745 |
frame->loader()->checkLoadComplete();
|
|
746 |
}
|
|
747 |
|
|
748 |
void DocumentLoader::addPlugInStreamLoader(ResourceLoader* loader)
|
|
749 |
{
|
|
750 |
m_plugInStreamLoaders.add(loader);
|
|
751 |
setLoading(true);
|
|
752 |
}
|
|
753 |
|
|
754 |
void DocumentLoader::removePlugInStreamLoader(ResourceLoader* loader)
|
|
755 |
{
|
|
756 |
m_plugInStreamLoaders.remove(loader);
|
|
757 |
updateLoading();
|
|
758 |
}
|
|
759 |
|
|
760 |
bool DocumentLoader::isLoadingMainResource() const
|
|
761 |
{
|
|
762 |
return !!m_mainResourceLoader;
|
|
763 |
}
|
|
764 |
|
|
765 |
bool DocumentLoader::isLoadingSubresources() const
|
|
766 |
{
|
|
767 |
return !m_subresourceLoaders.isEmpty();
|
|
768 |
}
|
|
769 |
|
|
770 |
bool DocumentLoader::isLoadingPlugIns() const
|
|
771 |
{
|
|
772 |
return !m_plugInStreamLoaders.isEmpty();
|
|
773 |
}
|
|
774 |
|
|
775 |
bool DocumentLoader::isLoadingMultipartContent() const
|
|
776 |
{
|
|
777 |
return m_mainResourceLoader && m_mainResourceLoader->isLoadingMultipartContent();
|
|
778 |
}
|
|
779 |
|
|
780 |
bool DocumentLoader::startLoadingMainResource(unsigned long identifier)
|
|
781 |
{
|
|
782 |
ASSERT(!m_mainResourceLoader);
|
|
783 |
m_mainResourceLoader = MainResourceLoader::create(m_frame);
|
|
784 |
m_mainResourceLoader->setIdentifier(identifier);
|
|
785 |
|
|
786 |
// FIXME: Is there any way the extra fields could have not been added by now?
|
|
787 |
// If not, it would be great to remove this line of code.
|
|
788 |
frameLoader()->addExtraFieldsToMainResourceRequest(m_request);
|
|
789 |
|
|
790 |
if (!m_mainResourceLoader->load(m_request, m_substituteData)) {
|
|
791 |
// FIXME: If this should really be caught, we should just ASSERT this doesn't happen;
|
|
792 |
// should it be caught by other parts of WebKit or other parts of the app?
|
|
793 |
LOG_ERROR("could not create WebResourceHandle for URL %s -- should be caught by policy handler level", m_request.url().string().ascii().data());
|
|
794 |
m_mainResourceLoader = 0;
|
|
795 |
return false;
|
|
796 |
}
|
|
797 |
|
|
798 |
return true;
|
|
799 |
}
|
|
800 |
|
|
801 |
void DocumentLoader::cancelMainResourceLoad(const ResourceError& error)
|
|
802 |
{
|
|
803 |
m_mainResourceLoader->cancel(error);
|
|
804 |
}
|
|
805 |
|
|
806 |
void DocumentLoader::subresourceLoaderFinishedLoadingOnePart(ResourceLoader* loader)
|
|
807 |
{
|
|
808 |
m_multipartSubresourceLoaders.add(loader);
|
|
809 |
m_subresourceLoaders.remove(loader);
|
|
810 |
updateLoading();
|
|
811 |
if (Frame* frame = m_frame)
|
|
812 |
frame->loader()->checkLoadComplete();
|
|
813 |
}
|
|
814 |
|
|
815 |
void DocumentLoader::iconLoadDecisionAvailable()
|
|
816 |
{
|
|
817 |
if (m_frame)
|
|
818 |
m_frame->loader()->iconLoadDecisionAvailable();
|
|
819 |
}
|
|
820 |
|
|
821 |
}
|