tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / loader / cache / CachedImage.h
1 /*
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.
6
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.
11
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.
16
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.
21 */
22
23 #ifndef CachedImage_h
24 #define CachedImage_h
25
26 #include "CachedResource.h"
27 #include "CachedResourceClient.h"
28 #include "SVGImageCache.h"
29 #include "ImageObserver.h"
30 #include "IntRect.h"
31 #include "Timer.h"
32 #include <wtf/Vector.h>
33
34 namespace WebCore {
35
36 class CachedResourceLoader;
37 class FloatSize;
38 class MemoryCache;
39 class RenderObject;
40 struct Length;
41
42 class CachedImage : public CachedResource, public ImageObserver {
43     friend class MemoryCache;
44
45 public:
46     CachedImage(const ResourceRequest&);
47     CachedImage(Image*);
48     virtual ~CachedImage();
49     
50     virtual void load(CachedResourceLoader*, const ResourceLoaderOptions&);
51
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(); }
55
56     std::pair<Image*, float> brokenImage(float deviceScaleFactor) const; // Returns an image and the image's resolution scale factor.
57     bool willPaintBrokenImage() const; 
58
59     bool canRender(const RenderObject* renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(renderer, multiplier).isEmpty(); }
60
61     void setContainerSizeForRenderer(const RenderObject*, const IntSize&, float);
62     bool usesImageContainerSize() const;
63     bool imageHasRelativeWidth() const;
64     bool imageHasRelativeHeight() const;
65     
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);
69
70     void removeClientForRenderer(RenderObject*);
71     virtual void didAddClient(CachedResourceClient*);
72     
73     virtual void allClientsRemoved();
74     virtual void destroyDecodedData();
75
76     virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived);
77     virtual void error(CachedResource::Status);
78     
79     // For compatibility, images keep loading even if there are HTTP errors.
80     virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return true; }
81
82     virtual bool isImage() const { return true; }
83
84     void clear();
85     
86     bool stillNeedsLoad() const { return !errorOccurred() && status() == Unknown && !isLoading(); }
87     void load();
88
89     // ImageObserver
90     virtual void decodedSizeChanged(const Image* image, int delta);
91     virtual void didDraw(const Image*);
92
93     virtual bool shouldPauseAnimation(const Image*);
94     virtual void animationAdvanced(const Image*);
95     virtual void changedInRect(const Image*, const IntRect&);
96
97 private:
98     Image* lookupOrCreateImageForRenderer(const RenderObject*);
99
100     void createImage();
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();
107
108     RefPtr<Image> m_image;
109 #if ENABLE(SVG)
110     OwnPtr<SVGImageCache> m_svgImageCache;
111 #endif
112     Timer<CachedImage> m_decodedDataDeletionTimer;
113     bool m_shouldPaintBrokenImage;
114 };
115
116 class CachedImageClient : public CachedResourceClient {
117 public:
118     virtual ~CachedImageClient() { }
119     static CachedResourceClientType expectedType() { return ImageType; }
120     virtual CachedResourceClientType resourceClientType() { return expectedType(); }
121
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) { }
125
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; }
131 };
132
133 }
134
135 #endif