| 1 | /* |
| 2 | * Copyright (C) 2006-2017 Apple 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 |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "IconLoader.h" |
| 28 | |
| 29 | #include "CachedRawResource.h" |
| 30 | #include "CachedResourceLoader.h" |
| 31 | #include "CachedResourceRequest.h" |
| 32 | #include "CachedResourceRequestInitiators.h" |
| 33 | #include "Document.h" |
| 34 | #include "DocumentLoader.h" |
| 35 | #include "Frame.h" |
| 36 | #include "FrameLoader.h" |
| 37 | #include "FrameLoaderClient.h" |
| 38 | #include "Logging.h" |
| 39 | #include "ResourceRequest.h" |
| 40 | #include "SharedBuffer.h" |
| 41 | #include <wtf/text/CString.h> |
| 42 | |
| 43 | namespace WebCore { |
| 44 | |
| 45 | IconLoader::IconLoader(DocumentLoader& documentLoader, const URL& url) |
| 46 | : m_documentLoader(documentLoader) |
| 47 | , m_url(url) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | IconLoader::~IconLoader() |
| 52 | { |
| 53 | stopLoading(); |
| 54 | } |
| 55 | |
| 56 | void IconLoader::startLoading() |
| 57 | { |
| 58 | if (m_resource) |
| 59 | return; |
| 60 | |
| 61 | auto* frame = m_documentLoader.frame(); |
| 62 | if (!frame) |
| 63 | return; |
| 64 | |
| 65 | ResourceRequest resourceRequest = m_url; |
| 66 | resourceRequest.setPriority(ResourceLoadPriority::Low); |
| 67 | #if !ERROR_DISABLED |
| 68 | // Copy this because we may want to access it after transferring the |
| 69 | // `resourceRequest` to the `request`. If we don't, then the LOG_ERROR |
| 70 | // below won't print a URL. |
| 71 | auto resourceRequestURL = resourceRequest.url(); |
| 72 | #endif |
| 73 | |
| 74 | CachedResourceRequest request(WTFMove(resourceRequest), ResourceLoaderOptions( |
| 75 | SendCallbackPolicy::SendCallbacks, |
| 76 | ContentSniffingPolicy::SniffContent, |
| 77 | DataBufferingPolicy::BufferData, |
| 78 | StoredCredentialsPolicy::DoNotUse, |
| 79 | ClientCredentialPolicy::CannotAskClientForCredentials, |
| 80 | FetchOptions::Credentials::Omit, |
| 81 | SecurityCheckPolicy::DoSecurityCheck, |
| 82 | FetchOptions::Mode::NoCors, |
| 83 | CertificateInfoPolicy::DoNotIncludeCertificateInfo, |
| 84 | ContentSecurityPolicyImposition::DoPolicyCheck, |
| 85 | DefersLoadingPolicy::AllowDefersLoading, |
| 86 | CachingPolicy::AllowCaching)); |
| 87 | |
| 88 | request.setInitiator(cachedResourceRequestInitiators().icon); |
| 89 | |
| 90 | auto cachedResource = frame->document()->cachedResourceLoader().requestIcon(WTFMove(request)); |
| 91 | m_resource = cachedResource.value_or(nullptr); |
| 92 | if (m_resource) |
| 93 | m_resource->addClient(*this); |
| 94 | else |
| 95 | LOG_ERROR("Failed to start load for icon at url %s (error: %s)" , resourceRequestURL.string().ascii().data(), cachedResource.error().localizedDescription().utf8().data()); |
| 96 | } |
| 97 | |
| 98 | void IconLoader::stopLoading() |
| 99 | { |
| 100 | if (m_resource) { |
| 101 | m_resource->removeClient(*this); |
| 102 | m_resource = nullptr; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void IconLoader::notifyFinished(CachedResource& resource) |
| 107 | { |
| 108 | ASSERT_UNUSED(resource, &resource == m_resource); |
| 109 | |
| 110 | // If we got a status code indicating an invalid response, then lets |
| 111 | // ignore the data and not try to decode the error page as an icon. |
| 112 | auto* data = m_resource->resourceBuffer(); |
| 113 | int status = m_resource->response().httpStatusCode(); |
| 114 | if (status && (status < 200 || status > 299)) |
| 115 | data = nullptr; |
| 116 | |
| 117 | static const char pdfMagicNumber[] = "%PDF" ; |
| 118 | static unsigned pdfMagicNumberLength = sizeof(pdfMagicNumber) - 1; |
| 119 | if (data && data->size() >= pdfMagicNumberLength && !memcmp(data->data(), pdfMagicNumber, pdfMagicNumberLength)) { |
| 120 | LOG(IconDatabase, "IconLoader::finishLoading() - Ignoring icon at %s because it appears to be a PDF" , m_resource->url().string().ascii().data()); |
| 121 | data = nullptr; |
| 122 | } |
| 123 | |
| 124 | LOG(IconDatabase, "IconLoader::finishLoading() - Committing iconURL %s to database" , m_resource->url().string().ascii().data()); |
| 125 | |
| 126 | // DocumentLoader::finishedLoadingIcon destroys this IconLoader as it finishes. This will automatically |
| 127 | // trigger IconLoader::stopLoading() during destruction, so we should just return here. |
| 128 | m_documentLoader.finishedLoadingIcon(*this, data); |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |