NPObjectMessageReceiver.cpp 8.2 KB

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