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/fetch/ResourcePtr.h"
27 #include "core/svg/graphics/SVGImageCache.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;
37 class ResourceFetcher;
44 class ImageResource FINAL : public Resource, public ImageObserver {
45 friend class MemoryCache;
48 typedef ImageResourceClient ClientType;
50 ImageResource(const ResourceRequest&);
51 ImageResource(WebCore::Image*);
52 // Exposed for testing
53 ImageResource(const ResourceRequest&, WebCore::Image*);
54 virtual ~ImageResource();
56 virtual void load(ResourceFetcher*, const ResourceLoaderOptions&) OVERRIDE;
58 WebCore::Image* image(); // Returns the nullImage() if the image is not available yet.
59 WebCore::Image* imageForRenderer(const RenderObject*); // Returns the nullImage() if the image is not available yet.
60 bool hasImage() const { return m_image.get(); }
61 bool currentFrameKnownToBeOpaque(const RenderObject*); // Side effect: ensures decoded image is in cache, therefore should only be called when about to draw the image.
63 static std::pair<WebCore::Image*, float> brokenImage(float deviceScaleFactor); // Returns an image and the image's resolution scale factor.
64 bool willPaintBrokenImage() const;
66 bool canRender(const RenderObject& renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(&renderer, multiplier).isEmpty(); }
68 void setContainerSizeForRenderer(const ImageResourceClient*, const IntSize&, float);
69 bool usesImageContainerSize() const;
70 bool imageHasRelativeWidth() const;
71 bool imageHasRelativeHeight() const;
72 // The device pixel ratio we got from the server for this image, or 1.0.
73 float devicePixelRatioHeaderValue() const { return m_devicePixelRatioHeaderValue; }
74 bool hasDevicePixelRatioHeaderValue() const { return m_hasDevicePixelRatioHeaderValue; }
77 NormalSize, // Report the size of the image associated with a certain renderer
78 IntrinsicSize // Report the intrinsic size, i.e. ignore whatever has been set extrinsically.
80 // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
81 LayoutSize imageSizeForRenderer(const RenderObject*, float multiplier, SizeType = NormalSize); // returns the size of the complete image.
82 void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
84 bool isAccessAllowed(SecurityOrigin*);
86 virtual void didAddClient(ResourceClient*) OVERRIDE;
87 virtual void didRemoveClient(ResourceClient*) OVERRIDE;
89 virtual void allClientsRemoved() OVERRIDE;
91 virtual void appendData(const char*, int) OVERRIDE;
92 virtual void error(Resource::Status) OVERRIDE;
93 virtual void responseReceived(const ResourceResponse&) OVERRIDE;
94 virtual void finishOnePart() OVERRIDE;
96 // For compatibility, images keep loading even if there are HTTP errors.
97 virtual bool shouldIgnoreHTTPStatusCodeErrors() const OVERRIDE { return true; }
99 virtual bool isImage() const OVERRIDE { return true; }
100 virtual bool stillNeedsLoad() const OVERRIDE { return !errorOccurred() && status() == Unknown && !isLoading(); }
103 virtual void decodedSizeChanged(const WebCore::Image*, int delta) OVERRIDE;
104 virtual void didDraw(const WebCore::Image*) OVERRIDE;
106 virtual bool shouldPauseAnimation(const WebCore::Image*) OVERRIDE;
107 virtual void animationAdvanced(const WebCore::Image*) OVERRIDE;
108 virtual void changedInRect(const WebCore::Image*, const IntRect&) OVERRIDE;
111 virtual bool isSafeToUnlock() const OVERRIDE;
112 virtual void destroyDecodedDataIfPossible() OVERRIDE;
117 void setCustomAcceptHeader();
119 void updateImage(bool allDataReceived);
121 // If not null, changeRect is the changed part of the image.
122 void notifyObservers(const IntRect* changeRect = 0);
124 virtual void switchClientsToRevalidatedResource() OVERRIDE;
126 typedef pair<IntSize, float> SizeAndZoom;
127 typedef HashMap<const ImageResourceClient*, SizeAndZoom> ContainerSizeRequests;
128 ContainerSizeRequests m_pendingContainerSizeRequests;
129 float m_devicePixelRatioHeaderValue;
131 RefPtr<WebCore::Image> m_image;
132 OwnPtr<SVGImageCache> m_svgImageCache;
133 bool m_loadingMultipartContent;
134 bool m_hasDevicePixelRatioHeaderValue;
137 DEFINE_RESOURCE_TYPE_CASTS(Image);