2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "IconLoader.h"
31 #include "FrameLoader.h"
32 #include "FrameLoaderClient.h"
33 #include "IconDatabase.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>
48 IconLoader::IconLoader(Frame* frame)
50 , m_loadIsInProgress(false)
54 PassOwnPtr<IconLoader> IconLoader::create(Frame* frame)
56 return adoptPtr(new IconLoader(frame));
59 IconLoader::~IconLoader()
63 void IconLoader::startLoading()
68 // Set flag so we can detect the case where the load completes before
69 // SubresourceLoader::create returns.
70 m_loadIsInProgress = true;
72 ResourceRequest resourceRequest(m_frame->loader()->icon()->url());
73 resourceRequest.setPriority(ResourceLoadPriorityLow);
75 RefPtr<SubresourceLoader> loader = resourceLoadScheduler()->scheduleSubresourceLoad(m_frame, this, resourceRequest);
77 LOG_ERROR("Failed to start load for icon at url %s", m_frame->loader()->icon()->url().string().ascii().data());
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();
85 void IconLoader::stopLoading()
90 void IconLoader::didReceiveResponse(SubresourceLoader* resourceLoader, const ResourceResponse& response)
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);
97 if (status && (status < 200 || status > 299)) {
98 ResourceHandle* handle = resourceLoader->handle();
99 finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
103 void IconLoader::didReceiveData(SubresourceLoader* unusedLoader, const char*, int unusedSize)
106 UNUSED_PARAM(unusedLoader);
107 UNUSED_PARAM(unusedSize);
109 LOG(IconDatabase, "IconLoader::didReceiveData() - Loader %p, number of bytes %i", unusedLoader, unusedSize);
112 void IconLoader::didFail(SubresourceLoader* resourceLoader, const ResourceError&)
114 LOG(IconDatabase, "IconLoader::didFail() - Loader %p", resourceLoader);
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
120 if (m_loadIsInProgress) {
121 ASSERT(resourceLoader == m_resourceLoader);
122 ResourceHandle* handle = resourceLoader->handle();
123 finishLoading(handle ? handle->firstRequest().url() : KURL(), 0);
127 void IconLoader::didReceiveAuthenticationChallenge(SubresourceLoader*, const AuthenticationChallenge&)
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();
134 void IconLoader::didFinishLoading(SubresourceLoader* resourceLoader, double)
136 LOG(IconDatabase, "IconLoader::didFinishLoading() - Loader %p", resourceLoader);
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
142 if (m_loadIsInProgress) {
143 ASSERT(resourceLoader == m_resourceLoader);
144 ResourceHandle* handle = resourceLoader->handle();
145 finishLoading(handle ? handle->firstRequest().url() : KURL(), m_resourceLoader->resourceData());
149 void IconLoader::finishLoading(const KURL& iconURL, PassRefPtr<SharedBuffer> data)
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
158 if (!iconURL.isEmpty() && m_loadIsInProgress) {
159 LOG(IconDatabase, "IconLoader::finishLoading() - Committing iconURL %s to database", iconURL.string().ascii().data());
160 m_frame->loader()->icon()->commitToDatabase(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();
171 void IconLoader::clearLoadingState()
173 m_resourceLoader = 0;
174 m_loadIsInProgress = false;