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 "core/FetchInitiatorTypeNames.h"
36 #include "core/dom/Document.h"
37 #include "core/fetch/FetchRequest.h"
38 #include "core/fetch/ResourceFetcher.h"
39 #include "core/frame/Settings.h"
40 #include "core/html/LinkRelAttribute.h"
41 #include "core/loader/PrerenderHandle.h"
42 #include "platform/Prerender.h"
43 #include "platform/network/DNS.h"
44
45 namespace WebCore {
46
prerenderRelTypesFromRelAttribute(const LinkRelAttribute & relAttribute)47 static unsigned prerenderRelTypesFromRelAttribute(const LinkRelAttribute& relAttribute)
48 {
49 unsigned result = 0;
50 if (relAttribute.isLinkPrerender())
51 result |= PrerenderRelTypePrerender;
52 if (relAttribute.isLinkNext())
53 result |= PrerenderRelTypeNext;
54
55 return result;
56 }
57
LinkLoader(LinkLoaderClient * client)58 LinkLoader::LinkLoader(LinkLoaderClient* client)
59 : m_client(client)
60 , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired)
61 , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired)
62 {
63 }
64
~LinkLoader()65 LinkLoader::~LinkLoader()
66 {
67 }
68
linkLoadTimerFired(Timer<LinkLoader> * timer)69 void LinkLoader::linkLoadTimerFired(Timer<LinkLoader>* timer)
70 {
71 ASSERT_UNUSED(timer, timer == &m_linkLoadTimer);
72 m_client->linkLoaded();
73 }
74
linkLoadingErrorTimerFired(Timer<LinkLoader> * timer)75 void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer)
76 {
77 ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer);
78 m_client->linkLoadingErrored();
79 }
80
notifyFinished(Resource * resource)81 void LinkLoader::notifyFinished(Resource* resource)
82 {
83 ASSERT(this->resource() == resource);
84
85 if (resource->errorOccurred())
86 m_linkLoadingErrorTimer.startOneShot(0, FROM_HERE);
87 else
88 m_linkLoadTimer.startOneShot(0, FROM_HERE);
89
90 clearResource();
91 }
92
didStartPrerender()93 void LinkLoader::didStartPrerender()
94 {
95 m_client->didStartLinkPrerender();
96 }
97
didStopPrerender()98 void LinkLoader::didStopPrerender()
99 {
100 m_client->didStopLinkPrerender();
101 }
102
didSendLoadForPrerender()103 void LinkLoader::didSendLoadForPrerender()
104 {
105 m_client->didSendLoadForLinkPrerender();
106 }
107
didSendDOMContentLoadedForPrerender()108 void LinkLoader::didSendDOMContentLoadedForPrerender()
109 {
110 m_client->didSendDOMContentLoadedForLinkPrerender();
111 }
112
loadLink(const LinkRelAttribute & relAttribute,const AtomicString & crossOriginMode,const String & type,const KURL & href,Document & document)113 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const AtomicString& crossOriginMode, const String& type, const KURL& href, Document& document)
114 {
115 if (relAttribute.isDNSPrefetch()) {
116 Settings* settings = document.settings();
117 // FIXME: The href attribute of the link element can be in "//hostname" form, and we shouldn't attempt
118 // to complete that as URL <https://bugs.webkit.org/show_bug.cgi?id=48857>.
119 if (settings && settings->dnsPrefetchingEnabled() && href.isValid() && !href.isEmpty())
120 prefetchDNS(href.host());
121 }
122
123 // FIXME(crbug.com/323096): Should take care of import.
124 if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource() || relAttribute.isTransitionExitingStylesheet()) && href.isValid() && document.frame()) {
125 if (!m_client->shouldLoadLink())
126 return false;
127 Resource::Type type = relAttribute.isLinkSubresource() ? Resource::LinkSubresource : Resource::LinkPrefetch;
128 FetchRequest linkRequest(ResourceRequest(document.completeURL(href)), FetchInitiatorTypeNames::link);
129 if (!crossOriginMode.isNull())
130 linkRequest.setCrossOriginAccessControl(document.securityOrigin(), crossOriginMode);
131 setResource(document.fetcher()->fetchLinkResource(type, linkRequest));
132 }
133
134 if (const unsigned prerenderRelTypes = prerenderRelTypesFromRelAttribute(relAttribute)) {
135 if (!m_prerender) {
136 m_prerender = PrerenderHandle::create(document, this, href, prerenderRelTypes);
137 } else if (m_prerender->url() != href) {
138 m_prerender->cancel();
139 m_prerender = PrerenderHandle::create(document, this, href, prerenderRelTypes);
140 }
141 // TODO(gavinp): Handle changes to rel types of existing prerenders.
142 } else if (m_prerender) {
143 m_prerender->cancel();
144 m_prerender.clear();
145 }
146 return true;
147 }
148
released()149 void LinkLoader::released()
150 {
151 // Only prerenders need treatment here; other links either use the Resource interface, or are notionally
152 // atomic (dns prefetch).
153 if (m_prerender) {
154 m_prerender->cancel();
155 m_prerender.clear();
156 }
157 }
158
159 }
160