1/*
2 * Copyright (C) 2014 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
15 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "MemoizedDOMResult.h"
29
30#if ENABLE(WEB_REPLAY)
31
32#include "ReplayInputTypes.h"
33#include "SerializationMethods.h"
34#include "WebReplayInputs.h"
35
36namespace WebCore {
37
38const AtomicString& MemoizedDOMResultBase::type() const
39{
40    return inputTypes().MemoizedDOMResult;
41}
42
43std::unique_ptr<MemoizedDOMResultBase> MemoizedDOMResultBase::createFromEncodedResult(const String& attribute, EncodedCType ctype, EncodedValue encodedValue, ExceptionCode exceptionCode)
44{
45    switch (ctype) {
46#define CREATE_DECODE_SWITCH_CASE(name, type) \
47    case CTypeTraits<type>::encodedType: { \
48        CTypeTraits<type>::CType result; \
49        if (!EncodingTraits<type>::decodeValue(encodedValue, result)) \
50            return nullptr; \
51        return std::make_unique<MemoizedDOMResult<type>>(attribute, result, exceptionCode); \
52    } \
53\
54
55FOR_EACH_MEMOIZED_CTYPE(CREATE_DECODE_SWITCH_CASE)
56#undef CREATE_DECODE_SWITCH_CASE
57    }
58
59    RELEASE_ASSERT_NOT_REACHED();
60    return nullptr;
61}
62
63} // namespace WebCore
64
65namespace JSC {
66
67using WebCore::EncodedCType;
68using WebCore::ExceptionCode;
69using WebCore::MemoizedDOMResult;
70using WebCore::SerializedScriptValue;
71
72const AtomicString& InputTraits<MemoizedDOMResultBase>::type()
73{
74    return WebCore::inputTypes().MemoizedDOMResult;
75}
76
77void InputTraits<MemoizedDOMResultBase>::encode(EncodedValue& encodedValue, const MemoizedDOMResultBase& input)
78{
79    encodedValue.put<String>(ASCIILiteral("attribute"), input.attribute());
80    encodedValue.put<EncodedCType>(ASCIILiteral("ctype"), input.ctype());
81    encodedValue.put<EncodedValue>(ASCIILiteral("result"), input.encodedResult());
82    if (input.exceptionCode())
83        encodedValue.put<ExceptionCode>(ASCIILiteral("exceptionCode"), input.exceptionCode());
84}
85
86bool InputTraits<MemoizedDOMResultBase>::decode(EncodedValue& encodedValue, std::unique_ptr<MemoizedDOMResultBase>& input)
87{
88    String attribute;
89    if (!encodedValue.get<String>(ASCIILiteral("attribute"), attribute))
90        return false;
91
92    EncodedCType ctype;
93    if (!encodedValue.get<EncodedCType>(ASCIILiteral("ctype"), ctype))
94        return false;
95
96    EncodedValue encodedResult;
97    if (!encodedValue.get<EncodedValue>(ASCIILiteral("result"), encodedResult))
98        return false;
99
100    ExceptionCode exceptionCode = 0;
101    encodedValue.get<ExceptionCode>(ASCIILiteral("exceptionCode"), exceptionCode);
102
103    std::unique_ptr<MemoizedDOMResultBase> decodedInput = MemoizedDOMResultBase::createFromEncodedResult(attribute, ctype, encodedResult, exceptionCode);
104    if (!decodedInput)
105        return false;
106    input = WTF::move(decodedInput);
107    return true;
108}
109
110} // namespace JSC
111
112#endif // ENABLE(WEB_REPLAY)
113