1 | /* |
2 | * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "NPVariantData.h" |
28 | |
29 | #if ENABLE(NETSCAPE_PLUGIN_API) |
30 | |
31 | #include "Decoder.h" |
32 | #include "Encoder.h" |
33 | #include "WebCoreArgumentCoders.h" |
34 | |
35 | namespace WebKit { |
36 | |
37 | NPVariantData::NPVariantData() |
38 | : m_type(NPVariantData::Void) |
39 | , m_boolValue(false) |
40 | , m_int32Value(0) |
41 | , m_doubleValue(0) |
42 | , m_localNPObjectIDValue(0) |
43 | , m_remoteNPObjectIDValue(0) |
44 | { |
45 | } |
46 | |
47 | NPVariantData NPVariantData::makeVoid() |
48 | { |
49 | return NPVariantData(); |
50 | } |
51 | |
52 | NPVariantData NPVariantData::makeNull() |
53 | { |
54 | NPVariantData npVariantData; |
55 | |
56 | npVariantData.m_type = NPVariantData::Null; |
57 | |
58 | return npVariantData; |
59 | } |
60 | |
61 | NPVariantData NPVariantData::makeBool(bool value) |
62 | { |
63 | NPVariantData npVariantData; |
64 | |
65 | npVariantData.m_type = NPVariantData::Bool; |
66 | npVariantData.m_boolValue = value; |
67 | |
68 | return npVariantData; |
69 | } |
70 | |
71 | NPVariantData NPVariantData::makeInt32(int32_t value) |
72 | { |
73 | NPVariantData npVariantData; |
74 | |
75 | npVariantData.m_type = NPVariantData::Int32; |
76 | npVariantData.m_int32Value = value; |
77 | |
78 | return npVariantData; |
79 | } |
80 | |
81 | NPVariantData NPVariantData::makeDouble(double value) |
82 | { |
83 | NPVariantData npVariantData; |
84 | |
85 | npVariantData.m_type = NPVariantData::Double; |
86 | npVariantData.m_doubleValue = value; |
87 | |
88 | return npVariantData; |
89 | } |
90 | |
91 | NPVariantData NPVariantData::makeString(const char* string, unsigned length) |
92 | { |
93 | NPVariantData npVariantData; |
94 | |
95 | npVariantData.m_type = NPVariantData::String; |
96 | npVariantData.m_stringValue = CString(string, length); |
97 | |
98 | return npVariantData; |
99 | } |
100 | |
101 | NPVariantData NPVariantData::makeLocalNPObjectID(uint64_t value) |
102 | { |
103 | NPVariantData npVariantData; |
104 | |
105 | npVariantData.m_type = NPVariantData::LocalNPObjectID; |
106 | npVariantData.m_localNPObjectIDValue = value; |
107 | |
108 | return npVariantData; |
109 | } |
110 | |
111 | NPVariantData NPVariantData::makeRemoteNPObjectID(uint64_t value) |
112 | { |
113 | NPVariantData npVariantData; |
114 | |
115 | npVariantData.m_type = NPVariantData::RemoteNPObjectID; |
116 | npVariantData.m_remoteNPObjectIDValue = value; |
117 | |
118 | return npVariantData; |
119 | } |
120 | |
121 | void NPVariantData::encode(IPC::Encoder& encoder) const |
122 | { |
123 | encoder << m_type; |
124 | |
125 | switch (type()) { |
126 | case NPVariantData::Void: |
127 | case NPVariantData::Null: |
128 | break; |
129 | case NPVariantData::Bool: |
130 | encoder << boolValue(); |
131 | break; |
132 | case NPVariantData::Int32: |
133 | encoder << int32Value(); |
134 | break; |
135 | case NPVariantData::Double: |
136 | encoder << doubleValue(); |
137 | break; |
138 | case NPVariantData::String: |
139 | encoder << stringValue(); |
140 | break; |
141 | case NPVariantData::LocalNPObjectID: |
142 | encoder << localNPObjectIDValue(); |
143 | break; |
144 | case NPVariantData::RemoteNPObjectID: |
145 | encoder << remoteNPObjectIDValue(); |
146 | break; |
147 | } |
148 | } |
149 | |
150 | Optional<NPVariantData> NPVariantData::decode(IPC::Decoder& decoder) |
151 | { |
152 | NPVariantData result; |
153 | uint32_t type; |
154 | if (!decoder.decode(type)) |
155 | return WTF::nullopt; |
156 | |
157 | // We special-case LocalNPObjectID and RemoteNPObjectID here so a LocalNPObjectID is |
158 | // decoded as a RemoteNPObjectID and vice versa. |
159 | // This is done because the type is from the perspective of the other connection, and |
160 | // thus we have to adjust it to match our own perspective. |
161 | if (type == NPVariantData::LocalNPObjectID) |
162 | type = NPVariantData::RemoteNPObjectID; |
163 | else if (type == NPVariantData::RemoteNPObjectID) |
164 | type = NPVariantData::LocalNPObjectID; |
165 | |
166 | result.m_type = type; |
167 | |
168 | switch (result.m_type) { |
169 | case NPVariantData::Void: |
170 | case NPVariantData::Null: |
171 | return result; |
172 | case NPVariantData::Bool: |
173 | if (!decoder.decode(result.m_boolValue)) |
174 | return WTF::nullopt; |
175 | return result; |
176 | case NPVariantData::Int32: |
177 | if (!decoder.decode(result.m_int32Value)) |
178 | return WTF::nullopt; |
179 | return result; |
180 | case NPVariantData::Double: |
181 | if (!decoder.decode(result.m_doubleValue)) |
182 | return WTF::nullopt; |
183 | return result; |
184 | case NPVariantData::String: |
185 | if (!decoder.decode(result.m_stringValue)) |
186 | return WTF::nullopt; |
187 | return result; |
188 | case NPVariantData::LocalNPObjectID: |
189 | if (!decoder.decode(result.m_localNPObjectIDValue)) |
190 | return WTF::nullopt; |
191 | return result; |
192 | case NPVariantData::RemoteNPObjectID: |
193 | if (!decoder.decode(result.m_remoteNPObjectIDValue)) |
194 | return WTF::nullopt; |
195 | return result; |
196 | } |
197 | |
198 | return WTF::nullopt; |
199 | } |
200 | |
201 | } // namespace WebKit |
202 | |
203 | #endif // ENABLE(NETSCAPE_PLUGIN_API) |
204 | |