5fc50c765164cacb1fed3421897898b9fc425958
[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 CSSValuePool& cssValuePool()
36 {
37 #if ENABLE(OILPAN)
38     DEFINE_STATIC_LOCAL(Persistent<CSSValuePool>, pool, (new CSSValuePool()));
39     return *pool;
40 #else
41     DEFINE_STATIC_LOCAL(CSSValuePool, pool, ());
42     return pool;
43 #endif // ENABLE(OILPAN)
44 }
45
46 CSSValuePool::CSSValuePool()
47     : m_inheritedValue(CSSInheritedValue::create())
48     , m_implicitInitialValue(CSSInitialValue::createImplicit())
49     , m_explicitInitialValue(CSSInitialValue::createExplicit())
50     , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
51     , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
52     , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
53 {
54     m_identifierValueCache.resize(numCSSValueKeywords);
55     m_pixelValueCache.resize(maximumCacheableIntegerValue + 1);
56     m_percentValueCache.resize(maximumCacheableIntegerValue + 1);
57     m_numberValueCache.resize(maximumCacheableIntegerValue + 1);
58 }
59
60 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSValueID ident)
61 {
62     if (ident <= 0)
63         return CSSPrimitiveValue::createIdentifier(ident);
64
65     if (!m_identifierValueCache[ident])
66         m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
67     return m_identifierValueCache[ident];
68 }
69
70 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(CSSPropertyID ident)
71 {
72     return CSSPrimitiveValue::createIdentifier(ident);
73 }
74
75 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
76 {
77     // These are the empty and deleted values of the hash table.
78     if (rgbValue == Color::transparent)
79         return m_colorTransparent;
80     if (rgbValue == Color::white)
81         return m_colorWhite;
82     // Just because it is common.
83     if (rgbValue == Color::black)
84         return m_colorBlack;
85
86     // Just wipe out the cache and start rebuilding if it gets too big.
87     const unsigned maximumColorCacheSize = 512;
88     if (m_colorValueCache.size() > maximumColorCacheSize)
89         m_colorValueCache.clear();
90
91     RefPtrWillBeRawPtr<CSSPrimitiveValue> dummyValue = nullptr;
92     ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
93     if (entry.isNewEntry)
94         entry.storedValue->value = CSSPrimitiveValue::createColor(rgbValue);
95     return entry.storedValue->value;
96 }
97
98 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
99 {
100     if (value < 0 || value > maximumCacheableIntegerValue)
101         return CSSPrimitiveValue::create(value, type);
102
103     int intValue = static_cast<int>(value);
104     if (value != intValue)
105         return CSSPrimitiveValue::create(value, type);
106
107     switch (type) {
108     case CSSPrimitiveValue::CSS_PX:
109         if (!m_pixelValueCache[intValue])
110             m_pixelValueCache[intValue] = CSSPrimitiveValue::create(value, type);
111         return m_pixelValueCache[intValue];
112     case CSSPrimitiveValue::CSS_PERCENTAGE:
113         if (!m_percentValueCache[intValue])
114             m_percentValueCache[intValue] = CSSPrimitiveValue::create(value, type);
115         return m_percentValueCache[intValue];
116     case CSSPrimitiveValue::CSS_NUMBER:
117         if (!m_numberValueCache[intValue])
118             m_numberValueCache[intValue] = CSSPrimitiveValue::create(value, type);
119         return m_numberValueCache[intValue];
120     default:
121         return CSSPrimitiveValue::create(value, type);
122     }
123 }
124
125 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createValue(const Length& value, const RenderStyle& style)
126 {
127     return CSSPrimitiveValue::create(value, style.effectiveZoom());
128 }
129
130 PassRefPtrWillBeRawPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
131 {
132     RefPtrWillBeMember<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, nullptr).storedValue->value;
133     if (!value)
134         value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
135     return value;
136 }
137
138 PassRefPtrWillBeRawPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
139 {
140     // Just wipe out the cache and start rebuilding if it gets too big.
141     const unsigned maximumFontFaceCacheSize = 128;
142     if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
143         m_fontFaceValueCache.clear();
144
145     RefPtrWillBeMember<CSSValueList>& value = m_fontFaceValueCache.add(string, nullptr).storedValue->value;
146     if (!value)
147         value = BisonCSSParser::parseFontFaceValue(string);
148     return value;
149 }
150
151 void CSSValuePool::trace(Visitor* visitor)
152 {
153     visitor->trace(m_inheritedValue);
154     visitor->trace(m_implicitInitialValue);
155     visitor->trace(m_explicitInitialValue);
156     visitor->trace(m_identifierValueCache);
157     visitor->trace(m_colorValueCache);
158     visitor->trace(m_colorTransparent);
159     visitor->trace(m_colorWhite);
160     visitor->trace(m_colorBlack);
161     visitor->trace(m_pixelValueCache);
162     visitor->trace(m_percentValueCache);
163     visitor->trace(m_numberValueCache);
164     visitor->trace(m_fontFaceValueCache);
165     visitor->trace(m_fontFamilyValueCache);
166 }
167
168 }