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.
23 #ifndef ImageResource_h
24 #define ImageResource_h
26 #include "core/CoreExport.h"
27 #include "core/fetch/ResourcePtr.h"
28 #include "platform/geometry/IntRect.h"
29 #include "platform/geometry/IntSizeHash.h"
30 #include "platform/geometry/LayoutSize.h"
31 #include "platform/graphics/ImageObserver.h"
32 #include "wtf/HashMap.h"
36 class ImageResourceClient
;
38 class ResourceFetcher
;
44 class SVGImageForContainer
;
46 class CORE_EXPORT ImageResource final
: public Resource
, public ImageObserver
{
47 friend class MemoryCache
;
50 typedef ImageResourceClient ClientType
;
52 static ResourcePtr
<ImageResource
> fetch(FetchRequest
&, ResourceFetcher
*);
54 ImageResource(blink::Image
*);
55 // Exposed for testing
56 ImageResource(const ResourceRequest
&, blink::Image
*);
57 ~ImageResource() override
;
59 void load(ResourceFetcher
*, const ResourceLoaderOptions
&) override
;
61 blink::Image
* image(); // Returns the nullImage() if the image is not available yet.
62 blink::Image
* imageForLayoutObject(const LayoutObject
*); // Returns the nullImage() if the image is not available yet.
63 bool hasImage() const { return m_image
.get(); }
64 // Side effect: ensures decoded image is in cache, therefore should only be called when about to draw the image.
65 // FIXME: Decoding image on the main thread is expensive, so rather than forcing decode, consider returning false
66 // when image is not decoded yet, as we do in case of deferred decoding.
67 bool currentFrameKnownToBeOpaque(const LayoutObject
*);
69 static std::pair
<blink::Image
*, float> brokenImage(float deviceScaleFactor
); // Returns an image and the image's resolution scale factor.
70 bool willPaintBrokenImage() const;
72 bool canRender(const LayoutObject
& layoutObject
, float multiplier
) { return !errorOccurred() && !imageSizeForLayoutObject(&layoutObject
, multiplier
).isEmpty(); }
74 void setContainerSizeForLayoutObject(const ImageResourceClient
*, const IntSize
&, float);
75 bool usesImageContainerSize() const;
76 bool imageHasRelativeWidth() const;
77 bool imageHasRelativeHeight() const;
78 // The device pixel ratio we got from the server for this image, or 1.0.
79 float devicePixelRatioHeaderValue() const { return m_devicePixelRatioHeaderValue
; }
80 bool hasDevicePixelRatioHeaderValue() const { return m_hasDevicePixelRatioHeaderValue
; }
83 NormalSize
, // Report the size of the image associated with a certain layoutObject
84 IntrinsicSize
, // Report the intrinsic size, i.e. ignore whatever has been set extrinsically.
85 IntrinsicCorrectedToDPR
, // Report the intrinsic size corrected to account for image density.
87 // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
88 LayoutSize
imageSizeForLayoutObject(const LayoutObject
*, float multiplier
, SizeType
= NormalSize
); // returns the size of the complete image.
89 void computeIntrinsicDimensions(Length
& intrinsicWidth
, Length
& intrinsicHeight
, FloatSize
& intrinsicRatio
);
91 bool isAccessAllowed(SecurityOrigin
*);
93 void updateImageAnimationPolicy();
95 void didAddClient(ResourceClient
*) override
;
96 void didRemoveClient(ResourceClient
*) override
;
98 void allClientsRemoved() override
;
100 void appendData(const char*, unsigned) override
;
101 void error(Resource::Status
) override
;
102 void responseReceived(const ResourceResponse
&, PassOwnPtr
<WebDataConsumerHandle
>) override
;
103 void finishOnePart() override
;
105 // For compatibility, images keep loading even if there are HTTP errors.
106 bool shouldIgnoreHTTPStatusCodeErrors() const override
{ return true; }
108 bool isImage() const override
{ return true; }
109 bool stillNeedsLoad() const override
{ return !errorOccurred() && status() == Unknown
&& !isLoading(); }
112 void decodedSizeChanged(const blink::Image
*, int delta
) override
;
113 void didDraw(const blink::Image
*) override
;
115 bool shouldPauseAnimation(const blink::Image
*) override
;
116 void animationAdvanced(const blink::Image
*) override
;
117 void changedInRect(const blink::Image
*, const IntRect
&) override
;
120 bool isSafeToUnlock() const override
;
121 void destroyDecodedDataIfPossible() override
;
124 class ImageResourceFactory
: public ResourceFactory
{
126 ImageResourceFactory()
127 : ResourceFactory(Resource::Image
) { }
129 Resource
* create(const ResourceRequest
& request
, const String
&) const override
131 return new ImageResource(request
);
134 ImageResource(const ResourceRequest
&);
138 void setCustomAcceptHeader();
140 void updateImage(bool allDataReceived
);
142 // If not null, changeRect is the changed part of the image.
143 void notifyObservers(const IntRect
* changeRect
= nullptr);
144 IntSize
svgImageSizeForLayoutObject(const LayoutObject
*) const;
145 blink::Image
* svgImageForLayoutObject(const LayoutObject
*);
146 bool loadingMultipartContent() const;
148 float m_devicePixelRatioHeaderValue
;
150 typedef HashMap
<const ImageResourceClient
*, RefPtr
<SVGImageForContainer
>> ImageForContainerMap
;
151 OwnPtr
<ImageForContainerMap
> m_imageForContainerMap
;
153 RefPtr
<blink::Image
> m_image
;
154 bool m_hasDevicePixelRatioHeaderValue
;
157 DEFINE_RESOURCE_TYPE_CASTS(Image
);