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"
28 #include "ImageObserver.h"
30 #include "IntSizeHash.h"
31 #include "LayoutSize.h"
32 #include "SVGImageCache.h"
33 #include <wtf/HashMap.h>
37 class CachedImageClient;
38 class CachedResourceLoader;
47 class CachedImage final : public CachedResource, public ImageObserver {
48 friend class MemoryCache;
51 enum CacheBehaviorType { AutomaticallyCached, ManuallyCached };
53 CachedImage(const ResourceRequest&, SessionID);
54 CachedImage(Image*, SessionID);
55 CachedImage(const URL&, Image*, SessionID);
56 CachedImage(const URL&, Image*, CacheBehaviorType, SessionID);
57 virtual ~CachedImage();
59 WEBCORE_EXPORT Image* image(); // Returns the nullImage() if the image is not available yet.
60 WEBCORE_EXPORT Image* imageForRenderer(const RenderObject*); // Returns the nullImage() if the image is not available yet.
61 bool hasImage() const { return m_image.get(); }
62 bool currentFrameKnownToBeOpaque(const RenderElement*);
64 std::pair<Image*, float> brokenImage(float deviceScaleFactor) const; // Returns an image and the image's resolution scale factor.
65 bool willPaintBrokenImage() const;
67 bool canRender(const RenderObject* renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(renderer, multiplier).isEmpty(); }
69 void setContainerSizeForRenderer(const CachedImageClient*, const LayoutSize&, float);
70 bool usesImageContainerSize() const;
71 bool imageHasRelativeWidth() const;
72 bool imageHasRelativeHeight() const;
74 void addDataBuffer(SharedBuffer&) override;
75 void finishLoading(SharedBuffer*) override;
81 // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
82 LayoutSize imageSizeForRenderer(const RenderObject*, float multiplier, SizeType = UsedSize); // returns the size of the complete image.
83 void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
85 bool isManuallyCached() const { return m_isManuallyCached; }
86 RevalidationDecision makeRevalidationDecision(CachePolicy) const override;
87 void load(CachedResourceLoader&, const ResourceLoaderOptions&) override;
89 bool isOriginClean(SecurityOrigin*);
94 CachedImage(CachedImage&, const ResourceRequest&, SessionID);
96 void setBodyDataFrom(const CachedResource&) final;
100 // If not null, changeRect is the changed part of the image.
101 void notifyObservers(const IntRect* changeRect = nullptr);
102 void checkShouldPaintBrokenImage();
104 void switchClientsToRevalidatedResource() override;
105 bool mayTryReplaceEncodedData() const override { return true; }
107 void didAddClient(CachedResourceClient*) override;
108 void didRemoveClient(CachedResourceClient*) override;
110 void allClientsRemoved() override;
111 void destroyDecodedData() override;
113 void addData(const char* data, unsigned length) override;
114 void error(CachedResource::Status) override;
115 void responseReceived(const ResourceResponse&) override;
117 // For compatibility, images keep loading even if there are HTTP errors.
118 bool shouldIgnoreHTTPStatusCodeErrors() const override { return true; }
120 bool stillNeedsLoad() const override { return !errorOccurred() && status() == Unknown && !isLoading(); }
123 void decodedSizeChanged(const Image*, int delta) override;
124 void didDraw(const Image*) override;
126 void animationAdvanced(const Image*) override;
127 void changedInRect(const Image*, const IntRect&) override;
129 void addIncrementalDataBuffer(SharedBuffer&);
131 void didReplaceSharedBufferContents() override;
133 typedef std::pair<LayoutSize, float> SizeAndZoom;
134 typedef HashMap<const CachedImageClient*, SizeAndZoom> ContainerSizeRequests;
135 ContainerSizeRequests m_pendingContainerSizeRequests;
137 RefPtr<Image> m_image;
138 std::unique_ptr<SVGImageCache> m_svgImageCache;
139 unsigned m_isManuallyCached : 1;
140 unsigned m_shouldPaintBrokenImage : 1;
143 } // namespace WebCore
145 SPECIALIZE_TYPE_TRAITS_CACHED_RESOURCE(CachedImage, CachedResource::ImageResource)
147 #endif // CachedImage_h