2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/css/CSSImageSetValue.h"
29 #include "FetchInitiatorTypeNames.h"
30 #include "core/css/CSSImageValue.h"
31 #include "core/css/CSSPrimitiveValue.h"
32 #include "core/dom/Document.h"
33 #include "core/fetch/FetchRequest.h"
34 #include "core/fetch/ImageResource.h"
35 #include "core/fetch/ResourceFetcher.h"
36 #include "core/rendering/style/StyleFetchedImageSet.h"
37 #include "core/rendering/style/StylePendingImage.h"
38 #include "wtf/text/StringBuilder.h"
42 CSSImageSetValue::CSSImageSetValue()
43 : CSSValueList(ImageSetClass, CommaSeparator)
44 , m_accessedBestFitImage(false)
49 CSSImageSetValue::~CSSImageSetValue()
51 if (m_imageSet && m_imageSet->isImageResourceSet())
52 toStyleFetchedImageSet(m_imageSet)->clearImageSetValue();
55 void CSSImageSetValue::fillImageSet()
57 size_t length = this->length();
60 CSSValue* imageValue = item(i);
61 String imageURL = toCSSImageValue(imageValue)->url();
64 ASSERT_WITH_SECURITY_IMPLICATION(i < length);
65 CSSValue* scaleFactorValue = item(i);
66 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue();
69 image.imageURL = imageURL;
70 image.scaleFactor = scaleFactor;
71 m_imagesInSet.append(image);
75 // Sort the images so that they are stored in order from lowest resolution to highest.
76 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::compareByScaleFactor);
79 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor()
82 size_t numberOfImages = m_imagesInSet.size();
83 for (size_t i = 0; i < numberOfImages; ++i) {
84 image = m_imagesInSet.at(i);
85 if (image.scaleFactor >= m_scaleFactor)
91 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(ResourceFetcher* loader, float deviceScaleFactor, const ResourceLoaderOptions& options)
95 m_scaleFactor = deviceScaleFactor;
97 if (!m_imagesInSet.size())
100 if (!m_accessedBestFitImage) {
101 // FIXME: In the future, we want to take much more than deviceScaleFactor into acount here.
102 // All forms of scale should be included: Page::pageScaleFactor(), Frame::pageZoomFactor(),
103 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698
104 ImageWithScale image = bestImageForScaleFactor();
105 if (Document* document = loader->document()) {
106 FetchRequest request(ResourceRequest(document->completeURL(image.imageURL)), FetchInitiatorTypeNames::css, options);
108 if (options.corsEnabled == IsCORSEnabled)
109 request.setCrossOriginAccessControl(loader->document()->securityOrigin(), options.allowCredentials);
111 if (ResourcePtr<ImageResource> cachedImage = loader->fetchImage(request)) {
112 m_imageSet = StyleFetchedImageSet::create(cachedImage.get(), image.scaleFactor, this);
113 m_accessedBestFitImage = true;
118 return (m_imageSet && m_imageSet->isImageResourceSet()) ? toStyleFetchedImageSet(m_imageSet) : 0;
121 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(ResourceFetcher* fetcher, float deviceScaleFactor)
123 return cachedImageSet(fetcher, deviceScaleFactor, ResourceFetcher::defaultResourceOptions());
126 StyleImage* CSSImageSetValue::cachedOrPendingImageSet(float deviceScaleFactor)
129 m_imageSet = StylePendingImage::create(this);
130 } else if (!m_imageSet->isPendingImage()) {
131 // If the deviceScaleFactor has changed, we may not have the best image loaded, so we have to re-assess.
132 if (deviceScaleFactor != m_scaleFactor) {
133 m_accessedBestFitImage = false;
134 m_imageSet = StylePendingImage::create(this);
138 return m_imageSet.get();
141 String CSSImageSetValue::customCSSText() const
143 StringBuilder result;
144 result.append("-webkit-image-set(");
146 size_t length = this->length();
152 const CSSValue* imageValue = item(i);
153 result.append(imageValue->cssText());
157 ASSERT_WITH_SECURITY_IMPLICATION(i < length);
158 const CSSValue* scaleFactorValue = item(i);
159 result.append(scaleFactorValue->cssText());
160 // FIXME: Eventually the scale factor should contain it's own unit http://wkb.ug/100120.
161 // For now 'x' is hard-coded in the parser, so we hard-code it here too.
168 return result.toString();
171 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const
173 if (!m_imageSet || !m_imageSet->isImageResourceSet())
175 if (Resource* cachedResource = toStyleFetchedImageSet(m_imageSet)->cachedImage())
176 return cachedResource->loadFailedOrCanceled();
180 CSSImageSetValue::CSSImageSetValue(const CSSImageSetValue& cloneFrom)
181 : CSSValueList(cloneFrom)
182 , m_accessedBestFitImage(false)
185 // Non-CSSValueList data is not accessible through CSS OM, no need to clone.
188 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::cloneForCSSOM() const
190 return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSImageSetValue(*this));
193 } // namespace WebCore