1 | /* |
2 | * Copyright (C) 2016 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. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "CSSStyleDeclaration.h" |
29 | #include "CSSValue.h" |
30 | #include "ExceptionOr.h" |
31 | #include <wtf/Ref.h> |
32 | #include <wtf/RefCounted.h> |
33 | #include <wtf/TypeCasts.h> |
34 | #include <wtf/WeakPtr.h> |
35 | #include <wtf/text/WTFString.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | class DeprecatedCSSOMValue : public RefCounted<DeprecatedCSSOMValue>, public CanMakeWeakPtr<DeprecatedCSSOMValue> { |
40 | public: |
41 | // Exactly match the IDL. No reason to add anything if it's not in the IDL. |
42 | enum Type { |
43 | CSS_INHERIT = 0, |
44 | CSS_PRIMITIVE_VALUE = 1, |
45 | CSS_VALUE_LIST = 2, |
46 | CSS_CUSTOM = 3 |
47 | }; |
48 | |
49 | // Override RefCounted's deref() to ensure operator delete is called on |
50 | // the appropriate subclass type. |
51 | void deref() |
52 | { |
53 | if (derefBase()) |
54 | destroy(); |
55 | } |
56 | |
57 | WEBCORE_EXPORT unsigned cssValueType() const; |
58 | |
59 | WEBCORE_EXPORT String cssText() const; |
60 | ExceptionOr<void> setCssText(const String&) { return { }; } // Will never implement. |
61 | |
62 | bool isComplexValue() const { return m_classType == DeprecatedComplexValueClass; } |
63 | bool isPrimitiveValue() const { return m_classType == DeprecatedPrimitiveValueClass; } |
64 | bool isValueList() const { return m_classType == DeprecatedValueListClass; } |
65 | |
66 | CSSStyleDeclaration& owner() const { return m_owner; } |
67 | |
68 | protected: |
69 | static const size_t ClassTypeBits = 2; |
70 | enum DeprecatedClassType { |
71 | DeprecatedComplexValueClass, |
72 | DeprecatedPrimitiveValueClass, |
73 | DeprecatedValueListClass |
74 | }; |
75 | |
76 | DeprecatedClassType classType() const { return static_cast<DeprecatedClassType>(m_classType); } |
77 | |
78 | DeprecatedCSSOMValue(DeprecatedClassType classType, CSSStyleDeclaration& owner) |
79 | : m_classType(classType) |
80 | , m_owner(owner) |
81 | { |
82 | } |
83 | |
84 | // NOTE: This class is non-virtual for memory and performance reasons. |
85 | // Don't go making it virtual again unless you know exactly what you're doing! |
86 | ~DeprecatedCSSOMValue() = default; |
87 | |
88 | private: |
89 | WEBCORE_EXPORT void destroy(); |
90 | |
91 | protected: |
92 | unsigned m_valueListSeparator : CSSValue::ValueListSeparatorBits; |
93 | unsigned m_classType : ClassTypeBits; // ClassType |
94 | |
95 | Ref<CSSStyleDeclaration> m_owner; |
96 | }; |
97 | |
98 | class DeprecatedCSSOMComplexValue : public DeprecatedCSSOMValue { |
99 | public: |
100 | static Ref<DeprecatedCSSOMComplexValue> create(const CSSValue& value, CSSStyleDeclaration& owner) |
101 | { |
102 | return adoptRef(*new DeprecatedCSSOMComplexValue(value, owner)); |
103 | } |
104 | |
105 | String cssText() const { return m_value->cssText(); } |
106 | |
107 | unsigned cssValueType() const { return m_value->cssValueType(); } |
108 | |
109 | protected: |
110 | DeprecatedCSSOMComplexValue(const CSSValue& value, CSSStyleDeclaration& owner) |
111 | : DeprecatedCSSOMValue(DeprecatedComplexValueClass, owner) |
112 | , m_value(const_cast<CSSValue&>(value)) |
113 | { |
114 | } |
115 | |
116 | private: |
117 | Ref<CSSValue> m_value; |
118 | }; |
119 | |
120 | } // namespace WebCore |
121 | |
122 | #define SPECIALIZE_TYPE_TRAITS_CSSOM_VALUE(ToValueTypeName, predicate) \ |
123 | SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \ |
124 | static bool isType(const WebCore::DeprecatedCSSOMValue& value) { return value.predicate; } \ |
125 | SPECIALIZE_TYPE_TRAITS_END() |
126 | |
127 | SPECIALIZE_TYPE_TRAITS_CSSOM_VALUE(DeprecatedCSSOMComplexValue, isComplexValue()) |
128 | |
129 | |
130 | |