2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2013 Google Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "core/css/resolver/StyleResourceLoader.h"
26 #include "core/CSSPropertyNames.h"
27 #include "core/css/CSSCursorImageValue.h"
28 #include "core/css/CSSImageValue.h"
29 #include "core/css/CSSSVGDocumentValue.h"
30 #include "core/css/resolver/ElementStyleResources.h"
31 #include "core/fetch/ResourceFetcher.h"
32 #include "core/rendering/style/ContentData.h"
33 #include "core/rendering/style/FillLayer.h"
34 #include "core/rendering/style/RenderStyle.h"
35 #include "core/rendering/style/StyleFetchedImage.h"
36 #include "core/rendering/style/StyleFetchedImageSet.h"
37 #include "core/rendering/style/StyleGeneratedImage.h"
38 #include "core/rendering/style/StylePendingImage.h"
39 #include "core/rendering/svg/ReferenceFilterBuilder.h"
43 StyleResourceLoader::StyleResourceLoader(ResourceFetcher* fetcher)
48 void StyleResourceLoader::loadPendingSVGDocuments(RenderStyle* renderStyle, ElementStyleResources& elementStyleResources)
50 if (!renderStyle->hasFilter() || elementStyleResources.pendingSVGDocuments().isEmpty())
53 Vector<RefPtr<FilterOperation> >& filterOperations = renderStyle->mutableFilter().operations();
54 for (unsigned i = 0; i < filterOperations.size(); ++i) {
55 RefPtr<FilterOperation> filterOperation = filterOperations.at(i);
56 if (filterOperation->type() == FilterOperation::REFERENCE) {
57 ReferenceFilterOperation* referenceFilter = toReferenceFilterOperation(filterOperation.get());
59 CSSSVGDocumentValue* value = elementStyleResources.pendingSVGDocuments().get(referenceFilter);
62 DocumentResource* resource = value->load(m_fetcher);
66 // Stash the DocumentResource on the reference filter.
67 ReferenceFilterBuilder::setDocumentResourceReference(referenceFilter, adoptPtr(new DocumentResourceReference(resource)));
71 elementStyleResources.clearPendingSVGDocuments();
74 static PassRefPtr<StyleImage> doLoadPendingImage(ResourceFetcher* fetcher, StylePendingImage* pendingImage, float deviceScaleFactor, const ResourceLoaderOptions& options)
76 if (CSSImageValue* imageValue = pendingImage->cssImageValue())
77 return imageValue->cachedImage(fetcher, options);
79 if (CSSImageGeneratorValue* imageGeneratorValue
80 = pendingImage->cssImageGeneratorValue()) {
81 imageGeneratorValue->loadSubimages(fetcher);
82 return StyleGeneratedImage::create(imageGeneratorValue);
85 if (CSSCursorImageValue* cursorImageValue
86 = pendingImage->cssCursorImageValue())
87 return cursorImageValue->cachedImage(fetcher, deviceScaleFactor);
89 if (CSSImageSetValue* imageSetValue = pendingImage->cssImageSetValue())
90 return imageSetValue->cachedImageSet(fetcher, deviceScaleFactor, options);
95 PassRefPtr<StyleImage> StyleResourceLoader::loadPendingImage(StylePendingImage* pendingImage, float deviceScaleFactor)
97 return doLoadPendingImage(m_fetcher, pendingImage, deviceScaleFactor, ResourceFetcher::defaultResourceOptions());
100 void StyleResourceLoader::loadPendingShapeImage(RenderStyle* renderStyle, ShapeValue* shapeValue, float deviceScaleFactor)
105 StyleImage* image = shapeValue->image();
106 if (!image || !image->isPendingImage())
109 ResourceLoaderOptions options = ResourceFetcher::defaultResourceOptions();
110 options.allowCredentials = DoNotAllowStoredCredentials;
111 options.credentialsRequested = ClientDidNotRequestCredentials;
112 options.corsEnabled = IsCORSEnabled;
114 shapeValue->setImage(doLoadPendingImage(m_fetcher, toStylePendingImage(image), deviceScaleFactor, options));
117 void StyleResourceLoader::loadPendingImages(RenderStyle* style, ElementStyleResources& elementStyleResources)
119 if (elementStyleResources.pendingImageProperties().isEmpty())
122 PendingImagePropertyMap::const_iterator::Keys end = elementStyleResources.pendingImageProperties().end().keys();
123 for (PendingImagePropertyMap::const_iterator::Keys it = elementStyleResources.pendingImageProperties().begin().keys(); it != end; ++it) {
124 CSSPropertyID currentProperty = *it;
126 switch (currentProperty) {
127 case CSSPropertyBackgroundImage: {
128 for (FillLayer* backgroundLayer = &style->accessBackgroundLayers(); backgroundLayer; backgroundLayer = backgroundLayer->next()) {
129 if (backgroundLayer->image() && backgroundLayer->image()->isPendingImage())
130 backgroundLayer->setImage(loadPendingImage(toStylePendingImage(backgroundLayer->image()), elementStyleResources.deviceScaleFactor()));
134 case CSSPropertyContent: {
135 for (ContentData* contentData = const_cast<ContentData*>(style->contentData()); contentData; contentData = contentData->next()) {
136 if (contentData->isImage()) {
137 StyleImage* image = toImageContentData(contentData)->image();
138 if (image->isPendingImage()) {
139 RefPtr<StyleImage> loadedImage = loadPendingImage(toStylePendingImage(image), elementStyleResources.deviceScaleFactor());
141 toImageContentData(contentData)->setImage(loadedImage.release());
147 case CSSPropertyCursor: {
148 if (CursorList* cursorList = style->cursors()) {
149 for (size_t i = 0; i < cursorList->size(); ++i) {
150 CursorData& currentCursor = cursorList->at(i);
151 if (StyleImage* image = currentCursor.image()) {
152 if (image->isPendingImage())
153 currentCursor.setImage(loadPendingImage(toStylePendingImage(image), elementStyleResources.deviceScaleFactor()));
159 case CSSPropertyListStyleImage: {
160 if (style->listStyleImage() && style->listStyleImage()->isPendingImage())
161 style->setListStyleImage(loadPendingImage(toStylePendingImage(style->listStyleImage()), elementStyleResources.deviceScaleFactor()));
164 case CSSPropertyBorderImageSource: {
165 if (style->borderImageSource() && style->borderImageSource()->isPendingImage())
166 style->setBorderImageSource(loadPendingImage(toStylePendingImage(style->borderImageSource()), elementStyleResources.deviceScaleFactor()));
169 case CSSPropertyWebkitBoxReflect: {
170 if (StyleReflection* reflection = style->boxReflect()) {
171 const NinePieceImage& maskImage = reflection->mask();
172 if (maskImage.image() && maskImage.image()->isPendingImage()) {
173 RefPtr<StyleImage> loadedImage = loadPendingImage(toStylePendingImage(maskImage.image()), elementStyleResources.deviceScaleFactor());
174 reflection->setMask(NinePieceImage(loadedImage.release(), maskImage.imageSlices(), maskImage.fill(), maskImage.borderSlices(), maskImage.outset(), maskImage.horizontalRule(), maskImage.verticalRule()));
179 case CSSPropertyWebkitMaskBoxImageSource: {
180 if (style->maskBoxImageSource() && style->maskBoxImageSource()->isPendingImage())
181 style->setMaskBoxImageSource(loadPendingImage(toStylePendingImage(style->maskBoxImageSource()), elementStyleResources.deviceScaleFactor()));
184 case CSSPropertyWebkitMaskImage: {
185 for (FillLayer* maskLayer = &style->accessMaskLayers(); maskLayer; maskLayer = maskLayer->next()) {
186 if (maskLayer->image() && maskLayer->image()->isPendingImage())
187 maskLayer->setImage(loadPendingImage(toStylePendingImage(maskLayer->image()), elementStyleResources.deviceScaleFactor()));
191 case CSSPropertyShapeOutside:
192 loadPendingShapeImage(style, style->shapeOutside(), elementStyleResources.deviceScaleFactor());
195 ASSERT_NOT_REACHED();
199 elementStyleResources.clearPendingImageProperties();
202 void StyleResourceLoader::loadPendingResources(RenderStyle* renderStyle, ElementStyleResources& elementStyleResources)
204 // Start loading images referenced by this style.
205 loadPendingImages(renderStyle, elementStyleResources);
207 // Start loading the SVG Documents referenced by this style.
208 loadPendingSVGDocuments(renderStyle, elementStyleResources);