initial import
[vuplus_webkit] / Source / WebKit2 / Shared / Plugins / NPObjectMessageReceiver.cpp
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 "NPObjectMessageReceiver.h"
28
29 #if ENABLE(PLUGIN_PROCESS)
30
31 #include "NPIdentifierData.h"
32 #include "NPRemoteObjectMap.h"
33 #include "NPRuntimeUtilities.h"
34 #include "NPVariantData.h"
35
36 namespace WebKit {
37
38 PassOwnPtr<NPObjectMessageReceiver> NPObjectMessageReceiver::create(NPRemoteObjectMap* npRemoteObjectMap, Plugin* plugin, uint64_t npObjectID, NPObject* npObject)
39 {
40     return adoptPtr(new NPObjectMessageReceiver(npRemoteObjectMap, plugin, npObjectID, npObject));
41 }
42
43 NPObjectMessageReceiver::NPObjectMessageReceiver(NPRemoteObjectMap* npRemoteObjectMap, Plugin* plugin, uint64_t npObjectID, NPObject* npObject)
44     : m_npRemoteObjectMap(npRemoteObjectMap)
45     , m_plugin(plugin)
46     , m_npObjectID(npObjectID)
47     , m_npObject(npObject)
48 {
49     retainNPObject(m_npObject);
50 }
51
52 NPObjectMessageReceiver::~NPObjectMessageReceiver()
53 {
54     m_npRemoteObjectMap->unregisterNPObject(m_npObjectID);
55
56     releaseNPObject(m_npObject);
57 }
58
59 void NPObjectMessageReceiver::deallocate()
60 {
61     delete this;
62 }
63
64 void NPObjectMessageReceiver::hasMethod(const NPIdentifierData& methodNameData, bool& returnValue)
65 {
66     if (!m_npObject->_class->hasMethod) {
67         returnValue = false;
68         return;
69     }
70     
71     returnValue = m_npObject->_class->hasMethod(m_npObject, methodNameData.createNPIdentifier());
72 }
73
74 void NPObjectMessageReceiver::invoke(const NPIdentifierData& methodNameData, const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
75 {
76     if (!m_npObject->_class->invoke) {
77         returnValue = false;
78         return;
79     }
80
81     Vector<NPVariant> arguments;
82     for (size_t i = 0; i < argumentsData.size(); ++i)
83         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
84
85     NPVariant result;
86     VOID_TO_NPVARIANT(result);
87
88     returnValue = m_npObject->_class->invoke(m_npObject, methodNameData.createNPIdentifier(), arguments.data(), arguments.size(), &result);
89     if (returnValue) {
90         // Convert the NPVariant to an NPVariantData.
91         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
92     }
93
94     // Release all arguments.
95     for (size_t i = 0; i < argumentsData.size(); ++i)
96         releaseNPVariantValue(&arguments[i]);
97     
98     // And release the result.
99     releaseNPVariantValue(&result);
100 }
101
102 void NPObjectMessageReceiver::invokeDefault(const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
103 {
104     if (!m_npObject->_class->invokeDefault) {
105         returnValue = false;
106         return;
107     }
108
109     Vector<NPVariant> arguments;
110     for (size_t i = 0; i < argumentsData.size(); ++i)
111         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
112
113     NPVariant result;
114     VOID_TO_NPVARIANT(result);
115
116     returnValue = m_npObject->_class->invokeDefault(m_npObject, arguments.data(), arguments.size(), &result);
117     if (returnValue) {
118         // Convert the NPVariant to an NPVariantData.
119         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
120     }
121
122     // Release all arguments.
123     for (size_t i = 0; i < argumentsData.size(); ++i)
124         releaseNPVariantValue(&arguments[i]);
125     
126     // And release the result.
127     releaseNPVariantValue(&result);
128 }
129
130 void NPObjectMessageReceiver::hasProperty(const NPIdentifierData& propertyNameData, bool& returnValue)
131 {
132     if (!m_npObject->_class->hasProperty) {
133         returnValue = false;
134         return;
135     }
136
137     returnValue = m_npObject->_class->hasProperty(m_npObject, propertyNameData.createNPIdentifier());
138 }
139
140 void NPObjectMessageReceiver::getProperty(const NPIdentifierData& propertyNameData, bool& returnValue, NPVariantData& resultData)
141 {
142     if (!m_npObject->_class->getProperty) {
143         returnValue = false;
144         return;
145     }
146
147     NPVariant result;
148     VOID_TO_NPVARIANT(result);
149
150     returnValue = m_npObject->_class->getProperty(m_npObject, propertyNameData.createNPIdentifier(), &result);
151     if (!returnValue)
152         return;
153
154     // Convert the NPVariant to an NPVariantData.
155     resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
156
157     // And release the result.
158     releaseNPVariantValue(&result);
159 }
160
161 void NPObjectMessageReceiver::setProperty(const NPIdentifierData& propertyNameData, const NPVariantData& propertyValueData, bool& returnValue)
162 {
163     if (!m_npObject->_class->setProperty) {
164         returnValue = false;
165         return;
166     }
167
168     NPVariant propertyValue = m_npRemoteObjectMap->npVariantDataToNPVariant(propertyValueData, m_plugin);
169
170     // Set the property.
171     returnValue = m_npObject->_class->setProperty(m_npObject, propertyNameData.createNPIdentifier(), &propertyValue);
172
173     // And release the value.
174     releaseNPVariantValue(&propertyValue);
175 }
176
177 void NPObjectMessageReceiver::removeProperty(const NPIdentifierData& propertyNameData, bool& returnValue)
178 {
179     if (!m_npObject->_class->removeProperty) {
180         returnValue = false;
181         return;
182     }
183
184     returnValue = m_npObject->_class->removeProperty(m_npObject, propertyNameData.createNPIdentifier());
185 }
186
187 void NPObjectMessageReceiver::enumerate(bool& returnValue, Vector<NPIdentifierData>& identifiersData)
188 {
189     if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(m_npObject->_class) || !m_npObject->_class->enumerate) {
190         returnValue = false;
191         return;
192     }
193
194     NPIdentifier* identifiers = 0;
195     uint32_t identifierCount = 0;
196
197     returnValue = m_npObject->_class->enumerate(m_npObject, &identifiers, &identifierCount);
198     if (!returnValue)
199         return;
200
201     for (uint32_t i = 0; i < identifierCount; ++i)
202         identifiersData.append(NPIdentifierData::fromNPIdentifier(identifiers[i]));
203
204     npnMemFree(identifiers);
205 }
206
207 void NPObjectMessageReceiver::construct(const Vector<NPVariantData>& argumentsData, bool& returnValue, NPVariantData& resultData)
208 {
209     if (!NP_CLASS_STRUCT_VERSION_HAS_CTOR(m_npObject->_class) || !m_npObject->_class->construct) {
210         returnValue = false;
211         return;
212     }
213
214     Vector<NPVariant> arguments;
215     for (size_t i = 0; i < argumentsData.size(); ++i)
216         arguments.append(m_npRemoteObjectMap->npVariantDataToNPVariant(argumentsData[i], m_plugin));
217
218     NPVariant result;
219     VOID_TO_NPVARIANT(result);
220
221     returnValue = m_npObject->_class->construct(m_npObject, arguments.data(), arguments.size(), &result);
222     if (returnValue) {
223         // Convert the NPVariant to an NPVariantData.
224         resultData = m_npRemoteObjectMap->npVariantToNPVariantData(result, m_plugin);
225     }
226
227     // Release all arguments.
228     for (size_t i = 0; i < argumentsData.size(); ++i)
229         releaseNPVariantValue(&arguments[i]);
230     
231     // And release the result.
232     releaseNPVariantValue(&result);
233 }
234
235 } // namespace WebKit
236
237 #endif // ENABLE(PLUGIN_PROCESS)
238