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

Side by Side Diff: sky/engine/core/css/CSSImageSetValue.cpp

Issue 1214633005: Remove CSS clients of ImageResource (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 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
« no previous file with comments | « sky/engine/core/css/CSSImageSetValue.h ('k') | sky/engine/core/css/CSSImageValue.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "sky/engine/core/css/CSSImageSetValue.h"
27
28 #include "gen/sky/core/FetchInitiatorTypeNames.h"
29 #include "sky/engine/core/css/CSSImageValue.h"
30 #include "sky/engine/core/css/CSSPrimitiveValue.h"
31 #include "sky/engine/core/dom/Document.h"
32 #include "sky/engine/core/fetch/FetchRequest.h"
33 #include "sky/engine/core/fetch/ImageResource.h"
34 #include "sky/engine/core/fetch/ResourceFetcher.h"
35 #include "sky/engine/core/rendering/style/StyleFetchedImageSet.h"
36 #include "sky/engine/core/rendering/style/StylePendingImage.h"
37 #include "sky/engine/wtf/text/StringBuilder.h"
38
39 namespace blink {
40
41 CSSImageSetValue::CSSImageSetValue()
42 : CSSValueList(ImageSetClass, CommaSeparator)
43 , m_accessedBestFitImage(false)
44 , m_scaleFactor(1)
45 {
46 }
47
48 CSSImageSetValue::~CSSImageSetValue()
49 {
50 if (m_imageSet && m_imageSet->isImageResourceSet())
51 toStyleFetchedImageSet(m_imageSet)->clearImageSetValue();
52 }
53
54 void CSSImageSetValue::fillImageSet()
55 {
56 size_t length = this->length();
57 size_t i = 0;
58 while (i < length) {
59 CSSImageValue* imageValue = toCSSImageValue(item(i));
60 String imageURL = imageValue->url();
61
62 ++i;
63 ASSERT_WITH_SECURITY_IMPLICATION(i < length);
64 CSSValue* scaleFactorValue = item(i);
65 float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue ();
66
67 ImageWithScale image;
68 image.imageURL = imageURL;
69 image.referrer = imageValue->referrer();
70 image.scaleFactor = scaleFactor;
71 m_imagesInSet.append(image);
72 ++i;
73 }
74
75 // Sort the images so that they are stored in order from lowest resolution t o highest.
76 std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::comp areByScaleFactor);
77 }
78
79 CSSImageSetValue::ImageWithScale CSSImageSetValue::bestImageForScaleFactor()
80 {
81 ImageWithScale image;
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)
86 return image;
87 }
88 return image;
89 }
90
91 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(ResourceFetcher* loader, float deviceScaleFactor, const ResourceLoaderOptions& options)
92 {
93 ASSERT(loader);
94
95 m_scaleFactor = deviceScaleFactor;
96
97 if (!m_imagesInSet.size())
98 fillImageSet();
99
100 if (!m_accessedBestFitImage) {
101 ImageWithScale image = bestImageForScaleFactor();
102 if (Document* document = loader->document()) {
103 FetchRequest request(ResourceRequest(document->completeURL(image.ima geURL)), FetchInitiatorTypeNames::css, options);
104 request.mutableResourceRequest().setHTTPReferrer(image.referrer);
105
106 if (ResourcePtr<ImageResource> cachedImage = loader->fetchImage(requ est)) {
107 m_imageSet = StyleFetchedImageSet::create(cachedImage.get(), ima ge.scaleFactor, this);
108 m_accessedBestFitImage = true;
109 }
110 }
111 }
112
113 return (m_imageSet && m_imageSet->isImageResourceSet()) ? toStyleFetchedImag eSet(m_imageSet) : 0;
114 }
115
116 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(ResourceFetcher* fetcher, float deviceScaleFactor)
117 {
118 return cachedImageSet(fetcher, deviceScaleFactor, ResourceFetcher::defaultRe sourceOptions());
119 }
120
121 StyleImage* CSSImageSetValue::cachedOrPendingImageSet(float deviceScaleFactor)
122 {
123 if (!m_imageSet) {
124 m_imageSet = StylePendingImage::create(this);
125 } else if (!m_imageSet->isPendingImage()) {
126 // If the deviceScaleFactor has changed, we may not have the best image loaded, so we have to re-assess.
127 if (deviceScaleFactor != m_scaleFactor) {
128 m_accessedBestFitImage = false;
129 m_imageSet = StylePendingImage::create(this);
130 }
131 }
132
133 return m_imageSet.get();
134 }
135
136 String CSSImageSetValue::customCSSText() const
137 {
138 StringBuilder result;
139 result.append("-webkit-image-set(");
140
141 size_t length = this->length();
142 size_t i = 0;
143 while (i < length) {
144 if (i > 0)
145 result.appendLiteral(", ");
146
147 const CSSValue* imageValue = item(i);
148 result.append(imageValue->cssText());
149 result.append(' ');
150
151 ++i;
152 ASSERT_WITH_SECURITY_IMPLICATION(i < length);
153 const CSSValue* scaleFactorValue = item(i);
154 result.append(scaleFactorValue->cssText());
155 // FIXME: Eventually the scale factor should contain it's own unit http: //wkb.ug/100120.
156 // For now 'x' is hard-coded in the parser, so we hard-code it here too.
157 result.append('x');
158
159 ++i;
160 }
161
162 result.append(')');
163 return result.toString();
164 }
165
166 CSSImageSetValue::CSSImageSetValue(const CSSImageSetValue& cloneFrom)
167 : CSSValueList(cloneFrom)
168 , m_accessedBestFitImage(false)
169 , m_scaleFactor(1)
170 {
171 // Non-CSSValueList data is not accessible through CSS OM, no need to clone.
172 }
173
174 PassRefPtr<CSSImageSetValue> CSSImageSetValue::cloneForCSSOM() const
175 {
176 return adoptRef(new CSSImageSetValue(*this));
177 }
178
179 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/core/css/CSSImageSetValue.h ('k') | sky/engine/core/css/CSSImageValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698