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 "CSSImageValue.h"
23
24#include "CSSValueKeywords.h"
25#include "MemoryCache.h"
26#include "CachedImage.h"
27#include "CachedResourceLoader.h"
28#include "StyleCachedImage.h"
29#include "StylePendingImage.h"
30
31namespace WebCore {
32
33CSSImageValue::CSSImageValue(const String& url)
34    : CSSPrimitiveValue(url, CSS_URI)
35    , m_accessedImage(false)
36{
37}
38
39CSSImageValue::CSSImageValue()
40    : CSSPrimitiveValue(CSSValueNone)
41    , m_accessedImage(true)
42{
43}
44
45CSSImageValue::~CSSImageValue()
46{
47    if (m_image && m_image->isCachedImage())
48        static_cast<StyleCachedImage*>(m_image.get())->cachedImage()->removeClient(this);
49}
50
51StyleImage* CSSImageValue::cachedOrPendingImage()
52{
53    if (getIdent() == CSSValueNone)
54        return 0;
55
56    if (!m_image)
57        m_image = StylePendingImage::create(this);
58
59    return m_image.get();
60}
61
62StyleCachedImage* CSSImageValue::cachedImage(CachedResourceLoader* loader)
63{
64    return cachedImage(loader, getStringValue());
65}
66
67StyleCachedImage* CSSImageValue::cachedImage(CachedResourceLoader* loader, const String& url)
68{
69    ASSERT(loader);
70
71    if (!m_accessedImage) {
72        m_accessedImage = true;
73
74        if (CachedImage* cachedImage = loader->requestImage(url)) {
75            cachedImage->addClient(this);
76            m_image = StyleCachedImage::create(cachedImage);
77        }
78    }
79
80    return (m_image && m_image->isCachedImage()) ? static_cast<StyleCachedImage*>(m_image.get()) : 0;
81}
82
83String CSSImageValue::cachedImageURL()
84{
85    if (!m_image || !m_image->isCachedImage())
86        return String();
87    return static_cast<StyleCachedImage*>(m_image.get())->cachedImage()->url();
88}
89
90void CSSImageValue::clearCachedImage()
91{
92    if (m_image && m_image->isCachedImage())
93        static_cast<StyleCachedImage*>(m_image.get())->cachedImage()->removeClient(this);
94    m_image = 0;
95    m_accessedImage = false;
96}
97
98} // namespace WebCore
99