1 | /* |
2 | * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
3 | * Copyright (C) 2004-2017 Apple Inc. All rights reserved. |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Library General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public License |
16 | * along with this library; see the file COPYING.LIB. If not, write to |
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | * Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "config.h" |
22 | #include "CSSImageValue.h" |
23 | |
24 | #include "CSSMarkup.h" |
25 | #include "CSSPrimitiveValue.h" |
26 | #include "CSSValueKeywords.h" |
27 | #include "CachedImage.h" |
28 | #include "CachedResourceLoader.h" |
29 | #include "CachedResourceRequest.h" |
30 | #include "CachedResourceRequestInitiators.h" |
31 | #include "DeprecatedCSSOMPrimitiveValue.h" |
32 | #include "Document.h" |
33 | #include "Element.h" |
34 | |
35 | namespace WebCore { |
36 | |
37 | CSSImageValue::CSSImageValue(URL&& url, LoadedFromOpaqueSource loadedFromOpaqueSource) |
38 | : CSSValue(ImageClass) |
39 | , m_url(WTFMove(url)) |
40 | , m_accessedImage(false) |
41 | , m_loadedFromOpaqueSource(loadedFromOpaqueSource) |
42 | { |
43 | } |
44 | |
45 | CSSImageValue::CSSImageValue(CachedImage& image) |
46 | : CSSValue(ImageClass) |
47 | , m_url(image.url()) |
48 | , m_cachedImage(&image) |
49 | , m_accessedImage(true) |
50 | { |
51 | } |
52 | |
53 | |
54 | CSSImageValue::~CSSImageValue() = default; |
55 | |
56 | bool CSSImageValue::isPending() const |
57 | { |
58 | return !m_accessedImage; |
59 | } |
60 | |
61 | CachedImage* CSSImageValue::loadImage(CachedResourceLoader& loader, const ResourceLoaderOptions& options) |
62 | { |
63 | if (!m_accessedImage) { |
64 | m_accessedImage = true; |
65 | |
66 | ResourceLoaderOptions loadOptions = options; |
67 | loadOptions.loadedFromOpaqueSource = m_loadedFromOpaqueSource; |
68 | CachedResourceRequest request(ResourceRequest(loader.document()->completeURL(m_url.string())), loadOptions); |
69 | if (m_initiatorName.isEmpty()) |
70 | request.setInitiator(cachedResourceRequestInitiators().css); |
71 | else |
72 | request.setInitiator(m_initiatorName); |
73 | |
74 | if (options.mode == FetchOptions::Mode::Cors) { |
75 | ASSERT(loader.document()); |
76 | request.updateForAccessControl(*loader.document()); |
77 | } |
78 | m_cachedImage = loader.requestImage(WTFMove(request)).value_or(nullptr); |
79 | } |
80 | return m_cachedImage.get(); |
81 | } |
82 | |
83 | bool CSSImageValue::traverseSubresources(const WTF::Function<bool (const CachedResource&)>& handler) const |
84 | { |
85 | if (!m_cachedImage) |
86 | return false; |
87 | return handler(*m_cachedImage); |
88 | } |
89 | |
90 | bool CSSImageValue::equals(const CSSImageValue& other) const |
91 | { |
92 | return m_url == other.m_url; |
93 | } |
94 | |
95 | String CSSImageValue::customCSSText() const |
96 | { |
97 | return serializeURL(m_url); |
98 | } |
99 | |
100 | Ref<DeprecatedCSSOMValue> CSSImageValue::createDeprecatedCSSOMWrapper(CSSStyleDeclaration& styleDeclaration) const |
101 | { |
102 | // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior. |
103 | return DeprecatedCSSOMPrimitiveValue::create(CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI), styleDeclaration); |
104 | } |
105 | |
106 | bool CSSImageValue::knownToBeOpaque(const RenderElement& renderer) const |
107 | { |
108 | if (!m_cachedImage) |
109 | return false; |
110 | return m_cachedImage->currentFrameKnownToBeOpaque(&renderer); |
111 | } |
112 | |
113 | } // namespace WebCore |
114 | |