CSSImageSetValue.cpp [plain text]
#include "config.h"
#include "CSSImageSetValue.h"
#if ENABLE(CSS_IMAGE_SET)
#include "CSSImageValue.h"
#include "CSSPrimitiveValue.h"
#include "CachedImage.h"
#include "CachedResourceLoader.h"
#include "CachedResourceRequest.h"
#include "CachedResourceRequestInitiators.h"
#include "CrossOriginAccessControl.h"
#include "Document.h"
#include "Page.h"
#include "StyleCachedImageSet.h"
#include "StylePendingImage.h"
#include <wtf/text/StringBuilder.h>
namespace WebCore {
CSSImageSetValue::CSSImageSetValue()
: CSSValueList(ImageSetClass, CommaSeparator)
, m_accessedBestFitImage(false)
, m_scaleFactor(1)
{
}
inline void CSSImageSetValue::detachPendingImage()
{
if (is<StylePendingImage>(m_imageSet.get()))
downcast<StylePendingImage>(*m_imageSet).detachFromCSSValue();
}
CSSImageSetValue::~CSSImageSetValue()
{
detachPendingImage();
if (is<StyleCachedImageSet>(m_imageSet.get()))
downcast<StyleCachedImageSet>(*m_imageSet).clearImageSetValue();
}
void CSSImageSetValue::fillImageSet()
{
size_t length = this->length();
size_t i = 0;
while (i < length) {
CSSValue* imageValue = item(i);
String imageURL = downcast<CSSImageValue>(*imageValue).url();
++i;
ASSERT_WITH_SECURITY_IMPLICATION(i < length);
CSSValue* scaleFactorValue = item(i);
float scaleFactor = downcast<CSSPrimitiveValue>(*scaleFactorValue).getFloatValue();
ImageWithScale image;
image.imageURL = imageURL;
image.scaleFactor = scaleFactor;
m_imagesInSet.append(image);
++i;
}
std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::compareByScaleFactor);
}
CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor()
{
ImageWithScale image;
size_t numberOfImages = m_imagesInSet.size();
for (size_t i = 0; i < numberOfImages; ++i) {
image = m_imagesInSet.at(i);
if (image.scaleFactor >= m_scaleFactor)
return image;
}
return image;
}
StyleCachedImageSet* CSSImageSetValue::cachedImageSet(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
{
Document* document = loader.document();
if (Page* page = document->page())
m_scaleFactor = page->deviceScaleFactor();
else
m_scaleFactor = 1;
if (!m_imagesInSet.size())
fillImageSet();
if (!m_accessedBestFitImage) {
ImageWithScale image = bestImageForScaleFactor();
CachedResourceRequest request(ResourceRequest(document->completeURL(image.imageURL)), options);
request.setInitiator(cachedResourceRequestInitiators().css);
if (options.requestOriginPolicy() == PotentiallyCrossOriginEnabled)
updateRequestForAccessControl(request.mutableResourceRequest(), document->securityOrigin(), options.allowCredentials());
if (CachedResourceHandle<CachedImage> cachedImage = loader.requestImage(request)) {
detachPendingImage();
m_imageSet = StyleCachedImageSet::create(cachedImage.get(), image.scaleFactor, this);
m_accessedBestFitImage = true;
}
}
return is<StyleCachedImageSet>(m_imageSet.get()) ? downcast<StyleCachedImageSet>(m_imageSet.get()) : nullptr;
}
StyleImage* CSSImageSetValue::cachedOrPendingImageSet(Document& document)
{
if (!m_imageSet)
m_imageSet = StylePendingImage::create(this);
else if (!m_imageSet->isPendingImage()) {
float deviceScaleFactor = 1;
if (Page* page = document.page())
deviceScaleFactor = page->deviceScaleFactor();
if (deviceScaleFactor != m_scaleFactor) {
m_accessedBestFitImage = false;
m_imageSet = StylePendingImage::create(this);
}
}
return m_imageSet.get();
}
String CSSImageSetValue::customCSSText() const
{
StringBuilder result;
result.appendLiteral("-webkit-image-set(");
size_t length = this->length();
size_t i = 0;
while (i < length) {
if (i > 0)
result.appendLiteral(", ");
const CSSValue* imageValue = item(i);
result.append(imageValue->cssText());
result.append(' ');
++i;
ASSERT_WITH_SECURITY_IMPLICATION(i < length);
const CSSValue* scaleFactorValue = item(i);
result.append(scaleFactorValue->cssText());
result.append('x');
++i;
}
result.append(')');
return result.toString();
}
bool CSSImageSetValue::traverseSubresources(const std::function<bool (const CachedResource&)>& handler) const
{
if (!is<StyleCachedImageSet>(m_imageSet.get()))
return false;
CachedImage* cachedResource = downcast<StyleCachedImageSet>(*m_imageSet).cachedImage();
ASSERT(cachedResource);
return handler(*cachedResource);
}
CSSImageSetValue::CSSImageSetValue(const CSSImageSetValue& cloneFrom)
: CSSValueList(cloneFrom)
, m_accessedBestFitImage(false)
, m_scaleFactor(1)
{
}
PassRefPtr<CSSImageSetValue> CSSImageSetValue::cloneForCSSOM() const
{
return adoptRef(new CSSImageSetValue(*this));
}
}
#endif // ENABLE(CSS_IMAGE_SET)