Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: third_party/WebKit/Source/core/css/CSSImageSetValue.cpp

Issue 1368613002: Replace RawPtr with RefPtr on StylePendingImage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix image set caching logic Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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"
39 #include "platform/weborigin/KURL.h" 38 #include "platform/weborigin/KURL.h"
40 #include "platform/weborigin/SecurityPolicy.h" 39 #include "platform/weborigin/SecurityPolicy.h"
41 #include "wtf/text/StringBuilder.h" 40 #include "wtf/text/StringBuilder.h"
42 41
43 namespace blink { 42 namespace blink {
44 43
45 CSSImageSetValue::CSSImageSetValue() 44 CSSImageSetValue::CSSImageSetValue()
46 : CSSValueList(ImageSetClass, CommaSeparator) 45 : CSSValueList(ImageSetClass, CommaSeparator)
47 , m_accessedBestFitImage(false) 46 , m_isCachePending(true)
48 , m_scaleFactor(1) 47 , m_cachedScaleFactor(1)
49 { 48 {
50 } 49 }
51 50
52 CSSImageSetValue::~CSSImageSetValue() 51 CSSImageSetValue::~CSSImageSetValue()
53 { 52 {
54 #if !ENABLE(OILPAN) 53 #if !ENABLE(OILPAN)
55 if (m_imageSet && m_imageSet->isImageResourceSet()) 54 if (m_cachedImageSet && m_cachedImageSet->isImageResourceSet())
56 toStyleFetchedImageSet(m_imageSet)->clearImageSetValue(); 55 toStyleFetchedImageSet(m_cachedImageSet)->clearImageSetValue();
57 #endif 56 #endif
58 } 57 }
59 58
60 void CSSImageSetValue::fillImageSet() 59 void CSSImageSetValue::fillImageSet()
61 { 60 {
62 size_t length = this->length(); 61 size_t length = this->length();
63 size_t i = 0; 62 size_t i = 0;
64 while (i < length) { 63 while (i < length) {
65 CSSImageValue* imageValue = toCSSImageValue(item(i)); 64 CSSImageValue* imageValue = toCSSImageValue(item(i));
66 String imageURL = imageValue->url(); 65 String imageURL = imageValue->url();
67 66
68 ++i; 67 ++i;
69 ASSERT_WITH_SECURITY_IMPLICATION(i < length); 68 ASSERT_WITH_SECURITY_IMPLICATION(i < length);
70 CSSValue* scaleFactorValue = item(i); 69 CSSValue* scaleFactorValue = item(i);
71 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue (); 70 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue ();
72 71
73 ImageWithScale image; 72 ImageWithScale image;
74 image.imageURL = imageURL; 73 image.imageURL = imageURL;
75 image.referrer = SecurityPolicy::generateReferrer(imageValue->referrer() .referrerPolicy, KURL(ParsedURLString, imageURL), imageValue->referrer().referre r); 74 image.referrer = SecurityPolicy::generateReferrer(imageValue->referrer() .referrerPolicy, KURL(ParsedURLString, imageURL), imageValue->referrer().referre r);
76 image.scaleFactor = scaleFactor; 75 image.scaleFactor = scaleFactor;
77 m_imagesInSet.append(image); 76 m_imagesInSet.append(image);
78 ++i; 77 ++i;
79 } 78 }
80 79
81 // Sort the images so that they are stored in order from lowest resolution t o highest. 80 // Sort the images so that they are stored in order from lowest resolution t o highest.
82 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::comp areByScaleFactor); 81 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::comp areByScaleFactor);
83 } 82 }
84 83
85 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor() 84 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor(float scaleFactor)
86 { 85 {
87 ImageWithScale image; 86 ImageWithScale image;
88 size_t numberOfImages = m_imagesInSet.size(); 87 size_t numberOfImages = m_imagesInSet.size();
89 for (size_t i = 0; i < numberOfImages; ++i) { 88 for (size_t i = 0; i < numberOfImages; ++i) {
90 image = m_imagesInSet.at(i); 89 image = m_imagesInSet.at(i);
91 if (image.scaleFactor >= m_scaleFactor) 90 if (image.scaleFactor >= scaleFactor)
92 return image; 91 return image;
93 } 92 }
94 return image; 93 return image;
95 } 94 }
96 95
97 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(Document* document, float deviceScaleFactor, const ResourceLoaderOptions& options) 96 bool CSSImageSetValue::isCachePending(float deviceScaleFactor) const
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)
98 { 108 {
99 ASSERT(document); 109 ASSERT(document);
100 110
101 m_scaleFactor = deviceScaleFactor;
102
103 if (!m_imagesInSet.size()) 111 if (!m_imagesInSet.size())
104 fillImageSet(); 112 fillImageSet();
105 113
106 if (!m_accessedBestFitImage) { 114 if (m_isCachePending || deviceScaleFactor != m_cachedScaleFactor) {
107 // FIXME: In the future, we want to take much more than deviceScaleFacto r into acount here. 115 // FIXME: In the future, we want to take much more than deviceScaleFacto r into acount here.
108 // All forms of scale should be included: Page::pageScaleFactor(), Local Frame::pageZoomFactor(), 116 // All forms of scale should be included: Page::pageScaleFactor(), Local Frame::pageZoomFactor(),
109 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698 117 // and any CSS transforms. https://bugs.webkit.org/show_bug.cgi?id=81698
110 ImageWithScale image = bestImageForScaleFactor(); 118 ImageWithScale image = bestImageForScaleFactor(deviceScaleFactor);
111 FetchRequest request(ResourceRequest(document->completeURL(image.imageUR L)), FetchInitiatorTypeNames::css, options); 119 FetchRequest request(ResourceRequest(document->completeURL(image.imageUR L)), FetchInitiatorTypeNames::css, options);
112 request.mutableResourceRequest().setHTTPReferrer(image.referrer); 120 request.mutableResourceRequest().setHTTPReferrer(image.referrer);
113 121
114 if (options.corsEnabled == IsCORSEnabled) 122 if (options.corsEnabled == IsCORSEnabled)
115 request.setCrossOriginAccessControl(document->securityOrigin(), opti ons.allowCredentials, options.credentialsRequested); 123 request.setCrossOriginAccessControl(document->securityOrigin(), opti ons.allowCredentials, options.credentialsRequested);
116 124
117 if (ResourcePtr<ImageResource> cachedImage = ImageResource::fetch(reques t, document->fetcher())) { 125 if (ResourcePtr<ImageResource> cachedImage = ImageResource::fetch(reques t, document->fetcher())) {
118 m_imageSet = StyleFetchedImageSet::create(cachedImage.get(), image.s caleFactor, this); 126 m_cachedImageSet = StyleFetchedImageSet::create(cachedImage.get(), i mage.scaleFactor, this);
119 m_accessedBestFitImage = true; 127 m_cachedScaleFactor = deviceScaleFactor;
128 m_isCachePending = false;
120 } 129 }
121 } 130 }
122 131
123 return (m_imageSet && m_imageSet->isImageResourceSet()) ? toStyleFetchedImag eSet(m_imageSet) : nullptr; 132 return (m_cachedImageSet && m_cachedImageSet->isImageResourceSet()) ? toStyl eFetchedImageSet(m_cachedImageSet) : nullptr;
124 } 133 }
125 134
126 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(Document* document, float deviceScaleFactor) 135 StyleFetchedImageSet* CSSImageSetValue::cacheImageSet(Document* document, float deviceScaleFactor)
127 { 136 {
128 return cachedImageSet(document, deviceScaleFactor, ResourceFetcher::defaultR esourceOptions()); 137 return cacheImageSet(document, deviceScaleFactor, ResourceFetcher::defaultRe sourceOptions());
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();
144 } 138 }
145 139
146 String CSSImageSetValue::customCSSText() const 140 String CSSImageSetValue::customCSSText() const
147 { 141 {
148 StringBuilder result; 142 StringBuilder result;
149 result.append("-webkit-image-set("); 143 result.append("-webkit-image-set(");
150 144
151 size_t length = this->length(); 145 size_t length = this->length();
152 size_t i = 0; 146 size_t i = 0;
153 while (i < length) { 147 while (i < length) {
(...skipping 14 matching lines...) Expand all
168 162
169 ++i; 163 ++i;
170 } 164 }
171 165
172 result.append(')'); 166 result.append(')');
173 return result.toString(); 167 return result.toString();
174 } 168 }
175 169
176 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const 170 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const
177 { 171 {
178 if (!m_imageSet || !m_imageSet->isImageResourceSet()) 172 if (!m_cachedImageSet || !m_cachedImageSet->isImageResourceSet())
179 return false; 173 return false;
180 if (Resource* cachedResource = toStyleFetchedImageSet(m_imageSet)->cachedIma ge()) 174 if (Resource* cachedResource = toStyleFetchedImageSet(m_cachedImageSet)->cac hedImage())
181 return cachedResource->loadFailedOrCanceled(); 175 return cachedResource->loadFailedOrCanceled();
182 return true; 176 return true;
183 } 177 }
184 178
185 DEFINE_TRACE_AFTER_DISPATCH(CSSImageSetValue) 179 DEFINE_TRACE_AFTER_DISPATCH(CSSImageSetValue)
186 { 180 {
187 visitor->trace(m_imageSet); 181 visitor->trace(m_cachedImageSet);
188 CSSValueList::traceAfterDispatch(visitor); 182 CSSValueList::traceAfterDispatch(visitor);
189 } 183 }
190 184
191 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::valueWithURLsMadeAbso lute() 185 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::valueWithURLsMadeAbso lute()
192 { 186 {
193 RefPtrWillBeRawPtr<CSSImageSetValue> value = CSSImageSetValue::create(); 187 RefPtrWillBeRawPtr<CSSImageSetValue> value = CSSImageSetValue::create();
194 for (auto& item : *this) 188 for (auto& item : *this)
195 item->isImageValue() ? value->append(toCSSImageValue(*item).valueWithURL MadeAbsolute()) : value->append(item); 189 item->isImageValue() ? value->append(toCSSImageValue(*item).valueWithURL MadeAbsolute()) : value->append(item);
196 return value.release(); 190 return value.release();
197 } 191 }
198 192
199 193
200 } // namespace blink 194 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/css/CSSImageSetValue.h ('k') | third_party/WebKit/Source/core/css/CSSImageValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698