2 * Copyright (C) 2013 University of Washington. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef EncodedValue_h
29 #define EncodedValue_h
31 #if ENABLE(WEB_REPLAY)
33 #include "InspectorValues.h"
34 #include <wtf/Forward.h>
35 #include <wtf/Vector.h>
39 class NondeterministicInputBase
;
40 template<typename T
> struct EncodingTraits
;
42 class EncodedValue final
{
44 explicit EncodedValue(RefPtr
<Inspector::InspectorValue
>&& value
)
48 : m_value(nullptr) { }
50 static EncodedValue
createObject()
52 return EncodedValue(Inspector::InspectorObject::create());
55 static EncodedValue
createArray()
57 return EncodedValue(Inspector::InspectorArray::create());
60 static EncodedValue
createString(const String
& value
)
62 return EncodedValue(Inspector::InspectorString::create(value
));
65 static EncodedValue
createString(const char* value
)
67 return EncodedValue(Inspector::InspectorString::create(value
));
71 void put(const String
&, const typename EncodingTraits
<T
>::DecodedType
&);
74 void append(const typename EncodingTraits
<T
>::DecodedType
&);
76 template<typename T
> bool get(const String
&, typename EncodingTraits
<T
>::DecodedType
&);
77 template<typename T
> bool get(const String
&, std::unique_ptr
<typename EncodingTraits
<T
>::DecodedType
>&);
79 template<typename T
> T
convertTo();
81 JS_EXPORT_PRIVATE RefPtr
<Inspector::InspectorObject
> asObject();
82 JS_EXPORT_PRIVATE RefPtr
<Inspector::InspectorArray
> asArray();
85 RefPtr
<Inspector::InspectorValue
> m_value
;
88 template<> JS_EXPORT_PRIVATE
bool EncodedValue::convertTo
<bool>();
89 template<> JS_EXPORT_PRIVATE
double EncodedValue::convertTo
<double>();
90 template<> JS_EXPORT_PRIVATE
float EncodedValue::convertTo
<float>();
91 template<> JS_EXPORT_PRIVATE
int32_t EncodedValue::convertTo
<int32_t>();
92 template<> JS_EXPORT_PRIVATE
int64_t EncodedValue::convertTo
<int64_t>();
93 template<> JS_EXPORT_PRIVATE
uint32_t EncodedValue::convertTo
<uint32_t>();
94 template<> JS_EXPORT_PRIVATE
uint64_t EncodedValue::convertTo
<uint64_t>();
95 template<> JS_EXPORT_PRIVATE String
EncodedValue::convertTo
<String
>();
98 struct EncodingTraits
{
99 typedef T DecodedType
;
101 static EncodedValue
encodeValue(const DecodedType
&);
103 static bool decodeValue(EncodedValue
&, DecodedType
&);
104 static bool decodeValue(EncodedValue
&, std::unique_ptr
<DecodedType
>&);
107 template<typename T
, size_t inlineCapacity
, typename OverflowHandler
>
108 struct EncodingTraits
<Vector
<T
, inlineCapacity
, OverflowHandler
>> {
109 typedef Vector
<typename EncodingTraits
<T
>::DecodedType
, inlineCapacity
, OverflowHandler
> DecodedType
;
111 static EncodedValue
encodeValue(const DecodedType
& vectorOfValues
)
113 EncodedValue encodedVector
= EncodedValue::createArray();
114 for (const typename EncodingTraits
<T
>::DecodedType
& value
: vectorOfValues
)
115 encodedVector
.append
<T
>(value
);
117 return WTF::move(encodedVector
);
120 static bool decodeValue(EncodedValue
& encodedVector
, DecodedType
& decodedValue
)
122 RefPtr
<Inspector::InspectorArray
> inspectorArray
= encodedVector
.asArray();
123 decodedValue
= Vector
<typename EncodingTraits
<T
>::DecodedType
, inlineCapacity
, OverflowHandler
>(inspectorArray
->length());
124 for (size_t i
= 0; i
< inspectorArray
->length(); ++i
) {
125 EncodedValue
encodedElement(inspectorArray
->get(i
));
126 if (!EncodingTraits
<T
>::decodeValue(encodedElement
, decodedValue
.at(i
)))
133 template<> struct EncodingTraits
<EncodedValue
> {
134 typedef EncodedValue DecodedType
;
135 // We should never attempt to decode or encode an encoded value,
136 // so encodeValue and decodeValue are intentionally omitted here.
139 // Specialize byte vectors to use base64 encoding.
140 template<> struct EncodingTraits
<Vector
<char>> {
141 typedef Vector
<char> DecodedType
;
142 static EncodedValue
encodeValue(const DecodedType
&);
143 static bool decodeValue(EncodedValue
&, DecodedType
&);
147 struct ScalarEncodingTraits
{
148 typedef T DecodedType
;
150 static JS_EXPORT_PRIVATE EncodedValue
encodeValue(const DecodedType
& decodedValue
);
151 static bool decodeValue(EncodedValue
& encodedValue
, DecodedType
& decodedValue
)
153 decodedValue
= encodedValue
.convertTo
<DecodedType
>();
158 template<> struct EncodingTraits
<bool> : public ScalarEncodingTraits
<bool> { };
159 template<> struct EncodingTraits
<double> : public ScalarEncodingTraits
<double> { };
160 template<> struct EncodingTraits
<float> : public ScalarEncodingTraits
<float> { };
161 template<> struct EncodingTraits
<int32_t> : public ScalarEncodingTraits
<int32_t> { };
162 template<> struct EncodingTraits
<int64_t> : public ScalarEncodingTraits
<int64_t> { };
163 template<> struct EncodingTraits
<uint32_t> : public ScalarEncodingTraits
<uint32_t> { };
164 template<> struct EncodingTraits
<uint64_t> : public ScalarEncodingTraits
<uint64_t> { };
166 template<> struct EncodingTraits
<String
> : public ScalarEncodingTraits
<String
> {
167 static EncodedValue
encodeValue(const String
& value
)
169 return EncodedValue::createString(value
);
173 // Base cases for loading and storing values.
174 template<> JS_EXPORT_PRIVATE
175 void EncodedValue::put
<EncodedValue
>(const String
& key
, const typename EncodingTraits
<EncodedValue
>::DecodedType
&);
177 template<> JS_EXPORT_PRIVATE
178 void EncodedValue::append
<EncodedValue
>(const typename EncodingTraits
<EncodedValue
>::DecodedType
&);
180 template<> JS_EXPORT_PRIVATE
181 bool EncodedValue::get
<EncodedValue
>(const String
& key
, typename EncodingTraits
<EncodedValue
>::DecodedType
&);
183 // Load and store types with an accompanying EncodingTraits implementation.
185 void EncodedValue::put(const String
& key
, const typename EncodingTraits
<T
>::DecodedType
& value
)
187 EncodedValue encodedValue
= EncodingTraits
<T
>::encodeValue(value
);
188 put
<EncodedValue
>(key
, encodedValue
);
192 void EncodedValue::append(const typename EncodingTraits
<T
>::DecodedType
& value
)
194 EncodedValue encodedValue
= EncodingTraits
<T
>::encodeValue(value
);
195 append
<EncodedValue
>(encodedValue
);
199 bool EncodedValue::get(const String
& key
, typename EncodingTraits
<T
>::DecodedType
& decodedValue
)
201 EncodedValue encodedValue
;
202 if (!get
<EncodedValue
>(key
, encodedValue
))
205 return EncodingTraits
<T
>::decodeValue(encodedValue
, decodedValue
);
209 bool EncodedValue::get(const String
& key
, std::unique_ptr
<typename EncodingTraits
<T
>::DecodedType
>& decodedValue
)
211 EncodedValue encodedValue
;
212 if (!get
<EncodedValue
>(key
, encodedValue
))
215 return EncodingTraits
<T
>::decodeValue(encodedValue
, decodedValue
);
220 using JSC::EncodedValue
;
221 using JSC::EncodingTraits
;
223 #endif // ENABLE(WEB_REPLAY)
225 #endif // EncodedValue_h