initial import
[vuplus_webkit] / Source / WebCore / loader / LinkLoader.cpp
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 "CSSStyleSelector.h"
36 #include "CSSStyleSheet.h"
37 #include "CachedCSSStyleSheet.h"
38 #include "CachedResourceLoader.h"
39 #include "ContainerNode.h"
40 #include "Document.h"
41 #include "Frame.h"
42 #include "FrameView.h"
43 #include "LinkRelAttribute.h"
44 #include "ResourceHandle.h"
45 #include "Settings.h"
46
47 namespace WebCore {
48
49 LinkLoader::LinkLoader(LinkLoaderClient* client)
50     : m_client(client)
51     , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired)
52     , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired)
53 {
54 }
55
56 LinkLoader::~LinkLoader()
57 {
58     if (m_cachedLinkResource)
59         m_cachedLinkResource->removeClient(this);
60 }
61
62 void LinkLoader::linkLoadTimerFired(Timer<LinkLoader>* timer)
63 {
64     ASSERT_UNUSED(timer, timer == &m_linkLoadTimer);
65     m_client->linkLoaded();
66 }
67
68 void LinkLoader::linkLoadingErrorTimerFired(Timer<LinkLoader>* timer)
69 {
70     ASSERT_UNUSED(timer, timer == &m_linkLoadingErrorTimer);
71     m_client->linkLoadingErrored();
72 }
73
74 void LinkLoader::notifyFinished(CachedResource* resource)
75 {
76     ASSERT_UNUSED(resource, m_cachedLinkResource.get() == resource);
77
78     if (m_cachedLinkResource->errorOccurred())
79         m_linkLoadingErrorTimer.startOneShot(0);
80     else 
81         m_linkLoadTimer.startOneShot(0);
82
83     m_cachedLinkResource->removeClient(this);
84     m_cachedLinkResource = 0;
85 }
86
87 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type,
88                           const KURL& href, Document* document)
89 {
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()) 
93             return false;
94         document->setIconURL(href.string(), type, relAttribute.m_iconType);
95     }
96
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);
103     }
104
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())
108             return false;
109         ResourceLoadPriority priority = ResourceLoadPriorityUnresolved;
110         CachedResource::Type type = CachedResource::LinkPrefetch;
111         // We only make one request to the cachedresourcelodaer if multiple rel types are
112         // specified, 
113         if (relAttribute.m_isLinkSubresource) {
114             priority = ResourceLoadPriorityLow;
115             type = CachedResource::LinkSubresource;
116         } else if (relAttribute.m_isLinkPrerender)
117             type = CachedResource::LinkPrerender;
118
119         ResourceRequest linkRequest(document->completeURL(href));
120         
121         if (m_cachedLinkResource) {
122             m_cachedLinkResource->removeClient(this);
123             m_cachedLinkResource = 0;
124         }
125         m_cachedLinkResource = document->cachedResourceLoader()->requestLinkResource(type, linkRequest, priority);
126         if (m_cachedLinkResource)
127             m_cachedLinkResource->addClient(this);
128     }
129 #endif
130     return true;
131 }
132
133
134 }