Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 35 | #include "FetchInitiatorTypeNames.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 36 | #include "core/dom/Document.h" |
| 37 | #include "core/html/LinkRelAttribute.h" |
| 38 | #include "core/loader/Prerenderer.h" |
Ben Murdoch | 3464d02 | 2013-07-25 10:06:57 +0100 | [diff] [blame] | 39 | #include "core/loader/cache/FetchRequest.h" |
| 40 | #include "core/loader/cache/ResourceFetcher.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 74 | void LinkLoader::notifyFinished(Resource* resource) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 75 | { |
| 76 | ASSERT_UNUSED(resource, m_cachedLinkResource.get() == resource); |
| 77 | |
| 78 | if (m_cachedLinkResource->errorOccurred()) |
| 79 | m_linkLoadingErrorTimer.startOneShot(0); |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 80 | else |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | |
Torne (Richard Coles) | e524955 | 2013-05-15 11:35:13 +0100 | [diff] [blame] | 107 | bool LinkLoader::loadLink(const LinkRelAttribute& relAttribute, const String& type, const KURL& href, Document* document) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 108 | { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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; |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 120 | Resource::Type type = relAttribute.isLinkSubresource() ? Resource::LinkSubresource : Resource::LinkPrefetch; |
| 121 | FetchRequest linkRequest(ResourceRequest(document->completeURL(href)), FetchInitiatorTypeNames::link); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 122 | if (m_cachedLinkResource) { |
| 123 | m_cachedLinkResource->removeClient(this); |
| 124 | m_cachedLinkResource = 0; |
| 125 | } |
Ben Murdoch | 3464d02 | 2013-07-25 10:06:57 +0100 | [diff] [blame] | 126 | m_cachedLinkResource = document->fetcher()->requestLinkResource(type, linkRequest); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 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 | { |
Ben Murdoch | fff8884 | 2013-07-30 15:20:09 +0100 | [diff] [blame] | 147 | // Only prerenders need treatment here; other links either use the Resource interface, or are notionally |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 148 | // atomic (dns prefetch). |
| 149 | if (m_prerenderHandle) { |
| 150 | m_prerenderHandle->cancel(); |
| 151 | m_prerenderHandle->removeClient(); |
| 152 | m_prerenderHandle.clear(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |