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.
29 #include "EncodedValue.h"
31 #if ENABLE(WEB_REPLAY)
33 #include "InspectorValues.h"
34 #include <wtf/text/Base64.h>
36 using namespace Inspector
;
40 RefPtr
<InspectorObject
> EncodedValue::asObject()
42 RefPtr
<InspectorObject
> result
;
43 bool castSucceeded
= m_value
->asObject(result
);
44 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
49 RefPtr
<InspectorArray
> EncodedValue::asArray()
51 RefPtr
<InspectorArray
> result
;
52 bool castSucceeded
= m_value
->asArray(result
);
53 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
58 EncodedValue EncodingTraits
<Vector
<char>>::encodeValue(const Vector
<char>& buffer
)
60 return EncodedValue::createString(base64Encode(buffer
));
63 bool EncodingTraits
<Vector
<char>>::decodeValue(EncodedValue
& encodedBuffer
, Vector
<char>& decodedValue
)
65 return base64Decode(encodedBuffer
.convertTo
<String
>(), decodedValue
);
68 template<> EncodedValue ScalarEncodingTraits
<bool>::encodeValue(const bool& value
)
70 return EncodedValue(InspectorBasicValue::create(value
));
73 template<> EncodedValue ScalarEncodingTraits
<double>::encodeValue(const double& value
)
75 return EncodedValue(InspectorBasicValue::create(value
));
78 template<> EncodedValue ScalarEncodingTraits
<float>::encodeValue(const float& value
)
80 return EncodedValue(InspectorBasicValue::create((double)value
));
83 template<> EncodedValue ScalarEncodingTraits
<int32_t>::encodeValue(const int32_t& value
)
85 return EncodedValue(InspectorBasicValue::create((double)value
));
88 template<> EncodedValue ScalarEncodingTraits
<int64_t>::encodeValue(const int64_t& value
)
90 return EncodedValue(InspectorBasicValue::create((double)value
));
93 template<> EncodedValue ScalarEncodingTraits
<uint32_t>::encodeValue(const uint32_t& value
)
95 return EncodedValue(InspectorBasicValue::create((double)value
));
98 template<> EncodedValue ScalarEncodingTraits
<uint64_t>::encodeValue(const uint64_t& value
)
100 return EncodedValue(InspectorBasicValue::create((double)value
));
103 template<> bool EncodedValue::convertTo
<bool>()
106 bool castSucceeded
= m_value
->asBoolean(result
);
107 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
112 template<> double EncodedValue::convertTo
<double>()
115 bool castSucceeded
= m_value
->asDouble(result
);
116 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
121 template<> float EncodedValue::convertTo
<float>()
124 bool castSucceeded
= m_value
->asDouble(result
);
125 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
130 template<> int32_t EncodedValue::convertTo
<int32_t>()
133 bool castSucceeded
= m_value
->asInteger(result
);
134 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
139 template<> int64_t EncodedValue::convertTo
<int64_t>()
142 bool castSucceeded
= m_value
->asInteger(result
);
143 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
148 template<> uint32_t EncodedValue::convertTo
<uint32_t>()
151 bool castSucceeded
= m_value
->asInteger(result
);
152 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
157 template<> uint64_t EncodedValue::convertTo
<uint64_t>()
160 bool castSucceeded
= m_value
->asInteger(result
);
161 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
166 template<> String
EncodedValue::convertTo
<String
>()
169 bool castSucceeded
= m_value
->asString(result
);
170 ASSERT_UNUSED(castSucceeded
, castSucceeded
);
176 void EncodedValue::put
<EncodedValue
>(const String
& key
, const typename EncodingTraits
<EncodedValue
>::DecodedType
& value
)
178 asObject()->setValue(key
, value
.m_value
.copyRef());
182 void EncodedValue::append
<EncodedValue
>(const typename EncodingTraits
<EncodedValue
>::DecodedType
& value
)
184 asArray()->pushValue(value
.m_value
.copyRef());
188 bool EncodedValue::get
<EncodedValue
>(const String
& key
, typename EncodingTraits
<EncodedValue
>::DecodedType
& decodedValue
)
190 RefPtr
<Inspector::InspectorValue
> value
;
191 if (!asObject()->getValue(key
, value
))
194 decodedValue
= EncodedValue(WTF::move(value
));
201 #endif // ENABLE(WEB_REPLAY)