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#pragma once
27
28#include "CSSInheritedValue.h"
29#include "CSSInitialValue.h"
30#include "CSSPrimitiveValue.h"
31#include "CSSPropertyNames.h"
32#include "CSSRevertValue.h"
33#include "CSSUnsetValue.h"
34#include "CSSValueKeywords.h"
35#include "ColorHash.h"
36#include <utility>
37#include <wtf/HashMap.h>
38#include <wtf/NeverDestroyed.h>
39#include <wtf/RefPtr.h>
40#include <wtf/text/AtomStringHash.h>
41
42namespace WebCore {
43
44class CSSValueList;
45
46enum class FromSystemFontID { No, Yes };
47
48class CSSValuePool {
49 WTF_MAKE_FAST_ALLOCATED;
50public:
51 static CSSValuePool& singleton();
52
53 RefPtr<CSSValueList> createFontFaceValue(const AtomString&);
54 Ref<CSSPrimitiveValue> createFontFamilyValue(const String&, FromSystemFontID = FromSystemFontID::No);
55 Ref<CSSInheritedValue> createInheritedValue() { return m_inheritedValue.get(); }
56 Ref<CSSInitialValue> createImplicitInitialValue() { return m_implicitInitialValue.get(); }
57 Ref<CSSInitialValue> createExplicitInitialValue() { return m_explicitInitialValue.get(); }
58 Ref<CSSUnsetValue> createUnsetValue() { return m_unsetValue.get(); }
59 Ref<CSSRevertValue> createRevertValue() { return m_revertValue.get(); }
60 Ref<CSSPrimitiveValue> createIdentifierValue(CSSValueID identifier);
61 Ref<CSSPrimitiveValue> createIdentifierValue(CSSPropertyID identifier);
62 Ref<CSSPrimitiveValue> createColorValue(const Color&);
63 Ref<CSSPrimitiveValue> createValue(double value, CSSPrimitiveValue::UnitType);
64 Ref<CSSPrimitiveValue> createValue(const String& value, CSSPrimitiveValue::UnitType type) { return CSSPrimitiveValue::create(value, type); }
65 Ref<CSSPrimitiveValue> createValue(const Length& value, const RenderStyle& style) { return CSSPrimitiveValue::create(value, style); }
66 Ref<CSSPrimitiveValue> createValue(const LengthSize& value, const RenderStyle& style) { return CSSPrimitiveValue::create(value, style); }
67 template<typename T> static Ref<CSSPrimitiveValue> createValue(T&& value) { return CSSPrimitiveValue::create(std::forward<T>(value)); }
68
69 void drain();
70
71private:
72 CSSValuePool();
73
74 typedef HashMap<Color, RefPtr<CSSPrimitiveValue>> ColorValueCache;
75 ColorValueCache m_colorValueCache;
76
77 typedef HashMap<AtomString, RefPtr<CSSValueList>> FontFaceValueCache;
78 FontFaceValueCache m_fontFaceValueCache;
79
80 typedef HashMap<std::pair<String, bool>, RefPtr<CSSPrimitiveValue>> FontFamilyValueCache;
81 FontFamilyValueCache m_fontFamilyValueCache;
82
83 friend class WTF::NeverDestroyed<CSSValuePool>;
84
85 LazyNeverDestroyed<CSSInheritedValue> m_inheritedValue;
86 LazyNeverDestroyed<CSSInitialValue> m_implicitInitialValue;
87 LazyNeverDestroyed<CSSInitialValue> m_explicitInitialValue;
88 LazyNeverDestroyed<CSSUnsetValue> m_unsetValue;
89 LazyNeverDestroyed<CSSRevertValue> m_revertValue;
90
91 LazyNeverDestroyed<CSSPrimitiveValue> m_transparentColor;
92 LazyNeverDestroyed<CSSPrimitiveValue> m_whiteColor;
93 LazyNeverDestroyed<CSSPrimitiveValue> m_blackColor;
94
95 static const int maximumCacheableIntegerValue = 255;
96
97 LazyNeverDestroyed<CSSPrimitiveValue> m_pixelValues[maximumCacheableIntegerValue + 1];
98 LazyNeverDestroyed<CSSPrimitiveValue> m_percentValues[maximumCacheableIntegerValue + 1];
99 LazyNeverDestroyed<CSSPrimitiveValue> m_numberValues[maximumCacheableIntegerValue + 1];
100 LazyNeverDestroyed<CSSPrimitiveValue> m_identifierValues[numCSSValueKeywords];
101};
102
103} // namespace WebCore
104