1f1504302ed949a0ee7fe6e28de14fe40fc6080d
[WebKit-https.git] / Source / WebCore / replay / SerializationMethods.cpp
1 /*
2  * Copyright (C) 2012 University of Washington. All rights reserved.
3  * Copyright (C) 2014 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
26  */
27
28 #include "config.h"
29 #include "SerializationMethods.h"
30
31 #if ENABLE(WEB_REPLAY)
32
33 #include "AllReplayInputs.h"
34 #include "ReplayInputTypes.h"
35 #include "SecurityOrigin.h"
36 #include "URL.h"
37
38 using WebCore::SecurityOrigin;
39 using WebCore::URL;
40 using WebCore::inputTypes;
41
42 #define IMPORT_FROM_WEBCORE_NAMESPACE(name) \
43 using WebCore::name; \
44
45 WEB_REPLAY_INPUT_NAMES_FOR_EACH(IMPORT_FROM_WEBCORE_NAMESPACE)
46 #undef IMPORT_FROM_WEBCORE_NAMESPACE
47
48 namespace JSC {
49
50 EncodedValue EncodingTraits<NondeterministicInputBase>::encodeValue(const NondeterministicInputBase& input)
51 {
52     EncodedValue encodedValue = EncodedValue::createObject();
53     const AtomicString& type = input.type();
54     encodedValue.put<String>(ASCIILiteral("type"), type.string());
55
56 #define ENCODE_IF_TYPE_TAG_MATCHES(name) \
57     if (type == inputTypes().name) { \
58         InputTraits<name>::encode(encodedValue, static_cast<const name&>(input)); \
59         return encodedValue; \
60     } \
61
62     JS_REPLAY_INPUT_NAMES_FOR_EACH(ENCODE_IF_TYPE_TAG_MATCHES)
63     WEB_REPLAY_INPUT_NAMES_FOR_EACH(ENCODE_IF_TYPE_TAG_MATCHES)
64 #undef ENCODE_IF_TYPE_TAG_MATCHES
65
66     // The macro won't work here because of the class template argument.
67     if (type == inputTypes().MemoizedDOMResult) {
68         InputTraits<MemoizedDOMResultBase>::encode(encodedValue, static_cast<const MemoizedDOMResultBase&>(input));
69         return encodedValue;
70     }
71
72     ASSERT_NOT_REACHED();
73     return EncodedValue();
74 }
75
76 bool EncodingTraits<NondeterministicInputBase>::decodeValue(EncodedValue& encodedValue, std::unique_ptr<NondeterministicInputBase>& input)
77 {
78     String type;
79     if (!encodedValue.get<String>(ASCIILiteral("type"), type))
80         return false;
81
82 #define DECODE_IF_TYPE_TAG_MATCHES(name) \
83     if (type == inputTypes().name) { \
84         std::unique_ptr<name> decodedInput; \
85         if (!InputTraits<name>::decode(encodedValue, decodedInput)) \
86             return false; \
87         \
88         input = std::move(decodedInput); \
89         return true; \
90     } \
91
92     JS_REPLAY_INPUT_NAMES_FOR_EACH(DECODE_IF_TYPE_TAG_MATCHES)
93     WEB_REPLAY_INPUT_NAMES_FOR_EACH(DECODE_IF_TYPE_TAG_MATCHES)
94 #undef DECODE_IF_TYPE_TAG_MATCHES
95
96     if (type == inputTypes().MemoizedDOMResult) {
97         std::unique_ptr<MemoizedDOMResultBase> decodedInput;
98         if (!InputTraits<MemoizedDOMResultBase>::decode(encodedValue, decodedInput))
99             return false;
100
101         input = std::move(decodedInput);
102         return true;
103     }
104
105     return false;
106 }
107
108 EncodedValue EncodingTraits<SecurityOrigin>::encodeValue(RefPtr<SecurityOrigin> input)
109 {
110     return EncodedValue::createString(input->toString());
111 }
112
113 bool EncodingTraits<SecurityOrigin>::decodeValue(EncodedValue& encodedValue, RefPtr<SecurityOrigin>& input)
114 {
115     input = SecurityOrigin::createFromString(encodedValue.convertTo<String>());
116     return true;
117 }
118
119 EncodedValue EncodingTraits<URL>::encodeValue(const URL& input)
120 {
121     return EncodedValue::createString(input.string());
122 }
123
124 bool EncodingTraits<URL>::decodeValue(EncodedValue& encodedValue, URL& input)
125 {
126     input = URL(WebCore::ParsedURLString, encodedValue.convertTo<String>());
127     return true;
128 }
129
130 } // namespace JSC
131
132 #endif // ENABLE(WEB_REPLAY)