| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 #include "core/css/CSSImageValue.h" | 29 #include "core/css/CSSImageValue.h" |
| 30 #include "core/css/CSSPrimitiveValue.h" | 30 #include "core/css/CSSPrimitiveValue.h" |
| 31 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
| 32 #include "core/fetch/FetchInitiatorTypeNames.h" | 32 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 33 #include "core/fetch/FetchRequest.h" | 33 #include "core/fetch/FetchRequest.h" |
| 34 #include "core/fetch/ImageResource.h" | 34 #include "core/fetch/ImageResource.h" |
| 35 #include "core/fetch/ResourceFetcher.h" | 35 #include "core/fetch/ResourceFetcher.h" |
| 36 #include "core/fetch/ResourceLoaderOptions.h" | 36 #include "core/fetch/ResourceLoaderOptions.h" |
| 37 #include "core/style/StyleFetchedImageSet.h" | 37 #include "core/style/StyleFetchedImageSet.h" |
| 38 #include "core/style/StylePendingImage.h" |
| 38 #include "platform/weborigin/KURL.h" | 39 #include "platform/weborigin/KURL.h" |
| 39 #include "platform/weborigin/SecurityPolicy.h" | 40 #include "platform/weborigin/SecurityPolicy.h" |
| 40 #include "wtf/text/StringBuilder.h" | 41 #include "wtf/text/StringBuilder.h" |
| 41 | 42 |
| 42 namespace blink { | 43 namespace blink { |
| 43 | 44 |
| 44 CSSImageSetValue::CSSImageSetValue() | 45 CSSImageSetValue::CSSImageSetValue() |
| 45 : CSSValueList(ImageSetClass, CommaSeparator) | 46 : CSSValueList(ImageSetClass, CommaSeparator) |
| 46 , m_isCachePending(true) | 47 , m_accessedBestFitImage(false) |
| 47 , m_cachedScaleFactor(1) | 48 , m_scaleFactor(1) |
| 48 { | 49 { |
| 49 } | 50 } |
| 50 | 51 |
| 51 CSSImageSetValue::~CSSImageSetValue() | 52 CSSImageSetValue::~CSSImageSetValue() |
| 52 { | 53 { |
| 53 #if !ENABLE(OILPAN) | 54 #if !ENABLE(OILPAN) |
| 54 if (m_cachedImageSet && m_cachedImageSet->isImageResourceSet()) | 55 if (m_imageSet && m_imageSet->isImageResourceSet()) |
| 55 toStyleFetchedImageSet(m_cachedImageSet)->clearImageSetValue(); | 56 toStyleFetchedImageSet(m_imageSet)->clearImageSetValue(); |
| 56 #endif | 57 #endif |
| 57 } | 58 } |
| 58 | 59 |
| 59 void CSSImageSetValue::fillImageSet() | 60 void CSSImageSetValue::fillImageSet() |
| 60 { | 61 { |
| 61 size_t length = this->length(); | 62 size_t length = this->length(); |
| 62 size_t i = 0; | 63 size_t i = 0; |
| 63 while (i < length) { | 64 while (i < length) { |
| 64 CSSImageValue* imageValue = toCSSImageValue(item(i)); | 65 CSSImageValue* imageValue = toCSSImageValue(item(i)); |
| 65 String imageURL = imageValue->url(); | 66 String imageURL = imageValue->url(); |
| 66 | 67 |
| 67 ++i; | 68 ++i; |
| 68 ASSERT_WITH_SECURITY_IMPLICATION(i < length); | 69 ASSERT_WITH_SECURITY_IMPLICATION(i < length); |
| 69 CSSValue* scaleFactorValue = item(i); | 70 CSSValue* scaleFactorValue = item(i); |
| 70 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue
(); | 71 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue
(); |
| 71 | 72 |
| 72 ImageWithScale image; | 73 ImageWithScale image; |
| 73 image.imageURL = imageURL; | 74 image.imageURL = imageURL; |
| 74 image.referrer = SecurityPolicy::generateReferrer(imageValue->referrer()
.referrerPolicy, KURL(ParsedURLString, imageURL), imageValue->referrer().referre
r); | 75 image.referrer = SecurityPolicy::generateReferrer(imageValue->referrer()
.referrerPolicy, KURL(ParsedURLString, imageURL), imageValue->referrer().referre
r); |
| 75 image.scaleFactor = scaleFactor; | 76 image.scaleFactor = scaleFactor; |
| 76 m_imagesInSet.append(image); | 77 m_imagesInSet.append(image); |
| 77 ++i; | 78 ++i; |
| 78 } | 79 } |
| 79 | 80 |
| 80 // Sort the images so that they are stored in order from lowest resolution t
o highest. | 81 // Sort the images so that they are stored in order from lowest resolution t
o highest. |
| 81 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::comp
areByScaleFactor); | 82 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::comp
areByScaleFactor); |
| 82 } | 83 } |
| 83 | 84 |
| 84 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor(float
scaleFactor) | 85 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor() |
| 85 { | 86 { |
| 86 ImageWithScale image; | 87 ImageWithScale image; |
| 87 size_t numberOfImages = m_imagesInSet.size(); | 88 size_t numberOfImages = m_imagesInSet.size(); |
| 88 for (size_t i = 0; i < numberOfImages; ++i) { | 89 for (size_t i = 0; i < numberOfImages; ++i) { |
| 89 image = m_imagesInSet.at(i); | 90 image = m_imagesInSet.at(i); |
| 90 if (image.scaleFactor >= scaleFactor) | 91 if (image.scaleFactor >= m_scaleFactor) |
| 91 return image; | 92 return image; |
| 92 } | 93 } |
| 93 return image; | 94 return image; |
| 94 } | 95 } |
| 95 | 96 |
| 96 bool CSSImageSetValue::isCachePending(float deviceScaleFactor) const | 97 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(Document* document, float
deviceScaleFactor, const ResourceLoaderOptions& options) |
| 97 { | |
| 98 return m_isCachePending || deviceScaleFactor != m_cachedScaleFactor; | |
| 99 } | |
| 100 | |
| 101 StyleImage* CSSImageSetValue::cachedImageSet(float deviceScaleFactor) | |
| 102 { | |
| 103 ASSERT(!isCachePending(deviceScaleFactor)); | |
| 104 return m_cachedImageSet.get(); | |
| 105 } | |
| 106 | |
| 107 StyleFetchedImageSet* CSSImageSetValue::cacheImageSet(Document* document, float
deviceScaleFactor, const ResourceLoaderOptions& options) | |
| 108 { | 98 { |
| 109 ASSERT(document); | 99 ASSERT(document); |
| 110 | 100 |
| 101 m_scaleFactor = deviceScaleFactor; |
| 102 |
| 111 if (!m_imagesInSet.size()) | 103 if (!m_imagesInSet.size()) |
| 112 fillImageSet(); | 104 fillImageSet(); |
| 113 | 105 |
| 114 if (m_isCachePending || deviceScaleFactor != m_cachedScaleFactor) { | 106 if (!m_accessedBestFitImage) { |
| 115 // FIXME: In the future, we want to take much more than deviceScaleFacto
r into acount here. | 107 // FIXME: In the future, we want to take much more than deviceScaleFacto
r into acount here. |
| 116 // All forms of scale should be included: Page::pageScaleFactor(), Local
Frame::pageZoomFactor(), | 108 // All forms of scale should be included: Page::pageScaleFactor(), Local
Frame::pageZoomFactor(), |
| 117 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698 | 109 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698 |
| 118 ImageWithScale image = bestImageForScaleFactor(deviceScaleFactor); | 110 ImageWithScale image = bestImageForScaleFactor(); |
| 119 FetchRequest request(ResourceRequest(document->completeURL(image.imageUR
L)), FetchInitiatorTypeNames::css, options); | 111 FetchRequest request(ResourceRequest(document->completeURL(image.imageUR
L)), FetchInitiatorTypeNames::css, options); |
| 120 request.mutableResourceRequest().setHTTPReferrer(image.referrer); | 112 request.mutableResourceRequest().setHTTPReferrer(image.referrer); |
| 121 | 113 |
| 122 if (options.corsEnabled == IsCORSEnabled) | 114 if (options.corsEnabled == IsCORSEnabled) |
| 123 request.setCrossOriginAccessControl(document->securityOrigin(), opti
ons.allowCredentials, options.credentialsRequested); | 115 request.setCrossOriginAccessControl(document->securityOrigin(), opti
ons.allowCredentials, options.credentialsRequested); |
| 124 | 116 |
| 125 if (ResourcePtr<ImageResource> cachedImage = ImageResource::fetch(reques
t, document->fetcher())) { | 117 if (ResourcePtr<ImageResource> cachedImage = ImageResource::fetch(reques
t, document->fetcher())) { |
| 126 m_cachedImageSet = StyleFetchedImageSet::create(cachedImage.get(), i
mage.scaleFactor, this); | 118 m_imageSet = StyleFetchedImageSet::create(cachedImage.get(), image.s
caleFactor, this); |
| 127 m_cachedScaleFactor = deviceScaleFactor; | 119 m_accessedBestFitImage = true; |
| 128 m_isCachePending = false; | |
| 129 } | 120 } |
| 130 } | 121 } |
| 131 | 122 |
| 132 return (m_cachedImageSet && m_cachedImageSet->isImageResourceSet()) ? toStyl
eFetchedImageSet(m_cachedImageSet) : nullptr; | 123 return (m_imageSet && m_imageSet->isImageResourceSet()) ? toStyleFetchedImag
eSet(m_imageSet) : nullptr; |
| 133 } | 124 } |
| 134 | 125 |
| 135 StyleFetchedImageSet* CSSImageSetValue::cacheImageSet(Document* document, float
deviceScaleFactor) | 126 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(Document* document, float
deviceScaleFactor) |
| 136 { | 127 { |
| 137 return cacheImageSet(document, deviceScaleFactor, ResourceFetcher::defaultRe
sourceOptions()); | 128 return cachedImageSet(document, deviceScaleFactor, ResourceFetcher::defaultR
esourceOptions()); |
| 129 } |
| 130 |
| 131 StyleImage* CSSImageSetValue::cachedOrPendingImageSet(float deviceScaleFactor) |
| 132 { |
| 133 if (!m_imageSet) { |
| 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); |
| 140 } |
| 141 } |
| 142 |
| 143 return m_imageSet.get(); |
| 138 } | 144 } |
| 139 | 145 |
| 140 String CSSImageSetValue::customCSSText() const | 146 String CSSImageSetValue::customCSSText() const |
| 141 { | 147 { |
| 142 StringBuilder result; | 148 StringBuilder result; |
| 143 result.append("-webkit-image-set("); | 149 result.append("-webkit-image-set("); |
| 144 | 150 |
| 145 size_t length = this->length(); | 151 size_t length = this->length(); |
| 146 size_t i = 0; | 152 size_t i = 0; |
| 147 while (i < length) { | 153 while (i < length) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 162 | 168 |
| 163 ++i; | 169 ++i; |
| 164 } | 170 } |
| 165 | 171 |
| 166 result.append(')'); | 172 result.append(')'); |
| 167 return result.toString(); | 173 return result.toString(); |
| 168 } | 174 } |
| 169 | 175 |
| 170 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const | 176 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const |
| 171 { | 177 { |
| 172 if (!m_cachedImageSet || !m_cachedImageSet->isImageResourceSet()) | 178 if (!m_imageSet || !m_imageSet->isImageResourceSet()) |
| 173 return false; | 179 return false; |
| 174 if (Resource* cachedResource = toStyleFetchedImageSet(m_cachedImageSet)->cac
hedImage()) | 180 if (Resource* cachedResource = toStyleFetchedImageSet(m_imageSet)->cachedIma
ge()) |
| 175 return cachedResource->loadFailedOrCanceled(); | 181 return cachedResource->loadFailedOrCanceled(); |
| 176 return true; | 182 return true; |
| 177 } | 183 } |
| 178 | 184 |
| 179 DEFINE_TRACE_AFTER_DISPATCH(CSSImageSetValue) | 185 DEFINE_TRACE_AFTER_DISPATCH(CSSImageSetValue) |
| 180 { | 186 { |
| 181 visitor->trace(m_cachedImageSet); | 187 visitor->trace(m_imageSet); |
| 182 CSSValueList::traceAfterDispatch(visitor); | 188 CSSValueList::traceAfterDispatch(visitor); |
| 183 } | 189 } |
| 184 | 190 |
| 185 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::valueWithURLsMadeAbso
lute() | 191 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::valueWithURLsMadeAbso
lute() |
| 186 { | 192 { |
| 187 RefPtrWillBeRawPtr<CSSImageSetValue> value = CSSImageSetValue::create(); | 193 RefPtrWillBeRawPtr<CSSImageSetValue> value = CSSImageSetValue::create(); |
| 188 for (auto& item : *this) | 194 for (auto& item : *this) |
| 189 item->isImageValue() ? value->append(toCSSImageValue(*item).valueWithURL
MadeAbsolute()) : value->append(item); | 195 item->isImageValue() ? value->append(toCSSImageValue(*item).valueWithURL
MadeAbsolute()) : value->append(item); |
| 190 return value.release(); | 196 return value.release(); |
| 191 } | 197 } |
| 192 | 198 |
| 193 | 199 |
| 194 } // namespace blink | 200 } // namespace blink |
| OLD | NEW |