blob: e1420d1e835ef757b24490246dc6635c7ec41782 [file] [log] [blame]
darin@apple.com640fa302008-02-15 05:03:55 +00001/*
eseidel13a64222006-05-16 05:33:34 +00002 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
jfbastien@apple.combcea9872017-12-04 23:34:57 +00003 * Copyright (C) 2004-2017 Apple Inc. All rights reserved.
eseidel13a64222006-05-16 05:33:34 +00004 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
eseidel13a64222006-05-16 05:33:34 +000019 */
weinigb35e93c2007-01-15 01:08:44 +000020
eseidel13a64222006-05-16 05:33:34 +000021#include "config.h"
22#include "CSSImageValue.h"
23
hyatt@apple.com77758fd2016-12-08 00:34:02 +000024#include "CSSMarkup.h"
25#include "CSSPrimitiveValue.h"
weinigb35e93c2007-01-15 01:08:44 +000026#include "CSSValueKeywords.h"
eseidel13a64222006-05-16 05:33:34 +000027#include "CachedImage.h"
abarth@webkit.org17d66c62010-09-08 10:26:02 +000028#include "CachedResourceLoader.h"
commit-queue@webkit.org5e7ea1b2012-10-22 23:35:28 +000029#include "CachedResourceRequest.h"
commit-queue@webkit.org864dbda2012-11-16 12:21:57 +000030#include "CachedResourceRequestInitiators.h"
hyatt@apple.comf368f1d2016-12-17 20:08:34 +000031#include "DeprecatedCSSOMPrimitiveValue.h"
yurys@chromium.org3f4777f2012-08-06 14:09:59 +000032#include "Document.h"
commit-queue@webkit.org864dbda2012-11-16 12:21:57 +000033#include "Element.h"
eseidel13a64222006-05-16 05:33:34 +000034
35namespace WebCore {
36
youenn@apple.com35065da2018-11-29 23:06:43 +000037CSSImageValue::CSSImageValue(URL&& url, LoadedFromOpaqueSource loadedFromOpaqueSource)
kling@webkit.org7a07e162012-03-20 12:38:45 +000038 : CSSValue(ImageClass)
aestes@apple.com905b0d92016-10-03 20:27:33 +000039 , m_url(WTFMove(url))
andreas.kling@nokia.com5fd2a712011-11-07 21:19:45 +000040 , m_accessedImage(false)
youenn@apple.com35065da2018-11-29 23:06:43 +000041 , m_loadedFromOpaqueSource(loadedFromOpaqueSource)
andreas.kling@nokia.com5fd2a712011-11-07 21:19:45 +000042{
43}
44
antti@apple.com18efcb12016-08-30 15:06:19 +000045CSSImageValue::CSSImageValue(CachedImage& image)
kling@webkit.org7a07e162012-03-20 12:38:45 +000046 : CSSValue(ImageClass)
antti@apple.com18efcb12016-08-30 15:06:19 +000047 , m_url(image.url())
antti@apple.comc78f9d42016-09-04 13:42:42 +000048 , m_cachedImage(&image)
timothy_horton@apple.comad938aa2011-12-08 23:06:15 +000049 , m_accessedImage(true)
50{
51}
52
darin@apple.com6b9561a2013-12-12 08:58:50 +000053
dbates@webkit.orgf21f3ae2017-10-19 23:48:45 +000054CSSImageValue::~CSSImageValue() = default;
simon.fraser@apple.combf850182010-08-26 22:15:58 +000055
antti@apple.com18efcb12016-08-30 15:06:19 +000056bool CSSImageValue::isPending() const
57{
antti@apple.comc78f9d42016-09-04 13:42:42 +000058 return !m_accessedImage;
commit-queue@webkit.org142e49b2016-09-02 16:26:40 +000059}
60
antti@apple.comc78f9d42016-09-04 13:42:42 +000061CachedImage* CSSImageValue::loadImage(CachedResourceLoader& loader, const ResourceLoaderOptions& options)
commit-queue@webkit.org142e49b2016-09-02 16:26:40 +000062{
antti@apple.comc78f9d42016-09-04 13:42:42 +000063 if (!m_accessedImage) {
64 m_accessedImage = true;
commit-queue@webkit.org142e49b2016-09-02 16:26:40 +000065
youenn@apple.com35065da2018-11-29 23:06:43 +000066 ResourceLoaderOptions loadOptions = options;
67 loadOptions.loadedFromOpaqueSource = m_loadedFromOpaqueSource;
68 CachedResourceRequest request(ResourceRequest(loader.document()->completeURL(m_url.string())), loadOptions);
antti@apple.comc78f9d42016-09-04 13:42:42 +000069 if (m_initiatorName.isEmpty())
70 request.setInitiator(cachedResourceRequestInitiators().css);
71 else
72 request.setInitiator(m_initiatorName);
eseidel13a64222006-05-16 05:33:34 +000073
antti@apple.comc78f9d42016-09-04 13:42:42 +000074 if (options.mode == FetchOptions::Mode::Cors) {
commit-queue@webkit.org858242f2016-10-13 10:34:15 +000075 ASSERT(loader.document());
76 request.updateForAccessControl(*loader.document());
antti@apple.comc78f9d42016-09-04 13:42:42 +000077 }
jfbastien@apple.combcea9872017-12-04 23:34:57 +000078 m_cachedImage = loader.requestImage(WTFMove(request)).value_or(nullptr);
eseidel13a64222006-05-16 05:33:34 +000079 }
antti@apple.comc78f9d42016-09-04 13:42:42 +000080 return m_cachedImage.get();
eseidel13a64222006-05-16 05:33:34 +000081}
82
cdumez@apple.com73750cc2017-06-19 19:53:16 +000083bool CSSImageValue::traverseSubresources(const WTF::Function<bool (const CachedResource&)>& handler) const
antti@apple.com99ddb942012-08-05 21:03:42 +000084{
antti@apple.comc78f9d42016-09-04 13:42:42 +000085 if (!m_cachedImage)
antti@apple.com99ddb942012-08-05 21:03:42 +000086 return false;
antti@apple.comc78f9d42016-09-04 13:42:42 +000087 return handler(*m_cachedImage);
antti@apple.com99ddb942012-08-05 21:03:42 +000088}
89
commit-queue@webkit.orgb4b784a2013-02-11 11:00:54 +000090bool CSSImageValue::equals(const CSSImageValue& other) const
91{
92 return m_url == other.m_url;
93}
94
timothy_horton@apple.comdf4356a2013-09-04 20:13:30 +000095String CSSImageValue::customCSSText() const
kling@webkit.org7a07e162012-03-20 12:38:45 +000096{
hyatt@apple.com77758fd2016-12-08 00:34:02 +000097 return serializeURL(m_url);
kling@webkit.org7a07e162012-03-20 12:38:45 +000098}
99
commit-queue@webkit.org37d24ee2017-07-21 21:20:25 +0000100Ref<DeprecatedCSSOMValue> CSSImageValue::createDeprecatedCSSOMWrapper(CSSStyleDeclaration& styleDeclaration) const
kling@webkit.org86db7122012-05-03 18:08:08 +0000101{
102 // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
commit-queue@webkit.org37d24ee2017-07-21 21:20:25 +0000103 return DeprecatedCSSOMPrimitiveValue::create(CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI), styleDeclaration);
kling@webkit.org86db7122012-05-03 18:08:08 +0000104}
105
simon.fraser@apple.com66864782018-04-27 17:59:25 +0000106bool CSSImageValue::knownToBeOpaque(const RenderElement& renderer) const
junov@google.comf3d27f22012-11-23 18:43:51 +0000107{
antti@apple.comc78f9d42016-09-04 13:42:42 +0000108 if (!m_cachedImage)
109 return false;
simon.fraser@apple.com66864782018-04-27 17:59:25 +0000110 return m_cachedImage->currentFrameKnownToBeOpaque(&renderer);
junov@google.comf3d27f22012-11-23 18:43:51 +0000111}
112
weinigb35e93c2007-01-15 01:08:44 +0000113} // namespace WebCore