[Cherry-pick] Refactor WrapShape to Shape/BasicShape
[framework/web/webkit-efl.git] / 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 "CSSCursorImageValue.h"
25 #include "CSSParser.h"
26 #include "CSSValueKeywords.h"
27 #include "Document.h"
28 #include "MemoryCache.h"
29 #include "CachedImage.h"
30 #include "CachedResourceLoader.h"
31 #include "StyleCachedImage.h"
32 #include "StylePendingImage.h"
33
34 namespace WebCore {
35
36 CSSImageValue::CSSImageValue(ClassType classType, const String& url)
37     : CSSValue(classType)
38     , m_url(url)
39     , m_accessedImage(false)
40 {
41 }
42
43 CSSImageValue::CSSImageValue(const String& url)
44     : CSSValue(ImageClass)
45     , m_url(url)
46     , m_accessedImage(false)
47 {
48 }
49
50 CSSImageValue::CSSImageValue(const String& url, StyleImage* image)
51     : CSSValue(ImageClass)
52     , m_url(url)
53     , m_image(image)
54     , m_accessedImage(true)
55 {
56 }
57
58 CSSImageValue::~CSSImageValue()
59 {
60 }
61
62 StyleImage* CSSImageValue::cachedOrPendingImage()
63 {
64     if (!m_image)
65         m_image = StylePendingImage::create(this);
66
67     return m_image.get();
68 }
69
70 StyleCachedImage* CSSImageValue::cachedImage(CachedResourceLoader* loader)
71 {
72     if (isCursorImageValue())
73         return static_cast<CSSCursorImageValue*>(this)->cachedImage(loader);
74     return cachedImage(loader, m_url);
75 }
76
77 StyleCachedImage* CSSImageValue::cachedImage(CachedResourceLoader* loader, const String& url)
78 {
79     ASSERT(loader);
80
81     if (!m_accessedImage) {
82         m_accessedImage = true;
83
84         ResourceRequest request(loader->document()->completeURL(url));
85         if (CachedResourceHandle<CachedImage> cachedImage = loader->requestImage(request))
86             m_image = StyleCachedImage::create(cachedImage.get());
87     }
88
89     return (m_image && m_image->isCachedImage()) ? static_cast<StyleCachedImage*>(m_image.get()) : 0;
90 }
91
92 String CSSImageValue::cachedImageURL()
93 {
94     if (!m_image || !m_image->isCachedImage())
95         return String();
96     return static_cast<StyleCachedImage*>(m_image.get())->cachedImage()->url();
97 }
98
99 void CSSImageValue::clearCachedImage()
100 {
101     m_image = 0;
102     m_accessedImage = false;
103 }
104
105 String CSSImageValue::customCssText() const
106 {
107     return "url(" + quoteCSSURLIfNeeded(m_url) + ")";
108 }
109
110 PassRefPtr<CSSValue> CSSImageValue::cloneForCSSOM() const
111 {
112     // NOTE: We expose CSSImageValues as URI primitive values in CSSOM to maintain old behavior.
113     RefPtr<CSSPrimitiveValue> uriValue = CSSPrimitiveValue::create(m_url, CSSPrimitiveValue::CSS_URI);
114     uriValue->setCSSOMSafe();
115     return uriValue.release();
116 }
117
118 } // namespace WebCore