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