File: ElementStyleResources.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (264 lines) | stat: -rw-r--r-- 10,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc.
 * All rights reserved.
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "core/css/resolver/ElementStyleResources.h"

#include "core/CSSPropertyNames.h"
#include "core/css/CSSGradientValue.h"
#include "core/css/CSSImageValue.h"
#include "core/css/CSSURIValue.h"
#include "core/dom/Document.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/style/ComputedStyle.h"
#include "core/style/ContentData.h"
#include "core/style/CursorData.h"
#include "core/style/FillLayer.h"
#include "core/style/FilterOperation.h"
#include "core/style/StyleFetchedImage.h"
#include "core/style/StyleFetchedImageSet.h"
#include "core/style/StyleGeneratedImage.h"
#include "core/style/StyleImage.h"
#include "core/style/StyleInvalidImage.h"
#include "core/style/StylePendingImage.h"
#include "core/svg/SVGElementProxy.h"

namespace blink {

ElementStyleResources::ElementStyleResources(Document& document,
                                             float deviceScaleFactor)
    : m_document(&document), m_deviceScaleFactor(deviceScaleFactor) {}

StyleImage* ElementStyleResources::styleImage(CSSPropertyID property,
                                              const CSSValue& value) {
  if (value.isImageValue())
    return cachedOrPendingFromValue(property, toCSSImageValue(value));

  if (value.isImageGeneratorValue())
    return generatedOrPendingFromValue(property,
                                       toCSSImageGeneratorValue(value));

  if (value.isImageSetValue())
    return setOrPendingFromValue(property, toCSSImageSetValue(value));

  return nullptr;
}

StyleImage* ElementStyleResources::generatedOrPendingFromValue(
    CSSPropertyID property,
    const CSSImageGeneratorValue& value) {
  if (value.isPending()) {
    m_pendingImageProperties.add(property);
    return StylePendingImage::create(value);
  }
  return StyleGeneratedImage::create(value);
}

StyleImage* ElementStyleResources::setOrPendingFromValue(
    CSSPropertyID property,
    const CSSImageSetValue& value) {
  if (value.isCachePending(m_deviceScaleFactor)) {
    m_pendingImageProperties.add(property);
    return StylePendingImage::create(value);
  }
  return value.cachedImage(m_deviceScaleFactor);
}

StyleImage* ElementStyleResources::cachedOrPendingFromValue(
    CSSPropertyID property,
    const CSSImageValue& value) {
  if (value.isCachePending()) {
    m_pendingImageProperties.add(property);
    return StylePendingImage::create(value);
  }
  value.restoreCachedResourceIfNeeded(*m_document);
  return value.cachedImage();
}

SVGElementProxy& ElementStyleResources::cachedOrPendingFromValue(
    const CSSURIValue& value) {
  return value.ensureElementProxy(*m_document);
}

void ElementStyleResources::loadPendingSVGDocuments(
    ComputedStyle* computedStyle) {
  if (!computedStyle->hasFilter())
    return;
  FilterOperations::FilterOperationVector& filterOperations =
      computedStyle->mutableFilter().operations();
  for (auto& filterOperation : filterOperations) {
    if (filterOperation->type() != FilterOperation::REFERENCE)
      continue;
    ReferenceFilterOperation& referenceOperation =
        toReferenceFilterOperation(*filterOperation);
    referenceOperation.elementProxy().resolve(*m_document);
  }
}

StyleImage* ElementStyleResources::loadPendingImage(
    ComputedStyle* style,
    StylePendingImage* pendingImage,
    CrossOriginAttributeValue crossOrigin) {
  if (CSSImageValue* imageValue = pendingImage->cssImageValue())
    return imageValue->cacheImage(*m_document, crossOrigin);

  if (CSSPaintValue* paintValue = pendingImage->cssPaintValue()) {
    StyleGeneratedImage* image = StyleGeneratedImage::create(*paintValue);
    style->addPaintImage(image);
    return image;
  }

  if (CSSImageGeneratorValue* imageGeneratorValue =
          pendingImage->cssImageGeneratorValue()) {
    imageGeneratorValue->loadSubimages(*m_document);
    return StyleGeneratedImage::create(*imageGeneratorValue);
  }

  if (CSSImageSetValue* imageSetValue = pendingImage->cssImageSetValue())
    return imageSetValue->cacheImage(*m_document, m_deviceScaleFactor,
                                     crossOrigin);

  ASSERT_NOT_REACHED();
  return nullptr;
}

void ElementStyleResources::loadPendingImages(ComputedStyle* style) {
  // We must loop over the properties and then look at the style to see if
  // a pending image exists, and only load that image. For example:
  //
  // <style>
  //    div { background-image: url(a.png); }
  //    div { background-image: url(b.png); }
  //    div { background-image: none; }
  // </style>
  // <div></div>
  //
  // We call styleImage() for both a.png and b.png adding the
  // CSSPropertyBackgroundImage property to the m_pendingImageProperties set,
  // then we null out the background image because of the "none".
  //
  // If we eagerly loaded the images we'd fetch a.png, even though it's not
  // used. If we didn't null check below we'd crash since the none actually
  // removed all background images.

  for (CSSPropertyID property : m_pendingImageProperties) {
    switch (property) {
      case CSSPropertyBackgroundImage: {
        for (FillLayer* backgroundLayer = &style->accessBackgroundLayers();
             backgroundLayer; backgroundLayer = backgroundLayer->next()) {
          if (backgroundLayer->image() &&
              backgroundLayer->image()->isPendingImage())
            backgroundLayer->setImage(loadPendingImage(
                style, toStylePendingImage(backgroundLayer->image())));
        }
        break;
      }
      case CSSPropertyContent: {
        for (ContentData* contentData =
                 const_cast<ContentData*>(style->contentData());
             contentData; contentData = contentData->next()) {
          if (contentData->isImage()) {
            StyleImage* image = toImageContentData(contentData)->image();
            if (image->isPendingImage())
              toImageContentData(contentData)
                  ->setImage(
                      loadPendingImage(style, toStylePendingImage(image)));
          }
        }
        break;
      }
      case CSSPropertyCursor: {
        if (CursorList* cursorList = style->cursors()) {
          for (size_t i = 0; i < cursorList->size(); ++i) {
            CursorData& currentCursor = cursorList->at(i);
            if (StyleImage* image = currentCursor.image()) {
              if (image->isPendingImage())
                currentCursor.setImage(
                    loadPendingImage(style, toStylePendingImage(image)));
            }
          }
        }
        break;
      }
      case CSSPropertyListStyleImage: {
        if (style->listStyleImage() &&
            style->listStyleImage()->isPendingImage())
          style->setListStyleImage(loadPendingImage(
              style, toStylePendingImage(style->listStyleImage())));
        break;
      }
      case CSSPropertyBorderImageSource: {
        if (style->borderImageSource() &&
            style->borderImageSource()->isPendingImage())
          style->setBorderImageSource(loadPendingImage(
              style, toStylePendingImage(style->borderImageSource())));
        break;
      }
      case CSSPropertyWebkitBoxReflect: {
        if (StyleReflection* reflection = style->boxReflect()) {
          const NinePieceImage& maskImage = reflection->mask();
          if (maskImage.image() && maskImage.image()->isPendingImage()) {
            StyleImage* loadedImage =
                loadPendingImage(style, toStylePendingImage(maskImage.image()));
            reflection->setMask(NinePieceImage(
                loadedImage, maskImage.imageSlices(), maskImage.fill(),
                maskImage.borderSlices(), maskImage.outset(),
                maskImage.horizontalRule(), maskImage.verticalRule()));
          }
        }
        break;
      }
      case CSSPropertyWebkitMaskBoxImageSource: {
        if (style->maskBoxImageSource() &&
            style->maskBoxImageSource()->isPendingImage())
          style->setMaskBoxImageSource(loadPendingImage(
              style, toStylePendingImage(style->maskBoxImageSource())));
        break;
      }
      case CSSPropertyWebkitMaskImage: {
        for (FillLayer* maskLayer = &style->accessMaskLayers(); maskLayer;
             maskLayer = maskLayer->next()) {
          if (maskLayer->image() && maskLayer->image()->isPendingImage())
            maskLayer->setImage(loadPendingImage(
                style, toStylePendingImage(maskLayer->image())));
        }
        break;
      }
      case CSSPropertyShapeOutside:
        if (style->shapeOutside() && style->shapeOutside()->image() &&
            style->shapeOutside()->image()->isPendingImage())
          style->shapeOutside()->setImage(loadPendingImage(
              style, toStylePendingImage(style->shapeOutside()->image()),
              CrossOriginAttributeAnonymous));
        break;
      default:
        ASSERT_NOT_REACHED();
    }
  }
}

void ElementStyleResources::loadPendingResources(ComputedStyle* computedStyle) {
  loadPendingImages(computedStyle);
  loadPendingSVGDocuments(computedStyle);
}

}  // namespace blink