Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSValuePool.cpp
1 /*
2  * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "core/css/CSSValuePool.h"
28
29 #include "core/css/parser/BisonCSSParser.h"
30 #include "core/css/CSSValueList.h"
31 #include "core/rendering/style/RenderStyle.h"
32
33 namespace WebCore {
34
35 DEFINE_GC_INFO(CSSValuePool);
36
37 CSSValuePool& cssValuePool()
38 {
39 #if ENABLE(OILPAN)
40     DEFINE_STATIC_LOCAL(Persistent<CSSValuePool>, pool, (new CSSValuePool()));
41     return *pool;
42 #else
43     DEFINE_STATIC_LOCAL(CSSValuePool, pool, ());
44     return pool;
45 #endif // ENABLE(OILPAN)
46 }
47
48 CSSValuePool::CSSValuePool()
49     : m_inheritedValue(CSSInheritedValue::create())
50     , m_implicitInitialValue(CSSInitialValue::createImplicit())
51     , m_explicitInitialValue(CSSInitialValue::createExplicit())
52     , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
53     , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
54     , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
55 {
56     m_identifierValueCache.resize(numCSSValueKeywords);
57     m_pixelValueCache.resize(maximumCacheableIntegerValue + 1);
58     m_percentValueCache.resize(maximumCacheableIntegerValue + 1);
59     m_numberValueCache.resize(maximumCacheableIntegerValue + 1);
60 }
61
62 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSValueID ident)
63 {
64     if (ident <= 0)
65         return CSSPrimitiveValue::createIdentifier(ident);
66
67     if (!m_identifierValueCache[ident])
68         m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
69     return m_identifierValueCache[ident];
70 }
71
72 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSPropertyID ident)
73 {
74     return CSSPrimitiveValue::createIdentifier(ident);
75 }
76
77 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
78 {
79     // These are the empty and deleted values of the hash table.
80     if (rgbValue == Color::transparent)
81         return m_colorTransparent;
82     if (rgbValue == Color::white)
83         return m_colorWhite;
84     // Just because it is common.
85     if (rgbValue == Color::black)
86         return m_colorBlack;
87
88     // Just wipe out the cache and start rebuilding if it gets too big.
89     const unsigned maximumColorCacheSize = 512;
90     if (m_colorValueCache.size() > maximumColorCacheSize)
91         m_colorValueCache.clear();
92
93     RefPtrWillBeRawPtr<CSSPrimitiveValue> dummyValue;
94     ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
95     if (entry.isNewEntry)
96         entry.storedValue->value = CSSPrimitiveValue::createColor(rgbValue);
97     return entry.storedValue->value;
98 }
99
100 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
101 {
102     if (value < 0 || value > maximumCacheableIntegerValue)
103         return CSSPrimitiveValue::create(value, type);
104
105     int intValue = static_cast<int>(value);
106     if (value != intValue)
107         return CSSPrimitiveValue::create(value, type);
108
109     switch (type) {
110     case CSSPrimitiveValue::CSS_PX:
111         if (!m_pixelValueCache[intValue])
112             m_pixelValueCache[intValue] = CSSPrimitiveValue::create(value, type);
113         return m_pixelValueCache[intValue];
114     case CSSPrimitiveValue::CSS_PERCENTAGE:
115         if (!m_percentValueCache[intValue])
116             m_percentValueCache[intValue] = CSSPrimitiveValue::create(value, type);
117         return m_percentValueCache[intValue];
118     case CSSPrimitiveValue::CSS_NUMBER:
119         if (!m_numberValueCache[intValue])
120             m_numberValueCache[intValue] = CSSPrimitiveValue::create(value, type);
121         return m_numberValueCache[intValue];
122     default:
123         return CSSPrimitiveValue::create(value, type);
124     }
125 }
126
127 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(const Length& value, const RenderStyle& style)
128 {
129     return CSSPrimitiveValue::create(value, style.effectiveZoom());
130 }
131
132 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
133 {
134     RefPtrWillBeMember<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, 0).storedValue->value;
135     if (!value)
136         value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
137     return value;
138 }
139
140 PassRefPtrWillBeRawPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
141 {
142     // Just wipe out the cache and start rebuilding if it gets too big.
143     const unsigned maximumFontFaceCacheSize = 128;
144     if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
145         m_fontFaceValueCache.clear();
146
147     RefPtrWillBeMember<CSSValueList>& value = m_fontFaceValueCache.add(string, 0).storedValue->value;
148     if (!value)
149         value = BisonCSSParser::parseFontFaceValue(string);
150     return value;
151 }
152
153 void CSSValuePool::trace(Visitor* visitor)
154 {
155     visitor->trace(m_inheritedValue);
156     visitor->trace(m_implicitInitialValue);
157     visitor->trace(m_explicitInitialValue);
158     visitor->trace(m_identifierValueCache);
159     visitor->trace(m_colorValueCache);
160     visitor->trace(m_colorTransparent);
161     visitor->trace(m_colorWhite);
162     visitor->trace(m_colorBlack);
163     visitor->trace(m_pixelValueCache);
164     visitor->trace(m_percentValueCache);
165     visitor->trace(m_numberValueCache);
166     visitor->trace(m_fontFaceValueCache);
167     visitor->trace(m_fontFamilyValueCache);
168 }
169
170 }