#include "config.h"
#include "CachedImage.h"
#include "BitmapImage.h"
#include "Cache.h"
#include "CachedResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "DocLoader.h"
#include "Frame.h"
#include "Request.h"
#include "SystemTime.h"
#include <wtf/Vector.h>
#if PLATFORM(CG)
#include "PDFDocumentImage.h"
#endif
#if ENABLE(SVG_AS_IMAGE)
#include "SVGImage.h"
#endif
using std::max;
namespace WebCore {
CachedImage::CachedImage(DocLoader* docLoader, const String& url, bool forCache)
: CachedResource(url, ImageResource, forCache)
{
m_image = 0;
m_status = Unknown;
if (!docLoader || docLoader->autoLoadImages()) {
m_loading = true;
cache()->loader()->load(docLoader, this, true);
} else
m_loading = false;
}
CachedImage::CachedImage(Image* image)
: CachedResource(String(), ImageResource, false )
{
m_image = image;
m_status = Cached;
m_loading = false;
}
CachedImage::~CachedImage()
{
delete m_image;
}
void CachedImage::ref(CachedResourceClient* c)
{
CachedResource::ref(c);
if (!imageRect().isEmpty())
c->imageChanged(this);
if (!m_loading)
c->notifyFinished(this);
}
void CachedImage::allReferencesRemoved()
{
if (m_image && !m_errorOccurred)
m_image->resetAnimation();
}
static Image* brokenImage()
{
static Image* brokenImage;
if (!brokenImage)
brokenImage = Image::loadPlatformResource("missingImage");
return brokenImage;
}
static Image* nullImage()
{
static BitmapImage nullImage;
return &nullImage;
}
Image* CachedImage::image() const
{
if (m_errorOccurred)
return brokenImage();
if (m_image)
return m_image;
return nullImage();
}
void CachedImage::setImageContainerSize(const IntSize& containerSize)
{
if (m_image)
m_image->setContainerSize(containerSize);
}
bool CachedImage::usesImageContainerSize() const
{
if (m_image)
return m_image->usesContainerSize();
return false;
}
bool CachedImage::imageHasRelativeWidth() const
{
if (m_image)
return m_image->hasRelativeWidth();
return false;
}
bool CachedImage::imageHasRelativeHeight() const
{
if (m_image)
return m_image->hasRelativeHeight();
return false;
}
IntSize CachedImage::imageSize() const
{
return (m_image ? m_image->size() : IntSize());
}
IntRect CachedImage::imageRect() const
{
return (m_image ? m_image->rect() : IntRect());
}
void CachedImage::notifyObservers()
{
CachedResourceClientWalker w(m_clients);
while (CachedResourceClient *c = w.next())
c->imageChanged(this);
}
void CachedImage::clear()
{
destroyDecodedData();
delete m_image;
m_image = 0;
setEncodedSize(0);
}
inline void CachedImage::createImage()
{
if (m_image)
return;
#if PLATFORM(CG)
if (m_response.mimeType() == "application/pdf") {
m_image = new PDFDocumentImage;
return;
}
#endif
#if ENABLE(SVG_AS_IMAGE)
if (m_response.mimeType() == "image/svg+xml") {
m_image = new SVGImage(this);
return;
}
#endif
m_image = new BitmapImage(this);
}
void CachedImage::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
{
m_data = data;
createImage();
bool sizeAvailable = false;
sizeAvailable = m_image->setData(m_data, allDataReceived);
if (sizeAvailable || allDataReceived) {
if (m_image->isNull()) {
error();
if (inCache())
cache()->remove(this);
return;
}
notifyObservers();
if (m_image)
setEncodedSize(m_image->data() ? m_image->data()->size() : 0);
}
if (allDataReceived) {
m_loading = false;
checkNotify();
}
}
void CachedImage::error()
{
clear();
m_errorOccurred = true;
notifyObservers();
m_loading = false;
checkNotify();
}
void CachedImage::checkNotify()
{
if (m_loading)
return;
CachedResourceClientWalker w(m_clients);
while (CachedResourceClient* c = w.next())
c->notifyFinished(this);
}
void CachedImage::destroyDecodedData()
{
if (m_image && !m_errorOccurred)
m_image->destroyDecodedData();
}
void CachedImage::decodedSizeChanged(const Image* image, int delta)
{
if (image != m_image)
return;
setDecodedSize(decodedSize() + delta);
}
void CachedImage::didDraw(const Image* image)
{
if (image != m_image)
return;
double timeStamp = Frame::currentPaintTimeStamp();
if (!timeStamp) timeStamp = currentTime();
CachedResource::didAccessDecodedData(timeStamp);
}
bool CachedImage::shouldPauseAnimation(const Image* image)
{
if (image != m_image)
return false;
CachedResourceClientWalker w(m_clients);
while (CachedResourceClient* c = w.next()) {
if (c->willRenderImage(this))
return false;
}
return true;
}
void CachedImage::animationAdvanced(const Image* image)
{
if (image == m_image)
notifyObservers();
}
}