initial import
[vuplus_webkit] / Source / WebKit2 / WebProcess / Plugins / PluginProcessConnection.cpp
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 "NPRuntimeObjectMap.h"
33 #include "PluginProcessConnectionManager.h"
34 #include "PluginProxy.h"
35 #include "WebProcess.h"
36 #include "WebProcessProxyMessages.h"
37 #include <WebCore/FileSystem.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 // The timeout, in seconds, when sending sync messages to the plug-in.
44 static const double syncMessageTimeout = 45;
45
46 static double defaultSyncMessageTimeout(const String& pluginPath)
47 {
48     // FIXME: We should key this off something other than the path.
49
50     // We don't want a message timeout for the AppleConnect plug-in.
51     if (pathGetFileName(pluginPath) == "AppleConnect.plugin")
52         return CoreIPC::Connection::NoTimeout;
53
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;
62
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;
67
68     if (WebProcess::shared().disablePluginProcessMessageTimeout())
69         return CoreIPC::Connection::NoTimeout;
70
71     return syncMessageTimeout;
72 }
73
74 PluginProcessConnection::PluginProcessConnection(PluginProcessConnectionManager* pluginProcessConnectionManager, const String& pluginPath, CoreIPC::Connection::Identifier connectionIdentifier)
75     : m_pluginProcessConnectionManager(pluginProcessConnectionManager)
76     , m_pluginPath(pluginPath)
77 {
78     m_connection = CoreIPC::Connection::createClientConnection(connectionIdentifier, this, WebProcess::shared().runLoop());
79
80     m_connection->setDefaultSyncMessageTimeout(defaultSyncMessageTimeout(m_pluginPath));
81     m_npRemoteObjectMap = NPRemoteObjectMap::create(m_connection.get());
82
83     m_connection->open();
84 }
85
86 PluginProcessConnection::~PluginProcessConnection()
87 {
88     ASSERT(!m_connection);
89     ASSERT(!m_npRemoteObjectMap);
90 }
91
92 void PluginProcessConnection::addPluginProxy(PluginProxy* plugin)
93 {
94     ASSERT(!m_plugins.contains(plugin->pluginInstanceID()));
95     m_plugins.set(plugin->pluginInstanceID(), plugin);
96 }
97
98 void PluginProcessConnection::removePluginProxy(PluginProxy* plugin)
99 {
100     ASSERT(m_plugins.contains(plugin->pluginInstanceID()));
101     m_plugins.remove(plugin->pluginInstanceID());
102
103     // Invalidate all objects related to this plug-in.
104     m_npRemoteObjectMap->pluginDestroyed(plugin);
105
106     if (!m_plugins.isEmpty())
107         return;
108
109     m_npRemoteObjectMap = nullptr;
110
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;
115
116     // This will cause us to be deleted.
117     m_pluginProcessConnectionManager->removePluginProcessConnection(this);
118 }
119
120 void PluginProcessConnection::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
121 {
122     ASSERT(arguments->destinationID());
123
124     PluginProxy* pluginProxy = m_plugins.get(arguments->destinationID());
125     if (!pluginProxy)
126         return;
127
128     pluginProxy->didReceivePluginProxyMessage(connection, messageID, arguments);
129 }
130
131 void PluginProcessConnection::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, OwnPtr<CoreIPC::ArgumentEncoder>& reply)
132 {
133     if (messageID.is<CoreIPC::MessageClassNPObjectMessageReceiver>()) {
134         m_npRemoteObjectMap->didReceiveSyncMessage(connection, messageID, arguments, reply);
135         return;
136     }
137
138     uint64_t destinationID = arguments->destinationID();
139
140     if (!destinationID) {
141         didReceiveSyncPluginProcessConnectionMessage(connection, messageID, arguments, reply);
142         return;
143     }
144
145     PluginProxy* pluginProxy = m_plugins.get(destinationID);
146     if (!pluginProxy)
147         return;
148
149     pluginProxy->didReceiveSyncPluginProxyMessage(connection, messageID, arguments, reply);
150 }
151
152 void PluginProcessConnection::didClose(CoreIPC::Connection*)
153 {
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);
157
158         pluginProxy->pluginProcessCrashed();
159     }
160 }
161
162 void PluginProcessConnection::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
163 {
164 }
165
166 void PluginProcessConnection::syncMessageSendTimedOut(CoreIPC::Connection*)
167 {
168     WebProcess::shared().connection()->send(Messages::WebProcessProxy::PluginSyncMessageSendTimedOut(m_pluginPath), 0);
169 }
170
171 void PluginProcessConnection::setException(const String& exceptionString)
172 {
173     NPRuntimeObjectMap::setGlobalException(exceptionString);
174 }
175
176 } // namespace WebKit
177
178 #endif // ENABLE(PLUGIN_PROCESS)