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(NETSCAPE_PLUGIN_API)
30
31#include "NPObjectMessageReceiverMessages.h"
32#include "NPRemoteObjectMap.h"
33#include "NPRuntimeObjectMap.h"
34#include "PluginProcessConnectionManager.h"
35#include "PluginProxy.h"
36#include "WebProcess.h"
37#include "WebProcessProxyMessages.h"
38#include <JavaScriptCore/JSObject.h>
39#include <wtf/FileSystem.h>
40
41namespace WebKit {
42
43PluginProcessConnection::PluginProcessConnection(PluginProcessConnectionManager* pluginProcessConnectionManager, uint64_t pluginProcessToken, IPC::Connection::Identifier connectionIdentifier, bool supportsAsynchronousPluginInitialization)
44 : m_pluginProcessConnectionManager(pluginProcessConnectionManager)
45 , m_pluginProcessToken(pluginProcessToken)
46 , m_supportsAsynchronousPluginInitialization(supportsAsynchronousPluginInitialization)
47{
48 m_connection = IPC::Connection::createClientConnection(connectionIdentifier, *this);
49
50 m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
51
52 m_connection->open();
53}
54
55PluginProcessConnection::~PluginProcessConnection()
56{
57 ASSERT(!m_connection);
58 ASSERT(!m_npRemoteObjectMap);
59}
60
61void PluginProcessConnection::addPluginProxy(PluginProxy* plugin)
62{
63 ASSERT(!m_plugins.contains(plugin->pluginInstanceID()));
64 m_plugins.set(plugin->pluginInstanceID(), plugin);
65}
66
67void PluginProcessConnection::removePluginProxy(PluginProxy* plugin)
68{
69 ASSERT(m_plugins.contains(plugin->pluginInstanceID()));
70 m_plugins.remove(plugin->pluginInstanceID());
71
72 // Invalidate all objects related to this plug-in.
73 m_npRemoteObjectMap->pluginDestroyed(plugin);
74
75 if (!m_plugins.isEmpty())
76 return;
77
78 m_npRemoteObjectMap = nullptr;
79
80 // We have no more plug-ins, invalidate the connection to the plug-in process.
81 ASSERT(m_connection);
82 m_connection->invalidate();
83 m_connection = nullptr;
84
85 // This will cause us to be deleted.
86 m_pluginProcessConnectionManager->removePluginProcessConnection(this);
87}
88
89void PluginProcessConnection::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
90{
91 ASSERT(decoder.destinationID());
92
93 PluginProxy* pluginProxy = m_plugins.get(decoder.destinationID());
94 if (!pluginProxy)
95 return;
96
97 pluginProxy->didReceivePluginProxyMessage(connection, decoder);
98}
99
100void PluginProcessConnection::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
101{
102 if (decoder.messageReceiverName() == Messages::NPObjectMessageReceiver::messageReceiverName()) {
103 m_npRemoteObjectMap->didReceiveSyncMessage(connection, decoder, replyEncoder);
104 return;
105 }
106
107 uint64_t destinationID = decoder.destinationID();
108
109 if (!destinationID) {
110 didReceiveSyncPluginProcessConnectionMessage(connection, decoder, replyEncoder);
111 return;
112 }
113
114 PluginProxy* pluginProxy = m_plugins.get(destinationID);
115 if (!pluginProxy)
116 return;
117
118 pluginProxy->didReceiveSyncPluginProxyMessage(connection, decoder, replyEncoder);
119}
120
121void PluginProcessConnection::didClose(IPC::Connection&)
122{
123 // The plug-in process must have crashed.
124 for (HashMap<uint64_t, PluginProxy*>::const_iterator::Values it = m_plugins.begin().values(), end = m_plugins.end().values(); it != end; ++it) {
125 PluginProxy* pluginProxy = (*it);
126
127 pluginProxy->pluginProcessCrashed();
128 }
129}
130
131void PluginProcessConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
132{
133}
134
135void PluginProcessConnection::setException(const String& exceptionString, CompletionHandler<void()>&& completionHandler)
136{
137 NPRuntimeObjectMap::setGlobalException(exceptionString);
138 completionHandler();
139}
140
141} // namespace WebKit
142
143#endif // ENABLE(NETSCAPE_PLUGIN_API)
144