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