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 "core/css/CSSImageValue.h"
30 #include "core/css/CSSPrimitiveValue.h"
31 #include "core/dom/Document.h"
32 #include "core/fetch/FetchInitiatorTypeNames.h"
33 #include "core/fetch/FetchRequest.h"
34 #include "core/fetch/ImageResource.h"
35 #include "core/fetch/ResourceFetcher.h"
36 #include "core/fetch/ResourceLoaderOptions.h"
37 #include "core/style/StyleFetchedImageSet.h"
38 #include "core/style/StylePendingImage.h"
39 #include "platform/weborigin/KURL.h"
40 #include "platform/weborigin/SecurityPolicy.h"
41 #include "wtf/text/StringBuilder.h"
45 CSSImageSetValue::CSSImageSetValue()
46 : CSSValueList(ImageSetClass
, CommaSeparator
)
47 , m_accessedBestFitImage(false)
52 CSSImageSetValue::~CSSImageSetValue()
55 if (m_imageSet
&& m_imageSet
->isImageResourceSet())
56 toStyleFetchedImageSet(m_imageSet
)->clearImageSetValue();
60 void CSSImageSetValue::fillImageSet()
62 size_t length
= this->length();
65 CSSImageValue
* imageValue
= toCSSImageValue(item(i
));
66 String imageURL
= imageValue
->url();
69 ASSERT_WITH_SECURITY_IMPLICATION(i
< length
);
70 CSSValue
* scaleFactorValue
= item(i
);
71 float scaleFactor
= toCSSPrimitiveValue(scaleFactorValue
)->getFloatValue();
74 image
.imageURL
= imageURL
;
75 image
.referrer
= SecurityPolicy::generateReferrer(imageValue
->referrer().referrerPolicy
, KURL(ParsedURLString
, imageURL
), imageValue
->referrer().referrer
);
76 image
.scaleFactor
= scaleFactor
;
77 m_imagesInSet
.append(image
);
81 // Sort the images so that they are stored in order from lowest resolution to highest.
82 std::sort(m_imagesInSet
.begin(), m_imagesInSet
.end(), CSSImageSetValue::compareByScaleFactor
);
85 CSSImageSetValue::ImageWithScale
CSSImageSetValue::bestImageForScaleFactor()
88 size_t numberOfImages
= m_imagesInSet
.size();
89 for (size_t i
= 0; i
< numberOfImages
; ++i
) {
90 image
= m_imagesInSet
.at(i
);
91 if (image
.scaleFactor
>= m_scaleFactor
)
97 StyleFetchedImageSet
* CSSImageSetValue::cachedImageSet(Document
* document
, float deviceScaleFactor
, const ResourceLoaderOptions
& options
)
101 m_scaleFactor
= deviceScaleFactor
;
103 if (!m_imagesInSet
.size())
106 if (!m_accessedBestFitImage
) {
107 // FIXME: In the future, we want to take much more than deviceScaleFactor into acount here.
108 // All forms of scale should be included: Page::pageScaleFactor(), LocalFrame::pageZoomFactor(),
109 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698
110 ImageWithScale image
= bestImageForScaleFactor();
111 FetchRequest
request(ResourceRequest(document
->completeURL(image
.imageURL
)), FetchInitiatorTypeNames::css
, options
);
112 request
.mutableResourceRequest().setHTTPReferrer(image
.referrer
);
114 if (options
.corsEnabled
== IsCORSEnabled
)
115 request
.setCrossOriginAccessControl(document
->securityOrigin(), options
.allowCredentials
, options
.credentialsRequested
);
117 if (ResourcePtr
<ImageResource
> cachedImage
= ImageResource::fetch(request
, document
->fetcher())) {
118 m_imageSet
= StyleFetchedImageSet::create(cachedImage
.get(), image
.scaleFactor
, this);
119 m_accessedBestFitImage
= true;
123 return (m_imageSet
&& m_imageSet
->isImageResourceSet()) ? toStyleFetchedImageSet(m_imageSet
) : nullptr;
126 StyleFetchedImageSet
* CSSImageSetValue::cachedImageSet(Document
* document
, float deviceScaleFactor
)
128 return cachedImageSet(document
, deviceScaleFactor
, ResourceFetcher::defaultResourceOptions());
131 StyleImage
* CSSImageSetValue::cachedOrPendingImageSet(float deviceScaleFactor
)
134 m_imageSet
= StylePendingImage::create(this);
135 } else if (!m_imageSet
->isPendingImage()) {
136 // If the deviceScaleFactor has changed, we may not have the best image loaded, so we have to re-assess.
137 if (deviceScaleFactor
!= m_scaleFactor
) {
138 m_accessedBestFitImage
= false;
139 m_imageSet
= StylePendingImage::create(this);
143 return m_imageSet
.get();
146 String
CSSImageSetValue::customCSSText() const
148 StringBuilder result
;
149 result
.append("-webkit-image-set(");
151 size_t length
= this->length();
155 result
.appendLiteral(", ");
157 const CSSValue
* imageValue
= item(i
);
158 result
.append(imageValue
->cssText());
162 ASSERT_WITH_SECURITY_IMPLICATION(i
< length
);
163 const CSSValue
* scaleFactorValue
= item(i
);
164 result
.append(scaleFactorValue
->cssText());
165 // FIXME: Eventually the scale factor should contain it's own unit http://wkb.ug/100120.
166 // For now 'x' is hard-coded in the parser, so we hard-code it here too.
173 return result
.toString();
176 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const
178 if (!m_imageSet
|| !m_imageSet
->isImageResourceSet())
180 if (Resource
* cachedResource
= toStyleFetchedImageSet(m_imageSet
)->cachedImage())
181 return cachedResource
->loadFailedOrCanceled();
185 DEFINE_TRACE_AFTER_DISPATCH(CSSImageSetValue
)
187 visitor
->trace(m_imageSet
);
188 CSSValueList::traceAfterDispatch(visitor
);
191 PassRefPtrWillBeRawPtr
<CSSImageSetValue
> CSSImageSetValue::valueWithURLsMadeAbsolute()
193 CSSImageSetValue
* value
= CSSImageSetValue::create().get();
194 for (auto& item
: *this)
195 item
->isImageValue() ? value
->append(toCSSImageValue(*item
).valueWithURLMadeAbsolute()) : value
->append(item
);
196 return adoptRefWillBeNoop(value
);