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