• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(PLUGIN_PROCESS)
30 
31 #include "ArgumentDecoder.h"
32 #include "ArgumentEncoder.h"
33 #include "WebCoreArgumentCoders.h"
34 #include <WebCore/NotImplemented.h>
35 
36 namespace WebKit {
37 
NPVariantData()38 NPVariantData::NPVariantData()
39     : m_type(NPVariantData::Void)
40     , m_boolValue(false)
41     , m_int32Value(0)
42     , m_doubleValue(0)
43     , m_localNPObjectIDValue(0)
44     , m_remoteNPObjectIDValue(0)
45 {
46 }
47 
makeVoid()48 NPVariantData NPVariantData::makeVoid()
49 {
50     return NPVariantData();
51 }
52 
makeNull()53 NPVariantData NPVariantData::makeNull()
54 {
55     NPVariantData npVariantData;
56 
57     npVariantData.m_type = NPVariantData::Null;
58 
59     return npVariantData;
60 }
61 
makeBool(bool value)62 NPVariantData NPVariantData::makeBool(bool value)
63 {
64     NPVariantData npVariantData;
65 
66     npVariantData.m_type = NPVariantData::Bool;
67     npVariantData.m_boolValue = value;
68 
69     return npVariantData;
70 }
71 
makeInt32(int32_t value)72 NPVariantData NPVariantData::makeInt32(int32_t value)
73 {
74     NPVariantData npVariantData;
75 
76     npVariantData.m_type = NPVariantData::Int32;
77     npVariantData.m_int32Value = value;
78 
79     return npVariantData;
80 }
81 
makeDouble(double value)82 NPVariantData NPVariantData::makeDouble(double value)
83 {
84     NPVariantData npVariantData;
85 
86     npVariantData.m_type = NPVariantData::Double;
87     npVariantData.m_doubleValue = value;
88 
89     return npVariantData;
90 }
91 
makeString(const char * string,unsigned length)92 NPVariantData NPVariantData::makeString(const char* string, unsigned length)
93 {
94     NPVariantData npVariantData;
95 
96     npVariantData.m_type = NPVariantData::String;
97     npVariantData.m_stringValue = CString(string, length);
98 
99     return npVariantData;
100 }
101 
makeLocalNPObjectID(uint64_t value)102 NPVariantData NPVariantData::makeLocalNPObjectID(uint64_t value)
103 {
104     NPVariantData npVariantData;
105 
106     npVariantData.m_type = NPVariantData::LocalNPObjectID;
107     npVariantData.m_localNPObjectIDValue = value;
108 
109     return npVariantData;
110 }
111 
makeRemoteNPObjectID(uint64_t value)112 NPVariantData NPVariantData::makeRemoteNPObjectID(uint64_t value)
113 {
114     NPVariantData npVariantData;
115 
116     npVariantData.m_type = NPVariantData::RemoteNPObjectID;
117     npVariantData.m_remoteNPObjectIDValue = value;
118 
119     return npVariantData;
120 }
121 
encode(CoreIPC::ArgumentEncoder * encoder) const122 void NPVariantData::encode(CoreIPC::ArgumentEncoder* encoder) const
123 {
124     encoder->encode(m_type);
125 
126     switch (type()) {
127     case NPVariantData::Void:
128     case NPVariantData::Null:
129         break;
130     case NPVariantData::Bool:
131         encoder->encode(boolValue());
132         break;
133     case NPVariantData::Int32:
134         encoder->encode(int32Value());
135         break;
136     case NPVariantData::Double:
137         encoder->encode(doubleValue());
138         break;
139     case NPVariantData::String:
140         encoder->encode(stringValue());
141         break;
142     case NPVariantData::LocalNPObjectID:
143         encoder->encode(localNPObjectIDValue());
144         break;
145     case NPVariantData::RemoteNPObjectID:
146         encoder->encode(remoteNPObjectIDValue());
147         break;
148     }
149 }
150 
decode(CoreIPC::ArgumentDecoder * decoder,NPVariantData & result)151 bool NPVariantData::decode(CoreIPC::ArgumentDecoder* decoder, NPVariantData& result)
152 {
153     uint32_t type;
154     if (!decoder->decode(type))
155         return false;
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 true;
172     case NPVariantData::Bool:
173         return decoder->decode(result.m_boolValue);
174     case NPVariantData::Int32:
175         return decoder->decode(result.m_int32Value);
176     case NPVariantData::Double:
177         return decoder->decode(result.m_doubleValue);
178     case NPVariantData::String:
179         return decoder->decode(result.m_stringValue);
180     case NPVariantData::LocalNPObjectID:
181         return decoder->decode(result.m_localNPObjectIDValue);
182     case NPVariantData::RemoteNPObjectID:
183         return decoder->decode(result.m_remoteNPObjectIDValue);
184     }
185 
186     return false;
187 }
188 
189 } // namespace WebKit
190 
191 #endif // ENABLE(PLUGIN_PROCESS)
192