2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "PluginProcessConnection.h"
29 #if ENABLE(PLUGIN_PROCESS)
31 #include "NPRemoteObjectMap.h"
32 #include "NPRuntimeObjectMap.h"
33 #include "PluginProcessConnectionManager.h"
34 #include "PluginProxy.h"
35 #include "WebProcess.h"
36 #include "WebProcessProxyMessages.h"
37 #include <WebCore/FileSystem.h>
39 using namespace WebCore;
43 // The timeout, in seconds, when sending sync messages to the plug-in.
44 static const double syncMessageTimeout = 45;
46 static double defaultSyncMessageTimeout(const String& pluginPath)
48 // FIXME: We should key this off something other than the path.
50 // We don't want a message timeout for the AppleConnect plug-in.
51 if (pathGetFileName(pluginPath) == "AppleConnect.plugin")
52 return CoreIPC::Connection::NoTimeout;
54 // We don't want a message timeout for the Microsoft SharePoint plug-in
55 // since it can spin a nested run loop in response to an NPN_Invoke, making it seem like
56 // the plug-in process is hung. See <rdar://problem/9536303>.
57 // FIXME: Instead of changing the default sync message timeout, CoreIPC could send an
58 // asynchronous message which the other process would have to reply to on the main thread.
59 // This way we could check if the plug-in process is actually hung or not.
60 if (pathGetFileName(pluginPath) == "SharePointBrowserPlugin.plugin")
61 return CoreIPC::Connection::NoTimeout;
63 // We don't want a message timeout for the BankID plug-in since it can spin a nested
64 // run loop when it's waiting for a reply to an AppleEvent.
65 if (pathGetFileName(pluginPath) == "PersonalPlugin.bundle")
66 return CoreIPC::Connection::NoTimeout;
68 if (WebProcess::shared().disablePluginProcessMessageTimeout())
69 return CoreIPC::Connection::NoTimeout;
71 return syncMessageTimeout;
74 PluginProcessConnection::PluginProcessConnection(PluginProcessConnectionManager* pluginProcessConnectionManager, const String& pluginPath, CoreIPC::Connection::Identifier connectionIdentifier)
75 : m_pluginProcessConnectionManager(pluginProcessConnectionManager)
76 , m_pluginPath(pluginPath)
78 m_connection = CoreIPC::Connection::createClientConnection(connectionIdentifier, this, WebProcess::shared().runLoop());
80 m_connection->setDefaultSyncMessageTimeout(defaultSyncMessageTimeout(m_pluginPath));
81 m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
86 PluginProcessConnection::~PluginProcessConnection()
88 ASSERT(!m_connection);
89 ASSERT(!m_npRemoteObjectMap);
92 void PluginProcessConnection::addPluginProxy(PluginProxy* plugin)
94 ASSERT(!m_plugins.contains(plugin->pluginInstanceID()));
95 m_plugins.set(plugin->pluginInstanceID(), plugin);
98 void PluginProcessConnection::removePluginProxy(PluginProxy* plugin)
100 ASSERT(m_plugins.contains(plugin->pluginInstanceID()));
101 m_plugins.remove(plugin->pluginInstanceID());
103 // Invalidate all objects related to this plug-in.
104 m_npRemoteObjectMap->pluginDestroyed(plugin);
106 if (!m_plugins.isEmpty())
109 m_npRemoteObjectMap = nullptr;
111 // We have no more plug-ins, invalidate the connection to the plug-in process.
112 ASSERT(m_connection);
113 m_connection->invalidate();
114 m_connection = nullptr;
116 // This will cause us to be deleted.
117 m_pluginProcessConnectionManager->removePluginProcessConnection(this);
120 void PluginProcessConnection::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
122 ASSERT(arguments->destinationID());
124 PluginProxy* pluginProxy = m_plugins.get(arguments->destinationID());
128 pluginProxy->didReceivePluginProxyMessage(connection, messageID, arguments);
131 void PluginProcessConnection::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, OwnPtr<CoreIPC::ArgumentEncoder>& reply)
133 if (messageID.is<CoreIPC::MessageClassNPObjectMessageReceiver>()) {
134 m_npRemoteObjectMap->didReceiveSyncMessage(connection, messageID, arguments, reply);
138 uint64_t destinationID = arguments->destinationID();
140 if (!destinationID) {
141 didReceiveSyncPluginProcessConnectionMessage(connection, messageID, arguments, reply);
145 PluginProxy* pluginProxy = m_plugins.get(destinationID);
149 pluginProxy->didReceiveSyncPluginProxyMessage(connection, messageID, arguments, reply);
152 void PluginProcessConnection::didClose(CoreIPC::Connection*)
154 // The plug-in process must have crashed.
155 for (HashMap<uint64_t, PluginProxy*>::const_iterator::Values it = m_plugins.begin().values(), end = m_plugins.end().values(); it != end; ++it) {
156 PluginProxy* pluginProxy = (*it);
158 pluginProxy->pluginProcessCrashed();
162 void PluginProcessConnection::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
166 void PluginProcessConnection::syncMessageSendTimedOut(CoreIPC::Connection*)
168 WebProcess::shared().connection()->send(Messages::WebProcessProxy::PluginSyncMessageSendTimedOut(m_pluginPath), 0);
171 void PluginProcessConnection::setException(const String& exceptionString)
173 NPRuntimeObjectMap::setGlobalException(exceptionString);
176 } // namespace WebKit
178 #endif // ENABLE(PLUGIN_PROCESS)