This source file includes following definitions.
- m_hasDevicePixelRatioHeaderValue
- m_image
- m_image
- load
- didAddClient
- didRemoveClient
- switchClientsToRevalidatedResource
- isSafeToUnlock
- destroyDecodedDataIfPossible
- allClientsRemoved
- brokenImage
- willPaintBrokenImage
- image
- imageForRenderer
- setContainerSizeForRenderer
- usesImageContainerSize
- imageHasRelativeWidth
- imageHasRelativeHeight
- imageSizeForRenderer
- computeIntrinsicDimensions
- notifyObservers
- clear
- setCustomAcceptHeader
- createImage
- clearImage
- appendData
- updateImage
- finishOnePart
- error
- responseReceived
- decodedSizeChanged
- didDraw
- shouldPauseAnimation
- animationAdvanced
- changedInRect
- currentFrameKnownToBeOpaque
- isAccessAllowed
#include "config.h"
#include "core/fetch/ImageResource.h"
#include "RuntimeEnabledFeatures.h"
#include "core/fetch/ImageResourceClient.h"
#include "core/fetch/MemoryCache.h"
#include "core/fetch/ResourceClient.h"
#include "core/fetch/ResourceClientWalker.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/FrameView.h"
#include "core/rendering/RenderObject.h"
#include "core/svg/graphics/SVGImage.h"
#include "platform/SharedBuffer.h"
#include "platform/graphics/BitmapImage.h"
#include "wtf/CurrentTime.h"
#include "wtf/StdLibExtras.h"
#include "wtf/Vector.h"
namespace WebCore {
ImageResource::ImageResource(const ResourceRequest& resourceRequest)
: Resource(resourceRequest, Image)
, m_devicePixelRatioHeaderValue(1.0)
, m_image(nullptr)
, m_loadingMultipartContent(false)
, m_hasDevicePixelRatioHeaderValue(false)
{
setStatus(Unknown);
setCustomAcceptHeader();
}
ImageResource::ImageResource(WebCore::Image* image)
: Resource(ResourceRequest(""), Image)
, m_image(image)
{
setStatus(Cached);
setLoading(false);
setCustomAcceptHeader();
}
ImageResource::ImageResource(const ResourceRequest& resourceRequest, WebCore::Image* image)
: Resource(resourceRequest, Image)
, m_image(image)
{
setStatus(Cached);
setLoading(false);
setCustomAcceptHeader();
}
ImageResource::~ImageResource()
{
clearImage();
}
void ImageResource::load(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
{
if (!fetcher || fetcher->autoLoadImages())
Resource::load(fetcher, options);
else
setLoading(false);
}
void ImageResource::didAddClient(ResourceClient* c)
{
if (m_data && !m_image && !errorOccurred()) {
createImage();
m_image->setData(m_data, true);
}
ASSERT(c->resourceClientType() == ImageResourceClient::expectedType());
if (m_image && !m_image->isNull())
static_cast<ImageResourceClient*>(c)->imageChanged(this);
Resource::didAddClient(c);
}
void ImageResource::didRemoveClient(ResourceClient* c)
{
ASSERT(c);
ASSERT(c->resourceClientType() == ImageResourceClient::expectedType());
m_pendingContainerSizeRequests.remove(static_cast<ImageResourceClient*>(c));
if (m_svgImageCache)
m_svgImageCache->removeClientFromCache(static_cast<ImageResourceClient*>(c));
Resource::didRemoveClient(c);
}
void ImageResource::switchClientsToRevalidatedResource()
{
ASSERT(resourceToRevalidate());
ASSERT(resourceToRevalidate()->isImage());
if (!m_pendingContainerSizeRequests.isEmpty()) {
ContainerSizeRequests switchContainerSizeRequests;
for (ContainerSizeRequests::iterator it = m_pendingContainerSizeRequests.begin(); it != m_pendingContainerSizeRequests.end(); ++it)
switchContainerSizeRequests.set(it->key, it->value);
Resource::switchClientsToRevalidatedResource();
ImageResource* revalidatedImageResource = toImageResource(resourceToRevalidate());
for (ContainerSizeRequests::iterator it = switchContainerSizeRequests.begin(); it != switchContainerSizeRequests.end(); ++it)
revalidatedImageResource->setContainerSizeForRenderer(it->key, it->value.first, it->value.second);
return;
}
Resource::switchClientsToRevalidatedResource();
}
bool ImageResource::isSafeToUnlock() const
{
return !m_image || (m_image->hasOneRef() && m_data->refCount() == 2);
}
void ImageResource::destroyDecodedDataIfPossible()
{
if (!hasClients() && !isLoading() && (!m_image || (m_image->hasOneRef() && m_image->isBitmapImage()))) {
m_image = nullptr;
setDecodedSize(0);
} else if (m_image && !errorOccurred()) {
m_image->destroyDecodedData(true);
}
}
void ImageResource::allClientsRemoved()
{
m_pendingContainerSizeRequests.clear();
if (m_image && !errorOccurred())
m_image->resetAnimation();
Resource::allClientsRemoved();
}
pair<WebCore::Image*, float> ImageResource::brokenImage(float deviceScaleFactor)
{
if (deviceScaleFactor >= 2) {
DEFINE_STATIC_REF(WebCore::Image, brokenImageHiRes, (WebCore::Image::loadPlatformResource("missingImage@2x")));
return std::make_pair(brokenImageHiRes, 2);
}
DEFINE_STATIC_REF(WebCore::Image, brokenImageLoRes, (WebCore::Image::loadPlatformResource("missingImage")));
return std::make_pair(brokenImageLoRes, 1);
}
bool ImageResource::willPaintBrokenImage() const
{
return errorOccurred();
}
WebCore::Image* ImageResource::image()
{
ASSERT(!isPurgeable());
if (errorOccurred()) {
return brokenImage(1).first;
}
if (m_image)
return m_image.get();
return WebCore::Image::nullImage();
}
WebCore::Image* ImageResource::imageForRenderer(const RenderObject* renderer)
{
ASSERT(!isPurgeable());
if (errorOccurred()) {
return brokenImage(1).first;
}
if (!m_image)
return WebCore::Image::nullImage();
if (m_image->isSVGImage()) {
WebCore::Image* image = m_svgImageCache->imageForRenderer(renderer);
if (image != WebCore::Image::nullImage())
return image;
}
return m_image.get();
}
void ImageResource::setContainerSizeForRenderer(const ImageResourceClient* renderer, const IntSize& containerSize, float containerZoom)
{
if (containerSize.isEmpty())
return;
ASSERT(renderer);
ASSERT(containerZoom);
if (!m_image) {
m_pendingContainerSizeRequests.set(renderer, SizeAndZoom(containerSize, containerZoom));
return;
}
if (!m_image->isSVGImage()) {
m_image->setContainerSize(containerSize);
return;
}
m_svgImageCache->setContainerSizeForRenderer(renderer, containerSize, containerZoom);
}
bool ImageResource::usesImageContainerSize() const
{
if (m_image)
return m_image->usesContainerSize();
return false;
}
bool ImageResource::imageHasRelativeWidth() const
{
if (m_image)
return m_image->hasRelativeWidth();
return false;
}
bool ImageResource::imageHasRelativeHeight() const
{
if (m_image)
return m_image->hasRelativeHeight();
return false;
}
LayoutSize ImageResource::imageSizeForRenderer(const RenderObject* renderer, float multiplier, SizeType sizeType)
{
ASSERT(!isPurgeable());
if (!m_image)
return IntSize();
LayoutSize imageSize;
if (m_image->isBitmapImage() && (renderer && renderer->shouldRespectImageOrientation() == RespectImageOrientation))
imageSize = toBitmapImage(m_image.get())->sizeRespectingOrientation();
else if (m_image->isSVGImage() && sizeType == NormalSize)
imageSize = m_svgImageCache->imageSizeForRenderer(renderer);
else
imageSize = m_image->size();
if (multiplier == 1.0f)
return imageSize;
float widthScale = m_image->hasRelativeWidth() ? 1.0f : multiplier;
float heightScale = m_image->hasRelativeHeight() ? 1.0f : multiplier;
LayoutSize minimumSize(imageSize.width() > 0 ? 1 : 0, imageSize.height() > 0 ? 1 : 0);
imageSize.scale(widthScale, heightScale);
imageSize.clampToMinimumSize(minimumSize);
ASSERT(multiplier != 1.0f || (imageSize.width().fraction() == 0.0f && imageSize.height().fraction() == 0.0f));
return imageSize;
}
void ImageResource::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
{
if (m_image)
m_image->computeIntrinsicDimensions(intrinsicWidth, intrinsicHeight, intrinsicRatio);
}
void ImageResource::notifyObservers(const IntRect* changeRect)
{
ResourceClientWalker<ImageResourceClient> w(m_clients);
while (ImageResourceClient* c = w.next())
c->imageChanged(this, changeRect);
}
void ImageResource::clear()
{
prune();
clearImage();
m_pendingContainerSizeRequests.clear();
setEncodedSize(0);
}
void ImageResource::setCustomAcceptHeader()
{
DEFINE_STATIC_LOCAL(const AtomicString, acceptWebP, ("image/webp,*/*;q=0.8", AtomicString::ConstructFromLiteral));
setAccept(acceptWebP);
}
inline void ImageResource::createImage()
{
if (m_image)
return;
if (m_response.mimeType() == "image/svg+xml") {
RefPtr<SVGImage> svgImage = SVGImage::create(this);
m_svgImageCache = SVGImageCache::create(svgImage.get());
m_image = svgImage.release();
} else {
m_image = BitmapImage::create(this);
}
if (m_image) {
if (m_image->usesContainerSize()) {
for (ContainerSizeRequests::iterator it = m_pendingContainerSizeRequests.begin(); it != m_pendingContainerSizeRequests.end(); ++it)
setContainerSizeForRenderer(it->key, it->value.first, it->value.second);
}
m_pendingContainerSizeRequests.clear();
}
}
inline void ImageResource::clearImage()
{
if (m_image)
m_image->setImageObserver(0);
m_image.clear();
}
void ImageResource::appendData(const char* data, int length)
{
Resource::appendData(data, length);
if (!m_loadingMultipartContent)
updateImage(false);
}
void ImageResource::updateImage(bool allDataReceived)
{
TRACE_EVENT0("webkit", "ImageResource::updateImage");
if (m_data)
createImage();
bool sizeAvailable = false;
if (m_image)
sizeAvailable = m_image->setData(m_data, allDataReceived);
if (sizeAvailable || allDataReceived) {
if (!m_image || m_image->isNull()) {
error(errorOccurred() ? status() : DecodeError);
if (memoryCache()->contains(this))
memoryCache()->remove(this);
return;
}
notifyObservers();
}
}
void ImageResource::finishOnePart()
{
if (m_loadingMultipartContent)
clear();
updateImage(true);
if (m_loadingMultipartContent)
m_data.clear();
Resource::finishOnePart();
}
void ImageResource::error(Resource::Status status)
{
clear();
Resource::error(status);
notifyObservers();
}
void ImageResource::responseReceived(const ResourceResponse& response)
{
if (m_loadingMultipartContent && m_data)
finishOnePart();
else if (response.isMultipart())
m_loadingMultipartContent = true;
if (RuntimeEnabledFeatures::clientHintsDprEnabled()) {
m_devicePixelRatioHeaderValue = response.httpHeaderField("DPR").toFloat(&m_hasDevicePixelRatioHeaderValue);
if (!m_hasDevicePixelRatioHeaderValue || m_devicePixelRatioHeaderValue <= 0.0) {
m_devicePixelRatioHeaderValue = 1.0;
m_hasDevicePixelRatioHeaderValue = false;
}
}
Resource::responseReceived(response);
}
void ImageResource::decodedSizeChanged(const WebCore::Image* image, int delta)
{
if (!image || image != m_image)
return;
setDecodedSize(decodedSize() + delta);
}
void ImageResource::didDraw(const WebCore::Image* image)
{
if (!image || image != m_image)
return;
double timeStamp = FrameView::currentFrameTimeStamp();
if (!timeStamp)
timeStamp = currentTime();
Resource::didAccessDecodedData(timeStamp);
}
bool ImageResource::shouldPauseAnimation(const WebCore::Image* image)
{
if (!image || image != m_image)
return false;
ResourceClientWalker<ImageResourceClient> w(m_clients);
while (ImageResourceClient* c = w.next()) {
if (c->willRenderImage(this))
return false;
}
return true;
}
void ImageResource::animationAdvanced(const WebCore::Image* image)
{
if (!image || image != m_image)
return;
notifyObservers();
}
void ImageResource::changedInRect(const WebCore::Image* image, const IntRect& rect)
{
if (!image || image != m_image)
return;
notifyObservers(&rect);
}
bool ImageResource::currentFrameKnownToBeOpaque(const RenderObject* renderer)
{
WebCore::Image* image = imageForRenderer(renderer);
if (image->isBitmapImage())
image->nativeImageForCurrentFrame();
return image->currentFrameKnownToBeOpaque();
}
bool ImageResource::isAccessAllowed(SecurityOrigin* securityOrigin)
{
if (!image()->currentFrameHasSingleSecurityOrigin())
return false;
if (passesAccessControlCheck(securityOrigin))
return true;
return !securityOrigin->taintsCanvas(response().url());
}
}