1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 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 "core/css/CSSImageValue.h"
23
24#include "core/FetchInitiatorTypeNames.h"
25#include "core/css/CSSMarkup.h"
26#include "core/dom/Document.h"
27#include "core/fetch/CrossOriginAccessControl.h"
28#include "core/fetch/FetchRequest.h"
29#include "core/fetch/ImageResource.h"
30#include "core/rendering/style/StyleFetchedImage.h"
31#include "core/rendering/style/StylePendingImage.h"
32#include "platform/weborigin/KURL.h"
33
34namespace blink {
35
36CSSImageValue::CSSImageValue(const String& rawValue, const KURL& url, StyleImage* image)
37    : CSSValue(ImageClass)
38    , m_relativeURL(rawValue)
39    , m_absoluteURL(url.string())
40    , m_image(image)
41    , m_accessedImage(image)
42{
43}
44
45CSSImageValue::~CSSImageValue()
46{
47}
48
49StyleImage* CSSImageValue::cachedOrPendingImage()
50{
51    if (!m_image)
52        m_image = StylePendingImage::create(this);
53
54    return m_image.get();
55}
56
57StyleFetchedImage* CSSImageValue::cachedImage(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
58{
59    ASSERT(fetcher);
60
61    if (!m_accessedImage) {
62        m_accessedImage = true;
63
64        FetchRequest request(ResourceRequest(m_absoluteURL), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, options);
65        request.mutableResourceRequest().setHTTPReferrer(m_referrer);
66
67        if (options.corsEnabled == IsCORSEnabled)
68            request.setCrossOriginAccessControl(fetcher->document()->securityOrigin(), options.allowCredentials, options.credentialsRequested);
69
70        if (ResourcePtr<ImageResource> cachedImage = fetcher->fetchImage(request))
71            m_image = StyleFetchedImage::create(cachedImage.get());
72    }
73
74    return (m_image && m_image->isImageResource()) ? toStyleFetchedImage(m_image) : 0;
75}
76
77void CSSImageValue::restoreCachedResourceIfNeeded(Document& document)
78{
79    if (!m_accessedImage || !m_image->isImageResource() || !document.fetcher())
80        return;
81    if (document.fetcher()->cachedResource(KURL(ParsedURLString, m_absoluteURL)))
82        return;
83
84    ImageResource* resource = m_image->cachedImage();
85    if (!resource)
86        return;
87
88    FetchRequest request(ResourceRequest(m_absoluteURL), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, resource->options());
89    document.fetcher()->requestLoadStarted(resource, request, ResourceFetcher::ResourceLoadingFromCache);
90}
91
92bool CSSImageValue::hasFailedOrCanceledSubresources() const
93{
94    if (!m_image || !m_image->isImageResource())
95        return false;
96    if (Resource* cachedResource = toStyleFetchedImage(m_image)->cachedImage())
97        return cachedResource->loadFailedOrCanceled();
98    return true;
99}
100
101bool CSSImageValue::equals(const CSSImageValue& other) const
102{
103    return m_absoluteURL == other.m_absoluteURL;
104}
105
106String CSSImageValue::customCSSText() const
107{
108    return "url(" + quoteCSSURLIfNeeded(m_absoluteURL) + ")";
109}
110
111PassRefPtrWillBeRawPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
112{
113    // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
114    RefPtrWillBeRawPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_absoluteURL, CSSPrimitiveValue::CSS_URI);
115    uriValue->setCSSOMSafe();
116    return uriValue.release();
117}
118
119bool CSSImageValue::knownToBeOpaque(const RenderObject* renderer) const
120{
121    return m_image ? m_image->knownToBeOpaque(renderer) : false;
122}
123
124void CSSImageValue::traceAfterDispatch(Visitor* visitor)
125{
126    CSSValue::traceAfterDispatch(visitor);
127}
128
129void CSSImageValue::reResolveURL(const Document& document)
130{
131    KURL url = document.completeURL(m_relativeURL);
132    if (url == m_absoluteURL)
133        return;
134    m_absoluteURL = url.string();
135    m_accessedImage = false;
136    m_image.clear();
137}
138
139} // namespace blink
140