/*
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *           (C) 2000 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#pragma once

#include "CachedImage.h"
#include "CachedResourceHandle.h"
#include "StyleImage.h"

namespace WebCore {

class CSSValue;
class CSSImageValue;
class Document;
class RenderSVGResourceContainer;
class RenderElement;
class TreeScope;

class StyleCachedImage FINAL : public StyleImage {
    WTF_MAKE_FAST_ALLOCATED;
public:
    static Ref<StyleCachedImage> create(CSSImageValue& cssValue, float scaleFactor = 1);
    virtual ~StyleCachedImage();

    virtual bool operator==(const StyleImage& other) const FINAL;

    virtual CachedImage* cachedImage() const FINAL;

    virtual WrappedImagePtr data() const FINAL { return m_cachedImage.get(); }

    virtual Ref<CSSValue> cssValue() const FINAL;
    
    virtual bool canRender(const RenderElement*, float multiplier) const FINAL;
    virtual bool isPending() const FINAL;
    virtual void load(CachedResourceLoader&, const ResourceLoaderOptions&) FINAL;
    virtual bool isLoaded(const RenderElement*) const FINAL;
    virtual bool errorOccurred() const FINAL;
    virtual FloatSize imageSize(const RenderElement*, float multiplier) const FINAL;
    virtual bool imageHasRelativeWidth() const FINAL;
    virtual bool imageHasRelativeHeight() const FINAL;
    virtual void computeIntrinsicDimensions(const RenderElement*, Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) FINAL;
    virtual bool usesImageContainerSize() const FINAL;
    virtual void setContainerContextForRenderer(const RenderElement&, const FloatSize&, float) FINAL;
    virtual void addClient(RenderElement&) FINAL;
    virtual void removeClient(RenderElement&) FINAL;
    bool hasClient(RenderElement&) const final;
    bool hasImage() const final;
    virtual PassRefPtr<Image> image(RenderElement*, const FloatSize&) const FINAL;
    virtual float imageScaleFactor() const FINAL;
    virtual bool knownToBeOpaque(const RenderElement*) const FINAL;
    bool usesDataProtocol() const final;

    KURL reresolvedURL(const Document&) const;

private:
    StyleCachedImage(CSSImageValue&, float);

    RenderSVGResourceContainer* uncheckedRenderSVGResource(TreeScope&, const AtomicString& fragment) const;
    RenderSVGResourceContainer* uncheckedRenderSVGResource(const RenderElement*) const;
    RenderSVGResourceContainer* renderSVGResource(const RenderElement*) const;
    bool isRenderSVGResource(const RenderElement*) const;

    KURL imageURL() const;

    Ref<CSSImageValue> m_cssValue;
    bool m_isPending { true };
    mutable float m_scaleFactor { 1 };
    mutable CachedResourceHandle<CachedImage> m_cachedImage;
    mutable Optional<bool> m_isRenderSVGResource;
    FloatSize m_containerSize;
};

}
