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