NPRemoteObjectMap.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "NPRemoteObjectMap.h"
  27. #if ENABLE(PLUGIN_PROCESS)
  28. #include "NPObjectMessageReceiver.h"
  29. #include "NPObjectProxy.h"
  30. #include "NPRuntimeUtilities.h"
  31. #include "NPVariantData.h"
  32. #include <wtf/OwnPtr.h>
  33. namespace WebKit {
  34. static uint64_t generateNPObjectID()
  35. {
  36. static uint64_t generateNPObjectID;
  37. return ++generateNPObjectID;
  38. }
  39. PassRefPtr<NPRemoteObjectMap> NPRemoteObjectMap::create(CoreIPC::Connection* connection)
  40. {
  41. return adoptRef(new NPRemoteObjectMap(connection));
  42. }
  43. NPRemoteObjectMap::NPRemoteObjectMap(CoreIPC::Connection* connection)
  44. : m_connection(connection)
  45. {
  46. }
  47. NPRemoteObjectMap::~NPRemoteObjectMap()
  48. {
  49. ASSERT(m_npObjectProxies.isEmpty());
  50. ASSERT(m_registeredNPObjects.isEmpty());
  51. }
  52. NPObject* NPRemoteObjectMap::createNPObjectProxy(uint64_t remoteObjectID, Plugin* plugin)
  53. {
  54. NPObjectProxy* npObjectProxy = NPObjectProxy::create(this, plugin, remoteObjectID);
  55. m_npObjectProxies.add(npObjectProxy);
  56. return npObjectProxy;
  57. }
  58. void NPRemoteObjectMap::npObjectProxyDestroyed(NPObject* npObject)
  59. {
  60. NPObjectProxy* npObjectProxy = NPObjectProxy::toNPObjectProxy(npObject);
  61. ASSERT(m_npObjectProxies.contains(npObjectProxy));
  62. m_npObjectProxies.remove(npObjectProxy);
  63. }
  64. uint64_t NPRemoteObjectMap::registerNPObject(NPObject* npObject, Plugin* plugin)
  65. {
  66. uint64_t npObjectID = generateNPObjectID();
  67. m_registeredNPObjects.set(npObjectID, NPObjectMessageReceiver::create(this, plugin, npObjectID, npObject).leakPtr());
  68. return npObjectID;
  69. }
  70. void NPRemoteObjectMap::unregisterNPObject(uint64_t npObjectID)
  71. {
  72. m_registeredNPObjects.remove(npObjectID);
  73. }
  74. static uint64_t remoteNPObjectID(Plugin* plugin, NPObject* npObject)
  75. {
  76. if (!NPObjectProxy::isNPObjectProxy(npObject))
  77. return 0;
  78. NPObjectProxy* npObjectProxy = NPObjectProxy::toNPObjectProxy(npObject);
  79. if (npObjectProxy->plugin() != plugin)
  80. return 0;
  81. return npObjectProxy->npObjectID();
  82. }
  83. NPVariantData NPRemoteObjectMap::npVariantToNPVariantData(const NPVariant& variant, Plugin* plugin)
  84. {
  85. switch (variant.type) {
  86. case NPVariantType_Void:
  87. return NPVariantData::makeVoid();
  88. case NPVariantType_Null:
  89. return NPVariantData::makeNull();
  90. case NPVariantType_Bool:
  91. return NPVariantData::makeBool(variant.value.boolValue);
  92. case NPVariantType_Int32:
  93. return NPVariantData::makeInt32(variant.value.intValue);
  94. case NPVariantType_Double:
  95. return NPVariantData::makeDouble(variant.value.doubleValue);
  96. case NPVariantType_String:
  97. return NPVariantData::makeString(variant.value.stringValue.UTF8Characters, variant.value.stringValue.UTF8Length);
  98. case NPVariantType_Object: {
  99. NPObject* npObject = variant.value.objectValue;
  100. if (uint64_t npObjectID = remoteNPObjectID(plugin, npObject)) {
  101. // FIXME: Under some circumstances, this might leak the NPObjectProxy object.
  102. // Figure out how to avoid that.
  103. retainNPObject(npObject);
  104. return NPVariantData::makeRemoteNPObjectID(npObjectID);
  105. }
  106. uint64_t npObjectID = registerNPObject(npObject, plugin);
  107. return NPVariantData::makeLocalNPObjectID(npObjectID);
  108. }
  109. }
  110. ASSERT_NOT_REACHED();
  111. return NPVariantData::makeVoid();
  112. }
  113. NPVariant NPRemoteObjectMap::npVariantDataToNPVariant(const NPVariantData& npVariantData, Plugin* plugin)
  114. {
  115. NPVariant npVariant;
  116. switch (npVariantData.type()) {
  117. case NPVariantData::Void:
  118. VOID_TO_NPVARIANT(npVariant);
  119. break;
  120. case NPVariantData::Null:
  121. NULL_TO_NPVARIANT(npVariant);
  122. break;
  123. case NPVariantData::Bool:
  124. BOOLEAN_TO_NPVARIANT(npVariantData.boolValue(), npVariant);
  125. break;
  126. case NPVariantData::Int32:
  127. INT32_TO_NPVARIANT(npVariantData.int32Value(), npVariant);
  128. break;
  129. case NPVariantData::Double:
  130. DOUBLE_TO_NPVARIANT(npVariantData.doubleValue(), npVariant);
  131. break;
  132. case NPVariantData::String: {
  133. NPString npString = createNPString(npVariantData.stringValue());
  134. STRINGN_TO_NPVARIANT(npString.UTF8Characters, npString.UTF8Length, npVariant);
  135. break;
  136. }
  137. case NPVariantData::LocalNPObjectID: {
  138. uint64_t npObjectID = npVariantData.localNPObjectIDValue();
  139. ASSERT(npObjectID);
  140. NPObjectMessageReceiver* npObjectMessageReceiver = m_registeredNPObjects.get(npObjectID);
  141. if (!npObjectMessageReceiver) {
  142. ASSERT_NOT_REACHED();
  143. VOID_TO_NPVARIANT(npVariant);
  144. break;
  145. }
  146. NPObject* npObject = npObjectMessageReceiver->npObject();
  147. ASSERT(npObject);
  148. retainNPObject(npObject);
  149. OBJECT_TO_NPVARIANT(npObject, npVariant);
  150. break;
  151. }
  152. case NPVariantData::RemoteNPObjectID: {
  153. NPObject* npObjectProxy = createNPObjectProxy(npVariantData.remoteNPObjectIDValue(), plugin);
  154. OBJECT_TO_NPVARIANT(npObjectProxy, npVariant);
  155. break;
  156. }
  157. }
  158. return npVariant;
  159. }
  160. void NPRemoteObjectMap::pluginDestroyed(Plugin* plugin)
  161. {
  162. Vector<NPObjectMessageReceiver*> messageReceivers;
  163. // Gather the receivers associated with this plug-in.
  164. for (HashMap<uint64_t, NPObjectMessageReceiver*>::const_iterator it = m_registeredNPObjects.begin(), end = m_registeredNPObjects.end(); it != end; ++it) {
  165. NPObjectMessageReceiver* npObjectMessageReceiver = it->value;
  166. if (npObjectMessageReceiver->plugin() == plugin)
  167. messageReceivers.append(npObjectMessageReceiver);
  168. }
  169. // Now delete all the receivers.
  170. deleteAllValues(messageReceivers);
  171. Vector<NPObjectProxy*> objectProxies;
  172. for (HashSet<NPObjectProxy*>::const_iterator it = m_npObjectProxies.begin(), end = m_npObjectProxies.end(); it != end; ++it) {
  173. NPObjectProxy* npObjectProxy = *it;
  174. if (npObjectProxy->plugin() == plugin)
  175. objectProxies.append(npObjectProxy);
  176. }
  177. // Invalidate and remove all proxies associated with this plug-in.
  178. for (size_t i = 0; i < objectProxies.size(); ++i) {
  179. NPObjectProxy* npObjectProxy = objectProxies[i];
  180. npObjectProxy->invalidate();
  181. ASSERT(m_npObjectProxies.contains(npObjectProxy));
  182. m_npObjectProxies.remove(npObjectProxy);
  183. }
  184. }
  185. void NPRemoteObjectMap::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder, OwnPtr<CoreIPC::MessageEncoder>& replyEncoder)
  186. {
  187. NPObjectMessageReceiver* messageReceiver = m_registeredNPObjects.get(decoder.destinationID());
  188. if (!messageReceiver)
  189. return;
  190. messageReceiver->didReceiveSyncNPObjectMessageReceiverMessage(connection, decoder, replyEncoder);
  191. }
  192. } // namespace WebKit
  193. #endif // ENABLE(PLUGIN_PROCESS)