2 * Copyright (C) 2011 Google 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 are
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
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.
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.
33 #include "LinkLoader.h"
35 #include "CSSStyleSelector.h"
36 #include "CSSStyleSheet.h"
37 #include "CachedCSSStyleSheet.h"
38 #include "CachedResourceLoader.h"
39 #include "ContainerNode.h"
42 #include "FrameView.h"
43 #include "LinkRelAttribute.h"
44 #include "ResourceHandle.h"
49 LinkLoader::LinkLoader(LinkLoaderClient* client)
51 , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired)
52 , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired)
56 LinkLoader::~LinkLoader()
58 if (m_cachedLinkResource)
59 m_cachedLinkResource->removeClient(this);
62 void LinkLoader::linkLoadTimerFired(Timer<LinkLoader>* timer)
64 ASSERT_UNUSED(timer, timer == &m_linkLoadTimer);
65 m_client->linkLoaded();
68 void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer)
70 ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer);
71 m_client->linkLoadingErrored();
74 void LinkLoader::notifyFinished(CachedResource* resource)
76 ASSERT_UNUSED(resource, m_cachedLinkResource.get() == resource);
78 if (m_cachedLinkResource->errorOccurred())
79 m_linkLoadingErrorTimer.startOneShot(0);
81 m_linkLoadTimer.startOneShot(0);
83 m_cachedLinkResource->removeClient(this);
84 m_cachedLinkResource = 0;
87 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type,
88 const KURL& href, Document* document)
90 // We'll record this URL per document, even if we later only use it in top level frames
91 if (relAttribute.m_iconType != InvalidIcon && href.isValid() && !href.isEmpty()) {
92 if (!m_client->shouldLoadLink())
94 document->setIconURL(href.string(), type, relAttribute.m_iconType);
97 if (relAttribute.m_isDNSPrefetch) {
98 Settings* settings = document->settings();
99 // FIXME: The href attribute of the link element can be in "//hostname" form, and we shouldn't attempt
100 // to complete that as URL <https://bugs.webkit.org/show_bug.cgi?id=48857>.
101 if (settings && settings->dnsPrefetchingEnabled() && href.isValid() && !href.isEmpty())
102 ResourceHandle::prepareForURL(href);
105 #if ENABLE(LINK_PREFETCH)
106 if ((relAttribute.m_isLinkPrefetch || relAttribute.m_isLinkPrerender || relAttribute.m_isLinkSubresource) && href.isValid() && document->frame()) {
107 if (!m_client->shouldLoadLink())
109 ResourceLoadPriority priority = ResourceLoadPriorityUnresolved;
110 CachedResource::Type type = CachedResource::LinkPrefetch;
111 // We only make one request to the cachedresourcelodaer if multiple rel types are
113 if (relAttribute.m_isLinkSubresource) {
114 priority = ResourceLoadPriorityLow;
115 type = CachedResource::LinkSubresource;
116 } else if (relAttribute.m_isLinkPrerender)
117 type = CachedResource::LinkPrerender;
119 ResourceRequest linkRequest(document->completeURL(href));
121 if (m_cachedLinkResource) {
122 m_cachedLinkResource->removeClient(this);
123 m_cachedLinkResource = 0;
125 m_cachedLinkResource = document->cachedResourceLoader()->requestLinkResource(type, linkRequest, priority);
126 if (m_cachedLinkResource)
127 m_cachedLinkResource->addClient(this);