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 "FetchInitiatorTypeNames.h"
25#include "core/css/CSSParser.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
33namespace WebCore {
34
35CSSImageValue::CSSImageValue(const String& url)
36    : CSSValue(ImageClass)
37    , m_url(url)
38    , m_accessedImage(false)
39{
40}
41
42CSSImageValue::CSSImageValue(const String& url, StyleImage* image)
43    : CSSValue(ImageClass)
44    , m_url(url)
45    , m_image(image)
46    , m_accessedImage(true)
47{
48}
49
50CSSImageValue::~CSSImageValue()
51{
52}
53
54StyleImage* CSSImageValue::cachedOrPendingImage()
55{
56    if (!m_image)
57        m_image = StylePendingImage::create(this);
58
59    return m_image.get();
60}
61
62StyleFetchedImage* CSSImageValue::cachedImage(ResourceFetcher* fetcher, const ResourceLoaderOptions& options, CORSEnabled corsEnabled)
63{
64    ASSERT(fetcher);
65
66    if (!m_accessedImage) {
67        m_accessedImage = true;
68
69        FetchRequest request(ResourceRequest(fetcher->document()->completeURL(m_url)), m_initiatorName.isEmpty() ? FetchInitiatorTypeNames::css : m_initiatorName, options);
70
71        if (corsEnabled == PotentiallyCORSEnabled)
72            updateRequestForAccessControl(request.mutableResourceRequest(), fetcher->document()->securityOrigin(), options.allowCredentials);
73
74        if (ResourcePtr<ImageResource> cachedImage = fetcher->fetchImage(request))
75            m_image = StyleFetchedImage::create(cachedImage.get());
76    }
77
78    return (m_image && m_image->isImageResource()) ? toStyleFetchedImage(m_image) : 0;
79}
80
81bool CSSImageValue::hasFailedOrCanceledSubresources() const
82{
83    if (!m_image || !m_image->isImageResource())
84        return false;
85    if (Resource* cachedResource = toStyleFetchedImage(m_image)->cachedImage())
86        return cachedResource->loadFailedOrCanceled();
87    return true;
88}
89
90bool CSSImageValue::equals(const CSSImageValue& other) const
91{
92    return m_url == other.m_url;
93}
94
95String CSSImageValue::customCSSText() const
96{
97    return "url(" + quoteCSSURLIfNeeded(m_url) + ")";
98}
99
100PassRefPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
101{
102    // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
103    RefPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI);
104    uriValue->setCSSOMSafe();
105    return uriValue.release();
106}
107
108bool CSSImageValue::knownToBeOpaque(const RenderObject* renderer) const
109{
110    return m_image ? m_image->knownToBeOpaque(renderer) : false;
111}
112
113} // namespace WebCore
114