Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSImageSetValue.cpp
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 "config.h"
27 #include "core/css/CSSImageSetValue.h"
28
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"
39
40 namespace WebCore {
41
42 CSSImageSetValue::CSSImageSetValue()
43     : CSSValueList(ImageSetClass, CommaSeparator)
44     , m_accessedBestFitImage(false)
45     , m_scaleFactor(1)
46 {
47 }
48
49 CSSImageSetValue::~CSSImageSetValue()
50 {
51     if (m_imageSet && m_imageSet->isImageResourceSet())
52         toStyleFetchedImageSet(m_imageSet)->clearImageSetValue();
53 }
54
55 void CSSImageSetValue::fillImageSet()
56 {
57     size_t length = this->length();
58     size_t i = 0;
59     while (i < length) {
60         CSSValue* imageValue = item(i);
61         String imageURL = toCSSImageValue(imageValue)->url();
62
63         ++i;
64         ASSERT_WITH_SECURITY_IMPLICATION(i < length);
65         CSSValue* scaleFactorValue = item(i);
66         float scaleFactor = toCSSPrimitiveValue(scaleFactorValue)->getFloatValue();
67
68         ImageWithScale image;
69         image.imageURL = imageURL;
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 to highest.
76     std::sort(m_imagesInSet.begin(), m_imagesInSet.end(), CSSImageSetValue::compareByScaleFactor);
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         // 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);
107
108             if (options.corsEnabled == IsCORSEnabled)
109                 request.setCrossOriginAccessControl(loader->document()->securityOrigin(), options.allowCredentials);
110
111             if (ResourcePtr<ImageResource> cachedImage = loader->fetchImage(request)) {
112                 m_imageSet = StyleFetchedImageSet::create(cachedImage.get(), image.scaleFactor, this);
113                 m_accessedBestFitImage = true;
114             }
115         }
116     }
117
118     return (m_imageSet && m_imageSet->isImageResourceSet()) ? toStyleFetchedImageSet(m_imageSet) : 0;
119 }
120
121 StyleFetchedImageSet* CSSImageSetValue::cachedImageSet(ResourceFetcher* fetcher, float deviceScaleFactor)
122 {
123     return cachedImageSet(fetcher, deviceScaleFactor, ResourceFetcher::defaultResourceOptions());
124 }
125
126 StyleImage* CSSImageSetValue::cachedOrPendingImageSet(float deviceScaleFactor)
127 {
128     if (!m_imageSet) {
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);
135         }
136     }
137
138     return m_imageSet.get();
139 }
140
141 String CSSImageSetValue::customCSSText() const
142 {
143     StringBuilder result;
144     result.append("-webkit-image-set(");
145
146     size_t length = this->length();
147     size_t i = 0;
148     while (i < length) {
149         if (i > 0)
150             result.append(", ");
151
152         const CSSValue* imageValue = item(i);
153         result.append(imageValue->cssText());
154         result.append(' ');
155
156         ++i;
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.
162         result.append('x');
163
164         ++i;
165     }
166
167     result.append(")");
168     return result.toString();
169 }
170
171 bool CSSImageSetValue::hasFailedOrCanceledSubresources() const
172 {
173     if (!m_imageSet || !m_imageSet->isImageResourceSet())
174         return false;
175     if (Resource* cachedResource = toStyleFetchedImageSet(m_imageSet)->cachedImage())
176         return cachedResource->loadFailedOrCanceled();
177     return true;
178 }
179
180 CSSImageSetValue::CSSImageSetValue(const CSSImageSetValue& cloneFrom)
181     : CSSValueList(cloneFrom)
182     , m_accessedBestFitImage(false)
183     , m_scaleFactor(1)
184 {
185     // Non-CSSValueList data is not accessible through CSS OM, no need to clone.
186 }
187
188 PassRefPtrWillBeRawPtr<CSSImageSetValue> CSSImageSetValue::cloneForCSSOM() const
189 {
190     return adoptRefCountedWillBeRefCountedGarbageCollected(new CSSImageSetValue(*this));
191 }
192
193 } // namespace WebCore