PluginProcessConnection.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "PluginProcessConnection.h"
  27. #if ENABLE(PLUGIN_PROCESS)
  28. #include "NPObjectMessageReceiverMessages.h"
  29. #include "NPRemoteObjectMap.h"
  30. #include "NPRuntimeObjectMap.h"
  31. #include "PluginProcessConnectionManager.h"
  32. #include "PluginProxy.h"
  33. #include "WebProcess.h"
  34. #include "WebProcessProxyMessages.h"
  35. #include <WebCore/FileSystem.h>
  36. #include <runtime/JSObject.h>
  37. using namespace WebCore;
  38. namespace WebKit {
  39. PluginProcessConnection::PluginProcessConnection(PluginProcessConnectionManager* pluginProcessConnectionManager, uint64_t pluginProcessToken, CoreIPC::Connection::Identifier connectionIdentifier, bool supportsAsynchronousPluginInitialization)
  40. : m_pluginProcessConnectionManager(pluginProcessConnectionManager)
  41. , m_pluginProcessToken(pluginProcessToken)
  42. , m_supportsAsynchronousPluginInitialization(supportsAsynchronousPluginInitialization)
  43. {
  44. m_connection = CoreIPC::Connection::createClientConnection(connectionIdentifier, this, RunLoop::main());
  45. m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
  46. m_connection->open();
  47. }
  48. PluginProcessConnection::~PluginProcessConnection()
  49. {
  50. ASSERT(!m_connection);
  51. ASSERT(!m_npRemoteObjectMap);
  52. }
  53. void PluginProcessConnection::addPluginProxy(PluginProxy* plugin)
  54. {
  55. ASSERT(!m_plugins.contains(plugin->pluginInstanceID()));
  56. m_plugins.set(plugin->pluginInstanceID(), plugin);
  57. }
  58. void PluginProcessConnection::removePluginProxy(PluginProxy* plugin)
  59. {
  60. ASSERT(m_plugins.contains(plugin->pluginInstanceID()));
  61. m_plugins.remove(plugin->pluginInstanceID());
  62. // Invalidate all objects related to this plug-in.
  63. m_npRemoteObjectMap->pluginDestroyed(plugin);
  64. if (!m_plugins.isEmpty())
  65. return;
  66. m_npRemoteObjectMap = nullptr;
  67. // We have no more plug-ins, invalidate the connection to the plug-in process.
  68. ASSERT(m_connection);
  69. m_connection->invalidate();
  70. m_connection = nullptr;
  71. // This will cause us to be deleted.
  72. m_pluginProcessConnectionManager->removePluginProcessConnection(this);
  73. }
  74. void PluginProcessConnection::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder)
  75. {
  76. ASSERT(decoder.destinationID());
  77. PluginProxy* pluginProxy = m_plugins.get(decoder.destinationID());
  78. if (!pluginProxy)
  79. return;
  80. pluginProxy->didReceivePluginProxyMessage(connection, decoder);
  81. }
  82. void PluginProcessConnection::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageDecoder& decoder, OwnPtr<CoreIPC::MessageEncoder>& replyEncoder)
  83. {
  84. if (decoder.messageReceiverName() == Messages::NPObjectMessageReceiver::messageReceiverName()) {
  85. m_npRemoteObjectMap->didReceiveSyncMessage(connection, decoder, replyEncoder);
  86. return;
  87. }
  88. uint64_t destinationID = decoder.destinationID();
  89. if (!destinationID) {
  90. didReceiveSyncPluginProcessConnectionMessage(connection, decoder, replyEncoder);
  91. return;
  92. }
  93. PluginProxy* pluginProxy = m_plugins.get(destinationID);
  94. if (!pluginProxy)
  95. return;
  96. pluginProxy->didReceiveSyncPluginProxyMessage(connection, decoder, replyEncoder);
  97. }
  98. void PluginProcessConnection::didClose(CoreIPC::Connection*)
  99. {
  100. // The plug-in process must have crashed.
  101. for (HashMap<uint64_t, PluginProxy*>::const_iterator::Values it = m_plugins.begin().values(), end = m_plugins.end().values(); it != end; ++it) {
  102. PluginProxy* pluginProxy = (*it);
  103. pluginProxy->pluginProcessCrashed();
  104. }
  105. }
  106. void PluginProcessConnection::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference, CoreIPC::StringReference)
  107. {
  108. }
  109. void PluginProcessConnection::setException(const String& exceptionString)
  110. {
  111. NPRuntimeObjectMap::setGlobalException(exceptionString);
  112. }
  113. } // namespace WebKit
  114. #endif // ENABLE(PLUGIN_PROCESS)