Home | History | Annotate | Download | only in loader
      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 "FetchInitiatorTypeNames.h"
     36 #include "core/dom/Document.h"
     37 #include "core/html/LinkRelAttribute.h"
     38 #include "core/loader/Prerenderer.h"
     39 #include "core/loader/cache/FetchRequest.h"
     40 #include "core/loader/cache/ResourceFetcher.h"
     41 #include "core/page/Settings.h"
     42 #include "core/platform/PrerenderHandle.h"
     43 #include "core/platform/network/DNS.h"
     44 
     45 namespace WebCore {
     46 
     47 LinkLoader::LinkLoader(LinkLoaderClient* client)
     48     : m_client(client)
     49     , m_linkLoadTimer(this, &LinkLoader::linkLoadTimerFired)
     50     , m_linkLoadingErrorTimer(this, &LinkLoader::linkLoadingErrorTimerFired)
     51 {
     52 }
     53 
     54 LinkLoader::~LinkLoader()
     55 {
     56     if (m_cachedLinkResource)
     57         m_cachedLinkResource->removeClient(this);
     58     if (m_prerenderHandle)
     59         m_prerenderHandle->removeClient();
     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(Resource* 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 void LinkLoader::didStartPrerender()
     88 {
     89     m_client->didStartLinkPrerender();
     90 }
     91 
     92 void LinkLoader::didStopPrerender()
     93 {
     94     m_client->didStopLinkPrerender();
     95 }
     96 
     97 void LinkLoader::didSendLoadForPrerender()
     98 {
     99     m_client->didSendLoadForLinkPrerender();
    100 }
    101 
    102 void LinkLoader::didSendDOMContentLoadedForPrerender()
    103 {
    104     m_client->didSendDOMContentLoadedForLinkPrerender();
    105 }
    106 
    107 bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type, const KURL& href, Document* document)
    108 {
    109     if (relAttribute.isDNSPrefetch()) {
    110         Settings* settings = document->settings();
    111         // FIXME: The href attribute of the link element can be in "//hostname" form, and we shouldn't attempt
    112         // to complete that as URL <https://bugs.webkit.org/show_bug.cgi?id=48857>.
    113         if (settings && settings->dnsPrefetchingEnabled() && href.isValid() && !href.isEmpty())
    114             prefetchDNS(href.host());
    115     }
    116 
    117     if ((relAttribute.isLinkPrefetch() || relAttribute.isLinkSubresource()) && href.isValid() && document->frame()) {
    118         if (!m_client->shouldLoadLink())
    119             return false;
    120         Resource::Type type = relAttribute.isLinkSubresource() ?  Resource::LinkSubresource : Resource::LinkPrefetch;
    121         FetchRequest linkRequest(ResourceRequest(document->completeURL(href)), FetchInitiatorTypeNames::link);
    122         if (m_cachedLinkResource) {
    123             m_cachedLinkResource->removeClient(this);
    124             m_cachedLinkResource = 0;
    125         }
    126         m_cachedLinkResource = document->fetcher()->requestLinkResource(type, linkRequest);
    127         if (m_cachedLinkResource)
    128             m_cachedLinkResource->addClient(this);
    129     }
    130 
    131     if (relAttribute.isLinkPrerender()) {
    132         if (!m_prerenderHandle) {
    133             m_prerenderHandle = document->prerenderer()->render(this, href);
    134         } else if (m_prerenderHandle->url() != href) {
    135             m_prerenderHandle->cancel();
    136             m_prerenderHandle = document->prerenderer()->render(this, href);
    137         }
    138     } else if (m_prerenderHandle) {
    139         m_prerenderHandle->cancel();
    140         m_prerenderHandle = 0;
    141     }
    142     return true;
    143 }
    144 
    145 void LinkLoader::released()
    146 {
    147     // Only prerenders need treatment here; other links either use the Resource interface, or are notionally
    148     // atomic (dns prefetch).
    149     if (m_prerenderHandle) {
    150         m_prerenderHandle->cancel();
    151         m_prerenderHandle->removeClient();
    152         m_prerenderHandle.clear();
    153     }
    154 }
    155 
    156 }
    157