Home | History | Annotate | Download | only in icon
      1 /*
      2  * Copyright (C) 2006 Apple Computer, 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 COMPUTER, 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 COMPUTER, 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 "Document.h"
     30 #include "Frame.h"
     31 #include "FrameLoader.h"
     32 #include "FrameLoaderClient.h"
     33 #include "IconDatabase.h"
     34 #include "Logging.h"
     35 #include "ResourceHandle.h"
     36 #include "ResourceLoadScheduler.h"
     37 #include "ResourceResponse.h"
     38 #include "ResourceRequest.h"
     39 #include "SharedBuffer.h"
     40 #include "SubresourceLoader.h"
     41 #include <wtf/UnusedParam.h>
     42 #include <wtf/text/CString.h>
     43 
     44 using namespace std;
     45 
     46 namespace WebCore {
     47 
     48 IconLoader::IconLoader(Frame* frame)
     49     : m_frame(frame)
     50     , m_loadIsInProgress(false)
     51 {
     52 }
     53 
     54 PassOwnPtr<IconLoader> IconLoader::create(Frame* frame)
     55 {
     56     return adoptPtr(new IconLoader(frame));
     57 }
     58 
     59 IconLoader::~IconLoader()
     60 {
     61 }
     62 
     63 void IconLoader::startLoading()
     64 {
     65     if (m_resourceLoader)
     66         return;
     67 
     68     // Set flag so we can detect the case where the load completes before
     69     // SubresourceLoader::create returns.
     70     m_loadIsInProgress = true;
     71 
     72     ResourceRequest resourceRequest(m_frame->loader()->iconURL());
     73     resourceRequest.setPriority(ResourceLoadPriorityLow);
     74 
     75     RefPtr<SubresourceLoader> loader = resourceLoadScheduler()->scheduleSubresourceLoad(m_frame, this, resourceRequest);
     76     if (!loader)
     77         LOG_ERROR("Failed to start load for icon at url %s", m_frame->loader()->iconURL().string().ascii().data());
     78 
     79     // Store the handle so we can cancel the load if stopLoading is called later.
     80     // But only do it if the load hasn't already completed.
     81     if (m_loadIsInProgress)
     82         m_resourceLoader = loader.release();
     83 }
     84 
     85 void IconLoader::stopLoading()
     86 {
     87     clearLoadingState();
     88 }
     89 
     90 void IconLoader::didReceiveResponse(SubresourceLoader* resourceLoader, const ResourceResponse& response)
     91 {
     92     // If we got a status code indicating an invalid response, then lets
     93     // ignore the data and not try to decode the error page as an icon.
     94     int status = response.httpStatusCode();
     95     LOG(IconDatabase, "IconLoader::didReceiveResponse() - Loader %p, response %i", resourceLoader, status);
     96 
     97     if (status && (status < 200 || status > 299)) {
     98         ResourceHandle* handle = resourceLoader->handle();
     99         finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
    100     }
    101 }
    102 
    103 void IconLoader::didReceiveData(SubresourceLoader* unusedLoader, const char*, int unusedSize)
    104 {
    105 #if LOG_DISABLED
    106     UNUSED_PARAM(unusedLoader);
    107     UNUSED_PARAM(unusedSize);
    108 #endif
    109     LOG(IconDatabase, "IconLoader::didReceiveData() - Loader %p, number of bytes %i", unusedLoader, unusedSize);
    110 }
    111 
    112 void IconLoader::didFail(SubresourceLoader* resourceLoader, const ResourceError&)
    113 {
    114     LOG(IconDatabase, "IconLoader::didFail() - Loader %p", resourceLoader);
    115 
    116     // Until <rdar://problem/5463392> is resolved and we can properly cancel SubresourceLoaders when they get an error response,
    117     // we need to be prepared to receive this call even after we've "finished loading" once.
    118     // After it is resolved, we can restore an assertion that the load is in progress if ::didFail() is called
    119 
    120     if (m_loadIsInProgress) {
    121         ASSERT(resourceLoader == m_resourceLoader);
    122         ResourceHandle* handle = resourceLoader->handle();
    123         finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
    124     }
    125 }
    126 
    127 void IconLoader::didReceiveAuthenticationChallenge(SubresourceLoader*, const AuthenticationChallenge&)
    128 {
    129     // We don't ever want to prompt for authentication just for a site icon, so
    130     // implement this method to cancel the resource load
    131     m_resourceLoader->cancel();
    132 }
    133 
    134 void IconLoader::didFinishLoading(SubresourceLoader* resourceLoader, double)
    135 {
    136     LOG(IconDatabase, "IconLoader::didFinishLoading() - Loader %p", resourceLoader);
    137 
    138     // Until <rdar://problem/5463392> is resolved and we can properly cancel SubresourceLoaders when they get an error response,
    139     // we need to be prepared to receive this call even after we've "finished loading" once.
    140     // After it is resolved, we can restore an assertion that the load is in progress if ::didFail() is called
    141 
    142     if (m_loadIsInProgress) {
    143         ASSERT(resourceLoader == m_resourceLoader);
    144         ResourceHandle* handle = resourceLoader->handle();
    145         finishLoading(handle ? handle->firstRequest().url() : KURL(), m_resourceLoader->resourceData());
    146     }
    147 }
    148 
    149 void IconLoader::finishLoading(const KURL& iconURL, PassRefPtr<SharedBuffer> data)
    150 {
    151     // When an icon load results in a 404 we commit it to the database here and clear the loading state.
    152     // But the SubresourceLoader continues pulling in data in the background for the 404 page if the server sends one.
    153     // Once that data finishes loading or if the load is cancelled while that data is being read, finishLoading ends up being called a second time.
    154     // We need to change SubresourceLoader to have a mode where it will stop itself after receiving a 404 so this won't happen -
    155     // in the meantime, we'll only commit this data to the IconDatabase if it's the first time ::finishLoading() is called
    156     // <rdar://problem/5463392> tracks that enhancement
    157 
    158     if (!iconURL.isEmpty() && m_loadIsInProgress) {
    159         LOG(IconDatabase, "IconLoader::finishLoading() - Committing iconURL %s to database", iconURL.string().ascii().data());
    160         m_frame->loader()->commitIconURLToIconDatabase(iconURL);
    161         // Setting the icon data only after committing to the database ensures that the data is
    162         // kept in memory (so it does not have to be read from the database asynchronously), since
    163         // there is a page URL referencing it.
    164         iconDatabase().setIconDataForIconURL(data, iconURL.string());
    165         m_frame->loader()->client()->dispatchDidReceiveIcon();
    166     }
    167 
    168     clearLoadingState();
    169 }
    170 
    171 void IconLoader::clearLoadingState()
    172 {
    173     m_resourceLoader = 0;
    174     m_loadIsInProgress = false;
    175 }
    176 
    177 }
    178