1/* 2 * Copyright (C) 2011 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 */ 31 32#include "config.h" 33#include "core/loader/LinkLoader.h" 34 35#include "FetchInitiatorTypeNames.h" 36#include "core/dom/Document.h" 37#include "core/fetch/FetchRequest.h" 38#include "core/fetch/ResourceFetcher.h" 39#include "core/html/LinkRelAttribute.h" 40#include "core/loader/PrerenderHandle.h" 41#include "core/frame/Settings.h" 42#include "platform/network/DNS.h" 43 44namespace WebCore { 45 46LinkLoader::LinkLoader(LinkLoaderClient* client) 47 : m_client(client) 48 , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired) 49 , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired) 50{ 51} 52 53LinkLoader::~LinkLoader() 54{ 55} 56 57void LinkLoader::linkLoadTimerFired(Timer<LinkLoader>* timer) 58{ 59 ASSERT_UNUSED(timer, timer == &m_linkLoadTimer); 60 m_client->linkLoaded(); 61} 62 63void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer) 64{ 65 ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer); 66 m_client->linkLoadingErrored(); 67} 68 69void LinkLoader::notifyFinished(Resource* resource) 70{ 71 ASSERT(this->resource() == resource); 72 73 if (resource->errorOccurred()) 74 m_linkLoadingErrorTimer.startOneShot(0); 75 else 76 m_linkLoadTimer.startOneShot(0); 77 78 clearResource(); 79} 80 81void LinkLoader::didStartPrerender() 82{ 83 m_client->didStartLinkPrerender(); 84} 85 86void LinkLoader::didStopPrerender() 87{ 88 m_client->didStopLinkPrerender(); 89} 90 91void LinkLoader::didSendLoadForPrerender() 92{ 93 m_client->didSendLoadForLinkPrerender(); 94} 95 96void LinkLoader::didSendDOMContentLoadedForPrerender() 97{ 98 m_client->didSendDOMContentLoadedForLinkPrerender(); 99} 100 101bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type, const KURL& href, Document& document) 102{ 103 if (relAttribute.isDNSPrefetch()) { 104 Settings* settings = document.settings(); 105 // FIXME: The href attribute of the link element can be in "//hostname" form, and we shouldn't attempt 106 // to complete that as URL <https://bugs.webkit.org/show_bug.cgi?id=48857>. 107 if (settings && settings->dnsPrefetchingEnabled() && href.isValid() && !href.isEmpty()) 108 prefetchDNS(href.host()); 109 } 110 111 // FIXME(crbug.com/323096): Should take care of import. 112 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && href.isValid() && document.frame()) { 113 if (!m_client->shouldLoadLink()) 114 return false; 115 Resource::Type type = relAttribute.isLinkSubresource() ? Resource::LinkSubresource : Resource::LinkPrefetch; 116 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), FetchInitiatorTypeNames::link); 117 setResource(document.fetcher()->fetchLinkResource(type, linkRequest)); 118 } 119 120 if (relAttribute.isLinkPrerender()) { 121 if (!m_prerender) { 122 m_prerender = PrerenderHandle::create(document, this, href); 123 } else if (m_prerender->url() != href) { 124 m_prerender->cancel(); 125 m_prerender = PrerenderHandle::create(document, this, href); 126 } 127 } else if (m_prerender) { 128 m_prerender->cancel(); 129 m_prerender.clear(); 130 } 131 return true; 132} 133 134void LinkLoader::released() 135{ 136 // Only prerenders need treatment here; other links either use the Resource interface, or are notionally 137 // atomic (dns prefetch). 138 if (m_prerender) { 139 m_prerender->cancel(); 140 m_prerender.clear(); 141 } 142} 143 144} 145