2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
4 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
26 #include "CachedResource.h"
27 #include "CachedResourceClient.h"
28 #include "SVGImageCache.h"
29 #include "ImageObserver.h"
32 #include <wtf/Vector.h>
36 class CachedResourceLoader;
42 class CachedImage : public CachedResource, public ImageObserver {
43 friend class MemoryCache;
46 CachedImage(const ResourceRequest&);
48 virtual ~CachedImage();
50 virtual void load(CachedResourceLoader*, const ResourceLoaderOptions&);
52 Image* image(); // Returns the nullImage() if the image is not available yet.
53 Image* imageForRenderer(const RenderObject*); // Returns the nullImage() if the image is not available yet.
54 bool hasImage() const { return m_image.get(); }
56 std::pair<Image*, float> brokenImage(float deviceScaleFactor) const; // Returns an image and the image's resolution scale factor.
57 bool willPaintBrokenImage() const;
59 bool canRender(const RenderObject* renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(renderer, multiplier).isEmpty(); }
61 void setContainerSizeForRenderer(const RenderObject*, const IntSize&, float);
62 bool usesImageContainerSize() const;
63 bool imageHasRelativeWidth() const;
64 bool imageHasRelativeHeight() const;
66 // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
67 IntSize imageSizeForRenderer(const RenderObject*, float multiplier); // returns the size of the complete image.
68 void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
70 void removeClientForRenderer(RenderObject*);
71 virtual void didAddClient(CachedResourceClient*);
73 virtual void allClientsRemoved();
74 virtual void destroyDecodedData();
76 virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived);
77 virtual void error(CachedResource::Status);
79 // For compatibility, images keep loading even if there are HTTP errors.
80 virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return true; }
82 virtual bool isImage() const { return true; }
86 bool stillNeedsLoad() const { return !errorOccurred() && status() == Unknown && !isLoading(); }
90 virtual void decodedSizeChanged(const Image* image, int delta);
91 virtual void didDraw(const Image*);
93 virtual bool shouldPauseAnimation(const Image*);
94 virtual void animationAdvanced(const Image*);
95 virtual void changedInRect(const Image*, const IntRect&);
98 Image* lookupOrCreateImageForRenderer(const RenderObject*);
101 size_t maximumDecodedImageSize();
102 // If not null, changeRect is the changed part of the image.
103 void notifyObservers(const IntRect* changeRect = 0);
104 void decodedDataDeletionTimerFired(Timer<CachedImage>*);
105 virtual PurgePriority purgePriority() const { return PurgeFirst; }
106 void checkShouldPaintBrokenImage();
108 RefPtr<Image> m_image;
110 OwnPtr<SVGImageCache> m_svgImageCache;
112 Timer<CachedImage> m_decodedDataDeletionTimer;
113 bool m_shouldPaintBrokenImage;
116 class CachedImageClient : public CachedResourceClient {
118 virtual ~CachedImageClient() { }
119 static CachedResourceClientType expectedType() { return ImageType; }
120 virtual CachedResourceClientType resourceClientType() { return expectedType(); }
122 // Called whenever a frame of an image changes, either because we got more data from the network or
123 // because we are animating. If not null, the IntRect is the changed rect of the image.
124 virtual void imageChanged(CachedImage*, const IntRect* = 0) { }
126 // Called to find out if this client wants to actually display the image. Used to tell when we
127 // can halt animation. Content nodes that hold image refs for example would not render the image,
128 // but RenderImages would (assuming they have visibility: visible and their render tree isn't hidden
129 // e.g., in the b/f cache or in a background tab).
130 virtual bool willRenderImage(CachedImage*) { return false; }