Change more of the IPC code to take connections by reference
[WebKit-https.git] / Source / WebKit2 / PluginProcess / PluginProcess.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 "PluginProcess.h"
28
29 #if ENABLE(NETSCAPE_PLUGIN_API)
30
31 #include "ArgumentCoders.h"
32 #include "Attachment.h"
33 #include "NetscapePlugin.h"
34 #include "NetscapePluginModule.h"
35 #include "PluginProcessConnectionMessages.h"
36 #include "PluginProcessCreationParameters.h"
37 #include "PluginProcessProxyMessages.h"
38 #include "WebProcessConnection.h"
39 #include <WebCore/MemoryPressureHandler.h>
40 #include <WebCore/NotImplemented.h>
41 #include <wtf/RunLoop.h>
42
43 #if PLATFORM(MAC)
44 #include <crt_externs.h>
45 #endif
46
47 using namespace WebCore;
48
49 namespace WebKit {
50
51 PluginProcess& PluginProcess::shared()
52 {
53     static NeverDestroyed<PluginProcess> pluginProcess;
54     return pluginProcess;
55 }
56
57 PluginProcess::PluginProcess()
58     : m_supportsAsynchronousPluginInitialization(false)
59     , m_minimumLifetimeTimer(RunLoop::main(), this, &PluginProcess::minimumLifetimeTimerFired)
60     , m_connectionActivity("PluginProcess connection activity.")
61 {
62     NetscapePlugin::setSetExceptionFunction(WebProcessConnection::setGlobalException);
63     m_audioHardwareListener = AudioHardwareListener::create(*this);
64 }
65
66 PluginProcess::~PluginProcess()
67 {
68 }
69
70 void PluginProcess::lowMemoryHandler(bool critical)
71 {
72     UNUSED_PARAM(critical);
73     if (shared().shouldTerminate())
74         shared().terminate();
75 }
76
77 void PluginProcess::initializeProcess(const ChildProcessInitializationParameters& parameters)
78 {
79     m_pluginPath = parameters.extraInitializationData.get("plugin-path");
80     platformInitializeProcess(parameters);
81
82     memoryPressureHandler().setLowMemoryHandler(lowMemoryHandler);
83     memoryPressureHandler().install();
84 }
85
86 void PluginProcess::removeWebProcessConnection(WebProcessConnection* webProcessConnection)
87 {
88     size_t vectorIndex = m_webProcessConnections.find(webProcessConnection);
89     ASSERT(vectorIndex != notFound);
90
91     m_webProcessConnections.remove(vectorIndex);
92     
93     if (m_webProcessConnections.isEmpty() && m_pluginModule) {
94         // Decrement the load count. This is balanced by a call to incrementLoadCount in createWebProcessConnection.
95         m_pluginModule->decrementLoadCount();
96     }        
97
98     enableTermination();
99 }
100
101 NetscapePluginModule* PluginProcess::netscapePluginModule()
102 {
103     if (!m_pluginModule) {
104         ASSERT(!m_pluginPath.isNull());
105         m_pluginModule = NetscapePluginModule::getOrCreate(m_pluginPath);
106
107 #if PLATFORM(MAC)
108         if (m_pluginModule) {
109             if (m_pluginModule->pluginQuirks().contains(PluginQuirks::PrognameShouldBeWebKitPluginHost))
110                 *const_cast<const char**>(_NSGetProgname()) = "WebKitPluginHost";
111         }
112 #endif
113     }
114
115     return m_pluginModule.get();
116 }
117
118 bool PluginProcess::shouldTerminate()
119 {
120     return m_webProcessConnections.isEmpty();
121 }
122
123 void PluginProcess::didReceiveMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder)
124 {
125     didReceivePluginProcessMessage(connection, decoder);
126 }
127
128 void PluginProcess::didClose(IPC::Connection&)
129 {
130     // The UI process has crashed, just go ahead and quit.
131     // FIXME: If the plug-in is spinning in the main loop, we'll never get this message.
132     stopRunLoop();
133 }
134
135 void PluginProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
136 {
137 }
138
139 void PluginProcess::initializePluginProcess(PluginProcessCreationParameters&& parameters)
140 {
141     ASSERT(!m_pluginModule);
142
143     m_supportsAsynchronousPluginInitialization = parameters.supportsAsynchronousPluginInitialization;
144     setMinimumLifetime(parameters.minimumLifetime);
145     setTerminationTimeout(parameters.terminationTimeout);
146
147     platformInitializePluginProcess(WTF::move(parameters));
148 }
149
150 void PluginProcess::createWebProcessConnection()
151 {
152     bool didHaveAnyWebProcessConnections = !m_webProcessConnections.isEmpty();
153
154 #if OS(DARWIN)
155     // Create the listening port.
156     mach_port_t listeningPort;
157     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
158
159     // Create a listening connection.
160     RefPtr<WebProcessConnection> connection = WebProcessConnection::create(IPC::Connection::Identifier(listeningPort));
161
162     if (m_audioHardwareListener) {
163         if (m_audioHardwareListener->hardwareActivity() == WebCore::AudioHardwareActivityType::IsActive)
164             connection->audioHardwareDidBecomeActive();
165         else if (m_audioHardwareListener->hardwareActivity() == WebCore::AudioHardwareActivityType::IsInactive)
166             connection->audioHardwareDidBecomeInactive();
167     }
168
169     m_webProcessConnections.append(connection.release());
170
171     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
172     parentProcessConnection()->send(Messages::PluginProcessProxy::DidCreateWebProcessConnection(clientPort, m_supportsAsynchronousPluginInitialization), 0);
173 #elif USE(UNIX_DOMAIN_SOCKETS)
174     IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
175
176     RefPtr<WebProcessConnection> connection = WebProcessConnection::create(socketPair.server);
177     m_webProcessConnections.append(connection.release());
178
179     IPC::Attachment clientSocket(socketPair.client);
180     parentProcessConnection()->send(Messages::PluginProcessProxy::DidCreateWebProcessConnection(clientSocket, m_supportsAsynchronousPluginInitialization), 0);
181 #else
182     notImplemented();
183 #endif
184
185     if (NetscapePluginModule* module = netscapePluginModule()) {
186         if (!didHaveAnyWebProcessConnections) {
187             // Increment the load count. This is matched by a call to decrementLoadCount in removeWebProcessConnection.
188             // We do this so that the plug-in module's NP_Shutdown won't be called until right before exiting.
189             module->incrementLoadCount();
190         }
191     }
192
193     disableTermination();
194 }
195
196 void PluginProcess::getSitesWithData(uint64_t callbackID)
197 {
198     Vector<String> sites;
199     if (NetscapePluginModule* module = netscapePluginModule())
200         sites = module->sitesWithData();
201
202     parentProcessConnection()->send(Messages::PluginProcessProxy::DidGetSitesWithData(sites, callbackID), 0);
203 }
204
205 void PluginProcess::clearSiteData(const Vector<String>& sites, uint64_t flags, uint64_t maxAgeInSeconds, uint64_t callbackID)
206 {
207     if (NetscapePluginModule* module = netscapePluginModule()) {
208         if (sites.isEmpty()) {
209             // Clear everything.
210             module->clearSiteData(String(), flags, maxAgeInSeconds);
211         } else {
212             for (size_t i = 0; i < sites.size(); ++i)
213                 module->clearSiteData(sites[i], flags, maxAgeInSeconds);
214         }
215     }
216
217     parentProcessConnection()->send(Messages::PluginProcessProxy::DidClearSiteData(callbackID), 0);
218 }
219
220 void PluginProcess::setMinimumLifetime(double lifetime)
221 {
222     if (lifetime <= 0.0)
223         return;
224     
225     disableTermination();
226     
227     m_minimumLifetimeTimer.startOneShot(lifetime);
228 }
229
230 void PluginProcess::minimumLifetimeTimerFired()
231 {
232     enableTermination();
233 }
234
235 #if !PLATFORM(COCOA)
236 void PluginProcess::initializeProcessName(const ChildProcessInitializationParameters&)
237 {
238 }
239
240 void PluginProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
241 {
242 }
243 #endif
244
245 void PluginProcess::audioHardwareDidBecomeActive()
246 {
247     for (auto& connection : m_webProcessConnections)
248         connection->audioHardwareDidBecomeActive();
249 }
250     
251 void PluginProcess::audioHardwareDidBecomeInactive()
252 {
253     for (auto& connection : m_webProcessConnections)
254         connection->audioHardwareDidBecomeInactive();
255 }
256
257 } // namespace WebKit
258
259 #endif // ENABLE(NETSCAPE_PLUGIN_API)
260