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 #ifdef ANDROID_BLOCK_NETWORK_IMAGE
87 if (!cachedResourceLoader || (cachedResourceLoader->autoLoadImages() && !cachedResourceLoader->shouldBlockNetworkImage(m_url)))
88 #else
89 if (!cachedResourceLoader || cachedResourceLoader->autoLoadImages())
90 #endif
91 CachedResource::load(cachedResourceLoader, true, DoSecurityCheck, true);
92 else
93 setLoading(false);
94 }
95
didAddClient(CachedResourceClient * c)96 void CachedImage::didAddClient(CachedResourceClient* c)
97 {
98 if (m_decodedDataDeletionTimer.isActive())
99 m_decodedDataDeletionTimer.stop();
100
101 if (m_data && !m_image && !errorOccurred()) {
102 createImage();
103 m_image->setData(m_data, true);
104 }
105
106 if (m_image && !m_image->isNull())
107 c->imageChanged(this);
108
109 CachedResource::didAddClient(c);
110 }
111
allClientsRemoved()112 void CachedImage::allClientsRemoved()
113 {
114 if (m_image && !errorOccurred())
115 m_image->resetAnimation();
116 if (double interval = memoryCache()->deadDecodedDataDeletionInterval())
117 m_decodedDataDeletionTimer.startOneShot(interval);
118 }
119
brokenImage()120 static Image* brokenImage()
121 {
122 DEFINE_STATIC_LOCAL(RefPtr<Image>, brokenImage, (Image::loadPlatformResource("missingImage")));
123 return brokenImage.get();
124 }
125
image() const126 Image* CachedImage::image() const
127 {
128 ASSERT(!isPurgeable());
129
130 if (errorOccurred() && m_shouldPaintBrokenImage)
131 return brokenImage();
132
133 if (m_image)
134 return m_image.get();
135
136 return Image::nullImage();
137 }
138
setImageContainerSize(const IntSize & containerSize)139 void CachedImage::setImageContainerSize(const IntSize& containerSize)
140 {
141 if (m_image)
142 m_image->setContainerSize(containerSize);
143 }
144
usesImageContainerSize() const145 bool CachedImage::usesImageContainerSize() const
146 {
147 if (m_image)
148 return m_image->usesContainerSize();
149
150 return false;
151 }
152
imageHasRelativeWidth() const153 bool CachedImage::imageHasRelativeWidth() const
154 {
155 if (m_image)
156 return m_image->hasRelativeWidth();
157
158 return false;
159 }
160
imageHasRelativeHeight() const161 bool CachedImage::imageHasRelativeHeight() const
162 {
163 if (m_image)
164 return m_image->hasRelativeHeight();
165
166 return false;
167 }
168
imageSize(float multiplier) const169 IntSize CachedImage::imageSize(float multiplier) const
170 {
171 ASSERT(!isPurgeable());
172
173 if (!m_image)
174 return IntSize();
175 if (multiplier == 1.0f)
176 return m_image->size();
177
178 // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
179 bool hasWidth = m_image->size().width() > 0;
180 bool hasHeight = m_image->size().height() > 0;
181 int width = m_image->size().width() * (m_image->hasRelativeWidth() ? 1.0f : multiplier);
182 int height = m_image->size().height() * (m_image->hasRelativeHeight() ? 1.0f : multiplier);
183 if (hasWidth)
184 width = max(1, width);
185 if (hasHeight)
186 height = max(1, height);
187 return IntSize(width, height);
188 }
189
imageRect(float multiplier) const190 IntRect CachedImage::imageRect(float multiplier) const
191 {
192 ASSERT(!isPurgeable());
193
194 if (!m_image)
195 return IntRect();
196 if (multiplier == 1.0f || (!m_image->hasRelativeWidth() && !m_image->hasRelativeHeight()))
197 return m_image->rect();
198
199 float widthMultiplier = (m_image->hasRelativeWidth() ? 1.0f : multiplier);
200 float heightMultiplier = (m_image->hasRelativeHeight() ? 1.0f : multiplier);
201
202 // Don't let images that have a width/height >= 1 shrink below 1 when zoomed.
203 bool hasWidth = m_image->rect().width() > 0;
204 bool hasHeight = m_image->rect().height() > 0;
205
206 int width = static_cast<int>(m_image->rect().width() * widthMultiplier);
207 int height = static_cast<int>(m_image->rect().height() * heightMultiplier);
208 if (hasWidth)
209 width = max(1, width);
210 if (hasHeight)
211 height = max(1, height);
212
213 int x = static_cast<int>(m_image->rect().x() * widthMultiplier);
214 int y = static_cast<int>(m_image->rect().y() * heightMultiplier);
215
216 return IntRect(x, y, width, height);
217 }
218
notifyObservers(const IntRect * changeRect)219 void CachedImage::notifyObservers(const IntRect* changeRect)
220 {
221 CachedResourceClientWalker w(m_clients);
222 while (CachedResourceClient* c = w.next())
223 c->imageChanged(this, changeRect);
224 }
225
checkShouldPaintBrokenImage()226 void CachedImage::checkShouldPaintBrokenImage()
227 {
228 Frame* frame = m_request ? m_request->cachedResourceLoader()->frame() : 0;
229 if (!frame)
230 return;
231
232 m_shouldPaintBrokenImage = frame->loader()->client()->shouldPaintBrokenImage(KURL(ParsedURLString, m_url));
233 }
234
clear()235 void CachedImage::clear()
236 {
237 destroyDecodedData();
238 m_image = 0;
239 setEncodedSize(0);
240 }
241
createImage()242 inline void CachedImage::createImage()
243 {
244 // Create the image if it doesn't yet exist.
245 if (m_image)
246 return;
247 #if USE(CG) && !USE(WEBKIT_IMAGE_DECODERS)
248 if (m_response.mimeType() == "application/pdf") {
249 m_image = PDFDocumentImage::create();
250 return;
251 }
252 #endif
253 #if ENABLE(SVG_AS_IMAGE)
254 if (m_response.mimeType() == "image/svg+xml") {
255 m_image = SVGImage::create(this);
256 return;
257 }
258 #endif
259 m_image = BitmapImage::create(this);
260 #if PLATFORM(ANDROID)
261 m_image->setURL(url());
262 #endif
263 }
264
maximumDecodedImageSize()265 size_t CachedImage::maximumDecodedImageSize()
266 {
267 Frame* frame = m_request ? m_request->cachedResourceLoader()->frame() : 0;
268 if (!frame)
269 return 0;
270 Settings* settings = frame->settings();
271 return settings ? settings->maximumDecodedImageSize() : 0;
272 }
273
data(PassRefPtr<SharedBuffer> data,bool allDataReceived)274 void CachedImage::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
275 {
276 m_data = data;
277
278 createImage();
279
280 bool sizeAvailable = false;
281
282 // Have the image update its data from its internal buffer.
283 // It will not do anything now, but will delay decoding until
284 // queried for info (like size or specific image frames).
285 sizeAvailable = m_image->setData(m_data, allDataReceived);
286
287 // Go ahead and tell our observers to try to draw if we have either
288 // received all the data or the size is known. Each chunk from the
289 // network causes observers to repaint, which will force that chunk
290 // to decode.
291 if (sizeAvailable || allDataReceived) {
292 size_t maxDecodedImageSize = maximumDecodedImageSize();
293 IntSize s = imageSize(1.0f);
294 size_t estimatedDecodedImageSize = s.width() * s.height() * 4; // no overflow check
295 if (m_image->isNull() || (maxDecodedImageSize > 0 && estimatedDecodedImageSize > maxDecodedImageSize)) {
296 error(errorOccurred() ? status() : DecodeError);
297 if (inCache())
298 memoryCache()->remove(this);
299 return;
300 }
301
302 // It would be nice to only redraw the decoded band of the image, but with the current design
303 // (decoding delayed until painting) that seems hard.
304 notifyObservers();
305
306 if (m_image)
307 setEncodedSize(m_image->data() ? m_image->data()->size() : 0);
308 }
309
310 if (allDataReceived) {
311 setLoading(false);
312 checkNotify();
313 }
314 }
315
error(CachedResource::Status status)316 void CachedImage::error(CachedResource::Status status)
317 {
318 checkShouldPaintBrokenImage();
319 clear();
320 setStatus(status);
321 ASSERT(errorOccurred());
322 m_data.clear();
323 notifyObservers();
324 setLoading(false);
325 checkNotify();
326 }
327
destroyDecodedData()328 void CachedImage::destroyDecodedData()
329 {
330 bool canDeleteImage = !m_image || (m_image->hasOneRef() && m_image->isBitmapImage());
331 if (isSafeToMakePurgeable() && canDeleteImage && !isLoading()) {
332 // Image refs the data buffer so we should not make it purgeable while the image is alive.
333 // Invoking addClient() will reconstruct the image object.
334 m_image = 0;
335 setDecodedSize(0);
336 if (!MemoryCache::shouldMakeResourcePurgeableOnEviction())
337 makePurgeable(true);
338 } else if (m_image && !errorOccurred())
339 m_image->destroyDecodedData();
340 }
341
decodedSizeChanged(const Image * image,int delta)342 void CachedImage::decodedSizeChanged(const Image* image, int delta)
343 {
344 if (image != m_image)
345 return;
346
347 setDecodedSize(decodedSize() + delta);
348 }
349
didDraw(const Image * image)350 void CachedImage::didDraw(const Image* image)
351 {
352 if (image != m_image)
353 return;
354
355 double timeStamp = FrameView::currentPaintTimeStamp();
356 if (!timeStamp) // If didDraw is called outside of a Frame paint.
357 timeStamp = currentTime();
358
359 CachedResource::didAccessDecodedData(timeStamp);
360 }
361
shouldPauseAnimation(const Image * image)362 bool CachedImage::shouldPauseAnimation(const Image* image)
363 {
364 if (image != m_image)
365 return false;
366
367 CachedResourceClientWalker w(m_clients);
368 while (CachedResourceClient* c = w.next()) {
369 if (c->willRenderImage(this))
370 return false;
371 }
372
373 return true;
374 }
375
animationAdvanced(const Image * image)376 void CachedImage::animationAdvanced(const Image* image)
377 {
378 if (image == m_image)
379 notifyObservers();
380 }
381
changedInRect(const Image * image,const IntRect & rect)382 void CachedImage::changedInRect(const Image* image, const IntRect& rect)
383 {
384 if (image == m_image)
385 notifyObservers(&rect);
386 }
387
388 } //namespace WebCore
389