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 "LinkLoader.h"
34
35#include "CSSStyleSheet.h"
36#include "CachedCSSStyleSheet.h"
37#include "CachedResourceLoader.h"
38#include "CachedResourceRequest.h"
39#include "ContainerNode.h"
40#include "DNS.h"
41#include "Document.h"
42#include "Frame.h"
43#include "FrameView.h"
44#include "LinkRelAttribute.h"
45#include "Settings.h"
46#include "StyleResolver.h"
47
48namespace WebCore {
49
50LinkLoader::LinkLoader(LinkLoaderClient* client)
51    : m_client(client)
52    , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired)
53    , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired)
54{
55}
56
57LinkLoader::~LinkLoader()
58{
59    if (m_cachedLinkResource)
60        m_cachedLinkResource->removeClient(this);
61}
62
63void LinkLoader::linkLoadTimerFired(Timer<LinkLoader>& timer)
64{
65    ASSERT_UNUSED(timer, &timer == &m_linkLoadTimer);
66    m_client->linkLoaded();
67}
68
69void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>& timer)
70{
71    ASSERT_UNUSED(timer, &timer == &m_linkLoadingErrorTimer);
72    m_client->linkLoadingErrored();
73}
74
75void LinkLoader::notifyFinished(CachedResource* resource)
76{
77    ASSERT_UNUSED(resource, m_cachedLinkResource.get() == resource);
78
79    if (m_cachedLinkResource->errorOccurred())
80        m_linkLoadingErrorTimer.startOneShot(0);
81    else
82        m_linkLoadTimer.startOneShot(0);
83
84    m_cachedLinkResource->removeClient(this);
85    m_cachedLinkResource = 0;
86}
87
88bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type,
89                          const String& sizes, const URL& href, Document* document)
90{
91    // We'll record this URL per document, even if we later only use it in top level frames
92    if (relAttribute.m_iconType != InvalidIcon && href.isValid() && !href.isEmpty()) {
93        if (!m_client->shouldLoadLink())
94            return false;
95        document->addIconURL(href.string(), type, sizes, relAttribute.m_iconType);
96    }
97
98    if (relAttribute.m_isDNSPrefetch) {
99        Settings* settings = document->settings();
100        // FIXME: The href attribute of the link element can be in "//hostname" form, and we shouldn't attempt
101        // to complete that as URL <https://bugs.webkit.org/show_bug.cgi?id=48857>.
102        if (settings && settings->dnsPrefetchingEnabled() && href.isValid() && !href.isEmpty())
103            prefetchDNS(href.host());
104    }
105
106#if ENABLE(LINK_PREFETCH)
107    if ((relAttribute.m_isLinkPrefetch || relAttribute.m_isLinkSubresource) && href.isValid() && document->frame()) {
108        if (!m_client->shouldLoadLink())
109            return false;
110        ResourceLoadPriority priority = ResourceLoadPriorityUnresolved;
111        CachedResource::Type type = CachedResource::LinkPrefetch;
112        // We only make one request to the cachedresourcelodaer if multiple rel types are
113        // specified,
114        if (relAttribute.m_isLinkSubresource) {
115            priority = ResourceLoadPriorityLow;
116            type = CachedResource::LinkSubresource;
117        }
118        CachedResourceRequest linkRequest(ResourceRequest(document->completeURL(href)), priority);
119
120        if (m_cachedLinkResource) {
121            m_cachedLinkResource->removeClient(this);
122            m_cachedLinkResource = 0;
123        }
124        m_cachedLinkResource = document->cachedResourceLoader()->requestLinkResource(type, linkRequest);
125        if (m_cachedLinkResource)
126            m_cachedLinkResource->addClient(this);
127    }
128#endif
129
130    return true;
131}
132
133void LinkLoader::released()
134{
135}
136
137}
138