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 "PluginProcessConnection.h"
28
29#if ENABLE(PLUGIN_PROCESS)
30
31#include "NPRemoteObjectMap.h"
32#include "PluginProcessConnectionManager.h"
33#include "PluginProxy.h"
34#include "WebProcess.h"
35#include "WebProcessProxyMessages.h"
36#include <WebCore/FileSystem.h>
37
38using namespace WebCore;
39
40namespace WebKit {
41
42// The timeout, in seconds, when sending sync messages to the plug-in.
43static const double syncMessageTimeout = 45;
44
45static double defaultSyncMessageTimeout(const String& pluginPath)
46{
47    // We don't want a message timeout for the AppleConnect plug-in.
48    // FIXME: We should key this off something other than the path.
49    if (pathGetFileName(pluginPath) == "AppleConnect.plugin")
50        return CoreIPC::Connection::NoTimeout;
51
52    return syncMessageTimeout;
53}
54
55PluginProcessConnection::PluginProcessConnection(PluginProcessConnectionManager* pluginProcessConnectionManager, const String& pluginPath, CoreIPC::Connection::Identifier connectionIdentifier)
56    : m_pluginProcessConnectionManager(pluginProcessConnectionManager)
57    , m_pluginPath(pluginPath)
58{
59    m_connection = CoreIPC::Connection::createClientConnection(connectionIdentifier, this, WebProcess::shared().runLoop());
60
61    m_connection->setDefaultSyncMessageTimeout(defaultSyncMessageTimeout(m_pluginPath));
62    m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
63
64    m_connection->open();
65}
66
67PluginProcessConnection::~PluginProcessConnection()
68{
69    ASSERT(!m_connection);
70    ASSERT(!m_npRemoteObjectMap);
71}
72
73void PluginProcessConnection::addPluginProxy(PluginProxy* plugin)
74{
75    ASSERT(!m_plugins.contains(plugin->pluginInstanceID()));
76    m_plugins.set(plugin->pluginInstanceID(), plugin);
77}
78
79void PluginProcessConnection::removePluginProxy(PluginProxy* plugin)
80{
81    ASSERT(m_plugins.contains(plugin->pluginInstanceID()));
82    m_plugins.remove(plugin->pluginInstanceID());
83
84    // Invalidate all objects related to this plug-in.
85    m_npRemoteObjectMap->pluginDestroyed(plugin);
86
87    if (!m_plugins.isEmpty())
88        return;
89
90    m_npRemoteObjectMap = nullptr;
91
92    // We have no more plug-ins, invalidate the connection to the plug-in process.
93    ASSERT(m_connection);
94    m_connection->invalidate();
95    m_connection = nullptr;
96
97    // This will cause us to be deleted.
98    m_pluginProcessConnectionManager->removePluginProcessConnection(this);
99}
100
101void PluginProcessConnection::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
102{
103    if (arguments->destinationID()) {
104        if (PluginProxy* pluginProxy = m_plugins.get(arguments->destinationID()))
105            pluginProxy->didReceivePluginProxyMessage(connection, messageID, arguments);
106        return;
107    }
108
109    ASSERT_NOT_REACHED();
110}
111
112CoreIPC::SyncReplyMode PluginProcessConnection::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, CoreIPC::ArgumentEncoder* reply)
113{
114    if (messageID.is<CoreIPC::MessageClassNPObjectMessageReceiver>())
115        return m_npRemoteObjectMap->didReceiveSyncMessage(connection, messageID, arguments, reply);
116
117    if (PluginProxy* pluginProxy = m_plugins.get(arguments->destinationID()))
118        return pluginProxy->didReceiveSyncPluginProxyMessage(connection, messageID, arguments, reply);
119
120    ASSERT_NOT_REACHED();
121    return CoreIPC::AutomaticReply;
122}
123
124void PluginProcessConnection::didClose(CoreIPC::Connection*)
125{
126    // The plug-in process must have crashed.
127    for (HashMap<uint64_t, PluginProxy*>::const_iterator::Values it = m_plugins.begin().values(), end = m_plugins.end().values(); it != end; ++it) {
128        PluginProxy* pluginProxy = (*it);
129
130        pluginProxy->pluginProcessCrashed();
131    }
132}
133
134void PluginProcessConnection::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
135{
136}
137
138void PluginProcessConnection::syncMessageSendTimedOut(CoreIPC::Connection*)
139{
140    WebProcess::shared().connection()->send(Messages::WebProcessProxy::PluginSyncMessageSendTimedOut(m_pluginPath), 0);
141}
142
143} // namespace WebKit
144
145#endif // ENABLE(PLUGIN_PROCESS)
146