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 "CSSValuePool.h"
28
29#include "CSSParser.h"
30#include "CSSStyleSheet.h"
31#include "CSSValueKeywords.h"
32#include "CSSValueList.h"
33
34namespace WebCore {
35
36CSSValuePool& cssValuePool()
37{
38    DEFINE_STATIC_LOCAL(CSSValuePool, pool, ());
39    return pool;
40}
41
42CSSValuePool::CSSValuePool()
43    : m_inheritedValue(CSSInheritedValue::create())
44    , m_implicitInitialValue(CSSInitialValue::createImplicit())
45    , m_explicitInitialValue(CSSInitialValue::createExplicit())
46    , m_colorTransparent(CSSPrimitiveValue::createColor(Color::transparent))
47    , m_colorWhite(CSSPrimitiveValue::createColor(Color::white))
48    , m_colorBlack(CSSPrimitiveValue::createColor(Color::black))
49{
50}
51
52PassRefPtr<CSSPrimitiveValue> CSSValuePool::createIdentifierValue(int ident)
53{
54    if (ident <= 0 || ident >= numCSSValueKeywords)
55        return CSSPrimitiveValue::createIdentifier(ident);
56
57    if (!m_identifierValueCache[ident])
58        m_identifierValueCache[ident] = CSSPrimitiveValue::createIdentifier(ident);
59    return m_identifierValueCache[ident];
60}
61
62PassRefPtr<CSSPrimitiveValue> CSSValuePool::createColorValue(unsigned rgbValue)
63{
64    // These are the empty and deleted values of the hash table.
65    if (rgbValue == Color::transparent)
66        return m_colorTransparent;
67    if (rgbValue == Color::white)
68        return m_colorWhite;
69    // Just because it is common.
70    if (rgbValue == Color::black)
71        return m_colorBlack;
72
73    // Just wipe out the cache and start rebuilding if it gets too big.
74    const int maximumColorCacheSize = 512;
75    if (m_colorValueCache.size() > maximumColorCacheSize)
76        m_colorValueCache.clear();
77
78    RefPtr<CSSPrimitiveValue> dummyValue;
79    ColorValueCache::AddResult entry = m_colorValueCache.add(rgbValue, dummyValue);
80    if (entry.isNewEntry)
81        entry.iterator->value = CSSPrimitiveValue::createColor(rgbValue);
82    return entry.iterator->value;
83}
84
85PassRefPtr<CSSPrimitiveValue> CSSValuePool::createValue(double value, CSSPrimitiveValue::UnitTypes type)
86{
87    if (value < 0 || value > maximumCacheableIntegerValue)
88        return CSSPrimitiveValue::create(value, type);
89
90    int intValue = static_cast<int>(value);
91    if (value != intValue)
92        return CSSPrimitiveValue::create(value, type);
93
94    RefPtr<CSSPrimitiveValue>* cache;
95    switch (type) {
96    case CSSPrimitiveValue::CSS_PX:
97        cache = m_pixelValueCache;
98        break;
99    case CSSPrimitiveValue::CSS_PERCENTAGE:
100        cache = m_percentValueCache;
101        break;
102    case CSSPrimitiveValue::CSS_NUMBER:
103        cache = m_numberValueCache;
104        break;
105    default:
106        return CSSPrimitiveValue::create(value, type);
107    }
108
109    if (!cache[intValue])
110        cache[intValue] = CSSPrimitiveValue::create(value, type);
111    return cache[intValue];
112}
113
114PassRefPtr<CSSPrimitiveValue> CSSValuePool::createFontFamilyValue(const String& familyName)
115{
116    RefPtr<CSSPrimitiveValue>& value = m_fontFamilyValueCache.add(familyName, 0).iterator->value;
117    if (!value)
118        value = CSSPrimitiveValue::create(familyName, CSSPrimitiveValue::CSS_STRING);
119    return value;
120}
121
122PassRefPtr<CSSValueList> CSSValuePool::createFontFaceValue(const AtomicString& string)
123{
124    // Just wipe out the cache and start rebuilding if it gets too big.
125    const int maximumFontFaceCacheSize = 128;
126    if (m_fontFaceValueCache.size() > maximumFontFaceCacheSize)
127        m_fontFaceValueCache.clear();
128
129    RefPtr<CSSValueList>& value = m_fontFaceValueCache.add(string, 0).iterator->value;
130    if (!value)
131        value = CSSParser::parseFontFaceValue(string);
132    return value;
133}
134
135void CSSValuePool::drain()
136{
137    m_colorValueCache.clear();
138    m_fontFaceValueCache.clear();
139    m_fontFamilyValueCache.clear();
140
141    for (int i = 0; i < numCSSValueKeywords; ++i)
142        m_identifierValueCache[i] = 0;
143
144    for (int i = 0; i < maximumCacheableIntegerValue; ++i) {
145        m_pixelValueCache[i] = 0;
146        m_percentValueCache[i] = 0;
147        m_numberValueCache[i] = 0;
148    }
149}
150
151}
152