15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    This library is free software; you can redistribute it and/or
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    modify it under the terms of the GNU Library General Public
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    License as published by the Free Software Foundation; either
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    version 2 of the License, or (at your option) any later version.
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    This library is distributed in the hope that it will be useful,
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    but WITHOUT ANY WARRANTY; without even the implied warranty of
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Library General Public License for more details.
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    You should have received a copy of the GNU Library General Public License
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    along with this library; see the file COPYING.LIB.  If not, write to
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    Boston, MA 02110-1301, USA.
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*/
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
233c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch#ifndef ImageResource_h
243c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch#define ImageResource_h
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "core/fetch/ResourcePtr.h"
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/graphics/SVGImageCache.h"
281e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/geometry/IntRect.h"
2951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/geometry/IntSizeHash.h"
301e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/geometry/LayoutSize.h"
3151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/graphics/ImageObserver.h"
32591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/HashMap.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
363c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdochclass ImageResourceClient;
373464d02a173573db42f8ee6bb07bb74fabf4f5f2Ben Murdochclass ResourceFetcher;
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class FloatSize;
39bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)class Length;
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class MemoryCache;
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class RenderObject;
42f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)class SecurityOrigin;
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)class ImageResource FINAL : public Resource, public ImageObserver {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    friend class MemoryCache;
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    typedef ImageResourceClient ClientType;
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
503c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch    ImageResource(const ResourceRequest&);
51c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ImageResource(blink::Image*);
5209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // Exposed for testing
53c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ImageResource(const ResourceRequest&, blink::Image*);
543c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch    virtual ~ImageResource();
5502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
5609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void load(ResourceFetcher*, const ResourceLoaderOptions&) OVERRIDE;
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
58c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    blink::Image* image(); // Returns the nullImage() if the image is not available yet.
59c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    blink::Image* imageForRenderer(const RenderObject*); // Returns the nullImage() if the image is not available yet.
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool hasImage() const { return m_image.get(); }
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    bool currentFrameKnownToBeOpaque(const RenderObject*); // Side effect: ensures decoded image is in cache, therefore should only be called when about to draw the image.
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
63c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    static std::pair<blink::Image*, float> brokenImage(float deviceScaleFactor); // Returns an image and the image's resolution scale factor.
6402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    bool willPaintBrokenImage() const;
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
6610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    bool canRender(const RenderObject& renderer, float multiplier) { return !errorOccurred() && !imageSizeForRenderer(&renderer, multiplier).isEmpty(); }
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
683c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch    void setContainerSizeForRenderer(const ImageResourceClient*, const IntSize&, float);
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool usesImageContainerSize() const;
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool imageHasRelativeWidth() const;
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    bool imageHasRelativeHeight() const;
72f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // The device pixel ratio we got from the server for this image, or 1.0.
73f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    float devicePixelRatioHeaderValue() const { return m_devicePixelRatioHeaderValue; }
74f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool hasDevicePixelRatioHeaderValue() const { return m_hasDevicePixelRatioHeaderValue; }
7502772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
76e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)    enum SizeType {
77e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)        NormalSize, // Report the size of the image associated with a certain renderer
78e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)        IntrinsicSize // Report the intrinsic size, i.e. ignore whatever has been set extrinsically.
79e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)    };
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // This method takes a zoom multiplier that can be used to increase the natural size of the image by the zoom.
81e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)    LayoutSize imageSizeForRenderer(const RenderObject*, float multiplier, SizeType = NormalSize); // returns the size of the complete image.
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
847242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    static void updateBitmapImages(HashSet<ImageResource*>&, bool redecodeImages = false);
857242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
86f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool isAccessAllowed(SecurityOrigin*);
87f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
8809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didAddClient(ResourceClient*) OVERRIDE;
8909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didRemoveClient(ResourceClient*) OVERRIDE;
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
9109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void allClientsRemoved() OVERRIDE;
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
9353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    virtual void appendData(const char*, int) OVERRIDE;
9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void error(Resource::Status) OVERRIDE;
9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void responseReceived(const ResourceResponse&) OVERRIDE;
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    virtual void finishOnePart() OVERRIDE;
9702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // For compatibility, images keep loading even if there are HTTP errors.
9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool shouldIgnoreHTTPStatusCodeErrors() const OVERRIDE { return true; }
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
10109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool isImage() const OVERRIDE { return true; }
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    virtual bool stillNeedsLoad() const OVERRIDE { return !errorOccurred() && status() == Unknown && !isLoading(); }
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // ImageObserver
105c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    virtual void decodedSizeChanged(const blink::Image*, int delta) OVERRIDE;
106c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    virtual void didDraw(const blink::Image*) OVERRIDE;
10709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
108c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    virtual bool shouldPauseAnimation(const blink::Image*) OVERRIDE;
109c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    virtual void animationAdvanced(const blink::Image*) OVERRIDE;
110c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    virtual void changedInRect(const blink::Image*, const IntRect&) OVERRIDE;
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
11209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)protected:
11309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual bool isSafeToUnlock() const OVERRIDE;
11409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void destroyDecodedDataIfPossible() OVERRIDE;
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private:
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void clear();
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
11953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void setCustomAcceptHeader();
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void createImage();
12153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    void updateImage(bool allDataReceived);
1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void clearImage();
1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // If not null, changeRect is the changed part of the image.
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void notifyObservers(const IntRect* changeRect = 0);
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    virtual void switchClientsToRevalidatedResource() OVERRIDE;
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    typedef pair<IntSize, float> SizeAndZoom;
1293c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch    typedef HashMap<const ImageResourceClient*, SizeAndZoom> ContainerSizeRequests;
130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ContainerSizeRequests m_pendingContainerSizeRequests;
131f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    float m_devicePixelRatioHeaderValue;
132926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
133c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    RefPtr<blink::Image> m_image;
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    OwnPtr<SVGImageCache> m_svgImageCache;
13553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    bool m_loadingMultipartContent;
136f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool m_hasDevicePixelRatioHeaderValue;
1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
1385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
13951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)DEFINE_RESOURCE_TYPE_CASTS(Image);
14051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif
144