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 "ImageObserver.h"
28#include "IntRect.h"
29#include "IntSizeHash.h"
30#include "LayoutSize.h"
31#include "SVGImageCache.h"
32#include <wtf/HashMap.h>
33#include <wtf/Vector.h>
34
35namespace WebCore {
36
37class CachedImageClient;
38class CachedResourceLoader;
39class FloatSize;
40class MemoryCache;
41class RenderElement;
42class RenderObject;
43class SecurityOrigin;
44
45struct Length;
46
47class CachedImage final : public CachedResource, public ImageObserver {
48    friend class MemoryCache;
49
50public:
51    enum CacheBehaviorType { AutomaticallyCached, ManuallyCached };
52
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();
58
59    Image* image(); // Returns the nullImage() if the image is not available yet.
60    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*); // Side effect: ensures decoded image is in cache, therefore should only be called when about to draw the image.
63
64    std::pair<Image*, float> brokenImage(float deviceScaleFactor) const; // Returns an image and the image's resolution scale factor.
65    bool willPaintBrokenImage() const;
66
67    bool canRender(const RenderObject* renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(renderer, multiplier).isEmpty(); }
68
69    void setContainerSizeForRenderer(const CachedImageClient*, const LayoutSize&, float);
70    bool usesImageContainerSize() const;
71    bool imageHasRelativeWidth() const;
72    bool imageHasRelativeHeight() const;
73
74    virtual void addDataBuffer(ResourceBuffer*) override;
75    virtual void finishLoading(ResourceBuffer*) override;
76
77    enum SizeType {
78        UsedSize,
79        IntrinsicSize
80    };
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);
84
85    bool isManuallyCached() const { return m_isManuallyCached; }
86    virtual bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
87
88#if ENABLE(DISK_IMAGE_CACHE)
89    virtual bool canUseDiskImageCache() const override;
90    virtual void useDiskImageCache() override;
91#endif
92
93    bool isOriginClean(SecurityOrigin*);
94
95private:
96    virtual void load(CachedResourceLoader*, const ResourceLoaderOptions&) override;
97
98    void clear();
99
100    void createImage();
101    void clearImage();
102    // If not null, changeRect is the changed part of the image.
103    void notifyObservers(const IntRect* changeRect = 0);
104    virtual PurgePriority purgePriority() const override { return PurgeFirst; }
105    void checkShouldPaintBrokenImage();
106
107    virtual void switchClientsToRevalidatedResource() override;
108    virtual bool mayTryReplaceEncodedData() const override { return true; }
109
110    virtual void didAddClient(CachedResourceClient*) override;
111    virtual void didRemoveClient(CachedResourceClient*) override;
112
113    virtual void allClientsRemoved() override;
114    virtual void destroyDecodedData() override;
115
116    virtual void addData(const char* data, unsigned length) override;
117    virtual void error(CachedResource::Status) override;
118    virtual void responseReceived(const ResourceResponse&) override;
119
120    // For compatibility, images keep loading even if there are HTTP errors.
121    virtual bool shouldIgnoreHTTPStatusCodeErrors() const override { return true; }
122
123    virtual bool stillNeedsLoad() const override { return !errorOccurred() && status() == Unknown && !isLoading(); }
124
125    // ImageObserver
126    virtual void decodedSizeChanged(const Image*, int delta) override;
127    virtual void didDraw(const Image*) override;
128
129    virtual void animationAdvanced(const Image*) override;
130    virtual void changedInRect(const Image*, const IntRect&) override;
131
132    void addIncrementalDataBuffer(ResourceBuffer*);
133
134    typedef std::pair<LayoutSize, float> SizeAndZoom;
135    typedef HashMap<const CachedImageClient*, SizeAndZoom> ContainerSizeRequests;
136    ContainerSizeRequests m_pendingContainerSizeRequests;
137
138    RefPtr<Image> m_image;
139    std::unique_ptr<SVGImageCache> m_svgImageCache;
140    unsigned char m_isManuallyCached : 1;
141    unsigned char m_shouldPaintBrokenImage : 1;
142};
143
144CACHED_RESOURCE_TYPE_CASTS(CachedImage, CachedResource, CachedResource::ImageResource)
145
146}
147
148#endif
149