1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22 */
23
24 #include "config.h"
25 #include "CachedImage.h"
26
27 #include "BitmapImage.h"
28 #include "MemoryCache.h"
29 #include "CachedResourceClient.h"
30 #include "CachedResourceClientWalker.h"
31 #include "CachedResourceLoader.h"
32 #include "CachedResourceRequest.h"
33 #include "Frame.h"
34 #include "FrameLoaderClient.h"
35 #include "FrameLoaderTypes.h"
36 #include "FrameView.h"
37 #include "Settings.h"
38 #include "SharedBuffer.h"
39 #include <wtf/CurrentTime.h>
40 #include <wtf/StdLibExtras.h>
41 #include <wtf/Vector.h>
42
43 #if USE(CG)
44 #include "PDFDocumentImage.h"
45 #endif
46
47 #if ENABLE(SVG_AS_IMAGE)
48 #include "SVGImage.h"
49 #endif
50
51 using std::max;
52
53 namespace WebCore {
54
CachedImage(const String & url)55 CachedImage::CachedImage(const String& url)
56 : CachedResource(url, ImageResource)
57 , m_image(0)
58 , m_decodedDataDeletionTimer(this, &CachedImage::decodedDataDeletionTimerFired)
59 , m_shouldPaintBrokenImage(true)
60 {
61 setStatus(Unknown);
62 }
63
CachedImage(Image * image)64 CachedImage::CachedImage(Image* image)
65 : CachedResource(String(), ImageResource)
66 , m_image(image)
67 , m_decodedDataDeletionTimer(this, &CachedImage::decodedDataDeletionTimerFired)
68 , m_shouldPaintBrokenImage(true)
69 {
70 setStatus(Cached);
71 setLoading(false);
72 }
73
~CachedImage()74 CachedImage::~CachedImage()
75 {
76 }
77
decodedDataDeletionTimerFired(Timer<CachedImage> *)78 void CachedImage::decodedDataDeletionTimerFired(Timer<CachedImage>*)
79 {
80 ASSERT(!hasClients());
81 destroyDecodedData();
82 }
83
load(CachedResourceLoader * cachedResourceLoader)84 void CachedImage::load(CachedResourceLoader* cachedResourceLoader)
85 {
86 if (!cachedResourceLoader || cachedResourceLoader->autoLoadImages())
87 CachedResource::load(cachedResourceLoader, true, DoSecurityCheck, true);
88 else
89 setLoading(false);
90 }
91
didAddClient(CachedResourceClient * c)92 void CachedImage::didAddClient(CachedResourceClient* c)
93 {
94 if (m_decodedDataDeletionTimer.isActive())
95 m_decodedDataDeletionTimer.stop();
96
97 if (m_data && !m_image && !errorOccurred()) {
98 createImage();
99 m_image->setData(m_data, true);
100 }
101
102 if (m_image && !m_image->isNull())
103 c->imageChanged(this);
104
105 CachedResource::didAddClient(c);
106 }
107
allClientsRemoved()108 void CachedImage::allClientsRemoved()
109 {
110 if (m_image && !errorOccurred())
111 m_image->resetAnimation();
112 if (double interval = memoryCache()->deadDecodedDataDeletionInterval())
113 m_decodedDataDeletionTimer.startOneShot(interval);
114 }
115
brokenImage()116 static Image* brokenImage()
117 {
118 DEFINE_STATIC_LOCAL(RefPtr<Image>, brokenImage, (Image::loadPlatformResource("missingImage")));
119 return brokenImage.get();
120 }
121
image() const122 Image* CachedImage::image() const
123 {
124 ASSERT(!isPurgeable());
125
126 if (errorOccurred() && m_shouldPaintBrokenImage)
127 return brokenImage();
128
129 if (m_image)
130 return m_image.get();
131
132 return Image::nullImage();
133 }
134
setImageContainerSize(const IntSize & containerSize)135 void CachedImage::setImageContainerSize(const IntSize& containerSize)
136 {
137 if (m_image)
138 m_image->setContainerSize(containerSize);
139 }
140
usesImageContainerSize() const141 bool CachedImage::usesImageContainerSize() const
142 {
143 if (m_image)
144 return m_image->usesContainerSize();
145
146 return false;
147 }
148
imageHasRelativeWidth() const149 bool CachedImage::imageHasRelativeWidth() const
150 {
151 if (m_image)
152 return m_image->hasRelativeWidth();
153
154 return false;
155 }
156
imageHasRelativeHeight() const157 bool CachedImage::imageHasRelativeHeight() const
158 {
159 if (m_image)
160 return m_image->hasRelativeHeight();
161
162 return false;
163 }
164
imageSize(float multiplier) const165 IntSize CachedImage::imageSize(float multiplier) const
166 {
167 ASSERT(!isPurgeable());
168
169 if (!m_image)
170 return IntSize();
171 if (multiplier == 1.0f)
172 return m_image->size();
173
174 // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
175 bool hasWidth = m_image->size().width() > 0;
176 bool hasHeight = m_image->size().height() > 0;
177 int width = m_image->size().width() * (m_image->hasRelativeWidth() ? 1.0f : multiplier);
178 int height = m_image->size().height() * (m_image->hasRelativeHeight() ? 1.0f : multiplier);
179 if (hasWidth)
180 width = max(1, width);
181 if (hasHeight)
182 height = max(1, height);
183 return IntSize(width, height);
184 }
185
imageRect(float multiplier) const186 IntRect CachedImage::imageRect(float multiplier) const
187 {
188 ASSERT(!isPurgeable());
189
190 if (!m_image)
191 return IntRect();
192 if (multiplier == 1.0f || (!m_image->hasRelativeWidth() && !m_image->hasRelativeHeight()))
193 return m_image->rect();
194
195 float widthMultiplier = (m_image->hasRelativeWidth() ? 1.0f : multiplier);
196 float heightMultiplier = (m_image->hasRelativeHeight() ? 1.0f : multiplier);
197
198 // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
199 bool hasWidth = m_image->rect().width() > 0;
200 bool hasHeight = m_image->rect().height() > 0;
201
202 int width = static_cast<int>(m_image->rect().width() * widthMultiplier);
203 int height = static_cast<int>(m_image->rect().height() * heightMultiplier);
204 if (hasWidth)
205 width = max(1, width);
206 if (hasHeight)
207 height = max(1, height);
208
209 int x = static_cast<int>(m_image->rect().x() * widthMultiplier);
210 int y = static_cast<int>(m_image->rect().y() * heightMultiplier);
211
212 return IntRect(x, y, width, height);
213 }
214
notifyObservers(const IntRect * changeRect)215 void CachedImage::notifyObservers(const IntRect* changeRect)
216 {
217 CachedResourceClientWalker w(m_clients);
218 while (CachedResourceClient* c = w.next())
219 c->imageChanged(this, changeRect);
220 }
221
checkShouldPaintBrokenImage()222 void CachedImage::checkShouldPaintBrokenImage()
223 {
224 Frame* frame = m_request ? m_request->cachedResourceLoader()->frame() : 0;
225 if (!frame)
226 return;
227
228 m_shouldPaintBrokenImage = frame->loader()->client()->shouldPaintBrokenImage(KURL(ParsedURLString, m_url));
229 }
230
clear()231 void CachedImage::clear()
232 {
233 destroyDecodedData();
234 m_image = 0;
235 setEncodedSize(0);
236 }
237
createImage()238 inline void CachedImage::createImage()
239 {
240 // Create the image if it doesn't yet exist.
241 if (m_image)
242 return;
243 #if USE(CG) && !USE(WEBKIT_IMAGE_DECODERS)
244 if (m_response.mimeType() == "application/pdf") {
245 m_image = PDFDocumentImage::create();
246 return;
247 }
248 #endif
249 #if ENABLE(SVG_AS_IMAGE)
250 if (m_response.mimeType() == "image/svg+xml") {
251 m_image = SVGImage::create(this);
252 return;
253 }
254 #endif
255 m_image = BitmapImage::create(this);
256 }
257
maximumDecodedImageSize()258 size_t CachedImage::maximumDecodedImageSize()
259 {
260 Frame* frame = m_request ? m_request->cachedResourceLoader()->frame() : 0;
261 if (!frame)
262 return 0;
263 Settings* settings = frame->settings();
264 return settings ? settings->maximumDecodedImageSize() : 0;
265 }
266
data(PassRefPtr<SharedBuffer> data,bool allDataReceived)267 void CachedImage::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
268 {
269 m_data = data;
270
271 createImage();
272
273 bool sizeAvailable = false;
274
275 // Have the image update its data from its internal buffer.
276 // It will not do anything now, but will delay decoding until
277 // queried for info (like size or specific image frames).
278 sizeAvailable = m_image->setData(m_data, allDataReceived);
279
280 // Go ahead and tell our observers to try to draw if we have either
281 // received all the data or the size is known. Each chunk from the
282 // network causes observers to repaint, which will force that chunk
283 // to decode.
284 if (sizeAvailable || allDataReceived) {
285 size_t maxDecodedImageSize = maximumDecodedImageSize();
286 IntSize s = imageSize(1.0f);
287 size_t estimatedDecodedImageSize = s.width() * s.height() * 4; // no overflow check
288 if (m_image->isNull() || (maxDecodedImageSize > 0 && estimatedDecodedImageSize > maxDecodedImageSize)) {
289 error(errorOccurred() ? status() : DecodeError);
290 if (inCache())
291 memoryCache()->remove(this);
292 return;
293 }
294
295 // It would be nice to only redraw the decoded band of the image, but with the current design
296 // (decoding delayed until painting) that seems hard.
297 notifyObservers();
298
299 if (m_image)
300 setEncodedSize(m_image->data() ? m_image->data()->size() : 0);
301 }
302
303 if (allDataReceived) {
304 setLoading(false);
305 checkNotify();
306 }
307 }
308
error(CachedResource::Status status)309 void CachedImage::error(CachedResource::Status status)
310 {
311 checkShouldPaintBrokenImage();
312 clear();
313 setStatus(status);
314 ASSERT(errorOccurred());
315 m_data.clear();
316 notifyObservers();
317 setLoading(false);
318 checkNotify();
319 }
320
destroyDecodedData()321 void CachedImage::destroyDecodedData()
322 {
323 bool canDeleteImage = !m_image || (m_image->hasOneRef() && m_image->isBitmapImage());
324 if (isSafeToMakePurgeable() && canDeleteImage && !isLoading()) {
325 // Image refs the data buffer so we should not make it purgeable while the image is alive.
326 // Invoking addClient() will reconstruct the image object.
327 m_image = 0;
328 setDecodedSize(0);
329 if (!MemoryCache::shouldMakeResourcePurgeableOnEviction())
330 makePurgeable(true);
331 } else if (m_image && !errorOccurred())
332 m_image->destroyDecodedData();
333 }
334
decodedSizeChanged(const Image * image,int delta)335 void CachedImage::decodedSizeChanged(const Image* image, int delta)
336 {
337 if (image != m_image)
338 return;
339
340 setDecodedSize(decodedSize() + delta);
341 }
342
didDraw(const Image * image)343 void CachedImage::didDraw(const Image* image)
344 {
345 if (image != m_image)
346 return;
347
348 double timeStamp = FrameView::currentPaintTimeStamp();
349 if (!timeStamp) // If didDraw is called outside of a Frame paint.
350 timeStamp = currentTime();
351
352 CachedResource::didAccessDecodedData(timeStamp);
353 }
354
shouldPauseAnimation(const Image * image)355 bool CachedImage::shouldPauseAnimation(const Image* image)
356 {
357 if (image != m_image)
358 return false;
359
360 CachedResourceClientWalker w(m_clients);
361 while (CachedResourceClient* c = w.next()) {
362 if (c->willRenderImage(this))
363 return false;
364 }
365
366 return true;
367 }
368
animationAdvanced(const Image * image)369 void CachedImage::animationAdvanced(const Image* image)
370 {
371 if (image == m_image)
372 notifyObservers();
373 }
374
changedInRect(const Image * image,const IntRect & rect)375 void CachedImage::changedInRect(const Image* image, const IntRect& rect)
376 {
377 if (image == m_image)
378 notifyObservers(&rect);
379 }
380
381 } //namespace WebCore
382