OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.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(PLUGIN_PROCESS)
30
31 #include "MachPort.h"
32 #include "NetscapePluginModule.h"
33 #include "PluginProcessProxyMessages.h"
34 #include "PluginProcessCreationParameters.h"
35 #include "WebProcessConnection.h"
36
37 namespace WebKit {
38
39 static const double shutdownTimeout = 15.0;
40
41 PluginProcess& PluginProcess::shared()
42 {
43     DEFINE_STATIC_LOCAL(PluginProcess, pluginProcess, ());
44     return pluginProcess;
45 }
46
47 PluginProcess::PluginProcess()
48     : m_shutdownTimer(RunLoop::main(), this, &PluginProcess::shutdownTimerFired)
49 #if USE(ACCELERATED_COMPOSITING) && PLATFORM(MAC)
50     , m_compositingRenderServerPort(MACH_PORT_NULL)
51 #endif
52 {
53 }
54
55 PluginProcess::~PluginProcess()
56 {
57 }
58
59 void PluginProcess::initialize(CoreIPC::Connection::Identifier serverIdentifier, RunLoop* runLoop)
60 {
61     ASSERT(!m_connection);
62
63     m_connection = CoreIPC::Connection::createClientConnection(serverIdentifier, this, runLoop);
64     m_connection->setDidCloseOnConnectionWorkQueueCallback(didCloseOnConnectionWorkQueue);
65     m_connection->open();
66 }
67
68 void PluginProcess::removeWebProcessConnection(WebProcessConnection* webProcessConnection)
69 {
70     size_t vectorIndex = m_webProcessConnections.find(webProcessConnection);
71     ASSERT(vectorIndex != notFound);
72
73     m_webProcessConnections.remove(vectorIndex);
74
75     if (m_webProcessConnections.isEmpty()) {
76         // Start the shutdown timer.
77         m_shutdownTimer.startOneShot(shutdownTimeout);
78     }
79 }
80
81 NetscapePluginModule* PluginProcess::netscapePluginModule()
82 {
83     if (!m_pluginModule) {
84         ASSERT(!m_pluginPath.isNull());
85         m_pluginModule = NetscapePluginModule::getOrCreate(m_pluginPath);
86
87 #if PLATFORM(MAC)
88         if (m_pluginModule) {
89             if (m_pluginModule->pluginQuirks().contains(PluginQuirks::PrognameShouldBeWebKitPluginHost))
90                 setprogname("WebKitPluginHost");
91         }
92 #endif
93     }
94
95     return m_pluginModule.get();
96 }
97
98 void PluginProcess::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments)
99 {
100     didReceivePluginProcessMessage(connection, messageID, arguments);
101 }
102
103 void PluginProcess::didClose(CoreIPC::Connection*)
104 {
105     // The UI process has crashed, just go ahead and quit.
106     // FIXME: If the plug-in is spinning in the main loop, we'll never get this message.
107     RunLoop::current()->stop();
108 }
109
110 void PluginProcess::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
111 {
112 }
113
114 NO_RETURN void PluginProcess::didFailToSendSyncMessage(CoreIPC::Connection*)
115 {
116     // We were making a synchronous call to a web process that doesn't exist any more.
117     // Callers are unlikely to be prepared for an error like this, so it's best to exit immediately.
118     exit(0);
119 }
120     
121 void PluginProcess::initializePluginProcess(const PluginProcessCreationParameters& parameters)
122 {
123     ASSERT(!m_pluginModule);
124
125     m_pluginPath = parameters.pluginPath;
126
127     platformInitialize(parameters);
128 }
129
130 void PluginProcess::createWebProcessConnection()
131 {
132     // FIXME: This is platform specific!
133
134     // Create the listening port.
135     mach_port_t listeningPort;
136     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
137
138     // Create a listening connection.
139     RefPtr<WebProcessConnection> connection = WebProcessConnection::create(listeningPort);
140     m_webProcessConnections.append(connection.release());
141
142     CoreIPC::MachPort clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
143     m_connection->send(Messages::PluginProcessProxy::DidCreateWebProcessConnection(clientPort), 0);
144
145     // Stop the shutdown timer.
146     m_shutdownTimer.stop();
147 }
148
149 void PluginProcess::shutdownTimerFired()
150 {
151     RunLoop::current()->stop();
152 }
153
154 } // namespace WebKit
155
156 #endif // ENABLE(PLUGIN_PROCESS)
157