blob: e67988ecd287d3eb7c404f8effee71b982d6a529 [file] [log] [blame]
eseidel94980f22006-01-09 09:29:48 +00001/*
eseidel94980f22006-01-09 09:29:48 +00002 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
darin91298e52006-06-12 01:10:17 +00004 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
darind51eb592007-05-24 22:56:33 +00005 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
eseidel94980f22006-01-09 09:29:48 +00006
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
ddkilzerc8eccec2007-09-26 02:29:57 +000019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
eseidel94980f22006-01-09 09:29:48 +000021*/
22
eseidel8eddecf2007-01-16 00:49:43 +000023#ifndef CachedImage_h
24#define CachedImage_h
eseidel94980f22006-01-09 09:29:48 +000025
darine775cf72006-07-09 22:48:56 +000026#include "CachedResource.h"
timothy_horton@apple.com3d652c92014-11-14 04:26:45 +000027#include "Image.h"
hyattc3e15802007-03-07 07:42:45 +000028#include "ImageObserver.h"
darin91298e52006-06-12 01:10:17 +000029#include "IntRect.h"
pdr@google.com0ee8f712012-12-18 05:01:49 +000030#include "IntSizeHash.h"
eae@chromium.org9717cd82012-11-07 18:33:44 +000031#include "LayoutSize.h"
eae@chromium.org88c682c2012-11-01 15:00:26 +000032#include "SVGImageCache.h"
pdr@google.com0ee8f712012-12-18 05:01:49 +000033#include <wtf/HashMap.h>
darin91298e52006-06-12 01:10:17 +000034#include <wtf/Vector.h>
eseidel94980f22006-01-09 09:29:48 +000035
darin5630e072006-01-30 07:27:12 +000036namespace WebCore {
eseidel94980f22006-01-09 09:29:48 +000037
commit-queue@webkit.org9ae3fb32012-08-08 17:54:28 +000038class CachedImageClient;
abarth@webkit.org17d66c62010-09-08 10:26:02 +000039class CachedResourceLoader;
zimmermann@webkit.orgcf38c8a2011-10-18 08:39:18 +000040class FloatSize;
abarth@webkit.org94d2d9f2010-11-08 06:46:03 +000041class MemoryCache;
darin@apple.com8cdf7122013-09-30 02:40:50 +000042class RenderElement;
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000043class RenderObject;
hmuller@adobe.comb1e25282013-10-25 20:36:21 +000044class SecurityOrigin;
darin@apple.com8cdf7122013-09-30 02:40:50 +000045
zimmermann@webkit.org3548f102011-10-18 09:09:30 +000046struct Length;
eseidel94980f22006-01-09 09:29:48 +000047
ddkilzer@apple.combac2d4d2014-02-03 19:34:30 +000048class CachedImage final : public CachedResource, public ImageObserver {
abarth@webkit.org94d2d9f2010-11-08 06:46:03 +000049 friend class MemoryCache;
ggarenfbd237b2007-08-10 02:30:30 +000050
hyatt215edaa2006-01-28 08:52:23 +000051public:
ddkilzer@apple.combac2d4d2014-02-03 19:34:30 +000052 enum CacheBehaviorType { AutomaticallyCached, ManuallyCached };
53
commit-queue@webkit.org9ab4d3e2014-03-05 20:11:03 +000054 CachedImage(const ResourceRequest&, SessionID);
55 CachedImage(Image*, SessionID);
56 CachedImage(const URL&, Image*, SessionID);
57 CachedImage(const URL&, Image*, CacheBehaviorType, SessionID);
hyatt215edaa2006-01-28 08:52:23 +000058 virtual ~CachedImage();
eseidel94980f22006-01-09 09:29:48 +000059
achristensen@apple.com0980d452014-08-20 21:56:55 +000060 WEBCORE_EXPORT Image* image(); // Returns the nullImage() if the image is not available yet.
61 WEBCORE_EXPORT Image* imageForRenderer(const RenderObject*); // Returns the nullImage() if the image is not available yet.
simon.fraser@apple.com1f3fb4c2011-01-06 05:44:38 +000062 bool hasImage() const { return m_image.get(); }
simon.fraser@apple.coma2f15d52014-08-08 18:55:51 +000063 bool currentFrameKnownToBeOpaque(const RenderElement*);
eseidel94980f22006-01-09 09:29:48 +000064
bdakin@apple.come6372a72011-09-14 18:35:49 +000065 std::pair<Image*, float> brokenImage(float deviceScaleFactor) const; // Returns an image and the image's resolution scale factor.
bdakin@apple.com563fc9a2011-09-12 21:52:01 +000066 bool willPaintBrokenImage() const;
bdakin@apple.comb49bc3d2011-09-10 06:28:00 +000067
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000068 bool canRender(const RenderObject* renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(renderer, multiplier).isEmpty(); }
hyatt3059a512006-02-02 08:40:45 +000069
zalan@apple.comfacdea72014-04-02 14:18:58 +000070 void setContainerSizeForRenderer(const CachedImageClient*, const LayoutSize&, float);
bdakin@apple.com05d7e8e2007-12-19 07:53:52 +000071 bool usesImageContainerSize() const;
72 bool imageHasRelativeWidth() const;
73 bool imageHasRelativeHeight() const;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +000074
darin@apple.com6644eab2014-11-04 16:47:53 +000075 virtual void addDataBuffer(SharedBuffer&) override;
76 virtual void finishLoading(SharedBuffer*) override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +000077
simon.fraser@apple.com74523e72013-08-30 00:23:23 +000078 enum SizeType {
79 UsedSize,
80 IntrinsicSize
81 };
zimmermann@webkit.org8cf217f2011-10-14 08:08:51 +000082 // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
simon.fraser@apple.com74523e72013-08-30 00:23:23 +000083 LayoutSize imageSizeForRenderer(const RenderObject*, float multiplier, SizeType = UsedSize); // returns the size of the complete image.
bdakin@apple.comfe75cce2012-04-30 15:42:28 +000084 void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
eseidel94980f22006-01-09 09:29:48 +000085
ddkilzer@apple.combac2d4d2014-02-03 19:34:30 +000086 bool isManuallyCached() const { return m_isManuallyCached; }
cdumez@apple.comacd577cf2014-12-18 17:21:03 +000087 virtual bool mustRevalidateDueToCacheHeaders(const CachedResourceLoader&, CachePolicy) const override;
cdumez@apple.com0bb03ec2015-01-28 05:53:53 +000088 virtual void load(CachedResourceLoader&, const ResourceLoaderOptions&) override;
dbates@webkit.org46a1c6f2013-12-19 18:10:07 +000089
hmuller@adobe.comb1e25282013-10-25 20:36:21 +000090 bool isOriginClean(SecurityOrigin*);
91
hyatt215edaa2006-01-28 08:52:23 +000092private:
japhet@chromium.org43b65bb2012-01-11 23:54:28 +000093 void clear();
94
darin91298e52006-06-12 01:10:17 +000095 void createImage();
jamesr@google.com05f64f92012-05-17 01:31:14 +000096 void clearImage();
simon.fraser@apple.comf7d140d2009-01-02 21:04:27 +000097 // If not null, changeRect is the changed part of the image.
darin@apple.com6644eab2014-11-04 16:47:53 +000098 void notifyObservers(const IntRect* changeRect = nullptr);
commit-queue@webkit.org05565aa2011-02-26 09:32:19 +000099 void checkShouldPaintBrokenImage();
eseidel94980f22006-01-09 09:29:48 +0000100
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000101 virtual void switchClientsToRevalidatedResource() override;
102 virtual bool mayTryReplaceEncodedData() const override { return true; }
pdr@google.com10f70cf2013-01-24 22:27:44 +0000103
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000104 virtual void didAddClient(CachedResourceClient*) override;
105 virtual void didRemoveClient(CachedResourceClient*) override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000106
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000107 virtual void allClientsRemoved() override;
108 virtual void destroyDecodedData() override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000109
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000110 virtual void addData(const char* data, unsigned length) override;
111 virtual void error(CachedResource::Status) override;
112 virtual void responseReceived(const ResourceResponse&) override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000113
114 // For compatibility, images keep loading even if there are HTTP errors.
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000115 virtual bool shouldIgnoreHTTPStatusCodeErrors() const override { return true; }
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000116
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000117 virtual bool stillNeedsLoad() const override { return !errorOccurred() && status() == Unknown && !isLoading(); }
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000118
timothy_horton@apple.com3d652c92014-11-14 04:26:45 +0000119 virtual bool decodedDataIsPurgeable() const override { return m_image && m_image->decodedDataIsPurgeable(); }
120
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000121 // ImageObserver
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000122 virtual void decodedSizeChanged(const Image*, int delta) override;
123 virtual void didDraw(const Image*) override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000124
commit-queue@webkit.org2dfe6cb2014-01-16 19:42:50 +0000125 virtual void animationAdvanced(const Image*) override;
126 virtual void changedInRect(const Image*, const IntRect&) override;
carlosgc@webkit.org80e15b42013-06-07 08:57:35 +0000127
darin@apple.com6644eab2014-11-04 16:47:53 +0000128 void addIncrementalDataBuffer(SharedBuffer&);
carlosgc@webkit.org3e212df2013-06-14 10:59:11 +0000129
zalan@apple.comfacdea72014-04-02 14:18:58 +0000130 typedef std::pair<LayoutSize, float> SizeAndZoom;
pdr@google.com0ee8f712012-12-18 05:01:49 +0000131 typedef HashMap<const CachedImageClient*, SizeAndZoom> ContainerSizeRequests;
132 ContainerSizeRequests m_pendingContainerSizeRequests;
133
eric@webkit.org5b265602008-08-14 00:29:26 +0000134 RefPtr<Image> m_image;
commit-queue@webkit.orgd6a51e82013-11-19 17:22:59 +0000135 std::unique_ptr<SVGImageCache> m_svgImageCache;
darin@apple.com6644eab2014-11-04 16:47:53 +0000136 unsigned m_isManuallyCached : 1;
137 unsigned m_shouldPaintBrokenImage : 1;
hyatt215edaa2006-01-28 08:52:23 +0000138};
eseidel94980f22006-01-09 09:29:48 +0000139
cdumez@apple.comc4a3af62014-10-06 00:31:05 +0000140} // namespace WebCore
ddkilzer@apple.comf7dd8932014-01-31 02:02:05 +0000141
cdumez@apple.comc4a3af62014-10-06 00:31:05 +0000142SPECIALIZE_TYPE_TRAITS_CACHED_RESOURCE(CachedImage, CachedResource::ImageResource)
darin5630e072006-01-30 07:27:12 +0000143
cdumez@apple.comc4a3af62014-10-06 00:31:05 +0000144#endif // CachedImage_h